Asymmetry between searching for closest package.json when packageManager is defined vs when devEngines.packageManager is defined after #643
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 55/100
- Type d'issue
- Bug
- Clarté
- Plutôt claire
- Activité
- Calme
- Stack technique
- node.js, typescript
- Domaine
- tooling
Piste de recherche
Commencez dans sources/specUtils.ts, en particulier dans la logique d’analyse de package.json autour des lignes 166-238, et reproduisez l’asymétrie avec DEBUG=corepack. Ajoutez une couverture fonctionnelle pour packageManager et devEngines.packageManager dans les fichiers package.json imbriqués, puis vérifiez que la recherche s’arrête de manière cohérente pour chacun des deux champs et que les données de sélection obsolètes ne sont plus utilisées.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.
- Langage dominant
- TypeScript
- Étoiles
- 3.8k
- Forks
- 282
- Merge moyen
- 3 h 21 min
- PR mergées (30 j)
- 1
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de nodejs/corepack
-
Difficulté 2/5 1-3 heures Accessibilité débutants 64/100
-
[BUG] corepack not use COREPACK_NPM_REGISTRY install of tarballPeut-être pris @Yash121l l’a pris il y a 26 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
-
`Cannot find module .../bin/pnpm.cjs` after upgrading from Corepack <=0.34.4 with pnpm 12 cachedPeut-être pris @abappi19 l’a pris il y a 4 jours. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 75/100
-
Support signature verification against a custom COREPACK_NPM_REGISTRYPeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 68/100
-
Error when upgrading pnpm to latest in `devEngines.packageManager`Peut-être pris @thiagobarbosa l’a pris il y a 75 jours. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 52/100
Toutes les issues de nodejs/corepack
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
NousResearch/hermes-agent#136483 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
Les mainteneurs répondent en général sous 1 jour
-
Tool errors containing cycles or BigInt crash getErrorMessage and replace the original failureOuvertefactory-active factory-automatic task-bug-reproduction-success task-identify-harness-labels-done task-identify-issue-type-done
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
vercel/ai#22796 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
[Bug]: Web chat input doesn't regain focus after a reply finishesPeut-être pris @GaijinSystems l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
zeroclaw-labs/zeroclaw#11658 ·
Les mainteneurs répondent en général sous 2 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
babylonlabs-io/babylon-toolkit#2711 ·
Les mainteneurs répondent en général sous 1 jour