cli
cli copied to clipboard
fix(npx): link and run own bins directly when possible
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
i have a couple of concerns with how this works, but also ideas about how to take this further and make it more reliable
- consider the pattern of
{ "devDependencies": { "my-package": "file:." }}which is one we even use ourselves. if this is done, then thenode_modules/my-packagedirectory will already exist and the call tosymlinkwill reject. - if your own package defines a bin
fooand you also have a dependency that defines a binfoo, 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 binfoofrom the root package and when this code cleans up, it removes thefoobin entirely - even though another dependency defined it.
to address these problems, here's what i'm thinking:
- modify
bin-linkssuch that the package it's creating bins for does not have to exist withinnode_modules - modify
bin-linkssuch that it can link bins into any arbitrary directory - when reifying, use
bin-linksto link the root package's bins into its own dedicated directory, i.e.node_modules/.local-bin - adjust
npm execsuch 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 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.