Backtestshugorun-20260930-140149#15245
hugo mod npm pack emits a publishable, checkout-named package.json
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 replace the checkout-derived name with a fixed default. The difference is in how they handle files that already exist. Patch A reads the existing `packages/hugoautogen/package.json` and keeps its `name`, `version` and `private` values. Every site that has already run `hugo mod npm pack` has a file whose name came from the checkout directory (e.g. `site-a`). Under A, that name is carried forward on every later run. The per-clone name variance the issue reports is therefore never fixed for existing users; only fresh workspaces get the new default. A also keeps `"private": false` if it is present. Its default name, `hugoautogen`, is unscoped, although the issue specifically says a bare name can be claimed on the registry by anyone and asks for a scoped one. Patch B always emits the scoped name `@gohugoio/hugoautogen` and `"private": true`. That matches the issue's stated expectation: non-publishable, stable across checkouts, and scoped. It removes the root cause, and existing manifests converge on the next pack. Tests: both update the testscript golden files. A's new test mainly checks its preservation behaviour, and that behaviour is the source of the flaw above. B's test runs pack under two different working-directory names and parses the JSON to check name, private and version. That directly exercises the reported variance and would fail before the fix. Scope and craft: B introduces a named constant, a short comment and a docs note, with minimal code change. A adds merge logic the issue did not strictly require, and that logic carries the risk described above. B is the better 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
38m 16s10 turns
Wall-clock time in the container
Full test suite
No new failures
3,731 passed · 99 skipped · 3,832 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
64vs 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
docs/content/en/hugo-modules/nodejs-dependencies.mdindex 98384e36c..b175a9c43 100644@@ -22,7 +22,7 @@ after v0.174.0, 15 minor releases after the improvement. ## Consolidating with `hugo mod npm pack` -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.+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 generated package is named `@gohugoio/hugoautogen` and marked `"private": true`. The resulting project structure: modules/npm/package_builder.goindex cea1dc8cd..485168322 100644@@ -43,6 +43,8 @@ const ( packageJSONName = "package.json" packageMetaJSONName = "hugo_packagemeta.json"++ autoGenPackageName = "@gohugoio/hugoautogen" ) var (@@ -195,14 +197,10 @@ func Pack(sourceFs, assetsWithDuplicatesPreservedFs afero.Fs, mods modules.Modul } } - name := "project"- rfi, err := sourceFs.Stat("")- if err == nil {- name = rfi.Name()- }-+ // Private so npm never publishes it; stable name so it doesn't vary per checkout. autoGenPkg := map[string]any{- "name": name,+ "name": autoGenPackageName,+ "private": true, "version": "0.1.0", dependenciesKey: moduleDeps, devDependenciesKey: moduleDevDeps,modules/npm/package_builder_integration_test.goindex 65649d7ce..00d036130 100644@@ -14,6 +14,7 @@ package npm_test import (+ "encoding/json" "strings" "testing" @@ -92,6 +93,34 @@ func TestPackageBuilder(t *testing.T) { b.Assert(string(meta2), qt.Equals, string(meta1)) } +// Issue: the generated packages/hugoautogen/package.json must have a stable+// name (not derived from the checkout directory) and be marked private.+func TestPackageBuilderAutoGenPackageJSONPrivateAndStableName(t *testing.T) {+ for _, dir := range []string{"mycheckout", "other-checkout-dir"} {+ t.Run(dir, func(t *testing.T) {+ files := getPackageBuilderTestFiles()+ b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(c *hugolib.IntegrationTestConfig) {+ c.NeedsOsFS = true+ c.WorkingDir = dir+ }))+ 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)++ var pkg map[string]any+ b.Assert(json.Unmarshal(data, &pkg), qt.IsNil)+ b.Assert(pkg["name"], qt.Equals, "@gohugoio/hugoautogen")+ b.Assert(pkg["private"], qt.Equals, true)+ b.Assert(pkg["version"], qt.Equals, "0.1.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 --
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
hugo mod npm pack now marks the generated packages/hugoautogen/package.json as "private": true and always names it @gohugoio/hugoautogen, no matter which directory the site is checked out into. The new regression test failed before the change and passes after it. The three mod_npm* command tests could not run here because they need to fetch modules from github.com and there is no network.
What was wrong: In Pack (modules/npm/package_builder.go), the name field came from the site root directory's name, so cloning into site-a gave "name": "site-a". The file also had no private field. Hugo rewrites the whole file on every run, so any fields set by hand were lost.
What I changed:
modules/npm/package_builder.go: added a constantautoGenPackageName = "@gohugoio/hugoautogen"and dropped the code that read the directory name. The generated file now has"name": "@gohugoio/hugoautogen","private": trueand"version": "0.1.0", plus the dependencies as before.docs/content/en/hugo-modules/nodejs-dependencies.md: one sentence stating the package's name and that it is private.- Tests:
- A new test,
TestPackageBuilderAutoGenPackageJSONPrivateAndStableNameinmodules/npm/package_builder_integration_test.go, runs the pack on the real filesystem from two differently named checkout folders (mycheckout,other-checkout-dir). It checks thatname,privateandversioncome out the same in both. - I updated the expected output in
testscripts/commands/mod_npm.txt,mod_npm__moduleorder.txtandmod_npm_withexisting.txt. In each, the old"name": "script-…"line (which came from the test's own folder name) became the new name plus"private": true. This follows from the fix rather than loosening the tests. The checksum lines in those files don't change, because the checksum never includes the generated file.
Test evidence:
- Before the fix: the new test failed with
got: "mycheckout"andgot: "other-checkout-dir"againstwant: "@gohugoio/hugoautogen". The existingTestPackageBuilderpassed. - After the fix: everything in
./modules/npm/passes, including both new subtests andTestPackageBuilder, and gofmt reports nothing. - Command tests:
TestCommands/mod_npm*stop at their firsthugo mod npm packwithCould not resolve host: github.com. They would fail the same way without my change, so their updated expected output is checked by reading it against the JSON encoder's output, not by a run. - Setup note:
./bt-testonly worked withGOMODCACHE=/bt/cache/go-mod GOPROXY=offadded, because the default Go module cache here is nearly empty and can't download.
Impact on existing behaviour: the only thing that used the old folder-based name was this file's name field. Sites that commit the file will see a one-time change to name and private in the file and in the lockfile's workspace entry, and after that it stays the same across clones. A reviewer pass found no unrelated changes.
Requirements from the report:
| Ask | Outcome |
|---|---|
Emit "private": true | Done; checked by the new test and the updated expected output. |
| Stable default name, not the folder name | Done; the test shows two different folder names give the same result. |
| Ideally a scoped name | Done: @gohugoio/hugoautogen. I chose the gohugoio scope on the assumption that Hugo's maintainers control it on npm, so nobody else could publish that name; I haven't confirmed that. |
| Hand-set fields don't survive a rerun | Not done: I did not add keeping an existing name. An already-committed "site-a" would then stay forever, which is the same problem again. Now that the default is stable and private, the docsy-example npm pkg set workaround should no longer be needed. |
Scoped name + private together (docsy case) | Covered by the two items above. |
The AGENTS.md and CLAUDE.md deletions in the working tree were already there when I started, and I left them alone. Nothing is committed.