Skip to content

Fix io.open w+ failing on a file that does not exist - #365

Merged
akeit0 merged 1 commit into
nuskey8:mainfrom
billythekidz:fix/io-open-w-plus-new-file
Oct 23, 2026
Merged

akeit0 merged 1 commit into
nuskey8:mainfrom
billythekidz:fix/io-open-w-plus-new-file

Conversation

@billythekidz

Copy link
Copy Markdown
Contributor

io.open(path, "w+") / "w+b" threw when the file did not exist. LuaFileOpenMode.WriteUpdate was mapped to FileMode.Truncate, which requires an existing file. Lua's w+ creates the file if needed and truncates it otherwise, so it is now mapped to FileMode.Create (still FileAccess.ReadWrite).

Verification

  • New test OpenWritePlus_CreatesMissingFileAndTruncatesExisting (create, write, seek, read back, reopen and check it is truncated) fails on main and passes with the change.
  • dotnet test tests/Lua.Tests/Lua.Tests.csproj -c Release --filter "TestCategory!=ExpectedFailure": 611 passed, 0 failed (610 on main + 1 new).
  • dotnet csharpier check . is clean.
  • Run on macOS (arm64), .NET 8 runtime only.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The narrow mapping change implements the required semantics, and the regression test covers creation and truncation with no identified blocking issues.

No open issues 🎉

What changed in this PR

Fixes io.open write-update mode so it creates missing files and truncates existing files while preserving read/write access.

Changes:

  • Maps LuaFileOpenMode.WriteUpdate to FileMode.Create instead of FileMode.Truncate.
  • Adds a regression test covering creation, writing, seeking, reading, and truncation on reopening.
File Description
tests/​Lua.Tests/​IOTests.cs Adds regression coverage for w+ file behavior.
src/​Lua/​IO/​FileSystem.cs Corrects the write-update file mode mapping.

Review effort: Balanced

@akeit0

akeit0 commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Thank you for the w+ creation fix and the regression test! Your original commit has been merged through #368 with its authorship preserved.

The broader seek/write checks also uncovered an adjacent existing issue: w+ was classified as append mode, so writes after a seek went to the end of the file. We fixed that in a follow-up and expanded coverage for both w+ and w+b, while checking that a+ and a+b still append after a seek. The combined changes passed local tests and Linux CI.

@akeit0 akeit0 mentioned this pull request Oct 25, 2026
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.

3 participants