Command copy exited with code 4 when building - Visual Studio restart solves it - windows

Every now and then when I build my solution here (with 7 projects in it) I get the dreaded 'Command copy exited with code 4' error, in Visual Studio 2010 Premium ed.
This is because of the post-build event not being able to go through.
Here's what solves the problem, temporarily
Sometimes: A restart of Visual Studio and I'm able to build the solution
Sometimes: Both a restart of Visual Studio and my file manager of choice (Q-Dir 4.37) solves it.
Here's what the post-build event looks like:
xcopy "$(SolutionDir)Solution Items\References\*.dll" "$(TargetDir)" /Y
When you get the command copy exited with code [insert value] error, it's normally because of the following:
read / write permissions
missing files
wrong directories
However - obviously at times when I build the solution, there's no problem.
FYI, I uninstalled ReSharper 5.1.1 two weeks ago and Visual Studio's been giving me some errors since then (among them not being able to debug). I re-installed Visual Studio and it's working better since then, but still get this problem. Could it have to do with some ReSharper stuff being somewhere?
Have you had the same problem and solved it? Or do you have any possible solution to it?

While /C may ignore errors, it might not be the real solution as there could be files that MUST be copied in order for the build to be successful.
The most common issue is the missing quotes around the pre-defined command tags (such as $TargetDir). When one creates various branches and paths in code or TFS, there is a very high chance for this to occur.
Sometimes if the file is read only, it will cause issues too. Add the /R option to allow read only files to be copied over. You can find list of available options at:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true
Another possible issue is that the underlying folder cannot be accessed. If so, trying performing "start xcopy" instead of "xcopy". This will open another command window but with admin priveleges.

I've invariably found this to be a file locking issue. Code 4 is Cannot Access File. One partial solution I found is to use the /C option for xcopy (which continues on error). Not really a solution but mostly it has stopped my builds from failing.
Another solution which only works on 32 bit is to use the unlocker tool to release the windows handles on the file before the copy.
Edit: I've just realised that it works under 64 bits too.

I crossed the same error, but it is not due to the file is locked, but the file is missing.
The reason why VS tried to copy an not existing file, is because of the Post-build event command.
After I cleared that, problem solved.
UPDATE:
As #rhughes commented:
The real issue is how to get the command here to work, rather than to
remove it.
and he is absolutely right.

I have also faced this problem.Double check the result in the error window.
In my case, a tailing \ was crashing xcopy (as I was using $(TargetDir)). In my case $(SolutionDir)..\bin. If you're using any other output, this needs to be adjusted.
Also note that start xcopy does not fix it, if the error is gone after compiling. It might have just been suppressed by the command line and no file has actually been copied!
You can btw manually execute your xcopy commands in a command shell. You will get more details when executing them there, pointing you in the right direction.

In case the post build event contains copy/xcopy command for copying build output to some directory(which usually is the most common post build operation) the problem can occur in case the full directory path either of source or target destinations contain folder names which include spaces. Remove space for the directory name(s) and try.

As mentioned in many sites, there are various reasons for this. For me it was due to the length of Source and Destination (Path length). I tried xcopy in the command prompt and I was unable to type the complete source and path (after some characters it wont allow you to type). I then reduced the path length and was able to run.
Hope this helps.

This can happen in multiple cases:
When the complete string path is longer than 254 chars.
When the name of the file to be copied is wrong.
When the target path is wrong.
When the readonly attribute is set on the copied file or target folder.

I got this error because the user account that TFS Build Service was running under did not have permissions to write to the destination folder. Right-click on the folder-->Properties-->Security.

Run VS in Administrator mode and it should work fine.

I got this error because of the file was opened in another instance.
when i closed the file and again re-build the solution, it was successfully copied.

I faced the same issue in case of XCOPY after build is done. In my case the issue was happening because of READ-ONLY permissions set on folders.
I added attrib -R command before XCOPY and it solved the issue.
Hope it helps someone!

I had the same error with xcopy in connection with the Test Engine. I am using VisualStudio Professional 2013. By default Test -> Test Settings -> Keep Test Execution Engine Running seems to be the reason for my error code 4 with xcopy. Switching it off solved the problem. The execution engine seems to keep hold on some .dlls.

I had the same problem.
A simple 'Clean Solution' in VS cleared the error, but it was a temporary solution.

I found that setting the file's Copy To Output Directory parameter to Copy Always seems to have cleared up the locking issue. Although now I have 2 copies of the files and need to delete one.

I had the same problem. However, nothing worked for me. I solved the issue by adding
exit 0
to my code. The problem was that while I was doing copying of the files, sometimes the last file could not be found, and the bat returned a non-zero value.
Hope this helps someone!

If you are running Windows 7 onward, you can try the new 'robocopy' command:
robocopy "$(SolutionDir)Solution Items\References\*.dll" "$(TargetDir)"
More information about robocopy can be found here.

I faced same issue.
I deleted post-build events and it started working.
Some times when we add some SQL components it may add post build commands also.

I am getting something similar using an xcopy with the /exclude option. In my case, I found that editing the post-build event (something harmless like a newline after the command) and saving the project causes the error to happen. Re-saving the file specified in the /exclude option causes it to work again.

As I am writing a DLL library I used the xcopy command to copy the library where the program can find and load it. After several times of opening and closing the program there was still an open process of it in taskmanager which i did not recognized.
Look for any process from which the file may be used and close it.

What fixed it for me:
dig down to the specific solution for the project you want i.e NOT the overall solution file for all the projects.
Do try - I tried everything else mentioned here but to no avail.

I don't see anything in here to suggest that this is a web-app but I have experienced this issue myself - I've got two xcopy commands on a post-build event and only one of them was failing. Something had a lock on the file, and it wasn't Visual Studio (as I tried restarting it.)
The only other thing that would have used the dll I built was IIS. And lo and behold,
A simple iisreset did the trick for me.

I had the same issue.
It was caused by having the same flag twice, for example:
if $(ConfigurationName) == Release (xcopy "$(TargetDir)." "$(SolutionDir)Deployment\$(ProjectName)\" /e /d /i /y /e)
Observe that the "/e" flag appears twice. Removing the duplicate solved the issue.

In my case my $(OutDir) was simply ..\..\Build\ i.e. some relative path. And, when I was trying to xcopy as follows
xcopy /y "$(OutDir)Proj1.dll" "Anypath\anyfolder\" I was getting the exit code error 4.
What's happening was, this command was getting executed in the $(OutDir) (in my case build folder) itself and not the directory where the csproj file of the project was located (as we would normally expect). Hence, I kept getting File not found error (corresponding to exit code 4).
I couldn't figure this out until I wrote cd in the Post Build events, to print which directory this was getting executed in.
So, to summarize, if we're wishing to copy / xcopy files from the $(OutDir), either use "$(TargetDir)" (which is complete path for output directory) or need not specify any path at all.

Can be caused by VMWare Workstation with Shared Folders
I have the problem always when the destinatinon folder of the xcopy is also mapped as Shared Folder in a VM.
I solved it with a script running in the vm and deleting the content of the shared folder.

To expand on rhughes answer,
The robocopy works beautifully, just incase you need to include sub directories you can use /e to include subs and copy empty directories or /s to include subs excluding empty directories.
Also robocopy will report back a few things like if new files were copied, this will cause VS to complain since anything above 0 is a failure and robocopy will return 1 if new files have been found. Its worth to mention that robocopy first compares the Source/Dest and only copies the updated/new files.
To get around this use:
(robocopy "$(SolutionDir)Solution Items\References\*.dll" "$(TargetDir)") ^& IF %ERRORLEVEL% LEQ 4 exit /B 0

If you are here because your project fails to build on a build server, but builds fine "manually" on a dev machine, and you are doing xcopy only for debugging and to emulate a production environment on a dev machine, then you may want to look at this solution:
https://stackoverflow.com/a/1732478/2279059
You simply turn off post build events on the build server using
msbuild foo.sln /p:PostBuildEvent=
This is not good enough if you have other post build events that also need to run on the build server, and it is not a general solution. However, since there are so many different causes of this problem, there cannot be a general solution. One of the many answers to this question (and its duplicates) will probably help, but be careful with approaches that only somehow circumvent error handling (such as xcopy /C). Those may work for you, particularly also in the build server scenario, but I think this one is more reliable, IF it can be used.
It has also been suggested that with newer versions of Visual Studio, the problem no longer exists, so if you are using an old version, consider updating your build tools.

Error code 4 can mean a lot of things, so I recommend reading the other answers as well until you find a solution that works for you AND you understand WHY it works (some solutions only disable error handling, which may only mask the problem but not solve it).
This can be a file locking issue related to parallel building. A workaround is to not use parallel building. This is the default behavior, but if you are using the -m option, then projects will be built in parallel. The following variations should not build projects in parallel, so you will not run into the file locking problem.
msbuild -m:1
msbuild -maxcpucount:1
msbuild
Note that, contrary to what has been said here, this even happens with the "latest" version of MSBuild (from Build Tools for Visual Studio 2019).
The best solution is probably to make sure you don't need to copy files in a post-build step. In some situations, you can also disable post-build steps when building with MSBuild on a build server: https://stackoverflow.com/a/55899347/2279059

I want to amplify and crystallize these two answers: #Vemul's, #Srihari Chinna's.
Make sure that your source path exists and that the process has access to it.
This is especially true if you're using variable substitution to assemble the source path.

Related

Xcode is not showing changes for files on SVN

We're using svn for version control on our Mac. Its working cool. But the only problem is we're multiple devs developing together and everyone can see any file changes status inside their Xcode ( attributes next to the file ) in their Xcode except me. How to resolve this?
This is what I want (see "M" next to the file name),
Even Xcode Source Control Menu is showing no changes.
I'm not sure if there's anything to set here?
I have checkout the code again and again, but still the problem persist.
I'm not sure, why this "Working Copies" menu "iOS" is disabled? Its enabled on other machine.
Any help would be highly appreciated.
I also encountered this problem, the following is my solution, hope I can help you.
Start the terminal, enter the code in the folder.
Type the command - svn status.
The output will be similar to this
svn: E155036: Please see the 'svn upgrade' command
svn: E155036: The working copy at '/Users/chao/svn/project'
is too old (format 29) to work with client version '1.9.4 (r1740329)' (expects
format 31). You need to upgrade the working copy first.
Type the command - svn upgrade.
The problem is resolved,I wish you good luck.
SVN can define status of working copy files and directories comparing your local files with the current repository located on the remote SVN server.
I believe that checking "Refresh server status automatically" will do the job.
You can say this is true when your local files will have attributes aside (U, M etc)
Having no luck, you may run the command line tool, which is usually more verbose. More details here: https://stackoverflow.com/a/19922150/195812

The specified task executable "cmd.exe" could not be run.The specified executable is not a valid application for this OS platform

I am getting this weird error on Windows 10 when I try to build any project. The projects are working fine if I build them on other OS.
The specified task executable "cmd.exe" could not be run. The
specified executable is not a valid application for this OS
platform.
When I tried to check the log I got to the following code in Nuget.targets file inside .nuget folder, I commented the code and the project builds fine now. I don't understand why the error. Any new projects that I create are working fine...
I am using Visual Studio 2013 on Windows 8, Windows Server 2008 and Visual Studio 2015 on Windows 10.
Code:
<Exec Command="$(RestoreCommand)"
LogStandardErrorAsError="true"
Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
What is the reason for this error ?
Is it because of OS or because of Visual Studio ?
How do I resolve it? I don't think commenting the code is the way to do it...
I got the same error, and repairing the corrupted files solved my issue. Try this:
In administrator command prompt, run sfc /scannow
It should emit log file in %windir%\Logs\CBS\CBS.log. Confirm that it repaired cmd.exe. For me I saw something like this Repairing corrupted file [ml:48{24},l:46{23}]"\??\C:\WINDOWS\SysWOW64"\[l:14{7}]"cmd.exe" from store
If that doesn't help you can try DISM /Online /Cleanup-Image /RestoreHealth
See more here
It happened to me when building my project on Windows 10. The problem was that the path of one file was very long (longer than 260 characters). So, by making it shorter, the problem solved for me.
I had the same problem with VS c++ OpenFrameWorks. I tried your suggestion but could not solve the problem. I solved it differently as under.
Soln:
Project Properties ->
Build Events ->Post-Build Event -> Command line
"robocopy "$(OF_ROOT)/export/vs/$(Platform_Actual)/" "$(ProjectDir)bin/" "*.dll" /njs /njh /np /fp /bytes if errorlevel 1 exit 0 else exit %errorlevel%"
Removed Above line from command-line.
OK, Solved.
If you started to face this error after deleting some files :
Unload your project
Edit Your csprok
Delete any reference for the your deleted files
Reload your project and Build
I faced the same issue when building a .NetCore App using a template that includes an Angular app with it, and i tried to delete the ClientApp folder. I had to remove multiple Target and ItemGroup tags from within the csproj.
To those who have landed here:
If you get this message I suggest that you do not, as #Hans Passant suggested, first assume that your machine or Windows has been corrupted.
Instead, I recommend that you suspect that your anti-virus or Carbon Black installation is to blame.
This was the case for me.
Someone from my IT Department disabled a certain Carbon Black policy and everything magically worked again.

Uninstalling IBM WebSphere

I am uninstalling WebSphere from my drive E by using IBM installation Manager, during uninstallation it gives me the following error:
"E:\Program Files\IBM\WebSphere\AppServer\bin\clearClassCache.bat" not found"
I have searched and found that the location of clearClassCache.bat file is:
"E:\Program Files\IBM\WebSphere\AppServer\profiles\AppSrv01\bin"
Should i copy the file from AppSrv01\bin to AppServer\bin and retry uninstallation? Or is there any other solution to this problem?
Scripts under AppServer\profiles\AppSrv01\bin should also be under AppServer\bin, but the copy under profiles is often (always?) modified to be profile-specific. So I doubt that copying it will work for you.
That specific script is under my AppServer\bin, so it seems it's been removed in your case. Possibly from the earlier part of the uninstallation.
Kinda an old question but here it goes.
Uninstall script is in <application_server>\uninstall folder. Not sure what is called under Windows but on Linux it is <application_server>\uninstall\uninstall ( yes script has the same name as folder)

Visual Studio unknown build error. The fully qualified name must be less than 260 characters

I'm having trouble while building my WPF solution. Everytime I try to build it, I get this error message:
Unknown build error, 'The specified path, file name, or both are too
long. The fully qualified file name must be less than 260 characters,
and the directory name must be less than 248 characters.'
Someone can help me? I've already verified all the fields and their extension paths are ok. Can this be a problem with the TortoiseSVN or something like this? I recently added a folder to my solution, can be something with this?
This is a well-known restriction in the Windows win32 api. The directory in which you stored your project is nested too deep. The full path name of a file cannot contain more than 259 characters. Beyond this, lots of C code that uses MAX_PATH starts failing due to buffer overflows.
Move your solution to another directory, one that's closer to the root.
I had a similar issue where the compiler reported that there was a problem with the file ASPNETCOMPILER. The actual issue was that the solution contained a node_modules folder, and this folder can contain very lengthy paths, and it looks like the compiler can't handle this.
Windows 8.1 and 10 have an option to increase the Win32 path limit:
Open Group Policy Editor (Press Windows+R and type gpedit.msc and hit Enter)
From the Group Policy Editor window, navigate to the following node: Local Computer Policy\Computer Configuration\Administrative Templates\System\Filesystem
Doubleclick on Enable Win32 long paths option and enable it.
This may allow you to temporarily move the problem folder out of the build, which may allow you to successfully build, then you may be able to add the folder back into the solution without affecting anything.
See here for additional reference - https://www.microfocus.com/documentation/filr/filr-4/filr-desktop/t47bx2ogpfz7.html
There is registry approach which can be used at least for Windows 10:
Value of the parameter HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled (Type: REG_DWORD) should be set to 1.
You don't even need top restart anything (IDE or PC).
This problem is caused by the restriction of Windows OS about handling long path greater than length 260. The solution of this problem for VS2019 is pretty straight-forward. But, for VS2017 users, we can use a workaround to solve the problem.
VS 2019
Open run window (windows-key + r), then type regedit and hit enter.
On the address bar of Registry Editor, enter this: HKLM\SYSTEM\CurrentControlSet\Control\FileSystem
On the right side, find the key LongPathsEnabled, double click on it, and change the value from 0 to 1
Restart VS2019, clean (if needed) and build your project again. The error should be fixed.
General workaround (VS2017/VS2019)
We can create a link of the original longer path to a shorter one. Then, VS will treat the shorter path as project root and the issue will be solved.
Create a shorter root path for project, e.g. we have created "D:\project" directory
Now open cmd (Command Prompt) and Create a symbolic link using this command: mklink /D "D:\project\myProject" "YOUR_ACTUAL_LONG_PATH_PROJECT_ROOT_DIRECTORY" [You may need to open cmd as administrator if above command fails to execute]
Now, open VS project from the "D:\project\myProject" directory and it should work.
No worry....This error comes because of long path name,Suppose your project folder name is "myproject".
You Just Change location of project folder...Put your Project "myproject" to D:\myproject or or F:\myproject drive.Then You Publish again..Its work...
Happy Coding...
It is issue with build defination workspace "Build Agent Folder location"
VS adds paths example:
$(SourceDir)E:\Somedirectory\ProjectName\
Just keep $(SourceDir) in filed
I had the same problem. My checkout path was shorter than the path used on my CI server. It built OK on the CI server, and all my colleagues machines. Our paths are the same length because our company has fixed length user names. I was the only machine that could not build the solution due to long paths.
If you go to these folders:
C:\Windows\Microsoft.NET\Framework
C:\Windows\Microsoft.NET\Framework64
C:\Users\[UserName]\AppData\Local\Temp
Find the version of .NET you are using with your solution, then delete the "Temporary ASP.NET Files" folders from which ever versions you think are effecting your build.
You can delete them all, your next build of a web project might take a little bit longer as there will be no pre-compiled assemblies...
This solution worked for me on a solution containing ASP.NET projects - it wont work on WPF projects that others have mentioned in their answers, unless their solution also contains ASP.NET projects.
Searching for the error string VS gives you brought me here, so I figured it might help someone else, if not the Op with a WPF build issue.
If these fail - you can try searching your solution root recursively for files/folders with paths that are greater the 260, by performing the following:
Open Powershell
cd <path to solution root>
cmd /c dir /s /b |? {$_.length -gt 260} > output.txt
This will pipe a list of files with paths > 260 to the output.txt file which will be newly created in the root of your solution.
This will help you find files within your solution that are too long.
For this, you have to run Power Shell on Windows as an administrator, and then type:
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" `
-Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force
This solved the issue

What's causing xcopy to tell me Access Denied?

The postbuild task for one of our solutions uses xcopy to move files into a common directory for build artifacts. For some reason, on my computer (and on a VM I tested), the xcopy fails with "Access Denied". Here's what I've done to try and isolate the problems:
I tried a normal copy; this works.
I double-checked that none of the files in question were read-only.
I checked the permissions on both the source and destination folder; I have full control of both.
I tried calling the xcopy from the command line in case the VS build process had locked the file.
I used Unlocker and Process Explorer to determine there were no locks on the source file.
What have I missed, other than paranoid conspiracy theories involving computers out to get me? This happens on my dev machine and a clean VM, but doesn't happen for anyone else on the project.
/r = Use this option to overwrite read-only files in destination. If
you don't use this option when you want to overwrite a read-only file
in destination, you'll be prompted with an "Access denied" message and
the xcopy command will stop running.
That's was mine resolution to this error.
Source
Problem solved; there's two pieces to the puzzle.
The /O switch requires elevation on Vista. Also, I noticed that xcopy is deprecated in Vista in favor of robocopy. Now I'm talking with our build engineers about this.
You need to run XCOPY as Administrator, there is no way around this.
If you don't want to run your copy as Administrator, then you must use ROBOCOPY instead.
Note, however, that with ROBOCOPY it is very tempting to use the /COPYALL switch, which copies auditing info as well and requires "Manage Auditing user right", which again invites you to run as Administrator as a quick solution. If you don't want to run your copy as Administrator, then don't use the /COPYALL (or /Copy:DATSOU) switch. Instead use /Copy:DATSO, as the U stands for aUditing.
Also note that if you are copying from NTFS to a FAT files system, there is no way you can "Copy NTFS Security to Destination Directory/File".
Usually this happens because there's another process locking the file. I bet your machine has a different number of cores/different speed than the others. Try inserting some sleeps to see if it solves the problem.
If you can delete the file in Windows Explorer, try using an elevated command prompt. Not sure why Windows Explorer does not ask permission here for a delete operation that needs admin rights via cmd.
if you are copying file to IIS folder, then you need run the batch file as admin.

Resources