Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deno.run() doesn't find .cmd executables in the PATH #16942

Closed
NfNitLoop opened this issue Dec 4, 2022 · 2 comments
Closed

Deno.run() doesn't find .cmd executables in the PATH #16942

NfNitLoop opened this issue Dec 4, 2022 · 2 comments
Labels
invalid what appeared to be an issue with Deno wasn't windows Related to Windows platform

Comments

@NfNitLoop
Copy link

Problem:

Deno.run() is unable to execute .cmd files unless they include the .cmd extension.

Expected:

When running on Windows, "foo" should be able to execute command foo whether it has an .exe or .cmd extension. (And possibly others? Do people still use .bat?)

Reproduction:

// bug.ts
async function run(...cmd: string[]) {
    const p = Deno.run({cmd})
    await p.status()
}

// is an .exe:
await run("protoc", "--version")

// workaround works, but requires always checking for Windows.
await run("npm.cmd", "--version")

// Does not work:
await run("npm", "--version")

Output:

deno run -A bug.ts

libprotoc 3.20.2
7.19.0
error: Uncaught (in promise) NotFound: program not found
    const p = Deno.run({cmd})
                   ^
    at opRun (deno:runtime/js/40_process.js:33:16)
    at Object.run (deno:runtime/js/40_process.js:116:17)
    at run (file:///C:/Users/Cody/code/feoblog-client/private/protobuf/bug.ts:3:20)
    at file:///C:/Users/Cody/code/feoblog-client/private/protobuf/bug.ts:9:7

Note:in both the Windows Command Prompt and PowerShell, both protoc and npm are runnable as commands without specifying a file extension:

> get-command protoc |Format-List

Name            : protoc.exe
CommandType     : Application
Definition      : C:\ProgramData\chocolatey\bin\protoc.exe
Extension       : .exe
Path            : C:\ProgramData\chocolatey\bin\protoc.exe
FileVersionInfo : File:             C:\ProgramData\chocolatey\bin\protoc.exe
                  InternalName:     protoc.exe
                  OriginalFilename: protoc.exe
                  FileVersion:      3.20.2.0
                  FileDescription:  Compiled with MSVC 19.0.24215.1 - Chocolatey
                  Shim
                  Product:          Protocol Buffers - Google's Data Interchange
                  Format - Chocolatey Shim
                  ProductVersion:   3.20.2.0 - Chocolatey Shim: 1.0.0
                  Debug:            False
                  Patched:          False
                  PreRelease:       False
                  PrivateBuild:     False
                  SpecialBuild:     False
                  Language:         Language Neutral


> get-command npm |Format-List

Name            : npm.cmd
CommandType     : Application
Definition      : C:\Program Files\nodejs\npm.cmd
Extension       : .cmd
Path            : C:\Program Files\nodejs\npm.cmd
FileVersionInfo : File:             C:\Program Files\nodejs\npm.cmd
                  InternalName:
                  OriginalFilename:
                  FileVersion:
                  FileDescription:
                  Product:
                  ProductVersion:
                  Debug:            False
                  Patched:          False
                  PreRelease:       False
                  PrivateBuild:     False
                  SpecialBuild:     False
                  Language:

Workaround

You can check whether you're on windows and add a .cmd. However, this also requires you to know whether a particular tool is distributed on Windows as an .exe or a .cmd. (i.e.: a blanket: "if windows, add .cmd" isn't a suitable fix, since it breaks anything that's distributed as an .exe.)

@dsherret
Copy link
Member

dsherret commented Dec 5, 2022

I was also confused about this a few months ago about this behavior in Rust's Command and investigated it then. This is expected behaviour. The same thing happens in node and is because CreateProcess in winapi does not check PATHEXT:

const { spawnSync } = require("node:child_process");

const result = spawnSync("npm", ["--version"]);
console.log(result.error); // ENOENT

More information: nodejs/node-v0.x-archive#2318

@dsherret dsherret added windows Related to Windows platform invalid what appeared to be an issue with Deno wasn't labels Dec 5, 2022
@dsherret
Copy link
Member

dsherret commented Dec 5, 2022

By the way, https://github.com/dsherret/deno_which will search the PATH using the PATHEXT on Windows to find the executable path. It might be useful for you. The annoyance is it requires permissions to read all the directories specified on the PATH in order to find the executable.

Another alternative is to run the command through cmd.

@dsherret dsherret closed this as completed Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid what appeared to be an issue with Deno wasn't windows Related to Windows platform
Projects
None yet
Development

No branches or pull requests

2 participants