dart-lang / pub

The pub command line tool
https://dart.dev/tools/pub/cmd
BSD 3-Clause "New" or "Revised" License
1.05k stars 230 forks source link

Packages installed via `pub global activate` ALWAYS close command prompt on windows on execution. #2670

Closed slightfoot closed 4 years ago

slightfoot commented 4 years ago

Environment

pub version: Pub 2.10.0-110.0.dev

Problem

When using a cmd added via pub global activate it always exits the cmd prompt session.

Expected behavior

Not to close the cmd prompt session.

Actual behavior

pub global activate melos on windows; pub generates a batch file in %USERPROFILE%\AppData\Roaming\Pub\Cache\bin which contains for example:

@echo off
rem This file was created by pub v2.10.0-110.0.dev.
rem Package: melos
rem Version: 0.3.10-dev.2
rem Executable: melos
rem Script: melos
if exist "[REDACTED]\AppData\Roaming\Pub\Cache\global_packages\melos\bin\melos.dart-2.10.0-110.0.dev.snapshot" (
  dart "[REDACTED]\AppData\Roaming\Pub\Cache\global_packages\melos\bin\melos.dart-2.10.0-110.0.dev.snapshot" %*
  rem The VM exits with code 253 if the snapshot version is out-of-date.    
  rem If it is, we need to delete it and run "pub global" manually. 
  if not errorlevel 253 (   
    exit /b %errorlevel%    
  )
  pub global run melos:melos %*
) else (
  pub global run melos:melos %*
)

When I now run melos help in the cmd prompt it will now show the help and exit the cmd prompt session. After some debugging I found the issue to be the above exit /b %errorlevel% command. The /b is supposed to keep the cmd prompt open, but it fails.

The reason for failure is that exit /b if run from "outside" a batch file will exit the local command prompt. The if statement here is actually a command and makes the exit command not part of the set of batch commands. So it exists the command prompt instead.

Adjusting the batch file as shown below will result in the original authors expectation and exit the batch file with the correct error code.

@echo off
rem This file was created by pub v2.10.0-110.0.dev.
rem Package: melos
rem Version: 0.3.10-dev.2
rem Executable: melos
rem Script: melos
if exist "C:\Users\Simon\AppData\Roaming\Pub\Cache\global_packages\melos\bin\melos.dart-2.10.0-110.0.dev.snapshot" (
  dart "C:\Users\Simon\AppData\Roaming\Pub\Cache\global_packages\melos\bin\melos.dart-2.10.0-110.0.dev.snapshot" %*
  rem The VM exits with code 253 if the snapshot version is out-of-date.
  rem If it is, we need to delete it and run "pub global" manually.
  if not errorlevel 253 (
    goto error
  )
  pub global run melos:melos %*
) else (
  pub global run melos:melos %*
)
goto :eof
:error
exit /b %errorlevel%
slightfoot commented 4 years ago

/cc @kevmoo

slightfoot commented 4 years ago

Fixed in PR #2671

kevmoo commented 4 years ago

CC @jonasfj @mit-mit @srujzs