Skip to content

build::write_output still uses truncate-then-write (non-atomic) for compiled artifacts #227

Description

@dean0x

Summary

build::write_output in crates/mds-cli/src/build.rs (~line 535) uses std::fs::write(&path, compiled):

std::fs::write(&path, compiled)
    .map_err(|e| miette::miette!("cannot write {}: {e}", path.display()))?;

std::fs::write is a truncate-then-write operation: it opens the file with O_TRUNC, then writes. If the process is killed, crashes, or encounters a write error mid-way, the output file is left truncated (empty or partially written). This is the same data-loss pattern that was fixed for mds fmt via atomic_write_file in PR #196.

Impact

For mds build output artifacts, a crash or OOM during a large compile leaves the output file empty. In CI pipelines that cache output artifacts, this can result in a corrupt cached file being used for subsequent builds.

Proposed fix

Route write_output through the same atomic_write_file helper introduced for mds fmt in PR #196 (crates/mds-cli/src/output.rs). This writes to a temp file in the same directory and renames atomically, guaranteeing the output is either the old content or the new content — never a partial write.

Note: write_output also writes stdout (None path arm), which is correct as-is (streaming; no temp-file needed).

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions