fix(npm): prioritize packageManager field and simplify lock file dete…#302916
fix(npm): prioritize packageManager field and simplify lock file dete…#302916flycran wants to merge 2 commits intomicrosoft:mainfrom
Conversation
|
Parent traversal should not happen for generic lockfile detection here. This change now uses Please restrict detection to the package root (or at least stop at workspace root), and only keep parent traversal where there is a PM-specific reason. |
|
“ |
…ipleLockFilesDetected
|
@meganrogge The parent traversal via Additionally, the upward traversal of I've created an online demo to verify the behavior described above: |
Summary
The
findPreferredPMfunction in the npm extension previously ignored thepackageManagerfield inpackage.json, which is the most authoritativeway to declare the intended package manager per the Node.js Corepack spec.
Additionally, the previous logic relied on
find-yarn-workspace-root, whichdetects the presence of a
workspacesfield inpackage.jsonand incorrectlytreats it as evidence of Yarn usage — regardless of the actual package manager
in use. This caused unexpected behavior reported in #170101 and #159465.
This PR refactors the detection logic with the following changes:
packageManagerfirst: Ifpackage.jsondeclares apackageManagerfield (e.g."packageManager": "pnpm@8.6.0"), it isused as the authoritative source for the package manager name.
functions with a single
LOCKFILE_CANDIDATEStable, iterating inpriority order and returning early as soon as two distinct lock files
are detected.
multipleLockFilesDetectedsemantics: Previously,whichPMresults could incorrectly inflate the lock file count. Now
multipleLockFilesDetectedonly reflects actual lock file conflicts,consistent with the warning message shown to the user in
tasks.ts.find-yarn-workspace-rootis no longerused and can be safely removed from
package.json.Testing
npx gulp compile-extension:npm— no errors.packageManagervalues in
package.json(e.g."pnpm@9.0.0","yarn@4.0.0"), confirmedthe correct package manager was invoked each time.
workspacesfield topackage.jsonand verified it no longerincorrectly influences package manager detection.
ran npm scripts, and confirmed the correct package manager was selected
based on the detected lock file.