-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
All cmd shims created for Invoke-Build have a mistake #339
Comments
From the top of my head, I would simply remove
|
Hi Roman, thanks for looking at this. I've just done some tests to try to confirm the behaviour you're talking about, and the potential problems didn't actually cause problems in practice, whereas removing the There are a couple of things to point out:
If I remove If a native app like robocopy changes the exit code inside a PS target script I think that's ok—if the target script hasn't reset the exit code then it's unlikely that anything calling the target script will be looking for an exit code. Finally, even if the target powershell script sets strict mode, I don't think this affects the calling scope where the |
Mixing PowerShell and CMD is pain at times. Nevertheless, it would be useful to They show that in PowerShell
|
Sorry, I'm not really following why we're talking about We want people to be able to write powershell scripts without needing to worry about whether they're executing in the context of a scoop shim or not, so calling Am I missing something here? |
Probably we are talking about different scenarios. Let's forget then about I just explained how |
It's not ok. The last exit code may be not zero but script works just fine (i.e. with robocopy). I do not think many scripts care of resetting exit codes. I am not sure what it is. What do you mean? |
P.S. I do not want to disturb too much. If you think all is fine for designed |
Hi Roman, thanks again for looking at this. I will close it for now, but if we can find a case where it's causing problems then we can re-open it again. It's an interesting behaviour that you've found—thanks for pointing it out. With the robocopy thing, I just meant that if a target script that calls robocopy doesn't care about setting it's own exit code, then it's ok if we don't clean that up. We just want to preserve whatever exit code the script finished with, so that it's transparent to the caller that the script executed in a completely different shell. |
The problem may be common for all shims created for PowerShell scripts. But I
have noticed it for Invoke-Build, so I explain it just for Invoke-Build.
Let's take a look at invoke-build.cmd
For Invoke-Build the correct shim would be
Because when IB fails it throws a terminating error and PowerShell in this case
exists with code 1. If IB works then PowerShell exits with code 0 itself. This
is a normal scenario.
But the extra part
exit $lastexitcode
is a mistake and it may break things.Same as above, if IB fails it throws a terminating error. But this means that
the next statement
exit $lastexitcode
is not called at all. So it is notneeded.
The case when IB succeeds is more interesting. The automatic variable
$lastexitcode
is changed when a native application is called.If no native command is called this variable is not even defined. In some cases
this will be treated as null and converted to 0. In other cases it may cause a
failure. Try to run this script in a fresh PowerShell session:
It fails: The variable '$LASTEXITCODE' cannot be retrieved because it has not been set.
So that if a build script actually sets
Set-StrictMode -Version 2
and callsno native commands then it always fails due to the shim
exit $LASTEXITCODE
.Then, if a native command was called then its exit code tells nothing about IB
success or failure. Suppose a build script invoked by IB is
robocopy
codes 0..3 are for success. Note that the functionexec
is told totake this into account and to not fail on 0..3 codes. But PowerShell still sets
$lastexitcode
to one of this values whenrobocopy
is called. And then theshim code uses it for exit. So in spite of the fact that IB succeeds the shim
exists with non zero code. This is the mistake.
As the author of Invoke-Build, I am glad and proud it is noticed by scoop.
Of course I would like Invoke-Build to be installed properly.
The text was updated successfully, but these errors were encountered: