Copying components
Copy declared Go components such as chat and file-picker into your app with nsgo add, rewrite imports, inspect changes with nsgo diff, and keep their license notices and source records.
Nội dung này hiện chưa có sẵn bằng ngôn ngữ của bạn.
When an app needs to change a component rather than only configure it, nsgo add copies a package that its module declares under components: in nsgo-module.yaml directly into the app. NSGo declares ui’s eight subpackages (chat, code-editor, commit-graph, diff-view, file-picker, flow-diagram, rich-editor and terminal) as copyable components and lists them in its built-in index:
nsgo add -list chat # find chat in NSGo's built-in indexnsgo add chat # copy ui/chat into components/chatnsgo add file-picker # copy ui/filepicker into components/filepickernsgo add -dir internal/components/chat chat # place chat at another path in the modulensgo add -tests=false file-picker # omit _test.go files and testdata/nsgo add -dry-run -diff chat # preview the plan and every copied fileYou can also copy any module’s declared component directly by path with nsgo add <module>[@version]#<component>, such as nsgo add github.com/nsgo-dev/nsgo#file-picker.
Once copied, the component is the app’s own Go code.
Where copies go
Section titled “Where copies go”By default, a copy lands at components/<package> relative to nsgo.yaml (or relative to the module root when there is no nsgo.yaml), so chat lands in components/chat and file-picker lands in components/filepicker. Set components.dir in nsgo.yaml to change the base directory:
components: dir: ui/componentsPass -dir <path> to place a single named component at another path inside the module; its last element must equal the component’s Go package name. nsgo add refuses destinations outside the module, inside vendor, testdata, nested modules, hidden/underscore directories, symlinked parents, existing paths, or paths that collide case-insensitively with an existing entry.
Version selection
Section titled “Version selection”When no @version is typed, nsgo add uses:
- the version of the source module that
go.modalready requires (this rule is final: if that version does not declare the component,nsgo addrefuses and prints thensgo add <name>@<version>command that takes a version that does); - otherwise, the version listed in the registry index (if added by name);
- otherwise,
@upgrade(the newest release).
A typed version higher than go.mod’s requirement announces the raise on the first line of the plan. A typed version lower than go.mod’s requirement leaves go.mod untouched and notes that the copy is older. Modules whose go directive is below 1.22 or newer than the app’s planned go line are refused.
What copying does
Section titled “What copying does”- Verifies the archive.
nsgo adddownloads the module zip in an isolated directory usingadd’s proxy-only,GOTOOLCHAIN=localenvironment, validates the zip structure (zip.CheckZip), and checks itsdirhash.HashZipdigest against both the download’sSumand any entry ingo.sumbefore reading files directly from the archive. - Checks copy rules and flags. Every file is checked against the copied-component allowlist, path restrictions, count/size limits (256 files, 1 MiB per file, 4 MiB total), UTF-8 without BOM, and Go AST rules (refusing
import "C", internal imports,tsxoutside tests,//go:build ignore, foreign//go:generate, and disallowed//go:embedtargets). Risky constructs (control/format characters, non-ASCII identifiers, imports ofunsafe,syscall,golang.org/x/sys/...,purego,os/exec,plugin, and//go:linkname///go:wasm*///go:cgo_*///go:debug) andgo vetfailures are shown with positions and refused under-y. - Rewrites imports and comments. Imports of the component itself, its
locales/package (such asui/chat/locales), and other components of the same module (which are copied automatically when needed) are rewritten to the copy’s import path while keeping their import names. Whole-path occurrences in comments are rewritten too, string literals containing rewritten paths are listed in the plan, and every Go file is formatted withgo/format. - Generates catalogs. When the component has
locales/*.yaml(aschatdoes),nsgo addgenerateslocales/catalog_gen.goin memory under the copy’s final import path with thisnsgo’s generator sonsgo checkpasses immediately. If the app’s own catalogs supply the originallocalespath, the plan names the key to update once the copy is imported. - Plans requirements and vets targets. External and non-component same-module imports are added to
go.modas direct requirements (at least at the versions the source module requires, never lowering the app’s). The planned copy is vetted withgo vet -mod=readonlyfor every target inbuild.targets(or the host withoutnsgo.yaml); targets whose toolchain the host lacks are listed undernot vettedand do not block-y. - Writes atomically. Under the module’s check lock, each copy is staged in a hidden
.nsgo-copy-*directory beside its destination and renamed into place along withgo.modandgo.sum. Any error or interrupt restores the module cleanly.
License notices and nsgo-copy.yaml
Section titled “License notices and nsgo-copy.yaml”License and notice documents from the source module root and directories between it and the component (such as NSGo’s LICENSE and NOTICE) are copied beside the component (duplicate filenames are refused). nsgo add classifies every license document the copy brings—including nested asset licenses such as icons/OFL.txt—and applies the app’s licenses policy immediately.
During nsgo build, a directory holding nsgo-copy.yaml receives its own section in THIRD_PARTY_NOTICES.txt headed <import path>, copied from <module> <version>, keeping those texts separate from the app’s own license section. For Apache-2.0 components such as chat and file-picker, mark files you modify as required by section 4(b); nsgo diff -stat lists every changed file.
Each copied directory holds a deterministic, hash-free nsgo-copy.yaml recording its source module, version, component name, test choice, copied files and generated files:
# Written by nsgo add. nsgo diff compares this directory with the copy of this version.version: 1module: github.com/nsgo-dev/nsgomodule_version: v0.4.0component: chattests: truefiles: - LICENSE - NOTICE - actions.go - actions_test.go - chat.go - chat_test.go - composer.go - controls.go - conversation.go - effort.go - find.go - locales/en.yaml - locales/locales.go - locales/zh-Hans.yaml - native_test.go - reasoning.go - reasoning_test.gogenerated: - locales/catalog_gen.goRunning nsgo add again at the same version reports the existing copy’s version and directory without making changes. Moving a copy needs no extra command; nsgo-copy.yaml moves with it.
Comparing copies with nsgo diff
Section titled “Comparing copies with nsgo diff”Use nsgo diff to inspect what your app changed in a copy, what another version would change, or which copies in the module have local edits:
nsgo diff # list each copy, its version, and edited/unedited statusnsgo diff chat # unified diff of local edits against the recorded versionnsgo diff components/chat # target a copy by directory pathnsgo diff chat@v0.5.0 # unified diff from the recorded version to v0.5.0nsgo diff chat@upgrade # unified diff from the recorded version to the newest releasensgo diff -stat chat # changed lines per file (also lists modified files for Apache-2.0 §4(b))nsgo diff -exit-code chat # exit with status 1 when there are differences- Target resolution. A directory holding
nsgo-copy.yamlmatches directly. A bare name matches the module’s ownnsgo-copy.yamlrecords first so it works offline; if two copies from different modules share the same component name,nsgo diffasks for the directory (ormodule#component). Otherwise it resolves the name through the built-in index or a configured@registry/nameand matches a record by module and component. - Clean reference copy.
nsgo diffreconstructs the recorded (or requested) version directly from the verified module zip (zip.CheckZipanddirhash.HashZipchecked againstgo.sum), applying the record’stestschoice, ancestor licenses, Go import and comment rewrites (with sibling copies at their current record directories) andgo/format, excluding generated catalog files such aslocales/catalog_gen.go. - Git-compatible output. Unified diffs use
diff --githeaders and module-root-relative paths with three lines of context, sogit applyandgit apply -Rapply them directly. Binary files reportBinary files ... differ. - Line endings and formatting. Files are compared with CRLF read as LF, and Go files whose
go/formatoutput matches the recorded version count as unedited. - Offline use and unavailable versions. Recorded versions are read from
GOMODCACHEfirst sonsgo diffworks offline underGOPROXY=off; otherwise they download through Go module proxies withGOTOOLCHAIN=local. When a recorded version can no longer be downloaded, every non-generated file counts as edited and the output explains why. - Safety. Every path in
nsgo-copy.yamlis validated on read (refusing escapes such as../form/form.gowith the line number), all reads stay inside anos.Rootat the copy’s directory, and symlinks are refused.
Updating a copy
Section titled “Updating a copy”Pass another version to nsgo add to update an existing copy file by file while keeping the app’s own edits:
nsgo add chat@v0.5.0 # update by component name from nsgo-copy.yamlnsgo add chat@upgrade # update to the newest releasensgo add chat@v0.4.0 # downgrade to an earlier versionnsgo add components/chat@v0.5.0 # disambiguate by directory when two copies share a namensgo add -dry-run chat@v0.5.0 # preview per-file actions without writingA component name is matched against the module’s nsgo-copy.yaml records first so resolution works offline; if two copies from different modules share a name, pass the copy’s directory or module@version#component.
For each file, nsgo add reconstructs the recorded version (base) and the target version (author) and compares them with the directory (app), reading CRLF as LF and formatting Go files before comparing:
- App left the file alone: takes the new version and preserves CRLF line endings if the file in the directory uses CRLF.
- Author left it alone, or both made the same change: stays untouched.
- Both changed it differently: the update is refused, naming every conflicting file and writing nothing.
- Author dropped it: deleted when the app left it alone; flagged when the app edited it (refused under
-y, or confirmed interactively). - Author added it and a file of that name already exists (compared case-insensitively): refused, writing nothing.
Around the files, locales/catalog_gen.go is regenerated from the resulting catalogs, other copied components imported from the same module move to the same version in the same plan, go.mod requirements, license policy, and target vetting work as for a new copy, and nsgo-copy.yaml records the new version and its files. A version whose module no longer declares the component is refused, naming that version.
Carrying app edits over by hand
Section titled “Carrying app edits over by hand”When both the app and the author edited the same file, nsgo add stops before writing anything rather than guessing at a line merge. Carry your edits over by hand with nsgo diff:
- Save your own edits against the recorded version:
nsgo diff chat > chat.patch
- Inspect what the author changed in the target version:
nsgo diff chat@v0.5.0
- Temporarily restore the conflicting file to the recorded version (for example with
git apply -Ron that file’s hunk fromchat.patch, or by reverting your edits to it) sonsgo addsees it as unedited. - Run
nsgo add chat@v0.5.0to take the new version cleanly and updatensgo-copy.yaml. - Re-apply your edits from
chat.patchonto the updated file, then runnsgo diff chatandnsgo checkto verify only your intended edits remain and the copy builds cleanly.
Upgrading NSGo with copies
Section titled “Upgrading NSGo with copies”NSGo and component modules can change APIs between releases. A copy compiles with the module version it was taken from, and may stop compiling after an upgrade. nsgo diagnoses broken copies before and after an upgrade without failing CI on version differences that break nothing:
-
Before upgrading (
nsgo add). When annsgo addplan moves a module that existing copies import—NSGo first among them—nsgo addvets those copies for the host against the plannedgo.modandgo.sumwithout modifying the module. Any copy that would stop compiling is listed undercopiesin the plan with its first compiler or vet error and thensgo diff <name>@<version>andnsgo add <name>@<version>commands. Under-y, a plan that breaks copied components is refused so automated runs never break copies silently; reviewing the plan interactively in a terminal still lets you confirm if you plan to update the copy right after. -
After an external upgrade (
nsgo check). Whengo.modmoves outsidensgo(such as ago getor Dependabot update),nsgo checkreads everynsgo-copy.yamlbefore vetting, validates its paths and confinement, and prints a warning for each copy whose source modulego.modrequires at another version:warn components/chat is chat from github.com/nsgo-dev/nsgo v0.4.2; this module requires v0.5.0: nsgo diff chat@v0.5.0, nsgo add chat@v0.5.0Up-to-date copies are not reported. If the moved module leaves the copy compiling,
nsgo checkprints the warning alone and exits 0. -
On compile or vet failures (
nsgo check,nsgo build,nsgo dev). Whenevergo vetorgo buildfails in a copied component’s package (matched from Go’s# <import path>lines),nsgoprints the same diagnostic line for the copy after thegocommand’s output so you immediately see which package is a copy, which version it came from, and which commands inspect and update it. If two copies in the module share a component name, the suggested commands use the copy’s relative directory path.