develooper Front page | perl.perl5.porters | Postings from February 2018

[perl #132717] `pl2bat` BAT files exit with incorrect ERRORLEVEL andprocess exit codes

From:
Roy Ivy III via RT
Date:
February 10, 2018 23:56
Subject:
[perl #132717] `pl2bat` BAT files exit with incorrect ERRORLEVEL andprocess exit codes
Message ID:
rt-4.0.24-18797-1518306959-1610.132717-15-0@perl.org
On Sun, 14 Jan 2018 14:39:20 -0800, LeonT wrote:
> On Sat, Jan 13, 2018 at 6:12 PM, Roy Ivy III <perlbug-
> followup@perl.org> wrote:
> > `pl2bat` BAT files exit with incorrect ERRORLEVEL and process exit
> > codes
> >
> > As currently defined, the BAT files created with `pl2bat` will exit
> > with
> > incorrect ERRORLEVEL codes.
> >
> > Examples of failures, (from the CMD shell):
> >
> > #1
> >
> > ``` batch
> > echo exit(10); > "%TEMP%\fail.pl"
> > pl2bat "%TEMP%\fail.pl"
> > "%TEMP%\fail.bat" && echo SUCCESS || echo FAIL
> > :: "FAIL"
> > echo %ERRORLEVEL%
> > :: "1" (wrong!, should be "10")
> > ```
> >
> > #2
> >
> > ``` batch
> > echo exit(-1); > "%TEMP%\fail.pl"
> > pl2bat "%TEMP%\fail.pl"
> > "%TEMP%\fail.bat" && echo SUCCESS || echo FAIL
> > :: "SUCCESS" (wrong!, should be "FAIL")
> > echo %ERRORLEVEL%
> > :: "1"
> > ```
> >
> > Changing...
> >
> > `if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul`
> >
> > ... to ...
> >
> > `if NOT "%errorlevel%" == "0" ( goto #_undefined_label_# 2>nul ||
> > "%COMSPEC%" /d /c exit %errorlevel% )`
> >
> > will exit the process correctly, returning a correct process code and
> > passing the correct ERRORLEVEL back up to the shell.
> >
> > With the change:
> >
> > ``` batch
> > echo exit(10); > "%TEMP%\fail.pl"
> > pl2bat "%TEMP%\fail.pl"
> > perl -pi.bak -e "$q=chr(34); $p=chr(37); $s=q{if errorlevel 1 goto
> > script_failed_so_exit_with_non_zero_val 2>nul}; $t=qq{if NOT
> > ${q}${p}errorlevel${p}${q} == ${q}0${q} ( goto #_undefined_label_#
> > 2>nul ||
> > ${q}${p}COMSPEC${p}${q} /d /c exit ${p}errorlevel${p} )};
> > s/$s/$t/eg;"
> > "%TEMP%\fail.bat"
> > "%TEMP%\fail.bat" && echo SUCCESS || echo FAIL
> > :: "FAIL"
> > echo %ERRORLEVEL%
> > :: "10" (now correct)
> > ```
> >
> > Additionally, it works for all non-zero ERRORLEVELs (such as the
> > common
> > "-1").
> >
> > ``` batch
> > echo exit(-1); > "%TEMP%\fail.pl"
> > pl2bat "%TEMP%\fail.pl"
> > perl -pi.bak -e "$q=chr(34); $p=chr(37); $s=q{if errorlevel 1 goto
> > script_failed_so_exit_with_non_zero_val 2>nul}; $t=qq{if NOT
> > ${q}${p}errorlevel${p}${q} == ${q}0${q} ( goto #_undefined_label_#
> > 2>nul ||
> > ${q}${p}COMSPEC${p}${q} /d /c exit ${p}errorlevel${p} )};
> > s/$s/$t/eg;"
> > "%TEMP%\fail.bat"
> > "%TEMP%\fail.bat" && echo SUCCESS || echo FAIL
> > :: "FAIL" (now correct)
> > echo %ERRORLEVEL%
> > :: "-1"
> > ```
> >
> > Thanks for the attention!
> 
> In the past two decades pl2bat got forked twice. First in
> Module::Build and the second time in ExtUtils::Helpers. The latter is
> the only one that has this issue fixed (primarily because it's the
> only one that includes tests). A few months ago I have extracted this
> logic into a new module (ExtUtils::PL2Bat), with the intention of
> making all of these modules use this single implementation that is
> actually maintained.
> 
> My main limitation here is not really being a Windows person, and
> hence currently not having a setup to test what I'm doing. I really
> could use a hand there.
> 
> Leon

Leon,

I forked [ExtUtils::PL2Bat](https://github.com/rivy/perl.ExtUtils-PL2Bat) and added all the needed corrections (to PL2Bat, the AppVeyor CI config, and testing). I've pushed a [PR](https://github.com/Perl-Toolchain-Gang/extutils-pl2bat/pull/3) for all the changes.

The PL2Bat changes include fixed handling of "echo", "ERRORLEVEL", and general environment state.

Testing now checks for correct process exit status as well as the actual exit code.

And the new AppVeyor config correctly tests against all `perl` versions back to v5.8.9; and all tests are now passing.

This should give you a much better Windows base for further changes. Let me know if I can be of further assistance, or if you need changes to the PR.

- Roy


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=132717



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About