Fix nob_needs_rebuild with open files in windows#246
Open
eduardomosko wants to merge 1 commit into
Open
Conversation
Member
|
Could you also please provide a minimal example that reproduces the problem you are having. |
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 necessaryBut 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 necessaryIn 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). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
So yeah, accept this if you want.