package.json files that start with a UTF-8 BOM are rejected as invalid JSON
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 82/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- javascript, node.js
- 领域
- cli
调研方向
阅读 lib/workspace.js、lib/package-managers.js 和 lib/config.js,从 issue 中提到的三个解析位置开始。检查现有测试如何覆盖 JSON 解析,然后为 getScripts()、detectPackageManager() 和 readWorkspaceConfig() 添加带前置 BOM 的用例。每个文件都能成功读取,且 BOM 不影响解析出的值,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Description
A package.json that starts with a UTF-8 byte order mark (BOM) is rejected by the CLI, while npm reads it without complaint. Files like this are common on Windows: Windows PowerShell 5.1 writes a BOM with Set-Content -Encoding UTF8 or Out-File -Encoding utf8, and some editors add one.
For such a project, the CLI:
- shows no scripts: opening the project shows
Could not read package.json: Invalid JSON in …instead of the script list, althoughnpm run <script>works in that folder; - would pick the wrong package manager: the
packageManagerfield is read with the sameJSON.parse, which fails silently, so a"packageManager": "[email protected]"project is detected as npm.
The CLI's own configuration file has the same problem: a config.json saved with a BOM, for example after editing it in an editor that adds one, is reported as Invalid configuration JSON.
Steps to reproduce
-
Create a project whose
package.jsonstarts with a BOM.In a POSIX shell:
mkdir -p /tmp/dws-bom/bom-app printf '\357\273\277{"name":"bom-app","packageManager":"[email protected]","scripts":{"hello":"echo hello"}}' \ > /tmp/dws-bom/bom-app/package.jsonIn PowerShell 7, the same file can be written with
Set-Content -Encoding utf8BOM; Windows PowerShell 5.1 adds the BOM with-Encoding UTF8. -
Check that npm accepts it:
cd /tmp/dws-bom/bom-app && npm run helloThis prints
hello. -
Start the published CLI on the workspace, then open
bom-app:DEV_WORKSPACE_CONFIG=/tmp/dws-bom/config.json \ npx -p @modulane/[email protected] dws --workspace /tmp/dws-bom
Actual behaviour
The project screen shows the error from getScripts(). Observed on Windows, with the folder path shortened:
Could not read package.json: Invalid JSON in …\bom-app\package.json: Unexpected token '', "{"name":""... is not valid JSON
Called directly on the same folder, detectPackageManager() returns { "name": "npm", "source": "default" } instead of pnpm from the packageManager field.
Expected behaviour
The BOM is ignored, as npm does: the script list shows hello, and the package manager line reads pnpm (from the packageManager field). A config.json with a BOM is read normally.
Cause
Three places pass the file content straight to JSON.parse, which rejects a leading U+FEFF:
getScripts()inworkspace.js;readPackageManagerField()inpackage-managers.js, which then falls back to the lockfile or npm without any message;readWorkspaceConfig()inconfig.js.
Suggested fix
- Remove a single leading
before parsing, in all three places, for example with a small shared helper that reads and parses a JSON file. Only the start of the file is affected, so a BOM-like character inside a string value is kept. - Add tests:
getScripts(),detectPackageManager()andreadWorkspaceConfig()each read a file that starts with a BOM.
I'm happy to open a PR for this.
Environment
@modulane/dev-workspace-cli0.5.0 from npm.lib/is identical onmainat dfccb6b.- Node.js 22.13.1 and npm 10.9.2, Windows 11. The parsing code is the same on every platform.
- 主要语言
- JavaScript
- 星标
- 0
- 派生
- 0
- 平均合并
- 46 分钟
- 30 天内合并 PR
- 15
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
相似的 Issue
-
Energy flow: add an "Other" row so the consumers add up to the consumption total可能已有人在做 @github-actions 今天认领。 未关闭enhancement
难度 2/5 1-3 小时 新手友好度 73/100
维护者通常 1 天内回复
-
Impact: Medium Level: Easy Type: Bug Value: Users
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
bug good first issue
难度 2/5 1-3 小时 新手友好度 82/100
NobiResearch/Ovenbird#148 ·
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 78/100
glossyio/traffic-monitor#264 ·
维护者通常 1 天内回复
-
balance good first issue
难度 2/5 1-3 小时 新手友好度 75/100
tobeficv/founders-way#5 ·