Visual Studio exited with code 1 post-build event explorer - visual-studio

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

Related

Post-Build should ignore Exit-Code of Sub-Task

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.

VS2019 Pre and Post build events always fails with code 1

I have a solution with just echo Hello post-build event and it always fails with the message The command "echo Hello!" exited with code 1.
Has someone an idea what could go wrong?
More info:
we have a team with about 15 developers. It always fails just for 3 of them
we have all Windows 10 and VS2019
we have tried different scripts and ended up with the echo Hello
we have tried with the prod solution and with an empty one as well
Full error message:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\
Microsoft.Common.CurrentVersion.targets(1328,5):
error MSB3073: The command "echo Hello!" exited with code 1.
Edit
Just have compared the Microsoft.Common.targets file from a developer with working events to one with non-working events. They have the same content.
I don't have Visual Studio in front of me to test this, but the problem feels like ERRORLEVEL may sometimes (randomly?) be non-zero on entry to the post-build script.
The problem is that ECHO does not affect ERRORLEVEL:
dir FileThatDoesNotExist Gives "File not found".
echo %ERRORLEVEL% Prints "1" ... an error.
echo Hello Prints "Hello".
echo %ERRORLEVEL% Still prints "1".
Therefore, if error-level happens to be non-zero, it will not be reset by the ECHO command. (Neither, as far as I can see, does REM affect it). There may be other ways of doing so, but DIR . > nul seems to work for me in resetting ERRORLEVEL to zero (it should always be possible to run DIR on the current directory!). The redirect should stop the output appearing in the build-log.
Obviously, if there was an earlier command in the post-build script that had failed, you probably don't want to ignore it. However, the pattern you're seeing (some users fail, some work) suggests that for some reason, Visual Studio is sometimes launching the post-build script with a non-zero error-level: with an empty script, or only ECHO commands, this will end up as the "result" of the post-build stage and the problem you're seeing.
Adding DIR . > nul to the TOP of the script should ensure that the exit-code is reset (and will allow real failures later in the script to be detected).
So the problem was the username. We had some freelancers in our company and they got a username starting with some special character. I can't remember which one, but I guess underscore. So when the admin sorts users by their user name they are on the top.
Changing the username has solved the problem in all our cases -_-

Running visual studio code in the current folder with batch file

I can follow below sequence of actions to run the Visual Studio Code in the current folder.
select the location bar in the current folder and run
(ALT + D):
cmd /k code .
I've tried to wrap the code . in the batch file (and place it in a folder where is declared in the PATH variable to run it as its own), it launches "Visual Studio Code" but doesn't close the command window until i close the visual studio code.
i know "start" command need to be there with exit command in the end but so far the combinations i've tried didn't really let me achieve that.
cmd.exe /k code .
exit
Any ideas?
this works for me,
Do not omit the first empty quote
Start "" "C:\Program Files (x86)\Microsoft VS Code\code.exe" <path_to_dir>\.
This works for me (be sure to start code and exit at the bottom of the batch file):
start "" code
exit
If you just want to start vscode from current folder from command line
type
code %cd%
Starts Visual Studio Code in current folder
#start code .
Starts Visual Studio Code in specific folder
#start code "<Specific Folder>"
Starts Visual Studio Code with a specific file
#start code "<Specific File>"
Save file as .bat
It works fine when I create cmd file with that script on the Windows 10 OS:
code c:\\path\\to\\Project | exit
[Run as] VS code extention for "Right click a file to run as the parameter of command in terminal."
https://marketplace.visualstudio.com/items?itemName=plylrnsdy.run-as
I tried this in command prompt and powershell:
call code
It works for me.

Visual Studio pre-build event; Checking exit code for each event

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)

error MSB3073: copy /d "C:\LOANAPP\UX\UserControls\*.css" "C:\LOANAPP\UX\GetQuotations\ThirdPartyOperator\CSS"" exited with code 1

We get this error in a project in a web application solution which has VSS as source control. The project has a script in the pre-build event command line in the VS 2005 Project property page - Build event is as below :
copy /d "$(SolutionDir)UX\UserControls\*.ascx" "$(SolutionDir)UX\GetQuotations\ThirdPartyOperator\UserControls"
copy /d "$(SolutionDir)UX\UserControls\*.master" "$(SolutionDir)UX\GetQuotations\ThirdPartyOperator\UserControls"
copy /d "$(SolutionDir)UX\UserControls\*.js" "$(SolutionDir)UX\GetQuotations\ThirdPartyOperator\Javascript"
copy /d "$(SolutionDir)UX\UserControls\*.css" "$(SolutionDir)UX\GetQuotations\ThirdPartyOperator\CSS"
gives this below error:
=============================================================
C:\LOANAPP\UX\UserControls\dhtmlwindow.css
C:\LOANAPP\UX\UserControls\modal.css
2 file(s) copied.
C:\LOANAPP\UX\UserControls\dhtmlwindow.css
**Access is denied.**
C:\LOANAPP\UX\UserControls\modal.css
**Access is denied.**
0 file(s) copied.
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets(823,9): error MSB3073: copy /d "C:\LOANAPP\UX\UserControls\*.ascx" "C:\LOANAPP\UX\GetQuotations\ThirdPartyOperator\UserControls"
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets(823,9): error MSB3073: copy /d "C:\LOANAPP\UX\UserControls\*.ascx" "C:\LOANAPP\UX\GetQuotations\ThirdPartyOperator\UserControls"
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets(823,9): error MSB3073: copy /d "C:\LOANAPP\UX\UserControls\*.master" "C:\LOANAPP\UX\GetQuotations\ThirdPartyOperator\UserControls"
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets(823,9): error MSB3073: copy /d "C:\LOANAPP\UX\UserControls\*.js" "C:\LOANAPP\UX\GetQuotations\ThirdPartyOperator\Javascript"
**C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets(823,9): error MSB3073: copy /d "C:\LOANAPP\UX\UserControls\*.css" "C:\LOANAPP\UX\GetQuotations\ThirdPartyOperator\CSS" exited with code 1.**
Because of this error we have whole solution build failing.
Could anyone help us out, so that we would be able to proceed with...
I am currently encountering the same problem.
Deleting the Release/Debug build directory prior to building you solution solves the issue for one build.
When rebuilding it after that, this has to be done again though...
I'll post a real solution when I find one...
Edit:
After investigating the problem some more, I found out that the problem in my solution occured because the copy command in my post-build step resulted in questions about whether or not to overwrite existing files. This somehow generates this problem.
The solution for me was to add the '/y'option to the copy/xcopy command you execute so these overwrite questions won't be asked.
If you rename the project name or change the name of the output file, change it in the post-build events and this can be solved.
error MSB3073: copy /d "C:\LOANAPP\UX\UserControls*.css" "C:\LOANAPP\UX\GetQuotations\ThirdPartyOperator\CSS" exited with code 1.
When a pre/post build command "exits with code 1", that means it failed. Just like if you said, "copy *.* f:\lolusux" and you didn't have a writable f: drive.
The solution is to fix the error.
What is the error, you say? I don't know. But I know how I'd go about figuring it out.
Run the build. In the build output, copy the pre/post build commands as you did in order to paste them into your question. They will have all the macros filled in with actual values, which may be part of the issue.
Paste this into Notepad. Clean all the cruft away (i.e., delete non-command text such as "error MSB3073: " and keep command text like "copy /d "C:\LOANAPP\UX\UserControls*.css" "C:\LOANAPP\UX\GetQuotations\ThirdPartyOperator\CSS")
Open a command window. Browse to the output directory of the project that fails to build. That's the root directory where these commands execute.
(optional for post build) disable post build commands and build the solution; this may be needed to make sure the files are in the expected state when the post build commands run.
Copy each line of the pre/post build commands from notepad, paste them into the command window, and execute them. From first to last, one by one. You will see the command fail (if you did everything right, if not, go back) and get the exact error message/cause of the failure.
Fix the real issue.
Just in case anyone finds that their problem isn't resolved by any of the above, I found that the error was being caused by folders in the path with spaces in their names. For instance:
MyFolder\ThirdParty Test\Blah\MoreFolders\BuildDirectory
...would fail, but
MyFolder\ThirdPartyTest\Blah...
Would work without issue. If you need to have the space in the path, surround the entire directory call in double quotes, like:
"MyFolder\ThirdParty Test\Blah\MoreFolders\BuildDirectory"
Or just the offending folder:
MyFolder\"ThirdParty Test"\Blah\MoreFolders\BuildDirectory

Resources