Issue 20571 - spawnProcess does not find .bat files
Summary: spawnProcess does not find .bat files
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All Windows
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-02-09 03:33 UTC by Jonathan Marler
Modified: 2024-12-01 16:36 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jonathan Marler 2020-02-09 03:33:42 UTC
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 torhu 2022-10-31 01:45:21 UTC
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 torhu 2022-10-31 03:20:59 UTC
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 dlangBugzillaToGithub 2024-12-01 16:36:18 UTC
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