{"record":{"id":"0a2af35ec0405acc","repo":"gohugoio/hugo","slug":"npm-pack-could-not-locate-existing-workspaces-arr","errorCode":null,"errorMessage":"npm pack: could not locate existing workspaces array for insertion","messagePattern":"npm pack: could not locate existing workspaces array for insertion","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"modules/npm/package_builder.go","lineNumber":326,"sourceCode":"\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\n\t\t\t\t\tif len(arrayContent) == 0 {\n\t\t\t\t\t\tinsertion = \"\\n\" + indent + indent + quoted + \"\\n\" + indent\n\t\t\t\t\t} else {\n\t\t\t\t\t\tinsertion = \",\\n\" + indent + indent + quoted + \"\\n\" + indent\n\t\t\t\t\t}\n\t\t\t\t\tresult := make([]byte, 0, len(data)+len(insertion))\n\t\t\t\t\tresult = append(result, data[:pos]...)\n\t\t\t\t\tresult = append(result, insertion...)\n\t\t\t\t\tresult = append(result, data[pos:]...)\n\t\t\t\t\treturn afero.WriteFile(fsys, packageJSONName, result, 0o666)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// We know a workspaces array exists but could not locate it reliably in the raw data.\n\t\treturn fmt.Errorf(\"npm pack: could not locate existing workspaces array for insertion\")\n\t}\n\n\t// No workspaces key — add before closing brace.\n\tlastBrace := bytes.LastIndexByte(data, '}')\n\tif lastBrace < 0 {\n\t\treturn fmt.Errorf(\"npm pack: malformed package.json\")\n\t}\n\n\tbefore := bytes.TrimRight(data[:lastBrace], \" \\t\\n\\r\")\n\tneedComma := len(before) > 0 && before[len(before)-1] != '{' && before[len(before)-1] != ','\n\n\tvar insertion string\n\tif needComma {\n\t\tinsertion = \",\\n\" + indent + `\"workspaces\": [` + \"\\n\" + indent + indent + quoted + \"\\n\" + indent + \"]\\n\"\n\t} else {\n\t\tinsertion = indent + `\"workspaces\": [` + \"\\n\" + indent + indent + quoted + \"\\n\" + indent + \"]\\n\"\n\t}\n","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/modules/npm/package_builder.go#L308-L344","documentation":"In ensureWorkspaceRef (modules/npm/package_builder.go:326). When workspaces is an array that does not already contain the workspace path, Hugo attempts a byte-level insertion by locating the ']' of the workspaces array in the raw file. If it cannot find the closing bracket reliably (malformed/reformatted layout), it gives up with this error rather than corrupting the file.","triggerScenarios":"package.json has a workspaces array but the raw bytes around it are shaped such that bytes.IndexByte for ']' after the workspaces key fails (e.g. the key appears but the array spans an unusual structure, or the file has non-standard formatting/escaping).","commonSituations":"Exotic JSON formatting tools; a workspaces key inside a string value that confuses byte scanning; manually mangled JSON that still parses but reorders braces; embedded quotes confusing the bracket scan.","solutions":["Reformat package.json with standard tooling (prettier / npm) so the workspaces array is conventionally formatted.","Convert workspaces to the simpler array form and let Hugo rewrite it.","Delete the workspaces key and let Hugo add a clean one."],"exampleFix":"// before: oddly formatted workspaces array confusing the byte scan\n\"workspaces\":[  \"a\",\"b\" ]\n\n// after: reformat\n\"workspaces\": [\"a\", \"b\"]","handlingStrategy":"validation","validationCode":"// reformat package.json so the workspaces array is conventionally laid out\ndata, _ := os.ReadFile(\"package.json\")\nvar pkg map[string]any\nif err := json.Unmarshal(data, &pkg); err != nil {\n    return err\n}\nout, _ := json.MarshalIndent(pkg, \"\", \"  \")\nreturn os.WriteFile(\"package.json\", append(out, '\\n'), 0o666)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Format package.json with prettier/npm so byte-level edits succeed.","Don't exotic-escape or reflow the workspaces array.","Let Hugo manage the workspaces array once it is standard-formatted."],"tags":["hugo","npm-pack","workspaces","formatting"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}