Repository navigation
feat: format the files of a workspace for editor extensions with dprint lsp - #1408
Merged
Merged
Conversation
…hema version 6 of the editor service Not choosing an action leaves running the process plugins undecided instead of denying them for the session.
A process plugin is now allowed with its own configuration in the configuration file, so changing the configuration of another plugin or the global configuration doesn't ask again. A plugin that isn't set up yet is allowed to be set up first because its configuration isn't known until then, and it's allowed with its configuration once it is. Running `dprint fmt` or `dprint config install` with a configuration file allows its process plugins in editors because that's the same as choosing to run them, which replaces `dprint config editor-approve`. With schema version 6 of the editor service, the install plugins message asks the user to allow the process plugins it installs, which replaces the message to ask again, and a format only asks when the plugins are installed.
…en running dprint check
Denying a process plugin that isn't set up denies it with any configuration because its configuration isn't known until then, so neither formatting nor installing asks about it again, and `Always Deny` stores that.
- The environment checks the download policy before each request, including each redirect, so a plugin's config schema url, a redirect, an npm tarball, a process plugin's archive, or a registry from an `.npmrc` can't download from a domain that isn't trusted without the user allowing it. - Reading a file on another machine (a UNC path or a `file://` url with a host on Windows) needs to be allowed like a download, unless the config file is on the same network share. - Allowing a download only allows what the user was shown, and what that leads to (ex. a config file that an allowed one extends) is asked about too. - Show the url instead of the error message when asking to download a config file that a config file extends.
…language server The editor service's schema version 6 is replaced by `dprint/*` methods of the language server, which an editor extension uses by providing the `dprint` experimental capability. They're only sent a document's text when formatting it, so the server doesn't sync the documents. One implementation formats for both language clients and editor extensions. - `dprint/format`, `dprint/fileInfo`, `dprint/configInfo`, `dprint/configSchema`, `dprint/approve`, and `dprint/reset`, along with the `dprint/configChanged` notification. - What needs to be approved (downloads from domains that aren't trusted and process plugins) is in the results as data, which an editor extension asks the user about and approves with `dprint/approve`. The server asks a language client's user itself. - Configuration file problems are published as diagnostics, progress uses `$/progress`, and what's logged uses `window/logMessage` for an editor extension. - `dprint/configInfo` has `formatsOtherFiles` instead of the includes, excludes, associations, and shebangs, and its plugins are `null` when they aren't known. - Remove `dprint editor-service --info` and `--schema-version`.
- Editors ask to allow process plugins unless `editor.allowProcessPlugins` is `true` in the settings file. - The process plugin decisions to always use are in a versioned `editor-approvals.json` file next to the settings file instead of the cache directory, so clearing the cache doesn't forget what was denied. Nothing is allowed and the file isn't written over when it can't be read, and the oldest decisions that allowed a plugin are forgotten first. - The `editor.trustedDomains` setting adds domains to download from without asking, which replaces the `DPRINT_TRUSTED_DOMAINS` environment variable because what an editor is started with may come from the project.
The server doesn't watch the file system, so the editor sends `workspace/didChangeWatchedFiles` for a config file, which has the server publish its diagnostics and send `dprint/configChanged`. The server registers it for a language client that supports that.
…another machine Every other kind of Windows prefix can reach another machine (ex. `\.\UNC\server\share`), a home directory path is checked too, and a path in `/net` is on another machine on macOS.
- Handle up to 64 requests and notifications at the same time instead of 4, so a request waiting on a config file's plugins being downloaded doesn't hold up the others (ex. a format on save for another config file). - `workspace/didChangeWatchedFiles` doesn't wait on handling the changes, which are handled together after a short delay. Only a config file that was resolved before is resolved again, an editor extension is told about the others with `dprint/configChanged`, and config files in `node_modules` are ignored. - Changing a config file only shuts down the plugins that no config file uses anymore instead of every plugin.
…equest failed Approving resolves the config file again and returns what's still needed, including a denied approval, so null only means it's ready. An approval that isn't pending anymore is a ContentModified error and a document that can't be formatted is InvalidParams. The approvalRequired reason replaces downloadRequired and processPluginsNotAllowed, and an approval says its config file and the configuration of the process plugins that run.
…e user saw and deny with any configuration A grant to set up a process plugin is for the trusted configuration of the config file, is only used for a plugin that isn't set up, and is consumed by the approval. Denying a process plugin that's set up denies it with any configuration so that changing it doesn't ask again.
…a config file needs approval The editor knows what a config file formats while the user needs to approve its plugins, including in a new session, and formatsOtherFiles comes from the config file instead of the resolved plugins.
… the results The server only shows them to a language client's user because an editor extension knows whether the user made the request.
Approvals are a list, each item is an object with what's known about it, and an approval lists the decisions it accepts. fileInfo and configInfo have a status like a format result, globalConfig says when the global config file is used for a document, a config file says its kind, and dprint/reset takes optional params.
configInfo has globs of the files the plugins may format, the files a config file was resolved from, which the editor watches, and more about each plugin. A document that can't be formatted is a reason it isn't formatted, a stale approval's error has what needs to be approved now, an approval says what was decided before, dprint/pendingApprovals lists the approvals, configSchema says whether it has each plugin's schema, the requests report progress on their token, fileInfo takes the text, null resets a setting, and configChanged says why.
…findings The editor extension sends the versions it supports and the server responds with the one it uses, or handles it like a language client when it doesn't support any. A config file that fails to resolve keeps what it was resolved from watched and tells the editor once it resolves again (ex. after what it extends was fixed or allowed), associations are made relative to the config file's directory in filePatterns, a deleted or changed config file updates what needs to be approved, a request reports the total progress of what's being done including what started before it, and the config schema is only said to have changed once per config file when a plugin's schema downloaded.
…its plugin file before it's allowed
…ing it needs to be allowed
…n environment variables in clear-cache
…obal config directory, or a directory above them
…, the global config directory, or a directory above them" This reverts commit f05f3f2.
…ne instead of to download it
… download from it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #1400. This also includes the commits of #1401, which GitHub shows as merged because they were added to this branch.
This lets an editor extension (dprint-vscode and dprint-intellij) format a whole workspace with one
dprint lspprocess, which finds the config file of each file itself. Editor extensions usedprint/*methods of the language server through an off-the-shelf JSON-RPC library, so there's one server implementation. Because opening a project isn't the same as choosing to run dprint in it, the server only downloads from untrusted domains, reads files on another machine, or runs process plugins after the user approves it.The full protocol is in
docs/editor-extension-development.md. This description summarizes it and lists what changes for users.The
dprint/*methodsdprintexperimental capability with the method versions it supports ({ "versions": [1] }), an optionalsessionId, and opt-infeatures.versionit uses;featuresit uses of the ones the client sent:fileUriformats a document as another file, ex. agit:document;watchFileshas the server register what it needs watched, for extensions built on a generic language client library;files);configFileNames,dotConfigFileNames,configFileGlobs).versionsand amessagesaying what to update. It advertises nothing else, and the methods fail with-32601. A dprint older than 0.65 has no capability, so the extension falls back to editor service schema 5.dprint/format,dprint/fileInfoanddprint/configInforeturn a result with the config file that was used and a status.dprint/configSchema.dprint/approve,dprint/pendingApprovals,dprint/decisionsanddprint/forgetDecisions.dprint/reset.dprint/configChangednotification.claim), when and how to show its message (show,severity), and what the user can do (actions). Older extensions then handle reasons added later.dprint/formattakesrangeswith UTF-16 positions. A position past the end is the end, and a range that starts after its end is anInvalidParamserror.window/showMessageRequest:Allow/Deny/Always Denyfor a download, a proxy, reading from another machine, and process plugins. A client that can't show actions is told to rundprint installinstead;window/logMessageand$/progress);Config files
.configand"inherit"), or uses the global config file when the extension asks for that.uriand the directory it's used for (dirUri). The global config file has results for each root directory it's used for.substdrive, or/tmpon macOS.git checkout), the server takes back the diagnostics published through it and sendsconfigChanged. It then uses another path the editor used that still leads there, or else the canonical path.watchUris.Watching
configFileGlobs, the ancestor directories it chooses to, and each result'swatchUris, and sendsworkspace/didChangeWatchedFiles.lstateach directory below the workspace folder, and don't poll with astatthat follows symlinks.watchFiles, the server registers the canonical directories it needs instead.dprint/configChangedis sent whenever what a config file formats may have changed. The extension gets the results of its known config files and its open documents again.Approvals
Downloads: plugins and extended config files download without asking only from
registry.npmjs.org,plugins.dprint.dev,github.com,githubusercontent.comandeditor.trustedDomains. That includes each redirect..npmrcproxies: a proxy set by an.npmrcother than the user's own~/.npmrcis approved like a download, because it receives every download and the setting's credentials.Network paths: reading a file on another machine is approved like a download (item kind
networkPath). Examples:/netor an automounter's directory;This covers config files, extended files,
.npmrcand.gitignorefiles, npm packages and plugin files.onAnotherMachineuntil it's allowed, so the extension doesn't open it.Process plugins: they aren't sandboxed, so they run only after approval. An approval covers the config file and each plugin with its configuration, unless
editor.allowProcessPluginsistrue.Downloads offer
allowandalwaysDeny, which are stored ineditor-approvals.jsonasdownloadDecisionsuntil forgotten, anddeny, which is for the session. An allow is for the exact url or path shown, not its domain (to trust a whole domain, the user adds it toeditor.trustedDomains). Process plugins offeralwaysAllow,denyandalwaysDeny, not a session-onlyallow, because the user would be asked again each session andalwaysAllowonly covers the plugins with their current configuration.Text from the project shown in a prompt (paths, urls, specifiers, proxy settings) is escaped, so it can't pretend to be something else.
Security scope
Decisions
editor-sessions/<id>.jsonin the cache directory). A server restarted with the samesessionIdafter a crash asks about the same things.0600), because it may have proxy credentials.<id>.lockfor the session. It deletes its file on theexitaftershutdown, and files nobody used for a week.alwaysAllow/alwaysDenyfor process plugins) go ineditor-approvals.jsonnext to the settings file. Every write takes an exclusive lock of.editor-approvals.lockfor the whole read and write. When the lock can't be taken, the decision is only kept for the session and the result says so.alwaysDenystored after a session allow wins whatever the clocks say.dprint/forgetDecisionsforgets the decisions of a config file (or all), including stored ones.dprint/decisionslists them.--configdprint lsp --config <path>uses one local config file for every file. That can be a path, a~/path or afile:url.npm:specifier exits with an error at start: the server watches the file and provides it by its path. Extend a remote file from a local one instead.configChangedwithout aconfigFile.Settings and approvals files
settings.jsonholdseditor.allowProcessPlugins,editor.trustedDomainsandinit. It's only read from the global config directory, never from a project.editor-approvals.jsonand.editor-approvals.locklive next to it.DPRINT_CONFIG_DIRwhen it isn't empty;dprint. On macOS that'sXDG_CONFIG_HOME, then~/Library/Application Supportwhen itsdprintdirectory exists, then~/.config.Compatibility
version/minVersion.minVersion; a version that doesn't know them ignores them and keeps them when writing.configHashversion for each allow.minVersionstill applies the file's denials, but never writes to it or uses its allows.editorthat a version doesn't know is only logged with a warning to update dprint, and the rest of the settings are used. So an older version doesn't honor a setting a newer one adds. Only a settings file that can't be read or parsed fails closed: no trusted domains and no stored allows. Properties outsideeditorare lenient too.Changes for the CLI
Allowin the prompt:dprint fmt,dprint check,dprint install,dprint config update, and the commands that output what the plugins do (output-resolved-config,output-file-paths,output-format-times,incremental-state). It isn't done in CI (CIistrueor1), and a failure to store it is only logged in debug mode. Once the stored allow is over a minute old, running dprint stores it again with the time of that run, so it's more recent than aDenymade in an editor session since.dprint config addanddprint initallow them too because the user chose the plugins, setting up the process plugins to do so (a failure there is only logged in debug mode).dprint fmt --stdindoesn't, because editor integrations run it without the user choosing to.editor-approvals.jsonasnetworkPathDecisions, each as the exact path with where it resolved to, and forgetting a config file's decisions removes them.dprint clear-cachestops the user's runningdprint lspprocesses that use the same cache directory and then deletes the whole cache directory, including the session files, so editors start their servers again with nothing from before. A server that uses another cache directory (DPRINT_CACHE_DIR) keeps running./netonly counts when it's an automounter's..configis an ordinary directory, not a.configdirectory whose config file applies to its parent.dprint initsaves its preferences to theinitproperty of the settings file. It readsinit.jsonwhile the settings file has none, and leaves it in place for dprint 0.64..npmrcproxy setting other than the user's own is ignored with a warning when it isn't the url of a proxy.Release notes (user-facing changes)
dprint lspask before downloading from a domain that isn't trusted, before reading a file on another machine, before using a project's.npmrcproxy, and before running process plugins. Running dprint in a project (ex.dprint fmt,dprint install,dprint config update,dprint config addordprint init) allows its process plugins, except in CI.~/.config/dprint/settings.json(or the platform's equivalent), witheditor.trustedDomains,editor.allowProcessPluginsandinit.--configthat's a symlink is now used for its own directory, not its target's, in the CLI and in editors.dprint lsp --configonly accepts a local file.dprint clear-cachestops the language servers that use the cache directory, so editors start them again.ensureStableFormatandverbosesettings.lsp-state.jsonbecomeseditor-state.json. What dprint 0.60–0.64 dismissed is still read.init.jsonis kept for older versions; preferences now live in the settings file.Known limitations and decisions
editor.trustedDomainsin the user's settings, so a project can't add it.editor.allowProcessPluginsis a dprint setting rather than an editor setting, because workspace settings can come from the project.configChangedrather than mapping paths.watchFiles, and how it looks uppublishDiagnosticsuris. The spec says so.~/Library/Application Support/dprintlater moves the config directory there, leaving~/.config/dprintunused.DPRINT_CONFIG_DIRis relative to each process's current directory.dprint/decisions(from running the CLI with one).