{"record":{"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/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/modules/npm/package_builder.go#L274-L310","documentation":"In ensureWorkspaceRef (modules/npm/package_builder.go:292). After appending the workspace path to the packages array of the workspaces object form, Hugo re-marshals the whole package.json with json.MarshalIndent using the detected indent. Failure is wrapped. json.MarshalIndent on a map[string]any parsed from valid JSON essentially never fails, so this is a defensive guard.","triggerScenarios":"The reassembled package object contains a value type json.Marshal cannot encode (e.g. a NaN float, a channel, or a circular structure introduced by an exotic field). Practically unreachable from normal package.json content.","commonSituations":"Corrupted in-memory representation after manual mutation; extremely unusual non-string value types in package.json that survived the earlier unmarshal as unsupported types.","solutions":["Inspect package.json for unusual value types (numbers, booleans, nested objects) and normalize them to standard package.json content.","Recreate package.json from a known-good source.","Report to Hugo if the file is valid JSON yet this still fires."],"exampleFix":"// before: package.json contains an unmarshallable value\n\"workspaces\": { \"packages\": [12345] }\n\n// after\n\"workspaces\": { \"packages\": [\"pkg-a\"] }","handlingStrategy":"validation","validationCode":"var pkg map[string]any\nif err := json.Unmarshal(data, &pkg); err != nil {\n    return err\n}\n// reject values json.Marshal cannot round-trip (NaN etc.)\nif _, err := json.Marshal(pkg); err != nil {\n    return fmt.Errorf(\"package.json contains unmarshallable values: %w\", err)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep package.json values to strings/objects/arrays/numbers/booleans/null.","Avoid exotic generated fields.","Validate with json round-trip in CI."],"tags":["hugo","npm-pack","json","workspaces"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}