Bug 17903 – dmd leaves behind bad executable when linker fails
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2017-10-16T13:35:23Z
Last change time
2022-11-23T09:30:13Z
Assigned to
No Owner
Creator
Jonathan Marler
Comments
Comment #0 by johnnymarler — 2017-10-16T13:35:23Z
When compiling an executable with DMD, if the linker fails then DMD will still create the target executable even though it is invalid and cannot run.
This breaks build tools that check the existence of the executable to know if the executable needs to be rebuilt (virtually all build tools do this). If you have a linker error, the first time you run your build dmd will leave behind an invalid executable. If you run the build again it will think that the executable has already been built and move on to the next part of the build.
One way to fix this would be to modify dmd to remove the executable if the linker fails. Another way would be to write the executable to a temporary file, and then rename it to the target executable or delete it depending on if the linker succeeded or failed. This way, if the compiler gets interrupted, or you lose power, or something happens that prevents dmd from cleaning up the binary, you still won't have a target executable.
The following can be used to reproduce the issue:
forcelinkerror.d
-------------
import std.stdio, foo;
void main()
{
writeln("This exe can run (foofunc() = %s)", foofunc());
}
-------------
foo.d
-------------
module foo;
int foofunc() { return 42; }
-------------
Run the following to force a linker error
> dmd forcelinkerror.d
This will leave behind an invalid executable forcelinkerror.exe
Comment #1 by razvan.nitu1305 — 2022-11-23T09:30:13Z
I cannot reproduce this. I don't understand how you could get an executable if the linker fails.