Skip to content

Write package.json atomically and skip unchanged writes - #54

Merged
jxom merged 1 commit into
mainfrom
fix/atomic-write-package-json
Nov 10, 2026
Merged

jxom merged 1 commit into
mainfrom
fix/atomic-write-package-json

Conversation

@jxom

@jxom jxom commented Nov 10, 2026

Copy link
Copy Markdown
Member

Problem

writePackageJson rewrote package.json with fs.writeFile, which truncates the file before writing. When a package runs zile dev as its postinstall in a pnpm workspace, pnpm concurrently reads that manifest through other projects' workspace links, can see an empty file, and fails with:

[ERR_PNPM_JSON_PARSE] Unexpected end of JSON input while parsing empty string in .../node_modules/<pkg>/package.json

Fix

  • Skip the write when the output matches the cached original manifest (the common zile dev case).
  • Otherwise write to package.json.<pid>.tmp and rename it over package.json, so readers see either the old or the new file, never a truncated one.
  • Patch changeset added.

Validation

  • New tests: an unchanged manifest keeps its inode and mtime; a changed manifest is written in full and no temp file is left behind. The unchanged-manifest test fails without the fix.
  • bun run check, bun run check:types, and bun run test (107 tests) pass.

Generated by Claude Code

`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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T22:57:43.632330Z 2755bff PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/zile@54

commit: 2755bff

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread src/Package.ts
// 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Comment thread src/Package.ts
Comment on lines +855 to +857
const temporary = `${file}.${process.pid}.tmp`
await fs.writeFile(temporary, output, 'utf-8')
await fs.rename(temporary, file)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@jxom
jxom merged commit dcf13cd into main Nov 10, 2026
7 checks passed
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.

2 participants