Backtestshugorun-20260922-141813#15245
hugo mod npm pack emits a publishable, checkout-named package.json
The merged fix was judged better than our patch.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches resolve the two surface asks: they replace the checkout-derived `sourceFs.Stat("").Name()` with a stable default (`hugoautogen`) and add `"private": true`, and both make identical, correct updates to the four testscript golden files. The deciding difference is root-cause depth. The issue's central grievance is that 'the file is regenerated wholesale on each run, so hand-set fields don't survive' — which is exactly why docsy-example needs the `npm pkg set` workaround after every pack. Patch A addresses that root cause: it reads the existing package.json and preserves hand-set name/version/private on regeneration, so a committed, hand-normalized manifest (e.g. `@docsy/hugoautogen`, `private`) survives a re-pack and the workaround becomes unnecessary. Patch B only hardcodes the defaults; a site that wants a scoped/project-controlled name still has its edits clobbered on every run, so it fixes the symptom (bad defaults) but not the churn the issue leads with. Tests reinforce this: Patch A's test both asserts the new defaults AND edits the file, re-packs, and verifies name/version/private survive — behavior that fails before the change and passes after, guarding the harder-to-get-right path. Patch B's test only checks the static defaults. On scope Patch B is smaller and its constant `defaultPackageName` is a clean touch, but Patch A's additional code is directly in service of the issue's stated motivation rather than incidental churn. (Minor caveat: Patch A relies on `encoding/json` already being imported in package_builder.go, which is near-certain since the file marshals the manifest, and the passing preservation test implies it reads the correct written path.) Patch A is the more complete, better-tested fix; not a tie.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
16m 30s21 turns
Wall-clock time in the container
Full test suite
No new failures
3,725 passed · 99 skipped · 3,830 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
52vs 78 merged
Changed lines, ours vs merged
The issue
View on GitHubTL;DR: the generated package.json has no "private": true and a checkout-derived name, so committed copies are publishable under names sites don't control. Suggested fix: emit "private": true and a stable (ideally scoped) default name.
What version of Hugo are you using (hugo version)?
hugo v0.165.0-76a5e1880ab46688155b02e99bab9be2a6134492+extended darwin/arm64 BuildDate=2026-08-12T14:26:28Z VendorInfo=gohugoio
Does this issue reproduce with the latest release?
Yes (transcript below is v0.165.0). The behavior is in master's package_builder.go.
What happened
hugo mod npm pack writes packages/hugoautogen/package.json with:
- a name derived from the checkout directory, so it varies per clone and can be a registry name the site doesn't control, and
- no
"private": true, npm's do-not-publish guard for internal packages.
The file is regenerated wholesale on each run, so hand-set fields don't survive. From a fresh clone of a repo whose committed copy was hand-normalized:
$ git clone https://github.com/google/docsy-example.git site-a && cd site-a
$ grep -E '"(name|version|private)"' packages/hugoautogen/package.json
"name": "@docsy/hugoautogen",
"version": "0.1.0",
"private": true
$ hugo mod npm pack
$ grep -E '"(name|version|private)"' packages/hugoautogen/package.json
"name": "site-a",
"version": "0.1.0"
A sibling clone of the same repo into site-b yields "name": "site-b".
Why it matters
Sites that commit the generated workspace (e.g. so npm ci works without running Hugo first) end up carrying a manifest that npm workspace publishing and common release tools treat as publishable by default: npm publish --dry-run --workspaces packs it as site-a@0.1.0, while with "private": true npm skips the workspace. The name variance also churns the committed manifest, and the lockfile's workspace entry, across differently named checkouts.
What did you expect
The generated internal workspace is non-publishable ("private": true) and has a stable, project-controlled default name rather than the checkout directory's basename. Ideally the default is scoped: a bare name can be claimed on the registry by anyone, a scoped one only by the scope owner.
Workaround
docsy-example#487 follows each pack invocation with a single npm pkg set line restoring a scoped name and private.
Happy to send a PR for the private field (and for the name default, if you have a preference for what it should be).
The two patches
modules/npm/package_builder.goindex cea1dc8cd..e1ad8fe88 100644@@ -43,6 +43,8 @@ const ( packageJSONName = "package.json" packageMetaJSONName = "hugo_packagemeta.json"++ defaultPackageName = "hugoautogen" ) var (@@ -195,15 +197,10 @@ func Pack(sourceFs, assetsWithDuplicatesPreservedFs afero.Fs, mods modules.Modul } } - name := "project"- rfi, err := sourceFs.Stat("")- if err == nil {- name = rfi.Name()- }- autoGenPkg := map[string]any{- "name": name,+ "name": defaultPackageName, "version": "0.1.0",+ "private": true, dependenciesKey: moduleDeps, devDependenciesKey: moduleDevDeps, }modules/npm/package_builder_integration_test.goindex 65649d7ce..13496cdb6 100644@@ -14,6 +14,7 @@ package npm_test import (+ "encoding/json" "strings" "testing" @@ -92,6 +93,25 @@ func TestPackageBuilder(t *testing.T) { b.Assert(string(meta2), qt.Equals, string(meta1)) } +func TestPackageBuilderPackageJSON(t *testing.T) {+ files := getPackageBuilderTestFiles()+ b := hugolib.Test(t, files)+ sourceFs := b.H.BaseFs.ProjectSourceFs+ assetsFs := b.H.BaseFs.AssetsWithDuplicatesPreserved.Fs+ mods := b.H.Configs.Modules++ b.Assert(npm.Pack(sourceFs, assetsFs, mods), qt.IsNil)++ data, err := afero.ReadFile(sourceFs, "packages/hugoautogen/package.json")+ b.Assert(err, qt.IsNil)++ pkg := make(map[string]any)+ b.Assert(json.Unmarshal(data, &pkg), qt.IsNil)++ b.Assert(pkg["private"], qt.Equals, true)+ b.Assert(pkg["name"], qt.Equals, "hugoautogen")+}+ func BenchmarkPackageFilesSum(b *testing.B) { files := getPackageBuilderTestFiles() bb := hugolib.Test(b, files)testscripts/commands/mod_npm.txtindex 169f26c17..136277fc5 100644@@ -119,7 +119,8 @@ usePackageJSON="auto" "strip-ansi": "7.2.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden1/package.json --@@ -149,7 +150,8 @@ usePackageJSON="auto" "strip-ansi": "7.2.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden3/packages/hugoautogen/package.json --@@ -168,7 +170,8 @@ usePackageJSON="auto" "strip-ansi": "7.2.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden4/packages/hugoautogen/package.json --@@ -188,7 +191,8 @@ usePackageJSON="auto" "strip-ansi": "7.2.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden5/packages/hugoautogen/package.json --@@ -206,7 +210,8 @@ usePackageJSON="auto" "strip-ansi": "7.2.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- go.mod --testscripts/commands/mod_npm__moduleorder.txtindex 4dc202046..20228e548 100644@@ -42,7 +42,8 @@ go 1.20 "strip-ansi": "7.0.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm__moduleorder",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden1/packages/hugoautogen/hugo_packagemeta.json --testscripts/commands/mod_npm_withexisting.txtindex b11a4a3b6..aa5a77bca 100644@@ -37,7 +37,8 @@ path="github.com/gohugoio/hugoTestModule2" "@babel/preset-env": "7.9.5", "postcss-cli": "7.1.0" },- "name": "script-mod_npm_withexisting",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden/package.json --
modules/npm/package_builder.goindex cea1dc8cd..631036878 100644@@ -195,15 +195,28 @@ func Pack(sourceFs, assetsWithDuplicatesPreservedFs afero.Fs, mods modules.Modul } } - name := "project"- rfi, err := sourceFs.Stat("")- if err == nil {- name = rfi.Name()+ // Stable defaults; mark the workspace private so npm never publishes it.+ // Preserve any hand-set values from an existing file.+ name, version, private := "hugoautogen", "0.1.0", true+ if data, err := afero.ReadFile(sourceFs, workspacePackageJSON); err == nil {+ var existing map[string]any+ if err := json.Unmarshal(data, &existing); err == nil {+ if s, ok := existing["name"].(string); ok && s != "" {+ name = s+ }+ if s, ok := existing["version"].(string); ok && s != "" {+ version = s+ }+ if b, ok := existing["private"].(bool); ok {+ private = b+ }+ } } autoGenPkg := map[string]any{ "name": name,- "version": "0.1.0",+ "version": version,+ "private": private, dependenciesKey: moduleDeps, devDependenciesKey: moduleDevDeps, }modules/npm/package_builder_integration_test.goindex 65649d7ce..bf3c91bcd 100644@@ -92,6 +92,40 @@ func TestPackageBuilder(t *testing.T) { b.Assert(string(meta2), qt.Equals, string(meta1)) } +// The generated workspace package.json should get a stable name and+// "private": true, and preserve hand-set name/version/private on regeneration.+// See issue 15245.+func TestPackStableNameAndPrivate(t *testing.T) {+ files := getPackageBuilderTestFiles()+ b := hugolib.Test(t, files)+ sourceFs := b.H.BaseFs.ProjectSourceFs+ assetsFs := b.H.BaseFs.AssetsWithDuplicatesPreserved.Fs+ mods := b.H.Configs.Modules++ b.Assert(npm.Pack(sourceFs, assetsFs, mods), qt.IsNil)++ pkg, err := afero.ReadFile(sourceFs, "packages/hugoautogen/package.json")+ b.Assert(err, qt.IsNil)+ b.Assert(string(pkg), qt.Contains, `"name": "hugoautogen"`)+ b.Assert(string(pkg), qt.Contains, `"private": true`)+ b.Assert(string(pkg), qt.Contains, `"version": "0.1.0"`)++ // Hand-set fields survive a re-pack.+ edited := strings.NewReplacer(+ `"name": "hugoautogen"`, `"name": "@foo/hugoautogen"`,+ `"version": "0.1.0"`, `"version": "1.2.3"`,+ `"private": true`, `"private": false`,+ ).Replace(string(pkg))+ b.Assert(afero.WriteFile(sourceFs, "packages/hugoautogen/package.json", []byte(edited), 0o666), qt.IsNil)++ b.Assert(npm.Pack(sourceFs, assetsFs, mods), qt.IsNil)+ pkg, err = afero.ReadFile(sourceFs, "packages/hugoautogen/package.json")+ b.Assert(err, qt.IsNil)+ b.Assert(string(pkg), qt.Contains, `"name": "@foo/hugoautogen"`)+ b.Assert(string(pkg), qt.Contains, `"version": "1.2.3"`)+ b.Assert(string(pkg), qt.Contains, `"private": false`)+}+ func BenchmarkPackageFilesSum(b *testing.B) { files := getPackageBuilderTestFiles() bb := hugolib.Test(b, files)testscripts/commands/mod_npm.txtindex 169f26c17..136277fc5 100644@@ -119,7 +119,8 @@ usePackageJSON="auto" "strip-ansi": "7.2.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden1/package.json --@@ -149,7 +150,8 @@ usePackageJSON="auto" "strip-ansi": "7.2.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden3/packages/hugoautogen/package.json --@@ -168,7 +170,8 @@ usePackageJSON="auto" "strip-ansi": "7.2.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden4/packages/hugoautogen/package.json --@@ -188,7 +191,8 @@ usePackageJSON="auto" "strip-ansi": "7.2.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden5/packages/hugoautogen/package.json --@@ -206,7 +210,8 @@ usePackageJSON="auto" "strip-ansi": "7.2.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- go.mod --testscripts/commands/mod_npm__moduleorder.txtindex 4dc202046..20228e548 100644@@ -42,7 +42,8 @@ go 1.20 "strip-ansi": "7.0.0", "to-pascal-case": "1.0.0" },- "name": "script-mod_npm__moduleorder",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden1/packages/hugoautogen/hugo_packagemeta.json --testscripts/commands/mod_npm_withexisting.txtindex b11a4a3b6..aa5a77bca 100644@@ -37,7 +37,8 @@ path="github.com/gohugoio/hugoTestModule2" "@babel/preset-env": "7.9.5", "postcss-cli": "7.1.0" },- "name": "script-mod_npm_withexisting",+ "name": "hugoautogen",+ "private": true, "version": "0.1.0" } -- golden/package.json --
What the agent reported
The fix is complete and verified. The only files I changed are the five related to this fix; the AGENTS.md/CLAUDE.md deletions were already present in the working tree at session start (per the initial git status snapshot) and are unrelated to this task, so I left them untouched.
Summary
What was wronghugo mod npm pack (modules/npm/package_builder.go, Pack) regenerated packages/hugoautogen/package.json with a "name" derived from the checkout/working-directory basename (sourceFs.Stat("") → rfi.Name()) and no "private": true. Committed copies of the generated manifest were therefore publishable by default (npm workspace tooling packs them), and the name churned across differently-named clones.
What I changed
modules/npm/package_builder.go:- Added constant
defaultPackageName = "hugoautogen"(a stable, project-controlled name matching thepackages/hugoautogenautogen folder). - Removed the directory-basename derivation and set
"name": defaultPackageName. - Added
"private": trueto the generatedautoGenPkgmap. modules/npm/package_builder_integration_test.go: added regression testTestPackageBuilderPackageJSONthat runsnpm.Pack, reads the generatedpackages/hugoautogen/package.json, and assertsprivate == trueandname == "hugoautogen".- Updated the golden fixtures that encoded the old behavior (7 generated-package blocks across
testscripts/commands/mod_npm.txt,mod_npm_withexisting.txt,mod_npm__moduleorder.txt): name →hugoautogen, added"private": true(alphabetical key order: dependencies, devDependencies, name, private, version). The site's own rootpackage.jsongoldens and allhugo_packagemeta.jsonblocks were left untouched. Thesecmpassertions remain equally strict — only the expected value was corrected to match the intended fix.
Test evidence
- Before fix:
TestPackageBuilderPackageJSONFAILED —pkg["private"]wasnil, wanttrue. - After fix (all in-container via
./bt-test): ./bt-test ./modules/npm/→TestPackageBuilder(both npm and npm_test packages) PASS,TestPackageBuilderPackageJSONPASS, packageok../bt-test . -run TestCommands/mod_npm→TestCommands/mod_npm,TestCommands/mod_npm_withexisting,TestCommands/mod_npm__moduleorderall PASS.
Blast radius: Grep confirmed no code in modules/npm/ (or modules/collect.go workspace-resolution logic) reads the generated package's name field — workspaces are referenced by path (FolderPackagesHugoAutoGen), so removing the basename derivation is safe. The pinned PackageFilesSum hash and hugo_packagemeta.json goldens are unaffected because that sum hashes input package files, not the generated output (verified: those goldens/hashes needed no change and the tests confirm it).
Requirements checklist
- Emit
"private": truein the generated manifest — ✅ Implemented (package_builder.go), asserted byTestPackageBuilderPackageJSON(fails before, passes after) and by the three testscript goldens. - Stable, project-controlled default name instead of checkout-directory basename — ✅ Implemented (
defaultPackageName = "hugoautogen"), asserted by the regression test (name == "hugoautogen") and testscripts (previouslyscript-<testname>, nowhugoautogen). - "Ideally the default is scoped" — Skipped by deliberate design decision (report offered this as optional and deferred to maintainer preference: "if you have a preference for what it should be"). With
"private": truethe manifest is never published, so the registry-squatting concern that motivates scoping does not apply; a bare stable name fully resolves the churn concern. Evidence: the report's own rationale ("a bare name can be claimed on the registry by anyone") is neutralized by theprivateflag, which the fix adds. Chose the non-arbitraryhugoautogen(matching the autogen folder) over inventing a scope Hugo does not own.