fix(amplify-cli-core): detect packageManager field in package.json hierarchy#14628
Open
danrivett wants to merge 2 commits intoaws-amplify:devfrom
Open
fix(amplify-cli-core): detect packageManager field in package.json hierarchy#14628danrivett wants to merge 2 commits intoaws-amplify:devfrom
danrivett wants to merge 2 commits intoaws-amplify:devfrom
Conversation
This was referenced Feb 27, 2026
…erarchy (aws-amplify#13931, aws-amplify#12001) Add findPackageManagerFieldInHierarchy() that walks up the directory tree looking for a packageManager field in package.json files, mimicking corepack's resolution behavior. When found, it takes precedence over lock file detection. This fixes two failure modes: 1. In Node.js 24+ (where corepack is enabled by default), Amplify picks the wrong package manager based on lock files, then fails when corepack rejects the invocation (e.g. "Command failed with exit code 1: yarn --version" when packageManager specifies pnpm). 2. In pnpm workspaces where the Amplify project is in a subdirectory and no lock file is present locally (the lock file lives at the workspace root outside the Amplify directory), Amplify falls back to whichever package manager executable it finds on PATH (typically yarn), ignoring the packageManager field that specifies pnpm.
Add tests for the new findPackageManagerFieldInHierarchy() function that detects the packageManager field in package.json hierarchy (corepack convention for monorepos). New test cases: - Detect pnpm from packageManager field in package.json - Detect npm from packageManager field in package.json - Detect yarn from packageManager field in package.json - Detect packageManager field from parent directory in hierarchy - packageManager field takes precedence over lock files - Falls back to lock file detection when packageManager executable not found
9159751 to
a1e9296
Compare
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.
Problem
The
getPackageManager()function inamplify-cli-coredoesn't detect thepackageManagerfield inpackage.json, which is the standard corepack convention for specifying which package manager a project uses (e.g.,"packageManager": "pnpm@10.17.0").This causes failures in two scenarios:
Corepack enforcement (Node.js 24+, where corepack is enabled by default): Amplify detects the wrong package manager based on lock files, then fails when corepack rejects the invocation. For example, running
amplify buildin a pnpm monorepo with"packageManager": "pnpm@10.x"produces:pnpm workspaces with no local lock file: When the Amplify project is in a subdirectory and no lock file is present locally (the lock file lives at the workspace root outside the Amplify directory), Amplify falls back to whichever package manager executable it finds on PATH (typically yarn), ignoring the
packageManagerfield that specifies pnpm.These failures will become increasingly common as corepack adoption grows and Node.js 24+ enables it by default.
Solution
Adds
findPackageManagerFieldInHierarchy()that walks up the directory tree looking for apackageManagerfield inpackage.jsonfiles, mimicking corepack's own resolution behavior. When found, it takes precedence over lock file detection. If the specified package manager executable isn't found in PATH, it gracefully falls back to the existing detection logic.Updated detection order
package.jsonexists (mandatory)packageManagerfield in package.json hierarchy (new)pnpm-lock.yamlis present + pnpm executableyarn.lockis present + yarn executablepackage-lock.jsonis presentIssue #, if available
Fixes #13931
Addresses #12001
Description of how you validated changes
amplify-cli-coretest suite passes (21 suites, 198 tests)"packageManager": "pnpm@10.x"in root package.jsonChecklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.