Skip to content
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

fix(core): fix package manager determination logic #28202

Closed
wants to merge 0 commits into from

Conversation

ThePlenkov
Copy link
Contributor

Current Behavior

Currently create-nx-workspace determines package manager based on the invoker's path and it might be totally misleading if some names are used in the path such as /ubuntu/my_project which is identified as bun command by mistake

Expected Behavior

It should correctly identify package manager

Related Issue(s)

Fixes #28201

Copy link

vercel bot commented Sep 30, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nx-dev ✅ Ready (Inspect) Visit Preview Oct 20, 2024 1:51pm

Copy link
Collaborator

@JamesHenry JamesHenry left a comment

Choose a reason for hiding this comment

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

I don't think this is working as intended based on the e2e results.

Based on my own experimentation I believe what we want is to replace the existing logic (and your change) with this:

  if (process.env.npm_config_user_agent) {
    for (const pm of packageManagerList) {
      if (process.env.npm_config_user_agent.startsWith(pm)) {
        return pm;
      }
    }
  }

  if (process.env.npm_execpath) {
    for (const pm of packageManagerList) {
      if (process.env.npm_execpath.includes(pm)) {
        return pm;
      }
    }
  }
  return 'npm';

(Obviously make sure the formatting is fixed before pushing)

Results on my machine:

image

@JamesHenry JamesHenry self-assigned this Oct 1, 2024
@ThePlenkov
Copy link
Contributor Author

@JamesHenry indeed - I didn't have intention to say it's a final code. So far I've been trying still to understand what is the functional meaning of this function. Like what is more important - to understand what is running the code or which npm was used during installation because is not the same.

Please note that if the code is run via node or deno directly - npm_config_user_agent is empty.
I've created some command today to prove that: https://www.npmjs.com/package/which-engine which also supports which-pm.

@ThePlenkov
Copy link
Contributor Author

@JamesHenry is there any way to automate formatting before pushing? like husky + format:write may be?

@ThePlenkov
Copy link
Contributor Author

OK I see now - seems like we are supposed to use pm used to trigger the call. In case if there is no package manager ( let's say we call globally installed create-nx-workspace or via node programmatically ) - then we just use npm as fallback.

@JamesHenry
How about this?

I intentionally use / in the startsWith to make sure it's yarn and node something like yarn_alternative.

In the includes script I break path into segments and we search for a segment = npm | yarn | bun. And segments like ubuntu will not be taken into account

export function detectInvokedPackageManager(): PackageManager {

  if (env.npm_config_user_agent) {
    for (const pm of packageManagerList) {
      if (env.npm_config_user_agent.startsWith(`${pm}/`)) {
        return pm;
      }
    }
  }

  if (env.npm_execpath) {
    for (const pm of packageManagerList) {
      if (env.npm_execpath.split(sep).includes(pm)) {
        return pm;
      }
    }
  }
  return 'npm';

}

@JamesHenry
Copy link
Collaborator

@ThePlenkov thanks looks reasonable

@ThePlenkov
Copy link
Contributor Author

I'm struggling to understand why test is failing. It is failing even locally when I run it on the upstream branch ( without my actual changes ). Can it be because of remote cache?

@JamesHenry
Copy link
Collaborator

@ThePlenkov I have brought things up to date with the latest, are you able to reproduce locally?

@ThePlenkov
Copy link
Contributor Author

I did, but couldn't reproduce failing tests((

@JamesHenry
Copy link
Collaborator

@ThePlenkov I think I have traced the issue, however I cannot push directly to the PR because you opened it from your master branch. Please open PRs from feature branches in future to allow collaborators to push directly.

Please can you try removing this hardcoded npm reference in the e2e utils, I am not sure why it was done but I believe it could be the cause of the error because based on the logs, the test workspace is getting created with pnpm but then is attempting an npm install later:

image

cc @FrozenPandaz

@ThePlenkov
Copy link
Contributor Author

@JamesHenry I'll create another PR better then

Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

/bin/sh: 1: bun: not found
2 participants