-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix package manager detection #7655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: a076927 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes package manager detection by reading from the npm_config_user_agent environment variable instead of checking for lock files. The implementation now correctly identifies the package manager used to run Hardhat across npm, yarn, pnpm, bun, and deno by parsing the user agent string that all these package managers set.
Key Changes
- Replaced lock file-based detection with environment variable parsing
- Added support for bun and deno package managers
- Updated tests to validate detection across all supported package managers with real-world user agent strings
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| v-next/hardhat/src/internal/cli/init/package-manager.ts | Reimplemented getPackageManager() to parse npm_config_user_agent, added bun/deno support to installation commands and peer dependency logic |
| v-next/hardhat/test/internal/cli/init/package-manager.ts | Replaced lock file tests with fixture-based tests using actual user agent strings from different package managers |
| v-next/hardhat/src/internal/cli/init/init.ts | Updated function calls to remove workspace parameter |
| .changeset/floppy-brooms-cheat.md | Added changeset entry |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| agentString: "bun/1.3.1 npm/? node/v24.3.0 linux arm64", | ||
| expected: "bun", | ||
| }, | ||
| { |
Copilot
AI
Nov 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate test fixture entries detected. Lines 53-56 and 57-60 contain identical agent strings and expected values. Consider removing one of these duplicate entries.
| agentString: "bun/1.3.1 npm/? node/v24.3.0 linux arm64", | |
| expected: "bun", | |
| }, | |
| { |
| await writeUtf8File("pnpm-lock.yaml", ""); | ||
| assert.equal(await getPackageManager(process.cwd()), "pnpm"); | ||
| }); | ||
| const fixtureValues = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have end-to-end tests to alert us in case any of the package managers change this behaviour?
They could change the the variable, format of the message or stop reporting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but I think the chances of that changing are remote, as they would break the entire ecosystem
| return DEFAULT; | ||
| } | ||
|
|
||
| const packageManager = userAgent.substring(0, firstSlashIndex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the package managers provide any guarantees on the message they use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
I noticed that I was getting the wrong package manager during the initialization of HH3, so I explored the situation.
I run all the possible combinations of package managers, and noticed that all of them provide the necessary information in the same env variable, so I fixed it.
I also added Bun and Deno as a best effort, as it was fairly cheap to do it.