Skip to content

cmd/nsgo: nsgo add copies a component into the app, rewriting its imports and recording its module and version #2467

Backlog

Description

@houko

Context

Part of #2459, whose Copied components section has the full rules. After #2460, #2461 and #2463, nsgo add only requires modules, and #2466 holds declared components to the copy rules. An app that wants to change a component copies it by hand, and then:

  • its imports still name the source: three files of ui/chat import ui/chat/locales, and ui/terminal's external test imports uiterminal "github.com/nsgo-dev/nsgo/ui/terminal" (ui/terminal/terminal_test.go:15);
  • its catalog_gen.go, generated by another NSGo, fails nsgo check as out of date, since main-module catalogs are checked with this nsgo's generator (cmd/nsgo/i18n_build.go:233-263, :281-311), and the app's catalogs keep supplying the original package (cmd/nsgo/i18n.go:30-34, ui/chat/locales/locales.go:1-3);
  • its license stays behind unless someone copies it. The build applies licenses only to other modules (cmd/nsgo/licenses.go:194, :282-305), and lists license documents in the app's directories under the app's own module path (:168-217, :250), as nsgo build: treat a Go workspace's modules as the app's own in license notices #1814 found for workspace modules;
  • nothing records where it came from, so its edits and the author's later fixes cannot be told apart.

A hand copy can also carry what nobody reviewed: a .syso file, an nsgo-natives.yaml (cmd/nsgo/package_natives.go:192-207), an AGENTS.md, a //go:generate line, bidi characters.

Why it matters

Copying is how an app takes a component and makes it its own. With a plan and a record, the copy is reviewed before it is written, and can be compared and updated later.

Proposal

  • Names. An index item with component is copied. module[@version]#name names a component that the module's nsgo-module.yaml declares, parsed as parseModuleTemplate parses templates (cmd/nsgo/init_module.go:19-37).
  • Version.
    • The typed version; else the version of the module that the app requires, which is final, so a version that does not declare the component is refused with the nsgo add <name>@<version> that takes one that does; else the item's version; else @upgrade.
    • A version that moves the app's requirement says so in the plan's first line. A typed version below the app's requirement leaves it alone, and the plan says the copy is older.
    • A module whose go line is newer than the app's after the plan, or below 1.22, is refused.
  • Source. go mod download -json runs in an isolated directory, as in downloadInitModule (init_module.go:174-215), with add's environment. x/mod's zip.CheckZip checks the archive, and dirhash.HashZip must equal the app's go.sum entry, when there is one, and the download's Sum. Files are read from that zip.
  • Rules. cmd/nsgo: nsgo check holds the components a module declares to the rules nsgo add copies by #2466's package, and:
  • Catalogs.
    • The copied catalogs are loaded under the copy's final import path, not importPathOf of the staging directory (i18n_build.go:324), and catalog_gen.go is generated in memory with nsgo i18n's code (generateCatalog, :313-345).
    • Supplies to other packages resolve with the planned go.mod through the overlay, not plain go list (:157-167).
    • The plan names the keys of the app's catalogs that supply the original locales. Once the app imports the copy, nsgo check's coverage warning names the copy's key (:215-216).
    • A plan that moves NSGo says that nsgo dev, build or i18n then rewrites the generated file.
  • Licenses.
    • Every license document the copy brings, beside it and inside it, is classified with the build's classifier and checked against the app's policy; a refusal names the licenses.allow line.
    • The documents of the module root, and of the directories between it and the component, are copied beside the copy, and two with one name are refused.
    • The build gives a main-module package directory that holds nsgo-copy.yaml its own section of THIRD_PARTY_NOTICES.txt, headed <import path>, copied from <module> <version>, and leaves those documents out of the app's own section (addLicensePackage, licenses.go:168-199).
  • Destination. components.dir in nsgo.yaml, default components, relative to nsgo.yaml; the module root's components without nsgo.yaml; -dir for the one component named, ending in the package's name. Refused:
    • outside the module, or in vendor, testdata, a directory starting with . or _, or a nested module;
    • an existing path, or one that differs only in case, that is not this component's copy;
    • a second copy of a component;
    • a module that go.mod replaces.
  • Record. nsgo-copy.yaml, as RFC: libraries and components by name: nsgo add, registries, copied components, and native needs declared by packages #2459 defines it: strict, deterministic, without a hash. Its paths are checked on every read, every read and write it drives goes through an os.Root at the copy's directory, and symlinks are refused.
  • Plan. It shows the files, the rewritten imports and comments, the go.mod changes, the licenses, and the targets vetted and not vetted:
  • Applying works as in cmd/nsgo: nsgo add, a go get that shows its plan first and never moves an app onto an unreleased NSGo #2460, holding the module's check lock (lockCheck, check.go:270-318).
    • The copy goes into a hidden staging directory beside its destination, which ./... skips as it skips nsgo dev -preview's host (preview.go:29-30), and is then renamed into place.
    • An error or interrupt removes it and restores go.mod and go.sum; the next add removes one an interrupted run left.
  • An existing copy. The same version is a no-op that prints the copy's version and directory. Another version is refused until cmd/nsgo: nsgo add <component>@<version> updates a copy file by file, keeping the app's edits #2469, naming nsgo diff <name>@<version>. nsgo remove's refusal of a component (cmd/nsgo: nsgo remove drops a library the module no longer imports, through a go mod tidy it shows first #2465) names the copy's directory.
  • Usage and docs:
    • nsgo add [-dry-run] [-y] [-diff] [-dir D] [-tests=false] ..., and the components: key in nsgo.yaml;
    • a new page, docs/public/en/copying-components.md (group Components), and its zh page;
    • the Adding libraries section of building.md;
    • skills/nsgo/SKILL.md;
    • a changelog.d fragment.

Acceptance

  • Offline tests use a file:// proxy and a module cache of their own (addTemplateVersion, cmd/nsgo/init_module_test.go:91-134; goEnv, cmd/nsgo/delegate_test.go:23-31). The app requires NSGo through a replace of this checkout, and a fixture module serves two components:
    • field: catalogs in en and zh-Hans, an _android.go file, a preview fixture, an embedded SVG in icons/ with an OFL.txt, a README.md, and an external test of its own package;
    • form: imports field and a package of example.com/flags.
  • Golden plans; the written files and the record equal golden files; loading and saving the record changes nothing
  • form brings field, and requires example.com/flags at the version form's go.mod requires, never below the app's
  • The generated catalog equals what nsgo i18n writes in place, and nsgo check passes; with the app supplying the original locales, the plan names that key
  • The copy vets for android_arm64 with its _android.go file; a target whose toolchain is missing, simulated, is listed as not vetted, and -y still applies
  • An app under a go.work, and one with a vendor directory without goEnv's -mod=mod, are planned and vetted; the second's next step is go mod vendor
  • Each refusal holds with -y and without it: a path with .. or a leading dot; a .syso; an AGENTS.md; a second package; an internal import; import "C"; a foreign //go:generate; a 2 MiB file; a destination in vendor, under a symlinked parent, already there, or components/Chat beside components/chat; copyleft under licenses.copyleft: error; a go.sum entry that does not match; a zip in the module cache changed after download
  • A U+202E in a comment, a Cyrillic letter in an identifier and an unsafe import are each shown with their position and refused under -y
  • An app that requires a version without the component is refused with the command; a typed version that raises the module says so in the first line
  • -tests=false copies no test, and the record says so
  • Without a terminal and without -y, nothing is written; an injected failure leaves the module as it was; a leftover staging directory is removed by the next add
  • A build's THIRD_PARTY_NOTICES.txt holds the copy's LICENSE, NOTICE and OFL.txt under <import path>, copied from <module> <version>, not under the app's own heading
  • nsgo remove field is refused, naming components/field
  • The docs (en and zh), the skill and the usage describe the command

Activity

  1. added
    enhancement
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clienhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions