Repository navigation
Write package.json atomically and skip unchanged writes - #54
Conversation
`fs.writeFile` truncates before writing, so a concurrent reader (e.g. pnpm linking workspace projects during a `zile dev` postinstall) could read an empty manifest and fail with ERR_PNPM_JSON_PARSE. Skip the write when the output is unchanged, and otherwise write a temporary file and rename it over package.json. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D3nmWguLFZjFR69g3R9p54
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2755bff277
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // A rewrite truncates the file before it writes, so a concurrent reader, such as pnpm linking a | ||
| // workspace project that depends on this package, can read an empty manifest. An unchanged | ||
| // manifest is not written, and a changed one replaces the file in one rename. | ||
| if (output === content) return |
There was a problem hiding this comment.
Compare against the current manifest before skipping a write
When this exported API writes the same package more than once in a process, content still contains the original manifest cached by readPackageJson, not the latest file contents. Reading version 1.0.0, writing 1.0.1, and then writing the original object silently leaves 1.0.1 on disk because the final call returns here. Compare against the current on-disk contents, or maintain a separate last-written snapshot, so restoring the original manifest is not mistaken for an unchanged write.
Useful? React with 👍 / 👎.
| const temporary = `${file}.${process.pid}.tmp` | ||
| await fs.writeFile(temporary, output, 'utf-8') | ||
| await fs.rename(temporary, file) |
There was a problem hiding this comment.
Use a unique temporary filename for each write
If callers invoke Package.writePackageJson concurrently for the same directory within one process, every invocation writes to the same temporary file. One invocation renames that file away and the others reject with ENOENT; overlapping writes can also modify the inode after it has become package.json, defeating the atomic-replacement guarantee. Concurrent calls against the changed implementation reproduce these failures. Give each invocation a unique temporary path, as the existing transform and tsconfig temporary files already do.
Useful? React with 👍 / 👎.
Problem
writePackageJsonrewrotepackage.jsonwithfs.writeFile, which truncates the file before writing. When a package runszile devas itspostinstallin a pnpm workspace, pnpm concurrently reads that manifest through other projects' workspace links, can see an empty file, and fails with:Fix
zile devcase).package.json.<pid>.tmpandrenameit overpackage.json, so readers see either the old or the new file, never a truncated one.Validation
bun run check,bun run check:types, andbun run test(107 tests) pass.Generated by Claude Code