cli icon indicating copy to clipboard operation
cli copied to clipboard

fix(npx): link and run own bins directly when possible

Open lukekarrys opened this issue 2 years ago • 2 comments

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 avatar Nov 12 '22 20:11 lukekarrys

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.

nlf avatar Nov 16 '22 17:11 nlf

@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.

lukekarrys avatar Nov 29 '22 22:11 lukekarrys