fix(ext/fs): retry without FILE_FLAG_BACKUP_SEMANTICS on Windows when driver rejects it#34686
Merged
Conversation
… driver rejects it Some Windows file system drivers (notably ImDisk-backed memory disks) reject `FILE_FLAG_BACKUP_SEMANTICS` for regular files and return `ERROR_INVALID_FUNCTION` (1). Deno passes that flag so `CreateFile` can open directories with the same code path as files, which breaks `Deno.readTextFile`, `Deno.writeTextFile`, `Deno.stat`, and friends on those volumes. Transparently retry the open without the flag when we hit `ERROR_INVALID_FUNCTION`. Directories will still fail without the flag, but on the drivers in question only regular-file operations need to succeed; other errors propagate unchanged. Refs #26257 Co-Authored-By: Divy Srivastava <[email protected]>
littledivy
approved these changes
Jun 2, 2026
littledivy
added a commit
to crowlKats/deno
that referenced
this pull request
Jun 10, 2026
… driver rejects it (denoland#34686) ## Summary Some Windows file system drivers — notably ImDisk-backed memory disks — reject `FILE_FLAG_BACKUP_SEMANTICS` for regular files and return `ERROR_INVALID_FUNCTION` (\`os error 1\`). Deno passes that flag so `CreateFile` can open directories with the same code path as files, which causes \`Deno.readTextFile\`, \`Deno.writeTextFile\`, \`Deno.stat\`, \`Deno.lstat\` (and anything that goes through them, such as \`std/fs/exists\`) to fail on those volumes: \`\`\`txt Error: Incorrect function. (os error 1): readfile 'y:\a.txt' Error: Incorrect function. (os error 1): writefile 'y:\test.txt' Error: Incorrect function. (os error 1): stat 'y:\a.txt' \`\`\` This PR transparently retries the open without the flag when we see `ERROR_INVALID_FUNCTION`. Directories will still fail to open on the problematic drivers (they need the backup-semantics flag), but on those drivers only regular-file operations actually need to succeed. Any other error from the original open still propagates unchanged. The fallback covers both code paths that explicitly set the flag on Windows: - `open_with_checked_path` (used by `read_file_*`, `write_file_*`, `open_*`, and the text-file variants that delegate to them). - `stat` / `lstat` (each open the path with `access_mode(0)` plus the flag to grab metadata via `GetFileInformationByHandle` / `NtQueryInformationFile`). ## Test plan - [x] `cargo +1.95.0 check -p deno_fs` (Linux host) - [x] `cargo +1.95.0 clippy -p deno_fs --no-deps` - [x] `cargo +1.95.0 fmt --check -p deno_fs` - [ ] CI Windows job exercises the standard read/write/stat code paths Hard to add an automated test for the ImDisk-only failure mode here since we'd need to provision a memory-disk volume on the Windows CI runner. Fixes denoland#26257 Closes denoland/divybot#406 Co-authored-by: divybot <[email protected]> Co-authored-by: Divy Srivastava <[email protected]>
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.
Summary
Some Windows file system drivers — notably ImDisk-backed memory disks —
reject
FILE_FLAG_BACKUP_SEMANTICSfor regular files and returnERROR_INVALID_FUNCTION(`os error 1`).Deno passes that flag so
CreateFilecan open directories with the samecode path as files, which causes `Deno.readTextFile`, `Deno.writeTextFile`,
`Deno.stat`, `Deno.lstat` (and anything that goes through them, such as
`std/fs/exists`) to fail on those volumes:
```txt
Error: Incorrect function. (os error 1): readfile 'y:\a.txt'
Error: Incorrect function. (os error 1): writefile 'y:\test.txt'
Error: Incorrect function. (os error 1): stat 'y:\a.txt'
```
This PR transparently retries the open without the flag when we see
ERROR_INVALID_FUNCTION. Directories will still fail to open on theproblematic drivers (they need the backup-semantics flag), but on those
drivers only regular-file operations actually need to succeed. Any
other error from the original open still propagates unchanged.
The fallback covers both code paths that explicitly set the flag on
Windows:
open_with_checked_path(used byread_file_*,write_file_*,open_*, and the text-file variants that delegate to them).stat/lstat(each open the path withaccess_mode(0)plus theflag to grab metadata via
GetFileInformationByHandle/NtQueryInformationFile).Test plan
cargo +1.95.0 check -p deno_fs(Linux host)cargo +1.95.0 clippy -p deno_fs --no-depscargo +1.95.0 fmt --check -p deno_fsHard to add an automated test for the ImDisk-only failure mode here
since we'd need to provision a memory-disk volume on the Windows CI
runner.
Refs #26257
Closes denoland/divybot#406