Home

Help! LPRun.exe exceptions not setting %errorlevel%

I love Linqpad and when I saw LPRun, I thought it should work pretty well for our build scripts. I would love to write the logic for them in C# instead of batch files. However, I have run into a major issue in that LPRun.exe always returns an %errorlevel% of zero when an exception occurs when a script is run.

Here is my script in Linqpad (C# Statements):
throw new Exception("whoops");
Here is the batch file that calls it:
"C:\Program Files (x86)\LINQPad4\LPRun.exe" "c:\Users\user\Documents\LINQPad Queries\TestError.linq"
ECHO %errorlevel%
Here is the output:
Exception: whoops
   at UserQuery in c:\Users\user\Documents\LINQPad Queries\TestError.linq:line 3
   at LINQPad.ExecutionModel.ClrQueryRunner.Run()
   at LINQPad.ExecutionModel.Server.RunQuery(QueryRunner runner)

C:\Users\user\Documents\LINQPad Queries>ECHO 0
0
I would expect the %errorlevel% to be set to some value other than zero (which indicates success). I had previously used scriptcs.exe and .csx files and this was their behavior so I was quite surprised when I switched to lprun.

As potential solutions to this problem I tried a number of different approaches all with the same result of %errorlevel% still being zero:

Setting Environment.ExitCode
Environment.ExitCode = 1;

Setting the environment variable explicity:
Environment.SetEnvironmentVariable("ERRORLEVEL", "1");

Returning a value from Main:
int Main() {
    return 1;
}
I guess I can accept that maybe this is a little outside of the original intention of LPRun and maybe Linqpad itself. However, I feel like there ought to be *some* way to return a value to the outside world. Is this something that can be considered a bug and be fixed, or is it just a "we won't support that" situation. I *love* Linqpad, already bought licenses for the build server and everything before discovering this issue (when I noticed our builds weren't working but were reporting success to Jenkins). Anybody have any creative solution that could work for me in the time being?

Comments

  • Thanks for the repro - this will be fixed for the next build.
  • There is a potential workaround/hack. You can call Environment.Exit and pass the error code you want to return. The danger with this method is that it will end the process immediately and prevent any cleanup from occurring. But it is a way to get LPRun to return the error code you want until the next build fixes it.
  • edited June 2015
    Dude! That's awesome, worked like a charm for my purpose and will be a great work around until he fixes the next build, thanks! Here is the pattern I'm using:
    void Main() {
        try {
            throw new Exception("oops");
        } catch (Exception ex) {
            ex.ToString().Dump();
            Environment.Exit(1);
        }
    }
    Output:
    C:\Users\user\Documents\LINQPad Queries>"C:\Program Files (x86)\LINQPad4 \LPRun.exe" "TestError.linq"
    System.Exception: oops
       at UserQuery.Main() in c:\Users\user\Documents\LINQPad Queries\TestError.linq:line 5
    
    C:\Users\user\Documents\LINQPad Queries>ECHO 1
    1
    Yay!
  • Joe -

    You implemented this functionality a while back. The requestor suggested some options.... did you implement them all? If not, which did you implement?

    Thanks

    User Joe
  • I've run into the same issue and I'd like to avoid Environment.Exit(…) in order to have a more graceful shutdown. Like @jsytniak, I'm wondering what's the status of this? One would imagine that it would be an easy fix so I am surprised to see this still unresolved (3 years on since it was reported).
  • This was fixed 3 years ago insofar as setting the %errorlevel% to 1 when the query throws an unhandled exception.

    In the 5.35.1 beta, I've also allowed you to return a non-zero value by returning an int value from Main, or by setting Environment.ExitCode.
  • Awesome and thanks a lot for the fix, Joe!
  • edited October 2019
    Using LINQPad 5.... I am finding that is necessary to use Environment.Exit(nonZero); Setting Environment.ExitCode alone or returning nonzero from Main still results in a zero return code.
  • edited October 2019
    I am also finding that when running a query with Environment.Exit(nonzero)... the status of the query when it completes is "Query ended unexpectedly". I guess that makes sense... but does seem to make the case for int Main working.
Sign In or Register to comment.