Asymmetry between searching for closest package.json when packageManager is defined vs when devEngines.packageManager is defined after #643
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 55/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- node.js, typescript
- 领域
- tooling
调研方向
从 sources/specUtils.ts 开始,重点查看第 166-238 行附近的 package.json 扫描逻辑,并使用 DEBUG=corepack 重现这种不对称性。为嵌套 package.json 文件中的 packageManager 和 devEngines.packageManager 添加功能覆盖,然后验证对于任一字段查找都会一致地停止,并且不再使用过时的选择数据。
由索引模型根据 Issue 内容生成。
描述
TL;DR I believe that in #643 one part of one line of code should have been changed from !selection.data.packageManager to !parsePackageJSON(selection.data) in order to make a consistent change.
As this was not done, there is now asymmetry in lookup of "package.json that defines package manager to be used", package.json that uses packageManager is always respected, package.jsons that use devEngine.packageManager are only respected when they are top-most package.json.
I'd like a check from @aduh95 as the author of #643, I am happy to provide a PR that cleans up the current asymmetry.
In the current code (encountered / verified experimentally on v0.34.5 with DEBUG=corepack ), the algorithm defined in https://github.com/nodejs/corepack/blame/v0.34.5/sources/specUtils.ts#L166-L238, way before #643 (and #642 which was merged immediately after), loops through package.json files, starting in current directory, and stops looping & updating the selection when package.json with packageManager field is found.
Otherwise, the selection will end up the last package.json (closest to filesystem root) encountered -- and in that case, we know packageManager is not set in it either, so when the code tries again! check on it on line 237, we return 'NoSpec' and will fallback to global PM version. This bit is important.
In #643, the basic support for reading devEngines.packageManager was added, including checking it's semver against packageManager field if both are present. It was tied into the original logic by replacing the code that was reading selection.data.packageManager with a call of new function that takes both fields into account: https://github.com/nodejs/corepack/commit/b4562688513f23e37e37b0d69a0daff33ca84c8d#diff-388b281fcee55f16c4f20b24ef733cc04cc2605d2e9753cffbb0ceb9ce8e6ef9L120-R193 (scroll to sources/specUtils.ts lines L120 / R 193).
However, the other critical piece, which says "look for package.json that defines package manager version to be used" , implemented in terms of "keep looping until package.json with packageManager is encountered) remained unchanged: (https://github.com/nodejs/corepack/blame/v0.34.5/sources/specUtils.ts#L173.
This is causing an asymmetry during the package.json looping/scanning. If there is a package.json with packageManager: <nonnullvalue>, the lookup will stop there. However, if the package.json contains (a valid) devEngines.packageManager field, the lookup will not stop and continue looping.
If there is no package.json above, the new feature for devEngines works "by accident", because the selection is not cleaned on each iteration, so https://github.com/nodejs/corepack/blame/v0.34.5/sources/specUtils.ts#L236 loads information from it.
However, in all other cases, the devEngines.packageManager value will be completely ignored.
e.g. in my case, this was inside a monorepo that had another package.json above it, closer to the root (it was npm monorepo package this package was was NOT part of). This other package.json was the last seen, it was stuck in selection and when fed into the rest of the function in https://github.com/nodejs/corepack/blame/v0.34.5/sources/specUtils.ts#L236 it decides the outcome. Since it did not have any PM definition, 'NoSpec' was returned it fallback to global PM.
other potential outcome is that if there would be any package.json above, with packageManager set, the lookup would stop on that file, as said, ignoring devEngines.packageManager in the first file.
Note: the root cause / logic from https://github.com/nodejs/corepack/issues/560 does not seem to apply here.
P.S.: I'd go for packageManager field, but there is high change of accidentally using non-corepack-aliased-npm on this project, and npm does only care about devEngines.packageManger, so hence my choice of using this approach.
My conclusion after this little deep dive: the current behavior seems hardly desirable as it is inconsistent, and should be unified in a way that the lookup stops for packages defining either packageManager of devEngines.packageManager. There is likely missing functional test coverage for this use cases of feature #643 that could be improved. Moreover, the pre-existing implementation choice where outdated selection data is kept is bug-prone and should be refactored.
- 主要语言
- TypeScript
- 星标
- 3.8k
- 派生
- 279
- 平均合并
- 1 小时 47 分钟
- 30 天内合并 PR
- 2
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
nodejs/corepack 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 64/100
-
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 4/5 3-5 天 新手友好度 68/100
-
难度 3/5 1-2 天 新手友好度 52/100
-
难度 3/5 1-2 天 新手友好度 68/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 75/100
-
bug v2
难度 2/5 1-3 小时 新手友好度 75/100
modelcontextprotocol/inspector#2458 · 1 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 75/100
railmapgen/rmp-gallery#4068 ·
-
Mend: dependency security vulnerability status: needs triage 🕵️♀️
难度 2/5 1-3 小时 新手友好度 70/100
carbon-design-system/ibm-products#9907 ·