{"record":{"id":"838649995811c264","repo":"gohugoio/hugo","slug":"npm-pack-unsupported-workspaces-type-t","errorCode":null,"errorMessage":"npm pack: unsupported workspaces type %T","messagePattern":"npm pack: unsupported workspaces type %T","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"modules/npm/package_builder.go","lineNumber":299,"sourceCode":"\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\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]...)","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/modules/npm/package_builder.go#L281-L317","documentation":"In ensureWorkspaceRef (modules/npm/package_builder.go:299). The workspaces field is type-switched; array ([]any), object (map[string]any) and nil are handled. Any other concrete type (e.g. a string or number) hits the default branch and returns 'unsupported workspaces type %T', printing the Go type.","triggerScenarios":"package.json has \"workspaces\": \"apps/*\" (a string) or \"workspaces\": 3 (a number). npm expects workspaces to be an array or an object with packages, so a scalar value is rejected.","commonSituations":"Typing workspaces as a single glob string instead of a one-element array; a config generator emitting the wrong shape; confusion with tools that accept string shortcuts.","solutions":["Change workspaces to an array: \"workspaces\": [\"apps/*\"].","Or use the object form: \"workspaces\": { \"packages\": [\"apps/*\"] }.","Validate package.json shape against the npm spec."],"exampleFix":"// before\n\"workspaces\": \"apps/*\"\n\n// after\n\"workspaces\": [\"apps/*\"]","handlingStrategy":"type-guard","validationCode":"var pkg map[string]any\n_ = json.Unmarshal(data, &pkg)\nswitch pkg[\"workspaces\"].(type) {\ncase []any, map[string]any, nil:\n    // ok\ndefault:\n    return fmt.Errorf(\"workspaces must be array or object, got %T\", pkg[\"workspaces\"])\n}","typeGuard":"func isSupportedWorkspaces(v any) bool {\n    switch v.(type) {\n    case []any, map[string]any, nil:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Always make workspaces an array (even single-element).","Validate package.json with a JSON schema in CI.","Avoid string-shortcut forms for workspaces."],"tags":["hugo","npm-pack","workspaces","type-mismatch"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}