Skip to content

fix: detect the rust-analyzer component in a multi-line components array - #22996

Merged
ChayimFriedman2 merged 2 commits into
rust-lang:masterfrom
Musteab:fix-multiline-components-detection
Aug 2, 2026
Merged

fix: detect the rust-analyzer component in a multi-line components array#22996
ChayimFriedman2 merged 2 commits into
rust-lang:masterfrom
Musteab:fix-multiline-components-detection

Conversation

@Musteab

@Musteab Musteab commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Closes #22980

hasToolchainFileWithRaDeclared matched the components array with . between the
brackets. . does not match newlines, so an array written over several lines was never
detected, even though rustup treats it identically to the single line form. Then extension
then fell back to the bundled server, and on a pinned toolchain older than the extension's
minimum supported version that produces a "toolchain too old" modal on every reload, with
nothing in the output channel explaining why the toolchain binary was skipped.

The elements are now matched with [^\]] instead. It crosses newlines but still stops at the
closing bracket, so the match cannot run past the end of the array into an unrelated key. Two
smaller things came with it: TOML literal strings ('rust-analyzer') are accepted, and the
old `?.length === 1 is gone, and a file containing two matching arrays counted as no match
at all.

Selection behaviour is otherwise unchanged. Detection is still opt-in, only when components
explicitly names rust-analyzer, and precedence between the toolchain server and the bundled
one is untouched. Having the component installed without declaring it still does not displace
the bundled server.

The predicate is split out as declaresRaComponent and exposed through the existing _private
object so it can be tested as a plain string check without mocking the filesystem. Tests cover
single line, multi line, literal strings, a components array without rust-analyzer, a file with
no components key, and rust-analyzer appearing outside the array.

🤖 AI-assisted: writing the tests and assisting in the fix

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 1, 2026
Comment thread editors/code/src/bootstrap.ts Outdated
// Matches a `components` array that lists `rust-analyzer`. The elements are matched with
// `[^\]]` rather than `.` so that the array may be spread over several lines, which is just
// as valid TOML as keeping it on one. TOML strings come in both quote flavours.
const RA_COMPONENT_RE = /components\s*=\s*\[[^\]]*["']rust-analyzer["'][^\]]*\]/;

@ChayimFriedman2 ChayimFriedman2 Aug 2, 2026

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.

I'd prefer:

/components\s*=\s*\[(?<array>.*?)\]*\]/s

Then checking if the capture group array contains rust-analyzer. The current regex is wrong when rust-analyzer appears in the rest of the file, not instead this array.

Of course my regex is also wrong, e.g. when rust-analyzer appears in a comment. Oh well. The only real fix is to pull a TOML parsing library, but I don't want to do this just for that. I guess my regex is enough for practical needs.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched to the named capture group since it reads better. The previous [^\]]* also stopped at the closing ], so it could not match a rust-analyzer elsewhere in the file. I tightened the test to cover a quoted mention after the array, and kept the quote check instead of includes() so unquoted mentions do not count. Happy to change that too if you prefer.

@ChayimFriedman2 ChayimFriedman2 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.

@ChayimFriedman2
ChayimFriedman2 added this pull request to the merge queue Aug 2, 2026
Merged via the queue into rust-lang:master with commit 3f98c4d Aug 2, 2026
19 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 2, 2026
@Musteab
Musteab deleted the fix-multiline-components-detection branch August 2, 2026 09:09
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.

Formatting of rust-toolchain.toml should not affect detection of the rust-analyzer component in vscode extension

3 participants