{"id":"80d1ab268d4291aa","repo":"gohugoio/hugo","slug":"npm-pack-failed-to-marshal-package-json-with-upda","errorCode":null,"errorMessage":"npm pack: failed to marshal package.json with updated workspaces: %w","messagePattern":"npm pack: failed to marshal package\\.json with updated workspaces: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"modules/npm/package_builder.go","lineNumber":292,"sourceCode":"\t\tpackagesSlice := toStringSlice(packagesVal)\n\t\tif slices.Contains(packagesSlice, workspacePath) {\n\t\t\tif runtime.GOOS == \"windows\" {\n\t\t\t\treturn afero.WriteFile(fsys, packageJSONName, data, 0o666)\n\t\t\t}\n\t\t\treturn nil\n\t\t}\n\n\t\t// Append the new workspace path to the packages slice.\n\t\tnewPkgs := make([]any, 0, len(packagesSlice)+1)\n\t\tfor _, s := range packagesSlice {\n\t\t\tnewPkgs = append(newPkgs, s)\n\t\t}\n\t\tnewPkgs = append(newPkgs, workspacePath)\n\t\tv[\"packages\"] = newPkgs\n\n\t\tupdated, err := json.MarshalIndent(pkg, \"\", indent)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"npm pack: failed to marshal package.json with updated workspaces: %w\", err)\n\t\t}\n\t\tupdated = append(updated, '\\n')\n\t\treturn afero.WriteFile(fsys, packageJSONName, updated, 0o666)\n\tcase nil:\n\t\t// Treat explicit null as \"no workspaces\"; handled below as missing key.\n\tdefault:\n\t\treturn fmt.Errorf(\"npm pack: unsupported workspaces type %T\", v)\n\t}\n\n\t// Try adding to existing workspaces array when present.\n\tif hasWS && wsVal != nil {\n\t\tif wsIdx := bytes.Index(data, []byte(`\"workspaces\"`)); wsIdx >= 0 {\n\t\t\trest := data[wsIdx:]\n\t\t\tif bracketOpen := bytes.IndexByte(rest, '['); bracketOpen >= 0 {\n\t\t\t\tif bracketClose := bytes.IndexByte(rest[bracketOpen:], ']'); bracketClose >= 0 {\n\t\t\t\t\tpos := wsIdx + bracketOpen + bracketClose\n\t\t\t\t\tarrayContent := bytes.TrimSpace(data[wsIdx+bracketOpen+1 : pos])\n\t\t\t\t\tvar insertion string","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/modules/npm/package_builder.go#L274-L310","documentation":"After appending Hugo's autogenerated workspace path to the object-form `workspaces.packages` array, Hugo re-serializes the whole package.json with json.MarshalIndent. This error wraps a marshal failure at that step. Since the input was just unmarshalled from JSON, marshal failures here are essentially never seen in practice — it's a defensive wrap around the encoder.","triggerScenarios":"`hugo mod npm pack` taking the object-form workspaces path (map with `packages`), followed by json.MarshalIndent failing — which for data round-tripped through json.Unmarshal (only strings, numbers, bools, maps, slices) does not normally occur.","commonSituations":"Practically unheard of in normal use; would require the in-memory package map to contain unmarshalable values, pointing at a Hugo bug or memory corruption rather than user config.","solutions":["Re-run the command; if it reproduces, note this indicates an internal inconsistency.","Convert `workspaces` to the simpler array form to bypass the object-form re-marshal path entirely.","Report the reproducer to the Hugo issue tracker with your package.json and Hugo version."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// ensure workspaces contains only JSON-serializable values (strings)\nfor _, w := range workspaces {\n    if _, ok := w.(string); !ok {\n        return fmt.Errorf(\"workspace entry %v is not a string\", w)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := b.Build(); err != nil {\n    if strings.Contains(err.Error(), \"failed to marshal package.json\") {\n        log.Fatal(\"package.json contains values that cannot be re-serialized; simplify workspaces to a string array\")\n    }\n    return err\n}","preventionTips":["Keep workspaces entries as plain strings/globs","Avoid exotic nested structures in package.json that round-trip poorly through generic JSON handling"],"tags":["hugo","npm-pack","json-marshal","internal"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}