Backtestshugorun-20261024-162305#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 drop the checkout-basename name, so both fix the core publishability problem. B uses a fixed scoped name (@gohugoio/hugoautogen), which is exactly what the issue asks for ("Ideally the default is scoped"), is fully deterministic across checkouts, and also updates the docs. A chooses a bare default ("hugoautogen"), which the issue explicitly calls out as claimable by anyone on the registry, and layers on a preserve-existing-fields mechanism. That mechanism has a real downside for the issue's own scenario: a repo whose committed manifest was generated by an older Hugo (name "site-a", no private) will keep the checkout-derived name forever after upgrade, because A copies any non-empty existing name. The independent TestBtJudgePackFreshCloneOfPreviouslyPackedSite and TestBtJudgePackDefaultNameIsScoped fail on A and pass on B for precisely these reasons, and I confirmed from the diff that this is genuine behaviour, not a test artifact. A's preserve-fields behaviour is also beyond the issue's ask and adds risk (e.g. honoring private=false, silently carrying stale names). On tests, both add meaningful integration tests and update goldens; B's test additionally exercises two differently named checkouts and asserts byte-identical output, which directly matches the reported symptom. B is simpler, more correct against the issue text, and better tested.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
19m 4s
Wall-clock time in the container
Full test suite
No new failures
81 passed · 2 skipped · 97 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
78vs 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..462b460af 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 `@gohugoio/hugoautogen` and is marked `"private": true`, so it is the same in every checkout and is never published to a registry.+ The resulting project structure: ```treemodules/npm/package_builder.goindex cea1dc8cd..d02dcdf53 100644@@ -43,6 +43,10 @@ const ( packageJSONName = "package.json" packageMetaJSONName = "hugo_packagemeta.json"++ // workspacePackageName is the name of the autogenerated workspace package.+ // It is stable across checkouts and scoped so it cannot be claimed on the registry by others.+ workspacePackageName = "@gohugoio/hugoautogen" ) var (@@ -195,15 +199,11 @@ 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 and must never be published. 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..84e39dfce 100644@@ -14,6 +14,7 @@ package npm_test import (+ "encoding/json" "strings" "testing" @@ -92,6 +93,46 @@ func TestPackageBuilder(t *testing.T) { b.Assert(string(meta2), qt.Equals, string(meta1)) } +// Issue: the generated workspace package.json must not be publishable+// and its name must not depend on the checkout directory.+func TestPackageBuilderWorkspacePackageIsPrivateWithStableName(t *testing.T) {+ files := getPackageBuilderTestFiles()+ b := hugolib.Test(t, files)+ assetsFs := b.H.BaseFs.AssetsWithDuplicatesPreserved.Fs+ mods := b.H.Configs.Modules++ readPkg := func(sourceFs afero.Fs) map[string]any {+ data, err := afero.ReadFile(sourceFs, "packages/hugoautogen/package.json")+ b.Assert(err, qt.IsNil)+ var m map[string]any+ b.Assert(json.Unmarshal(data, &m), qt.IsNil)+ return m+ }++ sourceFs := b.H.BaseFs.ProjectSourceFs+ b.Assert(npm.Pack(sourceFs, assetsFs, mods), qt.IsNil)+ pkg := readPkg(sourceFs)+ b.Assert(pkg["private"], qt.Equals, true)+ b.Assert(pkg["name"], qt.Equals, "@gohugoio/hugoautogen")+ b.Assert(pkg["version"], qt.Equals, "0.1.0")++ // Two checkouts of the same project in differently named directories.+ mem := afero.NewMemMapFs()+ for _, dir := range []string{"/site-a", "/site-b"} {+ checkoutFs := afero.NewBasePathFs(mem, dir)+ b.Assert(afero.WriteFile(checkoutFs, "package.json", []byte(`{"name": "mysite"}`), 0o666), qt.IsNil)+ b.Assert(npm.Pack(checkoutFs, assetsFs, mods), qt.IsNil)+ pkg := readPkg(checkoutFs)+ b.Assert(pkg["private"], qt.Equals, true)+ b.Assert(pkg["name"], qt.Equals, "@gohugoio/hugoautogen")+ }+ a, err := afero.ReadFile(mem, "/site-a/packages/hugoautogen/package.json")+ b.Assert(err, qt.IsNil)+ bb, err := afero.ReadFile(mem, "/site-b/packages/hugoautogen/package.json")+ b.Assert(err, qt.IsNil)+ b.Assert(string(a), qt.Equals, string(bb))+}+ 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 --