Skip to content

feat: format the files of a workspace with dprint's language server - #192

Merged
dsherret merged 141 commits into
mainfrom
feat/workspace-editor-service
Nov 20, 2026
Merged

dsherret merged 141 commits into
mainfrom
feat/workspace-editor-service

Conversation

@dsherret

@dsherret dsherret commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Implements the protocol from dprint/dprint#1408 (released in dprint 0.65.0), whose docs/editor-extension-development.md describes it (the dprint/* methods of dprint lsp).

What this does

When the dprint in use supports the dprint/* methods, the extension formats with dprint's language server instead of starting a dprint editor-service for each config file:

  • One dprint lsp per dprint executable formats the whole workspace, and it finds each file's config file itself. Usually that's one process. There are more when a project uses its own dprint (ex. from its node_modules). The processes are kept across workspace changes (ex. a config file was created) and are only started again for a new executable, a new version, or the "Dprint: Restart" command. The extension sends a document's text only when formatting it, so dprint doesn't need a copy of every open document.
  • Session id: each connection sends its own sessionId. A server that crashed, or that the extension killed, is started again with the same id, so dprint keeps what the user decided for the session (ex. a denied download) without asking again. A server that exits with code 0 after a shutdown ended its session, so the next one starts a new session and the extension forgets which approvals it asked about. The restart command always starts a new session.
  • Approvals: dprint now asks before it downloads from a domain that isn't trusted or runs process plugins that weren't allowed. When a format needs that, the extension shows dprint's message with the decisions dprint offers ("Allow", "Deny" and "Always Deny" for a download, plus "Always Allow" for process plugins), and a "Show Details" button that opens what's approved in a read-only document. Once something is allowed, the document is formatted again. A save doesn't wait on the prompt; the document is formatted and saved again after it's allowed. A format the user didn't run asks only when dprint says to claim the document or dprint is the language's default formatter, and each approval is asked at most once until the user decides. Each process has its own prompts.
  • Status bar and commands:
    • A "dprint" status bar item with a shield icon and "Dprint: Approve Configuration" ask about everything that's pending, including what was denied, starting with the active document's config file.
    • "Dprint: Show Approval Decisions" lists what was decided across the processes, showing an "Always" decision once even when processes list it by different paths. From there the user can forget the decisions of one config file or every decision. "Dprint: Forget Approval Decisions" forgets every decision. Both are sent to every process.
  • Formatter registration: the extension is the formatter only for the files a config file's plugins may format (every file when they format others, ex. associations), and for the languages whose default formatter is dprint. Range formatting, including several ranges for "format modifications", the per-folder useGlobalConfig setting, untitled documents and notebook cells are supported. Config file problems go to the problems panel, plugin downloads show progress in the status bar, and messages are shown the way dprint's show and severity say.
  • Watching: dprint doesn't watch files, so the extension tells it about changes (workspace/didChangeWatchedFiles):
    • config files in the workspace, through the existing workspace watcher;
    • the files a config file was resolved from (watchUris, ex. a file it extends, including for the global config file);
    • config files in the ancestor directories of a workspace folder that has no config file of its own, using the names dprint says it looks up.
    • These use DirWatchers, which watches each file through its directory and never watches a directory that's a symlink or is reached through one (apart from a workspace folder and its ancestors). That way a git pull that swaps in a symlink to a network share isn't followed without the user allowing it. A directory that's replaced or created again is watched again.
    • The home directory and its ancestors aren't watched, because on macOS a watch of one gets every file event below it. They're checked again when the window regains focus and when a document is saved, which finds a config file that was created, deleted or edited there.
  • Failed start and restarts:
    • A server that exits before it answers initialize, or can't be spawned, failed to start. It isn't started again until its settings change or the backend is initialized again. After a crash, the user is shown its stderr.
    • A server that keeps crashing soon after it started is started again after a delay that grows from 1 s to 30 s. A request waiting on that delay can be cancelled.
    • A server that's started again and turns out to be a dprint older than this protocol (ex. after a downgrade) is stopped, and the user is told to update dprint or restart it.
  • Older dprint: when the default dprint doesn't have the methods (or doesn't have dprint lsp at all), the existing backend (schema version 5 of the editor service) is used as before. A config file whose own dprint is too old is formatted with schema version 5 by itself, alongside the language server for the rest. When the method versions don't match, the extension shows dprint's message about what to update instead of falling back.

Other changes

  • Config files in a .config directory are supported (feat: support config file in a .config directory #191).
  • On Windows, a bare dprint is only looked up on the PATH, and the process starts with NoDefaultCurrentDirectoryInExePath, so a repository can't provide the dprint that runs. The process tree is killed together, so a dprint started through npm's dprint.cmd doesn't keep running.
  • The server's workspace folders are kept in the editor's order, so an untitled document is formatted in the same first folder by both.
  • New dependency: vscode-jsonrpc for the connection.

User-visible behavior changes

  • Formatting with a config file that needs a download from an untrusted domain, or uses process plugins, asks the user first instead of running right away.
  • New commands: "Dprint: Approve Configuration", "Dprint: Show Approval Decisions" and "Dprint: Forget Approval Decisions", plus a status bar item while something needs approval.
  • Fewer dprint processes for multi-folder workspaces.
  • A config file is opened from a notification only if dprint's latest result for it doesn't say it's on another machine.

Known limitations

  • Before a dprint process starts, the extension finds config files with its built-in names, and so does the search of a workspace folder's ancestor directories. A name a newer dprint adds is only used once that process is running (ex. for the ancestor watchers).
  • vscode.workspace.findFiles follows symlinks to directories when it searches the workspace, which VS Code's own search does as well. The ancestor search, the watchers and the checks never follow one.
  • A config file created in the home directory or above is noticed on the next focus or save, not right away.
  • The extension doesn't map the paths of a directory it knows by several (ex. two symlinks to it), so it refreshes on every dprint/configChanged instead of only for the documents of the config file that changed.

Testing

  • Unit tests (npm run test:unit, 476 tests) cover the connection (restarts, sessions, failed starts, restart delay, cancellation, workspace folder order), approvals and decisions, DirWatchers (symlinks, replaced and created directories, case-insensitive names), the config path rules (ancestors, .config directories, network share roots, the home directory), the server support checks, and format results.
  • The integration tests (npm test) were run with dprint 0.64.0 on the PATH (schema version 5) and with a build of feat: format the files of a workspace for editor extensions with dprint lsp dprint#1408 on the PATH. One run used an empty cache directory, which exercises the new test for approving a download from an untrusted domain.

…supports it

One dprint process formats the files of the whole workspace for each dprint
executable in use. It falls back to the existing backend in the legacy folder
when a dprint executable doesn't support schema version 6.
- show dprint's message for why a document wasn't formatted
- get the config file schema from dprint
- send the ensure stable format and verbose settings instead of restarting
- let dprint show the error when a config file fails to resolve
- Keep formatting with the current state while initializing again instead
  of unregistering the formatter until the new state is ready.
- Send whether to show the no config file notification with each format,
  which the global config command turns off.
- Don't start the legacy backend after being disposed and have queued
  initializations share the next one.
- Accept range formats whose edits are just outside the range when the text
  outside the range is unchanged.
- Reject a request whose response is malformed and don't respond to a show
  message request of a process that exited.
- Don't share a process between folders for a relative dprint.path that
  resolves from the folder.
- Don't watch an ancestor config file twice.
- Read the settings of a format at the file it's formatted as.
- keep a replaced state's editor services until their formats finish and
  the user answered their messages
- only notify about a file without a config file once per session
- fall back to schema version 5 when the default dprint is too old
- only send ensureStableFormat when it's on so DPRINT_EDITOR_STABLE_FORMAT
  applies otherwise
- update the user data files to format when useGlobalConfig changes
- refresh the state being initialized when a config file changes
- log why the global config command failed before dprint is initialized
- log the config diagnostics of a format
- test the backend switching, the helpers and more of the connection
- identify documents by uri and send the format's data as JSON, with the
  language of an untitled document or notebook cell so dprint picks the
  file name
- let dprint expand a range to whole lines and say when formatting it
  changed the text outside of it
- write dprint's log messages to the output at their level
- show dprint's progress in the status bar
- show the problems with config files in the problems panel
- refresh the formatting registration when dprint says a config changed
- Keep the editor services when the workspace changes (ex. a config file is created or the workspace folders change), so what the user chose for the session is kept. The restart command resets them instead of starting dprint again, unless the executable, its version, or dprint's environment variables changed.
- Format a config file whose dprint doesn't support schema version 6 with schema version 5 on its own instead of the whole workspace.
- Only be the formatter of the files a config file's plugins may format, or every file when that isn't known (ex. its process plugins weren't allowed or it has associations).
- Format several ranges in one request (ex. format modifications).
- Send the settings of each workspace folder.
- Add a command to ask to allow the process plugins of a config file again.
- Only show a plugin's error and not a syntax error.
- Use the file info to show the global config commands.
- Don't run dprint again to check if it's installed when it doesn't support schema version 6.
dprint only downloads plugins when the editor asks for it with schema version 6 of the editor service. The extension is only used in trusted workspaces, so it installs a config file's Wasm plugins once it finds the config file, and its process plugins when a document is formatted because installing them asks the user to allow them. A format waits for the install and then formats the document. The command to allow process plugins becomes a command to install the plugins, which also asks to allow them.
… to download from domains that aren't trusted

- The restart command starts new editor services instead of resetting the
  running ones, which forgets what the user chose for the session.
- Handle the `downloadRequired` reason that replaced `pluginsNotInstalled`.
  dprint downloads from trusted domains itself, so the extension no longer
  installs the Wasm plugins when it finds a config file and only installs when
  a format needs a download from a domain that isn't trusted.
dprint dropped schema version 6 of the editor service in favor of the
`dprint/*` methods of `dprint lsp`, so the workspace backend now uses those
over standard LSP framing with vscode-jsonrpc. A dprint whose language server
doesn't have the `dprint` experimental capability falls back to schema
version 5, for the whole workspace or per config file as before.

- Approvals are asked about when formatting (including on save) with dprint's
  message, and the "Dprint: Approve Configuration" command asks about every
  pending approval, including denied ones. It replaces "Dprint: Install
  Plugins".
- The extension shows the no configuration file notification itself, where
  dismissing it is stored in the global or workspace state.
- `useGlobalConfig` is only read from the user's settings and is sent with
  each request.
- The config file diagnostics, progress, log messages, and shown messages are
  the standard LSP messages.
@dsherret dsherret changed the title feat: format with schema version 6 of the editor service feat: format the files of a workspace with dprint's language server Oct 29, 2026
The server doesn't watch the file system, so the extension sends
`workspace/didChangeWatchedFiles` for a config file it sees change, which has
the server publish its problems right away instead of on the next format.
…anguage server

Sends the version of the dprint methods, uses the approvalRequired reason, shows what process plugins run with, formats again when an approval changed, gets the plugins again only when dprint says a config file changed, and formats the files of a config file in a .config directory.
…nguage server

Asks about each pending approval with the decisions dprint says it has, uses the status of fileInfo and configInfo, sends when the global config file is used for each document, registers the file patterns dprint provides, and watches the files a config file was resolved from.
…findings

Sends the versions of the dprint methods the extension supports, asks about what's pending for the config file when an approval changed, offers only the decisions dprint says an approval has, uses dprint's global config setting when the folder's is off, shows each config file's error once per config file, gets the plugins again when the server restarts, and doesn't create file watchers for a stopped state.
…erver

Shows the labels of an approval's decisions, asks for the info of a config file by its uri, gets only the schema again when only it changed and everything when every config file may have, uses the file patterns dprint provides, and handles the new error code for an approval that changed.
- show the messages of the results like their `show` says
- use whether a decision allows what's approved and the status of the config file after approving
- recognize an approval that changed by its error data
- key the shown config file errors by canonical path like the paths dprint provides
…approving

- register no patterns for a config file whose status says not to claim its files
- throw the error of a denial that failed and of an approval that's still pending
- tell the user whether dprint or the extension needs to be updated when the versions don't match
- show messages by their severity and key the errors shown once by the config file of the result
- only ask about approvals when dprint says to claim the document, the user ran a command, or
  dprint is the default formatter of its language
- get what's pending for one config file after an approve fails, and deny what replaced a denied
  approval
- say when the config file of an approval is outside the workspace
- add the "Dprint: Forget Approval Decisions" command
…on asking

- forget which approvals were asked about once dprint forgot the decisions
- ask about and show what's pending for every config file dprint used, including in a
  status bar item that asks when it's clicked
- tell the user when dprint or the extension needs to be updated
- format and save a document again once what it needed is approved instead of making
  the save wait on the prompt
- show a failure like its show policy says and use the folder's default formatter setting
…of dprint

- show dprint's message when it doesn't support the extension's versions
- use the nested format result, show notices, and offer the actions of messages
- show what process plugins that weren't set up run with
- send the session's decisions again to a dprint process that was started again, and only
  deny what replaced a denied approval when it's some of what the user saw
- add the "Dprint: Show Approval Decisions" command
- don't start a dprint that isn't on the path from the workspace folder on Windows
- show server text in notifications without Markdown links
- send a session decision again when it's asked about, instead of on restart
- resolve a bare executable name on the absolute path directories on every platform
- don't have cmd.exe look for commands in the workspace folder
- don't fall back to schema version 5 after a version mismatch
- show the descriptions of the decisions and their kinds
- show what process plugins run with in a document instead of the cut off notification
- always show the config file when asking
- handle what needs to be approved for a config file in one shape and show notices of approving and forgetting
- use the new decision, action, and config change names
- handle result kinds that are newer than the extension with their claim, approvals, and actions
- make the types match the spec
- show the config file first in an approval prompt so a long message can't push it out
- send the session's decisions again once a restarted dprint is refreshed
- ask about the other approvals when one was denied, and send a decision again when its approval is pending again after it changed
- don't fall back to schema version 5 when any executable has a version mismatch
- show what process plugins run with in a read-only document
- show a formatted document's notice for a command, only open file actions, and show server text without links or icons
- keep quoted PATH directories
…ancestor directories, and sync the doc copies with the round 21 spec
… watched ancestor, and test combining what the connections say about a config file
…ame approval at the same time, and say where the extension's config file names are used
…d may be another approval in another version
…er exits with code 0, which was stopped with shutdown
…onfig file, and find the config files again when dprint says an unknown one was created
…tching them, and keep the global configs for the session

- don't watch the home directory or its ancestors for ancestor config files (a watcher of one on macOS gets every event below it); look in the ancestor directories again when the window is focused or a document is saved instead
- move choosing the ancestor directories to watch into a tested pure function (`getWorkspaceFolderAncestorWatchDirs`), using the default dprint process's names for a folder without a config file
- only find the config files again for a `configChanged` `created` of an unknown config file outside the workspace folders, which the workspace watcher already handles
- tell dprint about a config file that was created or deleted in an ancestor directory, not only changed
- note the global config file from `fileInfo` results too, and keep one per dprint process for the session so a reinitialization keeps watching its `watchUris`
- simplify finding the approvals of a connection and `isKnownConfigFile`
- test that the session isn't ended by a death by a signal or by an exit after the connection stopped the server
- reword stale comments
…nd sync the configFileGlobs doc

The server appends the workspace folders that are added, so the ones from the first that's out of order are removed and added again, which keeps the first workspace folder (where an untitled document is formatted) the same in the extension and dprint.
… add combining the decisions of the processes by config file

combineDecisions keeps one of each "Always" decision, which another process may list by another path of its config file, and getForgettableConfigFiles gets the config files whose decisions can be forgotten.
…ig commands again on each configChanged

- the decisions quick pick offers to forget the decisions of each config file, which is sent to each dprint process, along with forgetting every decision
- every dprint/configChanged (including one for a config file the extension doesn't know) checks again whether the active document is formatted with the global config file, because the extension doesn't map the paths of a directory it knows by several
… decisions and the parent watcher exceptions
…dprint when a config file there changed

- the check on focus and save only looks in the home directory and its ancestors, which aren't watched, for a folder whose config file isn't below the home directory, sharing what it looked at across the folders, and not while a state is being initialized
- tell dprint when a config file that's used from the home directory or one of its ancestors changed, by comparing its version with the last check
- only check again when the window gets the focus back, not when only whether it's active changed
- rename the option to onConfigFilesMayNeedFindingAgain, and fix the docs of the watched ancestor directories
…gs change

A server that exits before it responds to `initialize` (or can't be spawned) failed to start, which a restart doesn't fix, so its requests are rejected with what it wrote to stderr, which is shown to the user when it was started again after a crash. It's started again once the workspace is initialized again (ex. the `dprint.path` setting or a config file was created), a config file changed, or the settings that are sent to dprint changed, and the restart command starts new servers.
…exiting soon after it started

- a server that exits (or is killed) within 30 seconds of starting delays the next start by a second, doubling up to 30 seconds, and one that ran longer resets it
- only a settings change or a reinitialization lets a server that failed to start be started again (`allowStart`), not a change of a config file
- the failed-start message says it's not started again before what the server wrote, and a placeholder failure doesn't replace a newer one
- share the regained-focus check (`onDidRegainWindowFocus`) and the version comparison (`getChangedVersions`), and fix stale comments
…arted again for an older dprint

- a request that waits on the restart delay or a killed server is cancelled with its token and rejected once the connection is disposed or closing, and `allowStart` clears the delay and the count of quick exits so it's started right away
- the restart delay is logged, and the killed-server wait and the restart delay share one wait
- a server that was started again that's an older dprint without the dprint methods is stopped, and one without the `lsp` subcommand says to update dprint or restart it to use its older editor service
- the failed-start message names what failed before saying it's not started again
@dsherret
dsherret merged commit b2394f9 into main Nov 20, 2026
3 checks passed
@dsherret dsherret mentioned this pull request Nov 20, 2026
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.

1 participant