Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: Use rustup rust-analyzer component when there is a toolchain file override for the opened workspace #17667

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions editors/code/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ async function getServer(
enableProposedApi: boolean | undefined;
} = context.extension.packageJSON;

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

if (vscode.workspace.workspaceFolders?.length === 1) {
// otherwise check if there is a toolchain override for the current vscode workspace
// and if the toolchain of this override has a rust-analyzer component
// if so, use the rust-analyzer component
const toolchainTomlExists = await fileExists(
vscode.Uri.joinPath(vscode.workspace.workspaceFolders[0]!.uri, "rust-toolchain.toml"),
Copy link
Member

Choose a reason for hiding this comment

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

Nb. this doesn't handle rust-toolchain, but I think it's fine.

);
if (toolchainTomlExists) {
const res = spawnSync("rustup", ["which", "rust-analyzer"], {
encoding: "utf8",
env: { ...process.env },
cwd: vscode.workspace.workspaceFolders[0]!.uri.fsPath,
});
if (!res.error && res.status === 0) {
return res.stdout.trim();
}
}
}

// finally, use the bundled one
const ext = process.platform === "win32" ? ".exe" : "";
const bundled = vscode.Uri.joinPath(context.extensionUri, "server", `rust-analyzer${ext}`);
const bundledExists = await vscode.workspace.fs.stat(bundled).then(
() => true,
() => false,
);
const bundledExists = await fileExists(bundled);
if (bundledExists) {
let server = bundled;
if (await isNixOs()) {
Expand Down Expand Up @@ -84,6 +102,13 @@ async function getServer(
return undefined;
}

async function fileExists(uri: vscode.Uri) {
return await vscode.workspace.fs.stat(uri).then(
() => true,
() => false,
);
}

export function isValidExecutable(path: string, extraEnv: Env): boolean {
log.debug("Checking availability of a binary at", path);

Expand Down
29 changes: 15 additions & 14 deletions editors/code/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,7 @@ export class Ctx implements RustAnalyzerExtensionApi {
}

if (!this._client) {
this._serverPath = await bootstrap(this.extCtx, this.config, this.state).catch(
(err) => {
let message = "bootstrap error. ";

message +=
'See the logs in "OUTPUT > Rust Analyzer Client" (should open automatically). ';
message +=
'To enable verbose logs use { "rust-analyzer.trace.extension": true }';

log.error("Bootstrap error", err);
throw new Error(message);
},
);
this._serverPath = await this.bootstrap();
text(spawn(this._serverPath, ["--version"]).stdout.setEncoding("utf-8")).then(
(data) => {
const prefix = `rust-analyzer `;
Expand Down Expand Up @@ -291,6 +279,19 @@ export class Ctx implements RustAnalyzerExtensionApi {
return this._client;
}

private async bootstrap(): Promise<string> {
return bootstrap(this.extCtx, this.config, this.state).catch((err) => {
let message = "bootstrap error. ";

message +=
'See the logs in "OUTPUT > Rust Analyzer Client" (should open automatically). ';
message += 'To enable verbose logs use { "rust-analyzer.trace.extension": true }';

log.error("Bootstrap error", err);
throw new Error(message);
});
}

async start() {
log.info("Starting language client");
const client = await this.getOrCreateClient();
Expand Down Expand Up @@ -501,7 +502,7 @@ export class Ctx implements RustAnalyzerExtensionApi {

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