Skip to content

Fix nob_needs_rebuild with open files in windows#246

Open
eduardomosko wants to merge 1 commit into
tsoding:mainfrom
eduardomosko:main
Open

Fix nob_needs_rebuild with open files in windows#246
eduardomosko wants to merge 1 commit into
tsoding:mainfrom
eduardomosko:main

Conversation

@eduardomosko

Copy link
Copy Markdown

Windows sucks

But anyway, I need to check if an exe needs_rebuild while it is running because of hot-reload reasons, and it won't work with GENERIC_READ.

As per https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea changing that to 0 works for querying the modification time.

If this parameter is zero, the application can query certain metadata such as file, directory, or device attributes without accessing that file or device, even if GENERIC_READ access would have been denied.

So yeah, accept this if you want.

@rexim

rexim commented Jul 15, 2026

Copy link
Copy Markdown
Member

Could you also please provide a minimal example that reproduces the problem you are having.

@eduardomosko

Copy link
Copy Markdown
Author

Yes, for sure. As I said, this has been an issue when hot-reloading, so to mimic that I just open the file for reading.

#define NOB_IMPLEMENTATION
#include "nob.h"

int main(int argc, char **argv) {
    NOB_GO_REBUILD_URSELF(argc, argv);

	const char *this_file = __FILE__;
	const char *file = "output.txt";
	const char *contents = "This is an example file\n";

	if (nob_file_exists(file)) {
		// Mimics running an exe
		FILE *f = fopen(file, "r");
	}

	if (nob_needs_rebuild1(file, this_file)) {
		nob_log(NOB_INFO, "rebuilding...");
		nob_write_entire_file(file, contents, strlen(contents));
	} else {
		nob_log(NOB_INFO, "no rebuild necessary");
	}
}

This works fine in linux:

$ clang-21 -o nob nob.c 
$ ./nob 
[INFO] rebuilding...
$ ./nob 
[INFO] no rebuild necessary

But in windows it errors and always tries to rebuild:

PS > clang -o nob.exe nob.c
PS > ./nob.exe
[INFO] rebuilding...
PS > ./nob.exe
[ERROR] Could not open file output.txt: The process cannot access the file because it is being used by another process.
[INFO] rebuilding...

With my change, the output is consistent in both platforms:

PS > clang -o nob.exe nob.c
PS > ./nob.exe
[INFO] rebuilding...
PS > ./nob.exe
[INFO] no rebuild necessary

In the real use-case one of my build targets would already be open when nob runs (the program that is executing) but it wouldn't need rebuild, so nob could just skip it, while rebuilding anything else that needs to be rebuilt (the shared library that is hot-reloaded).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants