fix: write OpenAPI YAML atomically - #22
Merged
Merged
Conversation
On disk-full or permission-denied partway through the write, fs::write leaves the target file truncated. Switch to NamedTempFile::new_in() + write_all() + persist() so that the original file is never touched until the full content is safely on disk.
Integration tests covering: successful write, overwrite, parent dir creation, nonexistent parent error, and a Linux-only test that makes the target directory read-only to confirm the original file survives.
Windows interprets paths without a drive letter as relative to the CWD, so /nonexistent/dir/spec.yaml would succeed after create_dir_all and invert the assertion. The test semantic is Unix-specific.
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
Replace
fs::writeinwrite_yamlwith an atomic temp-file + rename pattern usingtempfile::NamedTempFile. On disk-full or permission-denied errors, the target file is left unchanged instead of being truncated to a broken partial write.Type of Change
Checklist
cargo fmt --allcleancargo clippy --all-targets --all-features -- -D warningscleancargo testpasses locallyCHANGELOG.mdunder[Unreleased](if user-facing)feat:,fix:,docs:, etc.)Testing
cargo test --lib— 217 unit tests pass (including existingwrite_yamltests)cargo test --test output_atomic— 5 integration tests:successful_write_creates_file— basic happy pathoverwrite_existing_file— overwrites correctlycreates_parent_directories— nested dir creationpartial_write_preserves_target(Linux-only) — makes dir read-only, confirms original file survives failed writewrite_to_nonexistent_parent_fails_gracefully— error on invalid pathcargo publish --dry-runpassesRelated Issues
None