Editor integration
You can have rust-analyzer run cargo cgp check when you save, bringing the rewritten errors of
Context-Generic Programming (CGP) code into your editor. One setting replaces its usual
cargo check command, so errors from the check lead with the same root causes as they do in a
terminal.
This page covers VS Code with the
rust-analyzer extension.
Before configuring it, install the cargo-cgp toolchain and
confirm that cargo cgp check works in a terminal inside your project.
Set it up
Open the project's .vscode/settings.json through Preferences: Open Workspace Settings (JSON)
in the command palette. Add the override to this workspace settings file at the project root:
{
"rust-analyzer.check.overrideCommand": [
"cargo", "cgp", "check", "--workspace", "--all-targets", "--message-format=json"
]
}
The next time you save a Rust file, rust-analyzer runs this command instead of cargo check. If the
errors you see do not change, run rust-analyzer: Restart server from the command palette.
Each part of the command has a job:
cargo cgp check: the check itself, the same command you run in a terminal.--workspace: checks every crate in the workspace, as rust-analyzer's own check does.--all-targets: includes tests, examples, and benchmarks, so an error in a test shows up too.--message-format=json: makes cargo print its errors as JSON, which is the only form rust-analyzer reads. The tool writes its rewritten errors in the same JSON format as the compiler, so nothing else is needed for them to reach the editor.
The full command belongs in
check.overrideCommand,
which runs it as written. The shorter
check.command setting
accepts only a cargo subcommand such as clippy. Giving it cgp would put rust-analyzer's flags
directly after cgp, where the tool expects check.
Workspace or user settings
Workspace settings limit this check to the project where you enable it. User settings instead apply across projects, including ones without CGP. Those projects still check correctly, but incur a separate nightly build with no CGP diagnostics to rewrite. Since workspace settings override user settings, a project can also enable or disable the command locally.
Sharing the settings file also makes installation a requirement for collaborators' save checks.
Only commit .vscode/settings.json if everyone opening the project has the tool. Otherwise,
rust-analyzer warns on each save instead of showing check errors until they install it or remove
the override.
What the editor shows
Rewritten check errors use the same editor locations as ordinary check errors: file underlines,
the Problems panel, and the hover over an underline. In the
Check page's program, the underline marks area in
rectangle.area(). Its diagnostic identifies the absent height field required by the provider:
[CGP-E001] missing field `height` on `Rectangle`
root cause: [CGP-E110] missing field `height` on `Rectangle`
this is required through the dependency chain:
[CGP-E101] consumer trait impl `CanCalculateArea` for context `Rectangle`
└─ [CGP-E102] provider trait impl `AreaCalculator` with context `Rectangle` for provider `RectangleArea`
└─ [CGP-E110] missing field `height` on `Rectangle`
add the field `height: f64` to `Rectangle`
The compiler's own code, E0599 here, stays attached to the error, as it does in a terminal.
Reading the output explains each part.
Importing a component key
A provider written in another module than its component imports the component's key beside its
provider trait, as #[cgp_impl]
explains. The header names that key without writing it, so rust-analyzer's own import assist has
nothing to offer on the header. With the override in place, cargo cgp check reports the missing key
as [CGP-E041] on the provider trait's name and attaches the import as
a fix. The quick-fix menu on that name, and on a wiring key that names a component the module does
not import, offers it under the title import the component, followed by the use line it inserts.
rust-analyzer marks it as the preferred fix when only one path imports the key.
[CGP-E070]'s fix, which names in the attribute a key the header cannot
derive, appears on the trait's name the same way.
The plain check behaves differently. The compiler's own import suggestion, when it makes one, appears
as a quick fix only on the module's first use line, not on the header, and stable Rust suggests no
import for a component from a library. Two things help under either check. rust-analyzer's import
assist offers to import a key written in the source, such as a wiring entry's key or the key in
#[cgp_impl(new RectangleArea: AreaCalculatorComponent)]. And hovering a provider trait in a use
line shows its documentation, which names the key a provider imports beside it.
What changes when the editor runs the tool
The override changes how the check runs as well as the wording of its diagnostics. These changes explain some differences you may notice after enabling it.
The first save can take longer because the pinned nightly builds every dependency into its own
target/cgp directory. Later saves reuse that build. Keeping it separate also lets the editor
check while ordinary terminal builds use their own target directory, without either waiting on
the other's cargo lock.
Because the override runs exactly as written, rust-analyzer does not add its usual check options.
Settings such as check.features,
check.allTargets, check.workspace, and check.extraArgs therefore need corresponding flags
in the command itself. For example, add "--all-features" or "--features", "my-feature" after
"--all-targets".
Alongside the check, rust-analyzer analyzes code as you type using its own engine. Errors from that analysis do not pass through the tool, so their wording stays the same. The rewritten compiler diagnostics arrive after saving, when the override command runs.
The separate compiler can also make the editor's result differ from your normal build.
The tool uses its pinned nightly and Rust's next-generation trait solver, which occasionally
accept or reject code differently from your own toolchain. Keep cargo check on that toolchain
as the build authority; the Check page explains the
distinction.
Turning it off
Delete the rust-analyzer.check.overrideCommand entry from the settings file you added it to.
rust-analyzer goes back to running cargo check on save.
When it does not work
If the check command fails to start, rust-analyzer reports a warning beginning
cargo check failed to start, even though you configured a different command. The rest contains
the command's output. For a missing setup, that includes the tool's own diagnostic:
cargo check failed to start: Cargo watcher failed, the command produced no valid metadata (exit code: ExitStatus(unix_wait_status(256))):
error: toolchain `nightly-2026-11-16` is not available:
error: toolchain 'nightly-2026-11-16-x86_64-unknown-linux-gnu' is not installed
help: run `rustup toolchain install nightly-2026-11-16-x86_64-unknown-linux-gnu` to install it
help: run `cargo cgp setup`
The nightly and platform in your warning depend on your installation. Troubleshooting explains these messages and their fixes. To separate a tool failure from editor configuration, run the same check in a terminal at the opened project's root:
cargo cgp check --workspace --all-targets
If the warning instead ends with cargo's no such command error, the editor cannot find the tool:
error: no such command: `cgp`
If the terminal command works but the editor reports this missing-command error, compare their
PATH environments. A desktop launcher may start VS Code without the shell setup that adds the
tool's location, such as a Nix profile. Starting VS Code from a terminal where
cargo cgp --version works lets it inherit that environment. Save again to retry the check.
If only some errors are rewritten, you are seeing the tool's limit rather than the editor's.
cargo cgp check leads with the root cause for the classes it recognizes, and the tool does not yet reshape
every class. Reading the output lists the errors you
are most likely to see as the compiler wrote them.
What this does not do
Only the on-save check uses this command. Completion, hover types, go-to-definition, and as-you-type diagnostics come from rust-analyzer's own analysis, so this override does not change them.
Verification covered the rust-analyzer language server on Linux, started with the configuration VS
Code would pass. Saving the example produced the rewritten diagnostic, the quoted startup warnings,
and the observed omission of rust-analyzer's other check options. On a crate with providers missing
their keys, the quick-fix menus held the import fixes described above. The VS Code window itself was
not tested. Other rust-analyzer editors can express check.overrideCommand in their own
configuration formats, but those editors, macOS, and Windows have not been tried.
An AI agent wrote this page using the CGP knowledge base, and checked its setup against the rust-analyzer language server. See How AI is used in this project.