Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit fac77a8

Browse files
committed
Auto merge of rust-lang#15558 - davidbarsky:davidbarsky/add-companion-extension-integration, r=Veykril
code: remove `rust-analyzer.discoverProjectCommand` in favor of a companion extension I think it's time to remove this functionality from the `rust-analyzer` and move it into a dedicated extension responsible for this. Selfishly, this changes makes it tenable to do progress reporting to the editor and potentially do some more complicated things around managing _which_ workspaces are being used.
2 parents 47e0d07 + 6260c63 commit fac77a8

File tree

5 files changed

+19
-67
lines changed

5 files changed

+19
-67
lines changed

editors/code/package.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,16 +471,13 @@
471471
"default": false,
472472
"type": "boolean"
473473
},
474-
"rust-analyzer.discoverProjectCommand": {
475-
"markdownDescription": "Sets the command that rust-analyzer uses to generate `rust-project.json` files. This command should only be used\n if a build system like Buck or Bazel is also in use. The command must accept files as arguments and return \n a rust-project.json over stdout.",
474+
"rust-analyzer.discoverProjectRunner": {
475+
"markdownDescription": "Sets the extension responsible for determining which extension the rust-analyzer extension uses to generate `rust-project.json` files. This should should only be used\n if a build system like Buck or Bazel is also in use.",
476476
"default": null,
477477
"type": [
478478
"null",
479-
"array"
480-
],
481-
"items": {
482-
"type": "string"
483-
}
479+
"string"
480+
]
484481
},
485482
"rust-analyzer.showUnlinkedFileNotification": {
486483
"markdownDescription": "Whether to show a notification for unlinked files asking the user to add the corresponding Cargo.toml to the linked projects setting.",

editors/code/src/commands.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as lc from "vscode-languageclient";
33
import * as ra from "./lsp_ext";
44
import * as path from "path";
55

6-
import { type Ctx, type Cmd, type CtxInit, discoverWorkspace } from "./ctx";
6+
import type { Ctx, Cmd, CtxInit } from "./ctx";
77
import { applySnippetWorkspaceEdit, applySnippetTextEdits } from "./snippets";
88
import { spawnSync } from "child_process";
99
import { type RunnableQuickPick, selectRunnable, createTask, createArgs } from "./run";
@@ -871,22 +871,16 @@ export function rebuildProcMacros(ctx: CtxInit): Cmd {
871871

872872
export function addProject(ctx: CtxInit): Cmd {
873873
return async () => {
874-
const discoverProjectCommand = ctx.config.discoverProjectCommand;
875-
if (!discoverProjectCommand) {
874+
const extensionName = ctx.config.discoverProjectRunner;
875+
// this command shouldn't be enabled in the first place if this isn't set.
876+
if (!extensionName) {
876877
return;
877878
}
878879

879-
const workspaces: JsonProject[] = await Promise.all(
880-
vscode.workspace.textDocuments
881-
.filter(isRustDocument)
882-
.map(async (file): Promise<JsonProject> => {
883-
return discoverWorkspace([file], discoverProjectCommand, {
884-
cwd: path.dirname(file.uri.fsPath),
885-
});
886-
}),
887-
);
880+
const command = `${extensionName}.discoverWorkspaceCommand`;
881+
const project: JsonProject = await vscode.commands.executeCommand(command);
888882

889-
ctx.addToDiscoveredWorkspaces(workspaces);
883+
ctx.addToDiscoveredWorkspaces([project]);
890884

891885
// this is a workaround to avoid needing writing the `rust-project.json` into
892886
// a workspace-level VS Code-specific settings folder. We'd like to keep the

editors/code/src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ export class Config {
253253
return this.get<boolean>("trace.extension");
254254
}
255255

256-
get discoverProjectCommand() {
257-
return this.get<string[] | undefined>("discoverProjectCommand");
256+
get discoverProjectRunner(): string | undefined {
257+
return this.get<string | undefined>("discoverProjectRunner");
258258
}
259259

260260
get problemMatcher(): string[] {

editors/code/src/ctx.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import * as vscode from "vscode";
22
import type * as lc from "vscode-languageclient/node";
33
import * as ra from "./lsp_ext";
4-
import * as path from "path";
54

65
import { Config, prepareVSCodeConfig } from "./config";
76
import { createClient } from "./client";
87
import {
9-
executeDiscoverProject,
108
isDocumentInWorkspace,
119
isRustDocument,
1210
isRustEditor,
@@ -24,7 +22,6 @@ import {
2422
import { execRevealDependency } from "./commands";
2523
import { PersistentState } from "./persistent_state";
2624
import { bootstrap } from "./bootstrap";
27-
import type { ExecOptions } from "child_process";
2825

2926
// We only support local folders, not eg. Live Share (`vlsl:` scheme), so don't activate if
3027
// only those are in use. We use "Empty" to represent these scenarios
@@ -58,17 +55,6 @@ export function fetchWorkspace(): Workspace {
5855
: { kind: "Workspace Folder" };
5956
}
6057

61-
export async function discoverWorkspace(
62-
files: readonly vscode.TextDocument[],
63-
command: string[],
64-
options: ExecOptions,
65-
): Promise<JsonProject> {
66-
const paths = files.map((f) => `"${f.uri.fsPath}"`).join(" ");
67-
const joinedCommand = command.join(" ");
68-
const data = await executeDiscoverProject(`${joinedCommand} ${paths}`, options);
69-
return JSON.parse(data) as JsonProject;
70-
}
71-
7258
export type CommandFactory = {
7359
enabled: (ctx: CtxInit) => Cmd;
7460
disabled?: (ctx: Ctx) => Cmd;
@@ -200,6 +186,12 @@ export class Ctx {
200186
};
201187

202188
let rawInitializationOptions = vscode.workspace.getConfiguration("rust-analyzer");
189+
if (this.config.discoverProjectRunner) {
190+
const command = `${this.config.discoverProjectRunner}.discoverWorkspaceCommand`;
191+
log.info(`running command: ${command}`);
192+
const project: JsonProject = await vscode.commands.executeCommand(command);
193+
this.addToDiscoveredWorkspaces([project]);
194+
}
203195

204196
if (this.workspace.kind === "Detached Files") {
205197
rawInitializationOptions = {
@@ -208,21 +200,6 @@ export class Ctx {
208200
};
209201
}
210202

211-
const discoverProjectCommand = this.config.discoverProjectCommand;
212-
if (discoverProjectCommand) {
213-
const workspaces: JsonProject[] = await Promise.all(
214-
vscode.workspace.textDocuments
215-
.filter(isRustDocument)
216-
.map(async (file): Promise<JsonProject> => {
217-
return discoverWorkspace([file], discoverProjectCommand, {
218-
cwd: path.dirname(file.uri.fsPath),
219-
});
220-
}),
221-
);
222-
223-
this.addToDiscoveredWorkspaces(workspaces);
224-
}
225-
226203
const initializationOptions = prepareVSCodeConfig(
227204
rawInitializationOptions,
228205
(key, obj) => {

editors/code/src/util.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,6 @@ export function execute(command: string, options: ExecOptions): Promise<string>
154154
});
155155
}
156156

157-
export function executeDiscoverProject(command: string, options: ExecOptions): Promise<string> {
158-
options = Object.assign({ maxBuffer: 10 * 1024 * 1024 }, options);
159-
log.info(`running command: ${command}`);
160-
return new Promise((resolve, reject) => {
161-
exec(command, options, (err, stdout, _) => {
162-
if (err) {
163-
log.error(err);
164-
reject(err);
165-
return;
166-
}
167-
168-
resolve(stdout.trimEnd());
169-
});
170-
});
171-
}
172-
173157
export class LazyOutputChannel implements vscode.OutputChannel {
174158
constructor(name: string) {
175159
this.name = name;

0 commit comments

Comments
 (0)