Skip to content

Commit 7e52128

Browse files
committed
Auto merge of rust-lang#17667 - Veykril:r-a-component-override, r=Veykril
Use rustup rust-analyzer component when there is a toolchain file override for the opened workspace Fixes rust-lang/rust-analyzer#17663
2 parents 91b8226 + 6f7a837 commit 7e52128

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

src/tools/rust-analyzer/editors/code/src/bootstrap.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ async function getServer(
4242
enableProposedApi: boolean | undefined;
4343
} = context.extension.packageJSON;
4444

45+
// check if the server path is configured explicitly
4546
const explicitPath = process.env["__RA_LSP_SERVER_DEBUG"] ?? config.serverPath;
4647
if (explicitPath) {
4748
if (explicitPath.startsWith("~/")) {
@@ -51,12 +52,29 @@ async function getServer(
5152
}
5253
if (packageJson.releaseTag === null) return "rust-analyzer";
5354

55+
if (vscode.workspace.workspaceFolders?.length === 1) {
56+
// otherwise check if there is a toolchain override for the current vscode workspace
57+
// and if the toolchain of this override has a rust-analyzer component
58+
// if so, use the rust-analyzer component
59+
const toolchainTomlExists = await fileExists(
60+
vscode.Uri.joinPath(vscode.workspace.workspaceFolders[0]!.uri, "rust-toolchain.toml"),
61+
);
62+
if (toolchainTomlExists) {
63+
const res = spawnSync("rustup", ["which", "rust-analyzer"], {
64+
encoding: "utf8",
65+
env: { ...process.env },
66+
cwd: vscode.workspace.workspaceFolders[0]!.uri.fsPath,
67+
});
68+
if (!res.error && res.status === 0) {
69+
return res.stdout.trim();
70+
}
71+
}
72+
}
73+
74+
// finally, use the bundled one
5475
const ext = process.platform === "win32" ? ".exe" : "";
5576
const bundled = vscode.Uri.joinPath(context.extensionUri, "server", `rust-analyzer${ext}`);
56-
const bundledExists = await vscode.workspace.fs.stat(bundled).then(
57-
() => true,
58-
() => false,
59-
);
77+
const bundledExists = await fileExists(bundled);
6078
if (bundledExists) {
6179
let server = bundled;
6280
if (await isNixOs()) {
@@ -84,6 +102,13 @@ async function getServer(
84102
return undefined;
85103
}
86104

105+
async function fileExists(uri: vscode.Uri) {
106+
return await vscode.workspace.fs.stat(uri).then(
107+
() => true,
108+
() => false,
109+
);
110+
}
111+
87112
export function isValidExecutable(path: string, extraEnv: Env): boolean {
88113
log.debug("Checking availability of a binary at", path);
89114

src/tools/rust-analyzer/editors/code/src/ctx.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,7 @@ export class Ctx implements RustAnalyzerExtensionApi {
187187
}
188188

189189
if (!this._client) {
190-
this._serverPath = await bootstrap(this.extCtx, this.config, this.state).catch(
191-
(err) => {
192-
let message = "bootstrap error. ";
193-
194-
message +=
195-
'See the logs in "OUTPUT > Rust Analyzer Client" (should open automatically). ';
196-
message +=
197-
'To enable verbose logs use { "rust-analyzer.trace.extension": true }';
198-
199-
log.error("Bootstrap error", err);
200-
throw new Error(message);
201-
},
202-
);
190+
this._serverPath = await this.bootstrap();
203191
text(spawn(this._serverPath, ["--version"]).stdout.setEncoding("utf-8")).then(
204192
(data) => {
205193
const prefix = `rust-analyzer `;
@@ -291,6 +279,19 @@ export class Ctx implements RustAnalyzerExtensionApi {
291279
return this._client;
292280
}
293281

282+
private async bootstrap(): Promise<string> {
283+
return bootstrap(this.extCtx, this.config, this.state).catch((err) => {
284+
let message = "bootstrap error. ";
285+
286+
message +=
287+
'See the logs in "OUTPUT > Rust Analyzer Client" (should open automatically). ';
288+
message += 'To enable verbose logs use { "rust-analyzer.trace.extension": true }';
289+
290+
log.error("Bootstrap error", err);
291+
throw new Error(message);
292+
});
293+
}
294+
294295
async start() {
295296
log.info("Starting language client");
296297
const client = await this.getOrCreateClient();
@@ -501,7 +502,7 @@ export class Ctx implements RustAnalyzerExtensionApi {
501502

502503
const toggleCheckOnSave = this.config.checkOnSave ? "Disable" : "Enable";
503504
statusBar.tooltip.appendMarkdown(
504-
`[Extension Info](command:analyzer.serverVersion "Show version and server binary info"): Version ${this.version}, Server Version ${this._serverVersion}` +
505+
`[Extension Info](command:rust-analyzer.serverVersion "Show version and server binary info"): Version ${this.version}, Server Version ${this._serverVersion}` +
505506
"\n\n---\n\n" +
506507
'[$(terminal) Open Logs](command:rust-analyzer.openLogs "Open the server logs")' +
507508
"\n\n" +

0 commit comments

Comments
 (0)