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

spawnProcess does not find .bat files #9788

Open
dlangBugzillaToGithub opened this issue Feb 9, 2020 · 2 comments
Open

spawnProcess does not find .bat files #9788

dlangBugzillaToGithub opened this issue Feb 9, 2020 · 2 comments
Labels
OS:Windows Issues Specific to Windows Severity:Enhancement

Comments

@dlangBugzillaToGithub
Copy link

johnnymarler (@marler8997) reported this on 2020-02-09T03:33:42Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=20571

CC List

  • torhu

Description

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));
}
@dlangBugzillaToGithub
Copy link
Author

torhu commented on 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.

@dlangBugzillaToGithub
Copy link
Author

torhu commented on 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.

@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS:Windows Issues Specific to Windows Severity:Enhancement
Projects
None yet
Development

No branches or pull requests

2 participants