Fix package manager detection in hydrogen upgrade for monorepos#3794
Open
JohnCashmore wants to merge 4 commits into
Open
Fix package manager detection in hydrogen upgrade for monorepos#3794JohnCashmore wants to merge 4 commits into
hydrogen upgrade for monorepos#3794JohnCashmore wants to merge 4 commits into
Conversation
Author
|
I have signed the CLA! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WHY are these changes introduced?
Fixes #3793
When a Hydrogen app lives inside a monorepo workspace (pnpm/yarn/bun), the lockfile sits at the workspace root — not in the app directory.
hydrogen upgradedetects the package manager with cli-kit'sgetPackageManager(appPath), which only checks that single directory for a lockfile before falling back to npm. The result: the upgrade runsnpm install --legacy-peer-depsinside a pnpm workspace, which fails onworkspace:protocol dependencies (or pollutes the app dir with a straypackage-lock.json), and the final "Undo these upgrades?" message printsnpm iinstead of the workspace's package manager.WHAT is this pull request doing?
findPackageManagerByLockfile(directory, options?)topackages/cli/src/lib/package-managers.ts: walks up from the app directory looking for a known lockfile, built on the existingpackageManagersmetadata table (including alternative lockfiles such asbun.lockandnpm-shrinkwrap.json).'unknown', preserving the existing npm +--legacy-peer-depsfallback path untouched.getPackageManager(appPath)call sites inupgrade.ts(dependency removal, dependency upgrade, and the undo-instructions message) with the new helper. The undo message is additionally wrapped inresolvePackageManagerName()so it can never render the literal stringunknown.upgrade.test.tspackage-manager mock to the new module and adds 9 unit tests covering: single-repo detection, monorepo ancestor detection (pnpm/yarn/bun), nearest-wins precedence, no-lockfile fallback, alternative lockfiles, and the same-directory tie-break order.@shopify/cli-hydrogen).Out of scope (left untouched, happy to follow up): the same single-directory detection pattern exists in
build.ts,setup/css.ts,setup/vite.ts, andlib/shell.ts.HOW to test your changes?
Unit tests:
pnpm --filter @shopify/cli-hydrogen test src/lib/package-managers.test.ts src/commands/hydrogen/upgrade.test.tsManual end-to-end:
pnpm-workspace.yamlwithpackages: ['apps/*']) containing a Hydrogen app atapps/storefrontpinned to an older Hydrogen version. Runpnpm installfrom the root (singlepnpm-lock.yamlat the root, no lockfile in the app dir) and commit.pnpm --filter @shopify/cli-hydrogen build) and run the upgrade against the app directory.pnpm add …(previouslynpm install --legacy-peer-deps), nopackage-lock.jsonappears in the app dir, the rootpnpm-lock.yamlupdates, and the closing undo message reads… && pnpm i.Checklist
resolvePath/dirname/joinPathand terminates whendirname(dir) === dir, which holds at filesystem roots on both POSIX (/) and Windows drive roots (C:\)