-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
[BUG] The generated .cmd script for CLI packages has faulty error handling #969
Comments
Interesting! Can you send a PR to https://github.com/npm/cmd-shim with this restructured output? |
@isaacs Done: npm/cmd-shim#46 |
This error originates from a CMD shell bug. This solution works because of another countervailing quirk which does pass the ERRORLEVEL out as the process exit code if and only if the command executing the target executable is the last command in the last parsable unit of the BAT/CMD file. And, this solution is better because it avoids another bug (not-resetting the window title between commands) caused by my initial fix for A couple of other notes...
|
I'm curious as to why If it's not needed, this construction should work: @setLocal
@echo off
set dp0=%~dp0
set "dp0=%dp0:~0,-1%" &rem clip the trailing path separator
IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)
endLocal & "%_prog%" "%dp0%\node_modules\yarn\bin\yarn.js" %* |
I was able to find an answer to the question of why So, now, I'm using this construction ( @setLocal
@echo off
goto :_START_
:set_real_dp0
@rem:: ref: "https://stackoverflow.com/questions/19781569/cmd-failure-of-d0-when-call-quotes-the-name-of-the-batch-file"
@rem:: ref: "https://stackoverflow.com/questions/12141482/what-is-the-reason-for-batch-file-path-referenced-with-dp0-sometimes-changes-o/26851883#26851883"
@rem:: ref: "https://www.dostips.com/forum/viewtopic.php?f=3&t=5057"
set dp0=%~dp0
set "dp0=%dp0:~0,-1%" &@rem:: clip trailing path separator
goto :EOF
:_START_
call :set_real_dp0
IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)
endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\node_modules\hexo-cli\bin\hexo" %* |
What / Why
npm version: 6.13.4 (node 12.14.1, 64 bit)
From what I understand, when you (globally) install a package via npm that also has a CLI component, wrapper scripts will get generated, in my example this was the "yarn" package (also verified the bug with other packages), which lead to the generation of a "yarn" shell script and a "yarn.cmd" batch file for Windows.
When I now use the CLI command via
yarn somescript
(which calls yarn.cmd because I'm on Windows), and the execution of said script fails, the errorlevel will be set to 1 correctly (this can be verified withecho %errorlevel%
), but somehow is ignored when using&&
.For example,
yarn somescript && dir
will still executedir
even whenyarn somescript
failed.After some experimentation, I found out that restructuring the generated
yarn.cmd
file solves the problem:With this, the errorcode is honored as expected. The only two changes necessary were moving
find_dp0
to the top and jumping over it, and to leave outEXITLOCAL
, which should not be necessary, since the documentation states that exiting the script will have the same effect. This allows the actual call to%_prog%
to be the last line in the script, therefore obviating the need to set the errorcode manually.How
Steps to Reproduce
On Windows:
Expected Behavior
dir
should not be run in the exampleedit: Also read this which explains why %errorlevel% is different from the observed
&&
behavior, but it doesn't change the fact that the.cmd
doesn't set the errorlevel correctlyThe text was updated successfully, but these errors were encountered: