I have a Visual Studio 2019 project with a Post-Build-Event, that is calling an EXE file I have written in C#.
Post-Build event:
MyTool.exe "$(TargetPath)"
This EXE file is doing some stuff and then calling another EXE file.
AnotherTool.exe SomeArguments
Problem is, if that second EXE file (AnotherTool.exe) gives an error (on StandardError output) or an exit code != 0, Visual Studio is "seeing" that codes, although the AnotherTool.exe is not called directly from the Post-Build event. The Post-Build just called the MyTool.exe.
I want MyTool.exe to handle that exit codes, so Visual Studio should ignore them. But the build fails, when AnotherTool.exe exits with an error.
Any ideas?
Edit: The "MyTool.exe" is calling the "AnotherTool.exe" using System.Diagnostics.Process. I set RedirectStandardOutput = true and RedirectStandardError = true, and then call the Process with Start() and WaitForExit().
But no matter what that process result is, MyTool.exe is always exiting with Environment.Exit(0) to give a clean exit.
Not an answer, but a Workaround that is working:
I created a batch file, that is calling:
start MyTool.exe %1
With this workaround, the build succeeds without error, and the tool chain is started correctly. I get no more error messages displayed in Visual Studio.
Trade off 1: The "MyTool.exe" is now displayed in a command window while running.
Trade off 2: If "MyTool.exe" fails with exit code != 0, Visual Studio won't notice.
If I remove the "start" from the batch, behaviour is like a direct call of MyTool.exe.
Related
I have a simple post-build event to open Explorer. However, when I include this line, I always get an error, "exited with code 1" but Exporer opens to the folder specified.
How can I do this without reciving the error?
E.g., add the following line to a post-build event:
explorer C:\\aa
Add another line "exit /b 0". This will tell the command to exit with error code 0.
Thank you for the suggestions.
I managed to solve it by changing to:
start "" C:\aa
I'm trying to launch a grunt process that will watch for .js file changes and transpile them into one file using grunt tasks. I can run it manually all day long, but I want to make it a pre-build so when a new dev gets the solution, building will launch the watcher. The problem (as you may have guessed) is that the grunt process stays running, so as a pre-build event, it never continues the build. I thought that using start would be asynchronous and would launch it without VS waiting on it to complete to continue the build, but I was mistaken. So, currently, my pre-build event is
cmd /c start $(SolutionDir)ProjectName.Web\run-grunt.cmd $(SolutionDir)
and run-grunt.cmd looks like
cd %1ProjectName.Web
grunt
This works but hangs until I close the cmd window which defeats the whole purpose. So, two questions:
Is this the wrong way to get this watcher kicked off?
Is there a way to structure these such that VS will launch the cmd and then resume the build without waiting for a return?
Have you tried setting your pre-build event to simply run a batch file that executes your current pre-build event? It seems like it will work because the batch file that Visual Studio executes will actually complete, leaving another command window in its wake.
I tested my theory with two batch files:
test1.bat
call "cmd /C start test2.bat"
echo test1
test2.bat
echo test2
pause
If you're still needing some parts of the grunt task to execute pre-build, you can break them out into separate tasks, so your pre-build event may end up looking like this:
cd $(SolutionDir)ProjectName.Web\
grunt build
grunt-watch.bat
I have a solution with multiple projects. One project only needs to build if both two events, in the pre-build event, exit with error code 0.
So I thought I could do the following:
"C:\Path\To\Binary1.exe" & "C:\path\to\binary2.exe"
In my test scenario something goes wrong so Binary1.exe exits with a non-zero value. But visual studio goes on building the project anyway.
When I run the pre-build event commandline in cmd and echo %errorlevel% I see the exit code being non-zero.
When I only put
"C:\Path\To\Binary1.exe"
in the pre-build event, the build is stopped and en error is shown in the Error List window of Visual Studio.
I am definitely sure that Binary1.exe is exiting with a non-zero value as its also shows a messagebox prior to exit.
I can think of one solution. Binary1.exe calling Binary2.exe and exiting with a non-zero exit code when Binary2.exe exits with a non-zero exit code. But that is not really a flexible solution.
To summarize:
How can I run multiple pre-build events and stop buidling when one of the commands returns a non-zero value?
I think yuou can do as follows:
run command 1
if ERRORLEVEL 1 (
exit /b 1
)
run command 2
If the two projects are in the same solution, you can set the dependency in Visual studio.
Right-click on the solution in the solution explorer and choose "Project Dependencies".
Set the 'last' project to be depending on the first two. In this case Visual studio will build in the right order and will stop building if one of the dependencies fail to build.
(Visual Studio 2013)
When I copy/paste the lines below into a cmd window it executes without a problem.
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
msbuild proj\projsln /p:Configuration=Debug
proj\proj\bin\Debug\proj.exe my args
However when I save it as DoStuff.bat I get the message below (which is the text from executing vcvars32.bat), then nothing else. It does not build my project and obviously doesn't run the newly built executable.
Why doesn't it and how do I have it run all three commands?
>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
Use CALL to call another batch file.
Well, there has to be a reason it isn't continuing. Is it that the command is waiting for some input? Thats all that I can think of. Try re-directing the output of the batch file to a log and see what is going on.
Alternatively, split the batch file into separate batch files and put a CALL before each call to the batch file.
I'm using Visual Studio 2005 Express Edition with SP1.
I have a Pre-Link Event which needs to invoke one program and send its output to another.
foo | bar
This command works as expected when invoked from a command line or batch file. The command fails when invoked from the Pre-Link Event (even if the Pre-Link Event invokes a separate batch file containing the command). 'foo' runs and produces output which appears in Visual Studio, but this output is not fed to 'bar', and in fact it appears 'bar' is never invoked at all. Even when the Pre-Link Event invokes CMD or NMAKE which in turn invokes the piped commands, only 'foo' runs.
Is Visual Studio broken and/or is there some arcane ritual I can perform to make this work?
This was answered here for the > operator. I suspect the same thing may be happening with pipes.