From 259bfd0c2bafc170b00306d7de595f5c138d405f Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 22 Oct 2026 21:07:22 +0200 Subject: [PATCH] S1: keep no package.json or go.mod outside the named tree in a narrowed build A file outside the key changed what is built (novox/hq ADR 0499 clause 4); a build that needs one names it among its paths. --- go/buildstep/narrow.go | 6 ++++-- go/buildstep/narrow_test.go | 21 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/go/buildstep/narrow.go b/go/buildstep/narrow.go index 9bc6e00..e27050f 100644 --- a/go/buildstep/narrow.go +++ b/go/buildstep/narrow.go @@ -9,8 +9,10 @@ import ( ) // narrowKept are the files of a module's directory a narrowed build keeps beside its named paths: its manifest and -// its lock files. -var narrowKept = []string{"module.json", "package.json", "package-lock.json", "go.mod", "go.sum"} +// its lock files, each an input of the module's key (ModuleTree's Manifest and Locks). package.json and go.mod are +// not kept: neither is in the named tree, so a build that needs one names it among its artifact's paths, which puts +// it in the key (novox/hq ADR 0499 clause 4). +var narrowKept = append([]string{"module.json"}, lockFiles...) // Narrow leaves in the fetched tree only what a build of an artifact that names its paths may see (novox/hq ADR 0499 // clause 4): r.Dir's named paths, its manifest and lock files, and .git; everything else is removed, so a file outside diff --git a/go/buildstep/narrow_test.go b/go/buildstep/narrow_test.go index 0af2bc8..91bb1c3 100644 --- a/go/buildstep/narrow_test.go +++ b/go/buildstep/narrow_test.go @@ -23,7 +23,8 @@ func filesUnder(t *testing.T, root string) []string { } // A build of an artifact that names its paths sees only those paths, its manifest and its lock files (novox/hq ADR 0499 -// clause 4): Narrow removes everything else of the fetched tree but .git; with no paths it changes nothing. +// clause 4): Narrow removes everything else of the fetched tree but .git, package.json and go.mod among it, since +// neither is in the named tree nor among the lock files; with no paths it changes nothing. func TestNarrowLeavesOnlyTheNamedPathsTheManifestAndTheLocks(t *testing.T) { tree := t.TempDir() for _, f := range []string{".git/HEAD", "module.json", "package.json", "package-lock.json", "go.sum", "src/index.ts", @@ -39,7 +40,7 @@ func TestNarrowLeavesOnlyTheNamedPathsTheManifestAndTheLocks(t *testing.T) { if err := Narrow(tree, FetchRequest{Dir: ".", Paths: []string{"src", "test", "tsconfig.json"}}); err != nil { t.Fatal(err) } - want := ".git/HEAD,go.sum,module.json,package-lock.json,package.json,src/deep/a.ts,src/index.ts,test/a.test.ts,tsconfig.json" + want := ".git/HEAD,go.sum,module.json,package-lock.json,src/deep/a.ts,src/index.ts,test/a.test.ts,tsconfig.json" if got := strings.Join(filesUnder(t, tree), ","); got != want { t.Fatalf("the narrowed tree holds %s\nwant %s", got, want) } @@ -61,7 +62,21 @@ func TestNarrowKeepsAModuleDirectorysPathsAndFailsOnAnAbsentOne(t *testing.T) { if err := Narrow(tree, FetchRequest{Dir: "modules/a", Paths: []string{"src"}}); err != nil { t.Fatal(err) } - if got := strings.Join(filesUnder(t, tree), ","); got != ".git/config,modules/a/go.mod,modules/a/module.json,modules/a/src/x.ts" { + if got := strings.Join(filesUnder(t, tree), ","); got != ".git/config,modules/a/module.json,modules/a/src/x.ts" { + t.Fatalf("the narrowed tree holds %s", got) + } +} + +// A build that needs package.json or go.mod names it among its paths, and so keys on it (ADR 0499 clause 4). +func TestNarrowKeepsAManifestOfTheLanguageOnlyWhenNamed(t *testing.T) { + tree := t.TempDir() + for _, f := range []string{"module.json", "package.json", "go.mod", "src/a.ts"} { + writeTreeFile(t, tree, f, "x") + } + if err := Narrow(tree, FetchRequest{Dir: ".", Paths: []string{"src", "package.json"}}); err != nil { + t.Fatal(err) + } + if got := strings.Join(filesUnder(t, tree), ","); got != "module.json,package.json,src/a.ts" { t.Fatalf("the narrowed tree holds %s", got) } }