Skip to content

Conversation

@alcuadrado
Copy link
Member

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.

@changeset-bot
Copy link

changeset-bot bot commented Nov 2, 2025

🦋 Changeset detected

Latest commit: a076927

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
hardhat Patch

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

@alcuadrado alcuadrado added the no docs needed This PR doesn't require links to documentation label Nov 2, 2025
Copy link
Contributor

Copilot AI left a 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.

Comment on lines 58 to 61
agentString: "bun/1.3.1 npm/? node/v24.3.0 linux arm64",
expected: "bun",
},
{
Copy link

Copilot AI Nov 2, 2025

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.

Suggested change
agentString: "bun/1.3.1 npm/? node/v24.3.0 linux arm64",
expected: "bun",
},
{

Copilot uses AI. Check for mistakes.
await writeUtf8File("pnpm-lock.yaml", "");
assert.equal(await getPackageManager(process.cwd()), "pnpm");
});
const fixtureValues = [
Copy link
Contributor

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.

Copy link
Member Author

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);
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

@alcuadrado alcuadrado added this pull request to the merge queue Nov 3, 2025
Merged via the queue into main with commit 9371964 Nov 3, 2025
201 checks passed
@alcuadrado alcuadrado deleted the fix-package-manager-detection branch November 3, 2025 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no docs needed This PR doesn't require links to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants