<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/83046>83046</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            llvm::sys::fs::access can spuriously fail on Windows when called with AccessMode::Exist
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          z2oh
      </td>
    </tr>
</table>

<pre>
    We are regularly seeing this issue surface as a compiler crash on Apple's LLVM fork: https://github.com/apple/llvm-project/issues/8224

https://github.com/llvm/llvm-project/blob/f1bb88bee248fb30e3145a2a19373233b7a59882/llvm/lib/Support/Windows/Path.inc#L617

I've been unable to reproduce this in isolation, but there seems to be some kind of race condition when calling `GetFileAttributesW` such that the fails on a valid path with `ERROR_ACCESS_DENIED`. I've managed to work around this locally by avoiding the call to `GetFileAttributesW` when the requested access mode is `AccessMode::Exist`, and instead calling the shell function `PathFileExistsW`:
```
if (Mode == AccessMode::Exist) {
  if (::PathFileExistsW(PathUtf16.begin())) {
    return std::error_code();
 } else {
    return errc::no_such_file_or_directory;
 }
}
```
I've added some logging here and verified that this function returns correctly when `GetFileAttributesW` would have failed with `ERROR_ACCESS_DENIED`. This function requires pulling in `shlwapi.h`.

I'm happy to make a PR with this patch, but I wanted to make an issue first to seek alternate solutions, or to see if anyone had insight as to why `GetFileAttributesW` is failing in this way.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVdFv47YP_muUF-IX2HLiOA95yF2aHwr0tkO7rY8BbdGxVkXySXIy768faKXXXtduGGAkjkJ-JD9-pDAEfbREG7H8JJa7GQ6xc37zp3TdrHZq3DwSoCfwdBwMejNCINL2CLHTAXQIA0EYfIsNAQZAaNyp14Y8NB5DB87Ctu8NCbkKcHf32xdonX8SxRa6GPsgiq2QeyH3Rx27oZ437iTkHpPH3pjz6X-9d79TE4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