Skip to content

Commit 862262c

Browse files
committed
Kludge for NOB_EXPERIMENTAL_DELETE_OLD on Windows
1 parent 8ddbc72 commit 862262c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

nob.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ NOBDEF void nob__go_rebuild_urself(int argc, char **argv, const char *source_pat
854854
nob_rename(old_binary_path, binary_path);
855855
exit(1);
856856
}
857-
#ifdef NOB_EXPERIMENTAL_DELETE_OLD
857+
#if defined(NOB_EXPERIMENTAL_DELETE_OLD) && !defined(_WIN32)
858858
// TODO: this is an experimental behavior behind a compilation flag.
859859
// Once it is confirmed that it does not cause much problems on both POSIX and Windows
860860
// we may turn it on by default.
@@ -864,6 +864,19 @@ NOBDEF void nob__go_rebuild_urself(int argc, char **argv, const char *source_pat
864864
nob_cmd_append(&cmd, binary_path);
865865
nob_da_append_many(&cmd, argv, argc);
866866
if (!nob_cmd_run_opt(&cmd, opt)) exit(1);
867+
868+
#if defined(NOB_EXPERIMENTAL_DELETE_OLD) && defined(_WIN32)
869+
// See https://github.com/tsoding/nob.h/issues/111
870+
// On Windows we can't delete the currently running executable, so we instead
871+
// launch an async process to do the deletion and exit to get out of the way.
872+
// TODO: There's a race condition here, sleeping any positive amount of time
873+
// before exiting seems enough to often trigger an "Access is denied" error.
874+
Nob_Procs procs = {0};
875+
opt.async = &procs;
876+
nob_cmd_append(&cmd, "cmd.exe", "/c", "del", old_binary_path);
877+
if (!nob_cmd_run_opt(&cmd, opt)) exit(1);
878+
#endif // NOB_EXPERIMENTAL_DELETE_OLD
879+
867880
exit(0);
868881
}
869882

0 commit comments

Comments
 (0)