Skip to content

Commit

Permalink
rdmd: Fix rebuilding a still-running program on Windows
Browse files Browse the repository at this point in the history
Windows locks the executable files of running processes.
While the program is running, we can't overwrite or delete
its executable file, but we can rename it out of the way.
  • Loading branch information
CyberShadow committed Dec 28, 2013
1 parent 1f39ed7 commit 085dc5d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion rdmd.d
Expand Up @@ -399,10 +399,11 @@ private int rebuild(string root, string fullExe,
string workDir, string objDir, in string[string] myDeps,
string[] compilerFlags, bool addStubMain)
{
auto fullExeTemp = fullExe ~ ".tmp";
string[] buildTodo()
{
auto todo = compilerFlags
~ [ "-of"~fullExe ]
~ [ "-of"~fullExeTemp ]
~ [ "-od"~objDir ]
~ [ "-I"~dirName(root) ]
~ [ root ];
Expand Down Expand Up @@ -455,6 +456,20 @@ private int rebuild(string root, string fullExe,
// directory. One will fail.
collectException(rmdirRecurse(objDir));
}
yap("mv ", fullExeTemp, " ", fullExe);
try
rename(fullExeTemp, fullExe);
catch (FileException e)
{
// This can occur on Windows if the executable is locked.
// Although we can't delete the file, we can still rename it.
auto oldExe = "%s.%s-%s".format(fullExe, Clock.currTime.stdTime,
thisProcessID);
yap("mv ", fullExe, " ", oldExe);
rename(fullExe, oldExe);
yap("mv ", fullExeTemp, " ", fullExe);
rename(fullExeTemp, fullExe);
}
}
return 0;
}
Expand Down

0 comments on commit 085dc5d

Please sign in to comment.