Backtestshugorun-20261009-153657#15245

Issue #15245 Merged fix PR #15246 Run 9 Oct 2026 · run-20261009-153657 gohugoio/hugo

hugo mod npm pack emits a publishable, checkout-named package.json

Better

Our patch was judged better than the merged fix.

In this judgment, Patch B = our agent, Patch A = the merged fix.

Both patches drop the checkout-derived name, default to "hugoautogen", add "private": true, and update the testscript goldens the same way.

Patch A also reads the existing package.json and keeps its name, version and private values. This keeps the root bug alive for anyone who already ran `hugo mod npm pack`. A committed manifest named after a checkout ("site-a", "script-mod_npm", etc.) is carried forward on every repack and never normalized. It can also keep "private": false. So the issue's core expectation, a stable project-controlled name plus the do-not-publish guard, is not reliably met for existing repos. Generation also now depends on prior file state, which makes output less deterministic. The benefit is that hand-set scoped names like docsy's survive, which the issue does mention. But the issue asks Hugo to emit a good default, and A achieves preservation by trusting whatever is on disk, generated or not.

Patch B always emits a fixed name and private: true. Output is deterministic and independent of the checkout directory, and stale checkout-named manifests are normalized, which fixes the root cause directly.

On tests, B is stronger:
- It explicitly packs into differently named base paths (/site-a, /site-b) and asserts identical output, which is the exact scenario in the issue.
- Its new testscript checks that an old checkout-named manifest is normalized.

A's test checks the defaults and its own preservation behavior.

B's doc and help-text updates are neutral but accurate. B is the more correct and better-tested fix.

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

33m 49s

Wall-clock time in the container

Full test suite

No new failures

79 passed · 2 skipped · 81 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

111vs 78 merged

Changed lines, ours vs merged

TL;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

Our patch+97 −14
commands/mod.goindex eed9cfb0f..0ba117097 100644@@ -55,6 +55,7 @@ func newModCommands() *modCommands {  The merged dependencies are written to packages/hugoautogen/package.json, and the root package.json is updated with a "workspaces" entry pointing to "packages/hugoautogen".+The generated package is named "hugoautogen" and is marked "private": true, so it is never published.  The source entries are read from either package.hugo.json or package.json in the module root, with package.hugo.json taking precedence if both exist. docs/content/en/commands/hugo_mod_npm_pack.mdindex 4482a4d1e..3292930e8 100644@@ -13,6 +13,7 @@ Merges Node.js dependencies from all Hugo modules into a "packages/hugoautogen"  The merged dependencies are written to packages/hugoautogen/package.json, and the root package.json is updated with a "workspaces" entry pointing to "packages/hugoautogen".+The generated package is named "hugoautogen" and is marked "private": true, so it is never published.  The source entries are read from either package.hugo.json or package.json in the module root, with package.hugo.json taking precedence if both exist. docs/content/en/hugo-modules/nodejs-dependencies.mdindex 98384e36c..80cd7972d 100644@@ -24,6 +24,8 @@ after v0.174.0, 15 minor releases after the improvement.  Run [`hugo mod npm pack`][] to collect Node dependencies from all modules and write them to `packages/hugoautogen/package.json`. Hugo also adds a `workspaces` entry to your project's root `package.json` pointing to this auto-generated package. +The auto-generated package is always named `hugoautogen`, regardless of the name of the directory the project is checked out in, and is marked `"private": true` so that npm and release tools never publish it.+ The resulting project structure:  ```treemodules/npm/package_builder.goindex cea1dc8cd..92be234d8 100644@@ -43,6 +43,9 @@ const (  	packageJSONName     = "package.json" 	packageMetaJSONName = "hugo_packagemeta.json"++	// workspacePackageName is the name of the generated workspace package.+	workspacePackageName = "hugoautogen" )  var (@@ -195,15 +198,13 @@ func Pack(sourceFs, assetsWithDuplicatesPreservedFs afero.Fs, mods modules.Modul 		} 	} -	name := "project"-	rfi, err := sourceFs.Stat("")-	if err == nil {-		name = rfi.Name()-	}-+	// The workspace is internal to the project: use a stable name that does+	// not depend on the checkout directory, and mark it private so that+	// npm (and release tools) never publish it. 	autoGenPkg := map[string]any{-		"name":             name,+		"name":             workspacePackageName, 		"version":          "0.1.0",+		"private":          true, 		dependenciesKey:    moduleDeps, 		devDependenciesKey: moduleDevDeps, 	}modules/npm/package_builder_integration_test.goindex 65649d7ce..fc494d4b7 100644@@ -14,6 +14,7 @@ package npm_test  import (+	"encoding/json" 	"strings" 	"testing" @@ -92,6 +93,42 @@ func TestPackageBuilder(t *testing.T) { 	b.Assert(string(meta2), qt.Equals, string(meta1)) } +func TestPackageBuilderWorkspacePackageIsPrivateWithStableName(t *testing.T) {+	files := getPackageBuilderTestFiles()+	b := hugolib.Test(t, files)+	assetsFs := b.H.BaseFs.AssetsWithDuplicatesPreserved.Fs+	mods := b.H.Configs.Modules++	readWorkspacePackage := func(fs afero.Fs) (string, map[string]any) {+		data, err := afero.ReadFile(fs, "packages/hugoautogen/package.json")+		b.Assert(err, qt.IsNil)+		var m map[string]any+		b.Assert(json.Unmarshal(data, &m), qt.IsNil)+		return string(data), m+	}++	b.Assert(npm.Pack(b.H.BaseFs.ProjectSourceFs, assetsFs, mods), qt.IsNil)+	projectContent, m := readWorkspacePackage(b.H.BaseFs.ProjectSourceFs)+	b.Assert(m["name"], qt.Equals, "hugoautogen")+	b.Assert(m["private"], qt.Equals, true)+	b.Assert(m["version"], qt.Equals, "0.1.0")++	// Packing the same project in differently named checkouts must+	// produce identical workspace manifests.+	var contents []string+	for _, checkout := range []string{"/site-a", "/site-b"} {+		sourceFs := afero.NewBasePathFs(afero.NewMemMapFs(), checkout)+		b.Assert(sourceFs.MkdirAll("", 0o777), qt.IsNil)+		b.Assert(npm.Pack(sourceFs, assetsFs, mods), qt.IsNil)+		content, m := readWorkspacePackage(sourceFs)+		b.Assert(m["name"], qt.Equals, "hugoautogen")+		b.Assert(m["private"], qt.Equals, true)+		contents = append(contents, content)+	}+	b.Assert(contents[0], qt.Equals, contents[1])+	b.Assert(contents[0], qt.Equals, projectContent)+}+ 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_private.txtnew file mode 100644index 000000000..cb3246e37@@ -0,0 +1,34 @@+# Test that mod npm pack writes a private workspace package with a stable name,+# independent of the checkout directory name.++hugo mod npm pack+cmp packages/hugoautogen/package.json golden/packages/hugoautogen/package.json++# A previously generated, checkout-named manifest is normalized on the next pack.+cp edited.json packages/hugoautogen/package.json+hugo mod npm pack+cmp packages/hugoautogen/package.json golden/packages/hugoautogen/package.json++-- hugo.toml --+baseURL = "https://example.org/"+-- package.json --+{+  "name": "mysite",+  "version": "1.0.0",+  "dependencies": {+    "is-odd": "3.0.0"+  }+}+-- edited.json --+{+  "name": "script-mod_npm_private",+  "version": "0.1.0"+}+-- golden/packages/hugoautogen/package.json --+{+  "dependencies": {},+  "devDependencies": {},+  "name": "hugoautogen",+  "private": true,+  "version": "0.1.0"+}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 -- 
Merged fix · PR #15246+66 −12
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 --