Skip to content

[Repo Assist] fix: use correct waitUntil API in onWillSaveTextDocument#131

Draft
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/fix-onsave-waituntil-2026-04-04-c80a8320de98075a
Draft

[Repo Assist] fix: use correct waitUntil API in onWillSaveTextDocument#131
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/fix-onsave-waituntil-2026-04-04-c80a8320de98075a

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 4, 2026

🤖 This is an automated draft PR from Repo Assist, an AI assistant.

Summary

Fixes two bugs in the onWillSaveTextDocument handler by replacing commands.executeCommand("editor.action.formatDocument") with a direct call to phpcbf.format(event.document) that returns TextEdit[].

Problem

The old code:

event.waitUntil(
    commands.executeCommand("editor.action.formatDocument")
);

Has two distinct bugs:

Bug 1 — Formats the wrong document

editor.action.formatDocument always targets the active text editor, not event.document. If a background PHP file is saved (e.g. via auto-save, or in a multi-root workspace where a non-focused document triggers a save), the active editor's document gets formatted instead of the document actually being saved.

Bug 2 — Wrong return type for waitUntil

commands.executeCommand returns Thenable(void), but event.waitUntil accepts Thenable(TextEdit[]) for the atomic-edit form. Passing Thenable(void) means VS Code never applies edits through its internal save pipeline. Instead, the format runs as an untracked side-effect that may race with the save operation rather than being properly sequenced as part of it.

The [VS Code API docs]((code.visualstudio.com/redacted) state:

Calling waitUntil with a Thenableresolvingaheadable(vscode.TextEdit[]) will apply the text edits to the document before it is saved.

Fix

Call phpcbf.format(event.document) directly, convert the result to a TextEdit covering the full document, and pass that to waitUntil. Errors are caught and converted to an empty TextEdit[] (no change), preventing VS Code from blocking the save on a formatter error.

Also reads phpcbf.onsave scoped to event.document.uri so per-folder settings in multi-root workspaces are correctly resolved (same fix as in PR #106).

Relates To

Test Status

All 7 unit tests pass (node --test test/unit.test.js). The full VS Code integration test harness requires a display and cannot run in CI.

Manual verification:

  1. Set phpcbf.onsave: true and editor.formatOnSave: false in a PHP workspace
  2. Save a PHP file with fixable errors — file should be fixed atomically on save
  3. Open two PHP files, keep the second active; save the first via the Explorer context menu — the first file should be formatted, not the second

Overlap note: This PR also includes the per-document onsave reading that PR #106 includes. If #106 merges first, only the waitUntil fix would need to be retained from this PR.

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@346204513ecfa08b81566450d7d599556807389f

Replace commands.executeCommand('editor.action.formatDocument') with a
direct call to phpcbf.format(event.document) returning TextEdit[].

Two bugs in the old approach:
1. editor.action.formatDocument always formats the *active* text editor,
   not event.document.  When a background PHP file is saved (e.g. via
   auto-save or a multi-root workspace scenario), the wrong document
   would be formatted.
2. commands.executeCommand returns Thenable<void>, while waitUntil
   accepts Thenable<TextEdit[]> for atomic-edit application.  Passing
   Thenable<void> means VS Code never applies edits through the save
   pipeline — the format runs as a side-effect that may race with the
   save rather than being part of it.

The fix calls phpcbf.format(event.document) directly, maps the result
to a TextEdit array, and passes that to event.waitUntil — matching the
documented VS Code API contract.

Also reads phpcbf.onsave scoped to event.document.uri so per-folder
settings in multi-root workspaces are correctly resolved.

Relates to #35 (onWillSaveTextDocument timeout errors).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants