Skip to content

fix(cli): a failed write reports ERR_WRITE_FAILED, not a raw Node errno - #6621

Merged
josephfarina merged 6 commits into
mainfrom
fix/cli-write-failed-error-code
Oct 5, 2026
Merged

josephfarina merged 6 commits into
mainfrom
fix/cli-write-failed-error-code

Conversation

@josephfarina

@josephfarina josephfarina commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

User impact

A write the filesystem refuses comes back as a raw Node errno error with the
wrong error code and an absolute host path in the message:

$ astryx --json template ai-chat readonly/x.tsx
{"error":"EACCES: permission denied, open '/home/you/project/readonly/x.tsx'",
 "code":"ERR_UNKNOWN"}

swizzle --output <unwritable> returned the mkdir equivalent. A consumer
branching on code — which is the field the contract says to branch on — sees
ERR_UNKNOWN and cannot tell a permissions problem from a bug in the CLI. And
the absolute path lands wherever the envelope lands: a CI log, an agent
transcript, a pasted bug report.

Problem and solution fit

Why does it happen? Nothing wraps the fs call, so Node's error reaches the
envelope untranslated and picks up the default ERR_UNKNOWN.

Why does that harm the task? "Unknown" tells the user to report a bug when the
answer is chmod. Retry logic and agents branch on the code and get nothing
useful.

Why does that matter? ERR_WRITE_FAILED is already in the frozen registry for
exactly this case
and already used by integration add. Two write leaves were
simply missing from it.

Expected behavior and authority

ERR_WRITE_FAILED from foundation/response/error-codes.mjs, already the code
api/integration/add-contribution.mjs throws for a failed write. Messages name
their target relative to the project (copy.mjs already does this for
ERR_FILE_EXISTS: `Refusing to overwrite existing file ${rel}`).

Smallest restoration

Nothing is partially written in either case, so only the code and the message
were wrong. One shared writeFailed(target, cwd, cause) in api/error.mjs
builds the error; the two write leaves wrap their fs calls with it.

  • api/template/copy/copy.mjs — the source is now read before the mkdir, so
    a failure below cannot leave a created directory behind.
  • api/swizzle/copy/copy.mjs — the mkdir and the per-file write.

The errno is kept (EACCES is the part that tells you what to fix); the path is
the project-relative one. ERR_WRITE_FAILED is added to both FunctionDocs'
throws.

Evidence

Reproduction before the change:

$ astryx --json template ai-chat readonly/x.tsx
{"error":"EACCES: permission denied, open '/tmp/…/readonly/x.tsx'","code":"ERR_UNKNOWN"}
$ astryx --json swizzle Button --output readonly/sub
{"error":"EACCES: permission denied, mkdir '/tmp/…/readonly/sub'","code":"ERR_UNKNOWN"}

Result after the change:

$ astryx --json template ai-chat readonly/x.tsx
{"error":"Could not write readonly/x.tsx: EACCES (no permission). Nothing was written.",
 "code":"ERR_WRITE_FAILED"}                                            # exit 1
$ astryx --json swizzle Button --output readonly/sub
{"error":"Could not write readonly/sub: EACCES (no permission). Nothing was written.",
 "code":"ERR_WRITE_FAILED"}

Representative unchanged path: template ai-chat src/page.tsx into a writable
directory still returns template.copy and exit 0 with the file on disk;
ERR_FILE_EXISTS, ERR_PATH_TRAVERSAL and the rest are untouched.

Regression test or other mutation-sensitive proof:
clients/cli/commands/write-failure.test.mjs, 3 tests. Verified in both
directions — with api/error.mjs and the two copy leaves reverted to main and
only the test applied, 2 fail (expected 'ERR_UNKNOWN' to be 'ERR_WRITE_FAILED'); with the fix all 3 pass. The tests also assert the
absolute temp-dir path does not appear in the message, and that nothing was
created. They skip under uid 0, where chmod 500 would not make the directory
unwritable and the assertion would be false rather than failing honestly.

Full packages/cli node suite green, plus check:repo, eslint,
readme:check and the CLI typechecks in an unbuilt tree.

Scope

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.

No response field changes. ERR_WRITE_FAILED is an existing registry entry, now
reachable from two more places and documented in both FunctionDocs.

Review fix (head b16941f, on main 505e3d8)

The review found that a swizzle failing part-way left the files it had already copied on disk, while the message said "Nothing was written." That also contradicts the registry's meaning for this code ("failed (and was rolled back)"). A swizzle that fails on a later file now undoes the copy before reporting: it deletes the files it created and puts back the bytes of any it replaced. writeFailed takes the files an undo could not restore and names them, instead of claiming nothing was written. The helper's comment and the changeset now say this.

New test in write-failure.test.mjs: the first output file exists, the middle ones do not, and the last one is read-only. An --overwrite swizzle then fails on the last file, puts the first file back, removes the new ones, and reports "Nothing was written." With the undo removed, the test fails (expected '// Copyright…' to be 'before first\n').

Evidence on that tree: the full CLI node suite (350 files, 5,555 tests), check:cli-structure, check:changesets, readme:check, typecheck:json-api, typecheck:authoring, cli-api-types-verify, eslint, and prettier all pass.

Review fix 2 (head 7d04911, on main 5bb03cf)

The review found one more case. The rollback snapshot read each existing destination before the guarded write. A destination that cannot be read back (no permission, or a directory with a copied file's name) threw a raw errno as ERR_UNKNOWN, with the absolute path in the unreadable case, and left the earlier files overwritten. The snapshot read now sits inside the guarded block. A throw there undoes the entries already written and reports ERR_WRITE_FAILED. The destination that failed is not in the undo list, because nothing was written to it.

New test: a directory sits where the last file goes. The --overwrite swizzle reports ERR_WRITE_FAILED naming that file, puts the first file back, removes the new ones, and leaves the directory. With the snapshot back outside the guard, it fails (expected 'ERR_UNKNOWN' to be 'ERR_WRITE_FAILED').

Evidence: on main 9143f03 plus this fix, the full CLI node suite (354 files, 5,576 tests), check:cli-structure, check:changesets, readme:check, both typechecks, cli-api-types-verify, eslint and prettier pass. The pushed head also merges main 5bb03cf (#6615), and the fix commit is byte-identical to the tested fix.

template into an unwritable directory returned {"error": "EACCES: permission
denied, open '/abs/host/path/readonly/x.tsx'", "code": "ERR_UNKNOWN"}, and
swizzle returned the mkdir equivalent. ERR_WRITE_FAILED is already in the frozen
registry for exactly this, and every other Astryx message names its target
relative to the project. Nothing is partially written, so only the code and the
message were wrong.
@vercel

vercel Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Oct 2, 2026 1:04am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 24, 2026
@astracat-bot

astracat-bot Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Oct 2, 2026, 1:35 AM UTC

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR

🧪 Sandbox Preview

View Sandbox for this PR

No new or modified components detected.

Bundle Size Summary

No component packages changed.

Accessibility Audit

Status: No accessibility violations detected.

Visual Regression

15 of 420 shot(s) changed.

A change here is a question, not a failure: check whether the after is the
picture you intended. Record that review on the PR. Baseline maintenance is an
explicit dispatch of CI; this report never rewrites the baseline or adds a release gate.

component story theme mode pixels
Chat Full AI Chat probe light 155,733
Chat Full AI Chat probe dark 155,005
Chat Default neutral dark 33,205
Chat Default neutral light 32,876
Chat Empty State probe light 23,531
Chat Empty State probe dark 22,622
ChatAutoScroll Scroll Behavior Comparison neutral dark 15,378
ChatAutoScroll Scroll Behavior Comparison neutral light 14,107
Chat With Attachments probe light 13,229
Chat With Attachments probe dark 11,065
SideNav Resizable in AppShell probe dark 3,208
Markdown Default probe dark 897
Markdown Default probe light 897
Markdown Default neutral dark 882
Markdown Default neutral light 858

Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@astracat-bot astracat-bot Bot 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.

Thanks — the core fix is right: a refused write now reports ERR_WRITE_FAILED with the target named relative to the project, instead of a raw Node errno as ERR_UNKNOWN with an absolute host path. The regression test covers both commands plus the unchanged happy path, and the docs and changeset line up.

One change is requested before approval. When swizzle fails part-way through copying, files it already wrote stay on disk, but the new message always ends "Nothing was written." — which also contradicts the registry's own description of this code ("and was rolled back"). Someone told nothing was written will retry without --overwrite and immediately hit the overwrite refusal, with a partial component left behind. Please make that one report truthful (name the partial state, or roll back like the theme writers do) and adjust the matching sentence in the helper comment and changeset. The mkdir failures and the single-file template case are fine as written.

Everything else looks good to merge once that wording is fixed.

[Automated review]

Review fix. ERR_WRITE_FAILED means the write failed and was rolled back, but a
swizzle that failed on a later file left the earlier ones on disk while the
message said nothing was written. It now deletes the files it created and puts
back the bytes of any it replaced before reporting, and writeFailed names any
file an undo could not restore instead of claiming nothing was written. New
test: with the last file read-only, an --overwrite swizzle restores the first
file, removes the new ones, and reports 'Nothing was written.'; it fails
without the undo.

@astracat-bot astracat-bot Bot 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.

Thanks — the rollback added in this revision fixes the part-way swizzle case from the last review: the new test passes (4/4 locally), replaced files are restored, and created files are removed before the error is reported.

One related case still needs a fix before approval. The rollback snapshots each existing destination with fs.readFileSync(dest) before the try that guards the write. If that destination cannot be read back — an unreadable file (mode 000) or a directory with a copied file's name — the snapshot read itself throws, so the raw errno escapes as ERR_UNKNOWN (with the absolute host path in the unreadable case) and files copied earlier in the run stay overwritten instead of being restored. Please capture the snapshot inside the protected block so any throw there first undoes the entries already written and then reports ERR_WRITE_FAILED, and add a regression case for an unreadable or directory destination.

Everything else looks good to merge once that is fixed.

[Automated review]

Review fix. The rollback read each existing destination before the guarded
write, so a destination that cannot be read back (no permission, or a directory
with a copied file's name) threw a raw errno as ERR_UNKNOWN and left the earlier
files overwritten. The snapshot read now sits inside the guarded block: a throw
there undoes the entries already written and reports ERR_WRITE_FAILED. New test:
a directory where the last file goes; it fails with ERR_UNKNOWN when the
snapshot is outside the guard.

@astracat-bot astracat-bot Bot 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.

Thanks — this revision closes the last case from the previous review. The rollback snapshot read is now inside the guarded block, so a destination that cannot be read back (an unreadable file, or a directory with a copied file's name) undoes the files already copied and reports ERR_WRITE_FAILED with a project-relative path, instead of escaping as ERR_UNKNOWN.

I re-verified at this revision: the write-failure suite passes 5/5 (including the new directory-destination case), an added check with a mode-000 destination file also reports ERR_WRITE_FAILED and restores the earlier file, and the adjacent envelope/exit-code/error-code suites pass. The core fix — refused writes reporting the existing ERR_WRITE_FAILED code without leaking an absolute host path, with part-way swizzle copies rolled back — looks good to merge.

[Automated review]

@josephfarina
josephfarina merged commit 31feb21 into main Oct 5, 2026
41 checks passed
josephfarina added a commit that referenced this pull request Oct 5, 2026
Mechanical refresh. #6621 (merged as 31feb21) moved template copy's source read
before the write and wrapped the mkdir and write in ERR_WRITE_FAILED; this PR
changed that read to replaceDemoMedia(). The resolution keeps both: the
replaceDemoMedia() read before the guarded mkdir+write. #6618 (merged as
6e56409) changed the generated README tables, so they are regenerated.
@github-actions
github-actions Bot deleted the fix/cli-write-failed-error-code branch October 6, 2026 06:25

This branch was successfully deployed

1 active deployment
Preview — 7d04911e Deployed Oct 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant