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(npx): link and run own bins directly when possible #5848

Draft
wants to merge 3 commits into
base: latest
Choose a base branch
from

Conversation

lukekarrys
Copy link
Member

@lukekarrys lukekarrys commented Nov 12, 2022

This is an alternative to #5842. I had pushed some commits to that PR but backed them out to make it clear that this is a different approach to the same issue.

This also includes two chore commits to refactor the tests. If the fix: from this PR doesn't get merged, I will make a few PR with only those chores.

Here is the relevant message from the fix commit:

This follows a similar approach to local and global bins that already
exist. This is achieved by linking a packages own bin script to
`node_modules/.bin` and the package within `node_modules/` similar to if
it had been installed with `--install-links=false`.

Then the linked bin is run and the symlinks are cleaned up immediately
after. This has the same effect as loading the current package into the
`npx` cache and running it, except it is quicker to not have to run any
Arborist commands.

Commits:

  • chore: add option for strict mock registry
  • fix(npx): link and run own bins directly when possible
  • chore(libnpmexec): refactor tests to use mock registry

@lukekarrys lukekarrys requested a review from a team as a code owner November 12, 2022 20:10
@lukekarrys lukekarrys changed the title lk/npx local bins and tests fix(npx): link and run own bins directly when possible Nov 12, 2022
This follows a similar approach to local and global bins that already
exist. This is achieved by linking a packages own bin script to
`node_modules/.bin` and the package within `node_modules/` similar to if
it had been installed with `--install-links=false`.

Then the linked bin is run and the symlinks are cleaned up immediately
after. This has the same effect as loading the current package into the
`npx` cache and running it, except it is quicker to not have to run any
Arborist commands.
Copy link
Member

@wraithgar wraithgar left a comment

Choose a reason for hiding this comment

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

yeah let's try it this way.

@nlf
Copy link
Contributor

nlf commented Nov 16, 2022

i have a couple of concerns with how this works, but also ideas about how to take this further and make it more reliable

  1. consider the pattern of { "devDependencies": { "my-package": "file:." }} which is one we even use ourselves. if this is done, then the node_modules/my-package directory will already exist and the call to symlink will reject.
  2. if your own package defines a bin foo and you also have a dependency that defines a bin foo, one of two things will happen. either the attempt to linkBins will reject due to the bin already existing, or we'll end up creating our new bin foo from the root package and when this code cleans up, it removes the foo bin entirely - even though another dependency defined it.

to address these problems, here's what i'm thinking:

  1. modify bin-links such that the package it's creating bins for does not have to exist within node_modules
  2. modify bin-links such that it can link bins into any arbitrary directory
  3. when reifying, use bin-links to link the root package's bins into its own dedicated directory, i.e. node_modules/.local-bin
  4. adjust npm exec such that it's aware of this new directory and checks there for bins first

if we take this path, then not only do we resolve the issue at hand, but we ensure there is no chance of collisions in either the directory name or the bin name. note, however, that doing this also means that we complicate the npm exec location logic a bit further. likely we'll want to consider implementing --location for npm exec at the same time or soon after making these changes.

@lukekarrys lukekarrys marked this pull request as draft November 28, 2022 20:29
@lukekarrys
Copy link
Member Author

@nlf that sounds like a good plan. I had attempted to address those issues in the initial PR but in the case of the conflicting bins, it would not link the local bin and instead use the original refified bin. I thought this would be required to maintain backwards compatability, but that's actually not true.

I converted this to a draft and pulled out the chores into #5902 and #5908. Once those land, I will take another look at the approach outlined above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants