Skip to content

fix(ext/fs): retry without FILE_FLAG_BACKUP_SEMANTICS on Windows when driver rejects it#34686

Merged
littledivy merged 1 commit into
mainfrom
orch/divybot-406
Jun 2, 2026
Merged

fix(ext/fs): retry without FILE_FLAG_BACKUP_SEMANTICS on Windows when driver rejects it#34686
littledivy merged 1 commit into
mainfrom
orch/divybot-406

Conversation

@divybot

@divybot divybot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

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

  • cargo +1.95.0 check -p deno_fs (Linux host)
  • cargo +1.95.0 clippy -p deno_fs --no-deps
  • 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.

Refs #26257

Closes denoland/divybot#406

… 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
littledivy merged commit 93d5bfa into main Jun 2, 2026
136 checks passed
@littledivy
littledivy deleted the orch/divybot-406 branch June 2, 2026 04:04
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]>
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