Skip to content

ExitCode does not bubble out bat files when not called from DOS #29582

@Mpdreamz

Description

@Mpdreamz

%ERRORLEVEL% is reported just fine in DOS. Note the CALL is mandatory when calling any batch file and using ||.

$ (CALL bin\elasticsearch-plugin.bat install --batch asfasf) || echo %ERRORLEVEL%
A tool for managing installed elasticsearch plugins

Commands
--------
list - Lists installed elasticsearch plugins
install - Install a plugin
remove - removes a plugin from Elasticsearch

Non-option arguments:
command

Option         Description
------         -----------
-h, --help     show help
-s, --silent   show minimal output
-v, --verbose  show verbose output
ERROR: Unknown plugin asfasf
64

But on powershell $LASTEXITCODE is 0:

PS> bin\elasticsearch-plugin.bat install --batch asfasf; echo $LASTEXITCODE
A tool for managing installed elasticsearch plugins

<snip>
ERROR: Unknown plugin asfasf
0

However if we modify the .bat files we distribute to explicitly exit

endlocal
endlocal
exit /B %ERRORLEVEL%

NOTE: ENDLOCAL does not alter %ERRORLEVEL%.

$LASTEXITCODE is set correctly in powershell.

PS > bin\elasticsearch-plugin.bat install --batch asfasf; echo $LASTEXITCODE
A tool for managing installed elasticsearch plugins

<snip>
ERROR: Unknown plugin asfasf
64

A similar result is observed when automating the bat files from C#

var bat = @"bin\elasticsearch-plugin.bat";
var args = "install --batch asfasf";
var startInfo = new ProcessStartInfo(bat, args)
{
	UseShellExecute = false,
	CreateNoWindow = true
};
	
var p = Process.Start(startInfo);
p.WaitForExit();
	
var exitCode = p.ExitCode;

exitCode will be 0 right now and 64 when we use the explict exit /B %ERRORLEVEL% fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    :Delivery/PackagingRPM and deb packaging, tar and zip archives, shell and batch scripts>bugTeam:DeliveryMeta label for Delivery team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions