Skip to content

fix(windows): canonicalize FileTime paths to prevent false overwrite rejections#20071

Open
lars-hagen wants to merge 2 commits intoanomalyco:devfrom
lars-hagen:fix/windows-filetime-path-canonicalization
Open

fix(windows): canonicalize FileTime paths to prevent false overwrite rejections#20071
lars-hagen wants to merge 2 commits intoanomalyco:devfrom
lars-hagen:fix/windows-filetime-path-canonicalization

Conversation

@lars-hagen
Copy link
Copy Markdown

@lars-hagen lars-hagen commented Mar 30, 2026

Issue for this PR

Closes #18028

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

On Windows the read tool normalizes file paths via Filesystem.normalizePath
(#16069), but FileTime stores and looks up paths using raw strings. When a
symlink, path-casing variant, or relative/absolute mismatch is involved, the
read stores one key and the edit/write assert looks up a different one,
producing a false "You must read file before overwriting it" error.

This covers all the scenarios reported in #18028:

  • Relative vs absolute (./tmp/file.txt vs c:/Users/.../tmp/file.txt)
  • Backslash vs forward slash
  • Drive-letter casing (c: vs C:)
  • Symlinks and junctions

This patch adds Filesystem.normalizePath at the entry of every FileTime
method (read, get, assert, getLock, withLock) so all callers share the
same canonical key. On non-Windows platforms normalizePath is a no-op, so
there is no behavioral change outside Windows.

Centralizing normalization in FileTime rather than patching each tool
individually means future tools cannot forget to normalize.

How to reproduce

On Windows, set up a symlinked directory with a file:

mkdir C:\actual
"hello" | Set-Content C:\actual\test.txt
New-Item -ItemType Junction -Path C:\link -Target C:\actual

Then in OpenCode, read and edit the file through the symlink:

  1. Read C:\link\test.txt
  2. Edit C:\link\test.txt

The read tool normalizes the path to C:\actual\test.txt and stores it under
that key. The edit tool passes the original C:\link\test.txt to
FileTime.assert, which does not find it. Result:

You must read file C:\link\test.txt before overwriting it. Use the Read tool first

This also reproduces with different drive-letter casing (c: vs C:) or
forward vs backslash variants of the same path.

How did you verify your code works?

  1. Ran the existing test suites locally on Windows:
    • bun test test/file/time.test.ts (16 pass)
    • bun test test/tool/edit.test.ts test/tool/write.test.ts (40 pass)
  2. Added a Windows-only regression test that calls FileTime.read with one
    path variant and FileTime.assert with another; confirms they resolve to
    the same entry.
  3. Manually verified end-to-end: read a symlinked file, then edit it via the
    edit tool across a Windows symlink boundary. The edit succeeds with this
    patch and fails without it.

Screenshots / recordings

Screenshot 2026-03-30 160124 edit

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Mar 30, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@lars-hagen lars-hagen force-pushed the fix/windows-filetime-path-canonicalization branch from 1ed499b to 060b779 Compare March 30, 2026 23:59
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.

Write tool overwrite guard compares raw path strings without canonicalization (Windows)

1 participant