cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] Got "Permission Denied" when execute local CLI command by `npm link` after git commit in current branch

Open LikeDreamwalker opened this issue 2 years ago • 4 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

This issue exists in the latest npm version

  • [X] I am using the latest npm

Current Behavior

It is a very weird issue. I am not sure if it is entirely related to npm, but it really brings a horrible experience.

  1. In my project to build a CLI, I execute npm link to link my CLI to global, and it works; I use test-cli create instead.
  2. Execute test-cli create got an inner execution issue, and it is because I forgot to use shebang, and I also added it, then it works.
  3. Everything works fine, so I commit this change to git. Didn't execute npm unlink at this time.
  4. After the commit, I retry to execute test-cli create, but I have an issue in terminal zsh: permission denied: test-cli create, or even test-cli itself.
  5. I tried to execute npm-unlink and npm-link, the issue still exists.
  6. I tried to delete test-cli in global node_modules and retry 5, the issue still exists.
  7. I tried to use chmod or other syntax to add permission on the target file, or even use sudo, and I am pretty sure the target file has permission to execute, but the issue still exists.
  8. I changed my branch from the current to the master (haven't merged yet), and everything works fine except the shebang issue.
  9. With 8, I re-checked my commit, there is nothing but only the line I add shebang.
  10. I also tried to clear the cache but still had the same issue.
  11. One thing is very strange, in this issue, when I change my codes and retry the npm link, it always returns me an up to date log with a during time. And when I try npm unlink -g test-cli, it also returns me an up to date log. The during time is almost the same around 700ms on this device.
  12. The target file (cli.js) can be executed directly by the node the whole time, and that's also a Plan B to keep on working.

So with the upper description, especially 11, I wonder if there are some issues between git, the file system, and npm? When I commit, git locks something but has a side effect on npm, so npm always thinks there is nothing changed and nothing to be done.

I think re-installing the node or deleting my local git repo can help, but I still wondering why. And the repo is for business so I cannot open it or provide any test files. Not sure if this issue only happens on my device.

Expected Behavior

No matter what we did to git, npm link can always link the current local module, or output some warnings or errors when there are issues in it.

Steps To Reproduce

  1. Find a project, in my case a CLI.
  2. Execute npm link and check if it can work well by link.
  3. Change some codes and commit them, especially the final file it executes.
  4. Retry 2.

Don't know if this can happen this issue.

Environment

  • npm: 10.2.0
  • Node.js: v18.18.1
  • OS Name: macOS Ventura 13.3
  • System Model Name: Macbook Pro
  • npm config:
; "user" config from /SecretPath/.npmrc

///SecretHost//:_auth = (protected) 
///SecretHost//:_authToken = (protected) 
; always-auth = true ; overridden by project
email = "[email protected]" 
; registry = "http:///SecretHost/" ; overridden by project

; "project" config from /SecretPath/Documents/Workplace/test-cli/.npmrc

always-auth = true 
fsevents_binary_host_mirror = "https://npm.taobao.org/mirrors/fsevents/" 
puppeteer_download_host = "https://npm.taobao.org/mirrors/" 
registry = "http:///SecretHost/" 

; node bin location = /SecretPath/.nvm/versions/node/v18.18.1/bin/node
; node version = v18.18.1
; npm local prefix = /SecretPath/Documents/Workplace/test-cli
; npm version = 10.2.0
; cwd = /SecretPath/Documents/Workplace/test-cli
; HOME = /Users/Secret
; Run `npm config ls -l` to show all defaults.

LikeDreamwalker avatar Oct 16 '23 08:10 LikeDreamwalker

I selected the wrong tag, and it should be Release 10.x, but I also tried npm 9 this issue still exists.

LikeDreamwalker avatar Oct 16 '23 08:10 LikeDreamwalker

Is there an existing issue for this?

  • [x] I have searched the existing issues

This issue exists in the latest npm version

  • [x] I am using the latest npm

Current Behavior

This drove me crazy! I'm under Windows where shebangs aren't common. I tried to run my CLI tool I was developing with VS Code with npx ., npm link; tool-name and npm install . --global; tool-name, but only got strange errors like:

[13484:1203/025158.924:ERROR:cache_util_win.cc(20)] Unable to move the cache: Access is denied. (0x5)
[13484:1203/025158.924:ERROR:disk_cache.cc(205)] Unable to create cache

...or:

[main 2023-12-03T01:35:01.552Z] update#setState idle
[9440:1203/023504.103:ERROR:jump_list.cc(301)] Failed to append custom category 'Recent Folders' to Jump List due to system privacy settings.
[main 2023-12-03T01:35:04.117Z] updateWindowsJumpList#setJumpList unexpected result: customCategoryAccessDeniedError
[main 2023-12-03T01:35:05.111Z] Extension host with pid 12920 exited with code: 0, signal: unknown.

After googling for the errors, I first thought this or this may solve my problem. It seemed to be related to VS Code in a strange way. VS Code was always opened with my index.js when trying to run my CLI tool.

But, as it turned out after much frustration, the output was just from VS Code and unrelated to the issue. VS Code surely will always make this output, just that I normally don't come to see it. The console was also used in an asynchronous way: VS Code printed output while the console window didn't expect output anymore, because the program started from the console already finished, but VS Code as a child process still used the console for output.

The mentioning of C++ source code files like jump_list.cc seems to come from Chromium, then Electron, then VS Code. This was highly irritating!

So, the problematic current behavior is that npx (or npm with exec command, respectively) and the .cmd, .ps1 files etc. generated in the local npm ecosystem open the file to be run in an editor instead of running it, or, if this is somehow by design, don't even warn you that a shebang is missing, which prevents the obviously intended action of running it with node.

Expected Behavior

Run .js files instead of opening them in the default editor, or at least print a warning about the missing shebang #!/usr/bin/env node.

If possible, a spawned child process on whose termination the spawning code doesn't wait, shouldn't be allowed to use the console.

Environment

  • npm: 10.2.4
  • Node.js: v20.10.0
  • OS Name: Windows 10 Home
  • System Model Name: (desktop)
  • npm config:
; "builtin" config from C:\Users\<redacted>\AppData\Roaming\npm\node_modules\npm\npmrc

prefix = "C:\\Users\\<redacted>\\AppData\\Roaming\\npm"

; node bin location = C:\Program Files\nodejs\node.exe
; node version = v20.10.0
; npm local prefix = C:\<redacted>
; npm version = 10.2.4
; cwd = C:\<redacted>
; HOME = C:\Users\<redacted>
; Run `npm config ls -l` to show all defaults.

Minimum Viable example

As an extra, here is a minimum viable example to trigger the problem:

package.json:

{
  "name": "script-gets-opened-example",
  "version": "1.0.0",
  "description": "",
  "bin": "./index.js"
}

index.js:

console.log("I'm just an innocent script.");
  • Run with npx ., npm link; script-gets-opened-example or npm install . --global; script-gets-opened-example to trigger the problem.
  • Add shebang #!/usr/bin/env node at start of index.js to solve the problem.

(Removal via npm rm --global script-gets-opened-example.)

Enyium avatar Dec 03 '23 06:12 Enyium

Is there an existing issue for this?

  • [x] I have searched the existing issues

This issue exists in the latest npm version

  • [x] I am using the latest npm

Current Behavior

This drove me crazy! I'm under Windows where shebangs aren't common. I tried to run my CLI tool I was developing with VS Code with npx ., npm link; tool-name and npm install . --global; tool-name, but only got strange errors like:

[13484:1203/025158.924:ERROR:cache_util_win.cc(20)] Unable to move the cache: Access is denied. (0x5)
[13484:1203/025158.924:ERROR:disk_cache.cc(205)] Unable to create cache

...or:

[main 2023-12-03T01:35:01.552Z] update#setState idle
[9440:1203/023504.103:ERROR:jump_list.cc(301)] Failed to append custom category 'Recent Folders' to Jump List due to system privacy settings.
[main 2023-12-03T01:35:04.117Z] updateWindowsJumpList#setJumpList unexpected result: customCategoryAccessDeniedError
[main 2023-12-03T01:35:05.111Z] Extension host with pid 12920 exited with code: 0, signal: unknown.

After googling for the errors, I first thought this or this may solve my problem. It seemed to be related to VS Code in a strange way. VS Code was always opened with my index.js when trying to run my CLI tool.

But, as it turned out after much frustration, the output was just from VS Code and unrelated to the issue. VS Code surely will always make this output, just that I normally don't come to see it. The console was also used in an asynchronous way: VS Code printed output while the console window didn't expect output anymore, because the program started from the console already finished, but VS Code as a child process still used the console for output.

The mentioning of C++ source code files like jump_list.cc seems to come from Chromium, then Electron, then VS Code. This was highly irritating!

So, the problematic current behavior is that npx (or npm with exec command, respectively) and the .cmd, .ps1 files etc. generated in the local npm ecosystem open the file to be run in an editor instead of running it, or, if this is somehow by design, don't even warn you that a shebang is missing, which prevents the obviously intended action of running it with node.

Expected Behavior

Run .js files instead of opening them in the default editor, or at least print a warning about the missing shebang #!/usr/bin/env node.

If possible, a spawned child process on whose termination the spawning code doesn't wait, shouldn't be allowed to use the console.

Environment

  • npm: 10.2.4
  • Node.js: v20.10.0
  • OS Name: Windows 10 Home
  • System Model Name: (desktop)
  • npm config:
; "builtin" config from C:\Users\<redacted>\AppData\Roaming\npm\node_modules\npm\npmrc

prefix = "C:\\Users\\<redacted>\\AppData\\Roaming\\npm"

; node bin location = C:\Program Files\nodejs\node.exe
; node version = v20.10.0
; npm local prefix = C:\<redacted>
; npm version = 10.2.4
; cwd = C:\<redacted>
; HOME = C:\Users\<redacted>
; Run `npm config ls -l` to show all defaults.

Minimum Viable example

As an extra, here is a minimum viable example to trigger the problem:

package.json:

{
  "name": "script-gets-opened-example",
  "version": "1.0.0",
  "description": "",
  "bin": "./index.js"
}

index.js:

console.log("I'm just an innocent script.");
  • Run with npx ., npm link; script-gets-opened-example or npm install . --global; script-gets-opened-example to trigger the problem.
  • Add shebang #!/usr/bin/env node at start of index.js to solve the problem.

(Removal via npm rm --global script-gets-opened-example.)

Thanks for sharing this in my scene I actually used the shebang manually but the issues exist :( But I am happy for you finding out your own solutions! I just gave up using npm link finally.

LikeDreamwalker avatar Dec 04 '23 11:12 LikeDreamwalker

Same problem here - this used to work in previous versions without an issue and it's extremely annoying

kkmuffme avatar Jan 18 '24 22:01 kkmuffme