Backtestshugorun-20261013-011256#15245

Issue #15245 Merged fix PR #15246 Run 13 Oct 2026 · run-20261013-011256 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 add `"private": true` and drop the checkout-directory-derived name, so both address the core complaint.

Correctness favors B.
- **Name choice.** B uses a stable, scoped default (`@gohugoio/hugoautogen`), which is exactly what the issue's 'Ideally the default is scoped' asks for. A picks the bare `hugoautogen`, which the issue says anyone can claim on the registry.
- **A's preservation logic causes a migration problem.** A keeps name, version and private from any existing generated file. Every project that already committed a manifest from the old behavior (e.g. `"name": "site-a"`) keeps that checkout-derived name forever. The root cause is never cleaned up for existing users.
- **A also accepts `private: false`.** It will faithfully preserve a hand-set `false`, so the do-not-publish guard can be silently reverted.
- **Where A has an edge.** Preserving hand-set values does help sites like docsy that want a custom scope. But that is beyond the issue's stated expectation of a stable default plus private, and it turns a generated file into a partially hand-maintained one, which adds risk.
- **B is deterministic.** It emits the same output regardless of checkout or prior state, which also removes the lockfile churn the issue describes.

Tests:
- Both update the testscript goldens. Those goldens would fail before the change, since they previously contained `script-mod_npm` etc.
- B's new test directly reproduces the issue scenario: two differently named checkouts (`site-a` and `site-b`) via BasePathFs. It asserts identical output with no directory name leaking, and checks parsed JSON fields rather than substrings.
- A's test checks the defaults and its own preservation feature, but not the checkout-name independence.

Scope and craft: B adds a named constant with an explanatory comment and a short doc note, a small, low-risk change. A's extra read-and-merge logic widens behavior and leaves legacy names in place. Overall B is the cleaner and more faithful 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

16m 42s

Wall-clock time in the container

Full test suite

No new failures

64 passed · 2 skipped · 80 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

89vs 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+74 −15
docs/content/en/hugo-modules/nodejs-dependencies.mdindex 98384e36c..80ca8ef84 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 named `@gohugoio/hugoautogen` regardless of the project's directory name, and is marked `"private": true` so that npm and workspace-aware release tools will not publish it.+ The resulting project structure:  ```treemodules/npm/package_builder.goindex cea1dc8cd..bc75e7212 100644@@ -43,6 +43,11 @@ const (  	packageJSONName     = "package.json" 	packageMetaJSONName = "hugo_packagemeta.json"++	// autoGenPackageName is the name of the generated workspace package.+	// It is stable across checkouts and scoped so that it cannot be claimed+	// on the npm registry by anyone but the scope owner.+	autoGenPackageName = "@gohugoio/hugoautogen" )  var (@@ -195,15 +200,12 @@ 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,-		"version":          "0.1.0",+		"name":    autoGenPackageName,+		"version": "0.1.0",+		// The generated workspace is internal to the project; prevent it+		// from being published by npm and workspace-aware release tools.+		"private":          true, 		dependenciesKey:    moduleDeps, 		devDependenciesKey: moduleDevDeps, 	}modules/npm/package_builder_integration_test.goindex 65649d7ce..49c318749 100644@@ -14,6 +14,7 @@ package npm_test  import (+	"encoding/json" 	"strings" 	"testing" @@ -92,6 +93,53 @@ func TestPackageBuilder(t *testing.T) { 	b.Assert(string(meta2), qt.Equals, string(meta1)) } +// The generated workspace package.json must be non-publishable and have+// a stable name that does not depend on the checkout directory's name.+func TestPackageBuilderAutoGenPackageNameAndPrivate(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++	readAutoGen := 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(sourceFs, assetsFs, mods), qt.IsNil)+	_, pkg := readAutoGen(sourceFs)+	b.Assert(pkg["name"], qt.Equals, "@gohugoio/hugoautogen")+	b.Assert(pkg["private"], qt.Equals, true)+	b.Assert(pkg["version"], qt.Equals, "0.1.0")++	// Two clones of the same project in differently named directories.+	mem := afero.NewMemMapFs()+	rootPackageJSON := []byte(`{+  "name": "mysite",+  "dependencies": {+    "foo": "1.0.0"+  }+}+`)+	var outputs []string+	for _, dir := range []string{"/work/site-a", "/work/site-b"} {+		b.Assert(afero.WriteFile(mem, dir+"/package.json", rootPackageJSON, 0o666), qt.IsNil)+		checkoutFs := afero.NewBasePathFs(mem, dir)+		b.Assert(npm.Pack(checkoutFs, assetsFs, mods), qt.IsNil)+		content, pkg := readAutoGen(checkoutFs)+		b.Assert(pkg["name"], qt.Equals, "@gohugoio/hugoautogen")+		b.Assert(pkg["private"], qt.Equals, true)+		b.Assert(content, qt.Not(qt.Contains), "site-a")+		b.Assert(content, qt.Not(qt.Contains), "site-b")+		outputs = append(outputs, content)+	}+	b.Assert(outputs[1], qt.Equals, outputs[0])+}+ func BenchmarkPackageFilesSum(b *testing.B) { 	files := getPackageBuilderTestFiles() 	bb := hugolib.Test(b, files)testscripts/commands/mod_npm.txtindex 169f26c17..82d409d22 100644@@ -119,7 +119,8 @@ usePackageJSON="auto"     "strip-ansi": "7.2.0",     "to-pascal-case": "1.0.0"   },-  "name": "script-mod_npm",+  "name": "@gohugoio/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": "@gohugoio/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": "@gohugoio/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": "@gohugoio/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": "@gohugoio/hugoautogen",+  "private": true,   "version": "0.1.0" } -- go.mod --testscripts/commands/mod_npm__moduleorder.txtindex 4dc202046..a765c285a 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": "@gohugoio/hugoautogen",+  "private": true,   "version": "0.1.0" } -- golden1/packages/hugoautogen/hugo_packagemeta.json --testscripts/commands/mod_npm_withexisting.txtindex b11a4a3b6..ffd8a1234 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": "@gohugoio/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 --