Bug 20571 – spawnProcess does not find .bat files

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2020-02-09T03:33:42Z
Last change time
2024-12-01T16:36:18Z
Assigned to
No Owner
Creator
Jonathan Marler
Moved to GitHub: phobos#9788 →

Comments

Comment #0 by johnnymarler — 2020-02-09T03:33:42Z
When you execute a program "foo" with spawnProcess, it will find "foo.exe" if it is a file in the PATH environment variable. However, it will not find programs with the ".bat" extension. When searching for a program in PATH on windows, all files with an extension from the PATHEXT environment variable should be checked, i.e. PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC I've created a small program to demonstrate/reproduce this issue: import std.file, std.process, std.stdio; void main(string[] args) { if (args.length == 1) runTest(); else writeln("running from an exe!"); } void runTest() { const runDir = "C:\\temp\\testworkdir"; const pathDir = "C:\\temp\\testpathdir"; if (!exists(runDir)) mkdir(runDir); if (!exists(pathDir)) mkdir(pathDir); chdir(runDir); const thisExe = thisExePath(); writefln("thisExePath is '%s'", thisExe); const tempExe = pathDir ~ "\\anexeprogram.exe"; std.file.copy(thisExe, tempExe); environment["PATH"] = environment["PATH"] ~ ";" ~ pathDir; writefln("Added '%s' to path", pathDir); run(["anexeprogram", "recursive"]); // works const tempBat = pathDir ~ "\\abatprogram.bat"; { auto batFile = File(tempBat, "w"); batFile.writeln("@echo running from a bat file"); } run(["abatprogram.bat"]); // works run(["abatprogram"]); // FAILS writefln("Success!"); } void run(string[] args) { writefln("Running %s", args); auto p = spawnProcess(args); assert(0 == wait(p)); }
Comment #1 by torhu — 2022-10-31T01:45:21Z
Since batch files are not executables, but interpreted by cmd.exe, you need to use spawnShell instead. Or are you suggesting that files with extensions in PATHEXT should be treated in a similar way to Linux shell scripts with shebang lines in them? It's a bit messy to implement that. The Windows ShellExecuteW function will run non-executables, but it doesn't allow you to set environment variables. So it's not a generic replacement for CreateProcessW.
Comment #2 by torhu — 2022-10-31T03:20:59Z
Nevermind, I see what you mean now. spawnProcess will run batch files, but it won't find them unless the .bat extension is included.
Comment #3 by robert.schadek — 2024-12-01T16:36:18Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/9788 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB