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

Automatically detect package manager (npm.packageManager) #102050

Open
nickmccurdy opened this issue Jul 10, 2020 · 8 comments · May be fixed by #102494
Open

Automatically detect package manager (npm.packageManager) #102050

nickmccurdy opened this issue Jul 10, 2020 · 8 comments · May be fixed by #102494

Comments

@nickmccurdy
Copy link

@nickmccurdy nickmccurdy commented Jul 10, 2020

Problem

When switching between many JavaScript projects, it's often easy to forget which package manager should be used in each project. JavaScript package managers aren't quite compatible either. Each package manager resolves dependencies differently, so accidentally installing with npm could cause a yarn project to break.

While VSCode has the npm.packageManager option to switch between npm, yarn, and pnpm, it's not very convenient to have to remember to set it on every project. Even if a user has a global preference, they will often have to clone several projects they're contributing to using other package managers anyway. Personally, I have so many projects installed that it's too inconvenient for me to set this option, so I end up figuring out which package manager to use manually and running it in the terminal.

Proposed option changes

I would like npm.packageManager to default to a new auto option, which would select a package manager based on the presence of workspace and lock files using preferred-pm. If a lock file doesn't exist, the user is probably using npm anyway, and we can just default to npm like we already are. It would also be nice to set up some sort of warning if multiple lock files are found, as most package managers do this to inform users that their package installations can break because of differences in version resolution.

Ideas for extension API changes

I'm not actually familiar with the extension APIs, but I think it would be nice to expose this as some sort of generic API for extensions so they don't have to reimplement this functionality or reinstall dependencies. This would also prevent extensions from breaking if VSCode changed its package manager detection heuristics in the future. This also might be useful for other internal features like automatically installing projects when cloned with the right package manager, though with the extension API users would have an alternative if something wasn't officially supported.

@nickmccurdy
Copy link
Author

@nickmccurdy nickmccurdy commented Jul 10, 2020

I'm also not against making an extension for this, but I figured it would be best to propose it internally first for better support with existing features and other extensions.

@alexr00
Copy link
Member

@alexr00 alexr00 commented Jul 10, 2020

I am not opposed to an auto option for npm.packageManager.

@vscode-triage-bot
Copy link
Collaborator

@vscode-triage-bot vscode-triage-bot commented Jul 10, 2020

This feature request is now a candidate for our backlog. The community has 60 days to upvote the issue. If it receives 20 upvotes we will move it to our backlog. If not, we will close it. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

@shivangg
Copy link

@shivangg shivangg commented Jul 11, 2020

@alexr00 I'm willing to work on this one.

Should the development be started only after this issue has moved from Backlog Candidate to the Backlog?

@alexr00
Copy link
Member

@alexr00 alexr00 commented Jul 13, 2020

@shivangg thanks for checking, especially since we aren't always consistent about what to do there. If you start working on it I will move it into the current milestone.

More spec-ing details:

I would like npm.packageManager to default to a new auto option, which would select a package manager based on the presence of workspace and lock files using preferred-pm.

Making auto the default sounds good to me.

If a lock file doesn't exist, the user is probably using npm anyway, and we can just default to npm like we already are.

Agree that default should be npm if there is not lock file.

It would also be nice to set up some sort of warning if multiple lock files are found, as most package managers do this to inform users that their package installations can break because of differences in version resolution.

A warning if there are multiple lock files AND auto is set makes sense. There should be no warning if auto is not set. However, it is still possible to simply proceed even if there are multiple lock files. So I would expect the behavior to be continue using one of the package managers and also show a warning notification. I would expect order of priority with multiple lock files to be npm, yarn, then pnpm.

shivangg added a commit to shivangg/vscode that referenced this issue Jul 14, 2020
@shivangg shivangg linked a pull request that will close this issue Jul 14, 2020
3 of 3 tasks complete
@shivangg
Copy link

@shivangg shivangg commented Jul 14, 2020

@alexr00 Thanks for the reply. Here's a draft PR.

As preferred-pm exposed an async function to query the preferred package manager, I had to make many of the dependent functions async. Let me know if their if a better/efficient way to prevent this.

shivangg added a commit to shivangg/vscode that referenced this issue Jul 14, 2020
shivangg added a commit to shivangg/vscode that referenced this issue Jul 19, 2020
@shivangg
Copy link

@shivangg shivangg commented Jul 19, 2020

@alexr00 I'm not sure of the cleanest way to detect multiple lockfiles. preferred-pm only returns the preferred pm in priority order npm > yarn > pnpm even in case of multiple lockfiles.

Is there a better way to proceed without deduplicating the preferred-pm logic in vs code?

@alexr00
Copy link
Member

@alexr00 alexr00 commented Jul 24, 2020

@shivangg, I don't know of a better way.

shivangg added a commit to shivangg/vscode that referenced this issue Aug 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants
You can’t perform that action at this time.