npm / cmd-shim

The cmd-shim used in npm
ISC License
76 stars 40 forks source link

[BUG] *.CMD shims don't work when they are in paths containing shell metachars #45

Open joeywatts opened 4 years ago

joeywatts commented 4 years ago

What / Why

Generated *.CMD files are unable to start correctly when the path to the CMD file contains certain characters.

When

Where

How

Current Behavior

'pm)\' is not recognized as an internal or external command,
operable program or batch file.
internal/modules/cjs/loader.js:969
    throw err;
    ^

Error: Cannot find module 'C:\test(n\node_modules\mkdirp\bin\cmd.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:17)
    at Function.Module._load (internal/modules/cjs/loader.js:859:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Steps to Reproduce

Two scenarios come to mind.

Scenario 1: Set NPM Prefix to New Path

  1. Set npm prefix to "C:/test(n&pm)"
  2. Globally install mkdirp (or any other binary package).
  3. Make sure "C:/test(n&pm)" is in your PATH.
  4. Attempt to run "mkdirp" (or whatever you installed).

Scenario 2: Local Dependencies

  1. Create an npm package in a folder like "C:/test(n&pm)/package"
  2. Install a local dependency which has a "bin" entry (like "mkdirp")
  3. Add a script to package.json which attempts to run the dependency's bin (like "trymkdir": "mkdirp mytestfolder")
  4. Attempt to run your script: npm run trymkdir

Expected Behavior

Who

References

wycats commented 4 years ago

This is a real issue (and well researched!). Any reason not to do this?

isaacs commented 3 years ago

We recently made the shift in npm v7 to use puka for exactly this purpose when passing arguments to cmd.exe. (And in the process, seem to have broken passing empty strings with "" to npm scripts, which will likely be fixed soon.). Unfortunately, translating the logic required into batch and powershell is nontrivial.

Patch welcome!

jcompagner commented 2 years ago

this commit: https://github.com/npm/cmd-shim/commit/4c37e048dee672237e8962fdffca28e20e9f976d

caused this issue because this:

SET dp0=%~dp0

will result in a wrong dir if the path is something like c:\something&somemore\

thing is if i quoted it

SET dp0="%~dp0"

then it doesn't work further in the script because then the dp0 variable has quotes (a bit stupid of cmd...) what we kind of want is get the full string without quotes

But for me kind of reverting that above commit works for me (so not using the "dp0" variable that has the content of ~dp0 but directly use ~dp0 everywhere..

jcompagner commented 2 years ago

what is exactly the problem with this pull request? https://github.com/npm/cmd-shim/pull/53/commits/022fcf1b0920ab0f83622516bf52c2be325b7483

because that keeps the current behavior quite as is and seems to fix the problem or does that really result in the same problem as issue #10 ?