Skip to content

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

@joeywatts

Description

@joeywatts

What / Why

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

When

  • Whenever the path contains a cmd.exe shell metacharacter (such as &).

Where

  • Any package with a "bin" entry.

How

Current Behavior

  • The shell metacharacters are parsed by the shell instead of the full argument being passed into node. With my npm prefix set to "C:/test(n&pm)" and after installing the mkdirp package globally, I see the following error when attempting to run "mkdirp" in cmd.exe.
'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'
�[90m    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:17)�[39m
�[90m    at Function.Module._load (internal/modules/cjs/loader.js:859:27)�[39m
�[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)�[39m
�[90m    at internal/main/run_main_module.js:17:47�[39m {
  code: �[32m'MODULE_NOT_FOUND'�[39m,
  requireStack: []
}

Steps to Reproduce

  • Orchestrate any scenario in which the .CMD files will be generated under a path containing "&" character (in theory the other shell metacharacters would also be problematic, see references for an article on windows command line escaping).

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

  • The .CMD files should be able to run without getting tripped up by shell metacharacters in the path.

Who

  • n/a

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions