Skip to content

Commit 5bdbac0

Browse files
committed
refactor(vscode): add ToolInterface.getBinary (#16066)
The extension needs to check if the binaries are the same. Should probably never happen, but my first tests are targeting the same server and fails because of the same `oxc.X` command from the two language server.
1 parent 3ee22b2 commit 5bdbac0

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

editors/vscode/client/extension.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,18 @@ export async function activate(context: ExtensionContext) {
5353
configService.onConfigChange = async function onConfigChange(event) {
5454
await linter.onConfigChange(event, configService, statusBarItemHandler);
5555
};
56+
const binaryPath = await linter.getBinary(context, outputChannel, configService);
5657

57-
await linter.activate(context, outputChannel, configService, statusBarItemHandler);
58+
// For the linter this should never happen, but just in case.
59+
if (!binaryPath) {
60+
statusBarItemHandler.setColorAndIcon('statusBarItem.errorBackground', 'error');
61+
statusBarItemHandler.updateToolTooltip('linter', 'Error: No valid oxc language server binary found.');
62+
statusBarItemHandler.show();
63+
outputChannel.error('No valid oxc language server binary found.');
64+
return;
65+
}
66+
67+
await linter.activate(context, binaryPath, outputChannel, configService, statusBarItemHandler);
5868
// Show status bar item after activation
5969
statusBarItemHandler.show();
6070
}

editors/vscode/client/tools/ToolInterface.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ import { ConfigService } from '../ConfigService';
33
import StatusBarItemHandler from '../StatusBarItemHandler';
44

55
export default interface ToolInterface {
6+
/**
7+
* Gets the path to the tool's language server binary (if applicable).
8+
*/
9+
getBinary(
10+
context: ExtensionContext,
11+
outputChannel: LogOutputChannel,
12+
configService: ConfigService,
13+
): Promise<string | undefined>;
614
/**
715
* Activates the tool and initializes any necessary resources.
816
*/
917
activate(
1018
context: ExtensionContext,
19+
binaryPath: string,
1120
outputChannel: LogOutputChannel,
1221
configService: ConfigService,
1322
statusBarItemHandler: StatusBarItemHandler,

editors/vscode/client/tools/linter.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,28 @@ export default class LinterTool implements ToolInterface {
2828
// LSP client instance
2929
private client: LanguageClient | undefined;
3030

31+
async getBinary(
32+
context: ExtensionContext,
33+
outputChannel: LogOutputChannel,
34+
configService: ConfigService,
35+
): Promise<string | undefined> {
36+
const bin = configService.getUserServerBinPath();
37+
if (workspace.isTrusted && bin) {
38+
try {
39+
await fsPromises.access(bin);
40+
return bin;
41+
} catch (e) {
42+
outputChannel.error(`Invalid bin path: ${bin}`, e);
43+
}
44+
}
45+
const ext = process.platform === 'win32' ? '.exe' : '';
46+
// NOTE: The `./target/release` path is aligned with the path defined in .github/workflows/release_vscode.yml
47+
return process.env.SERVER_PATH_DEV ?? join(context.extensionPath, `./target/release/oxc_language_server${ext}`);
48+
}
49+
3150
async activate(
3251
context: ExtensionContext,
52+
binaryPath: string,
3353
outputChannel: LogOutputChannel,
3454
configService: ConfigService,
3555
statusBarItemHandler: StatusBarItemHandler,
@@ -63,30 +83,13 @@ export default class LinterTool implements ToolInterface {
6383

6484
context.subscriptions.push(applyAllFixesFile);
6585

66-
async function findBinary(): Promise<string> {
67-
const bin = configService.getUserServerBinPath();
68-
if (workspace.isTrusted && bin) {
69-
try {
70-
await fsPromises.access(bin);
71-
return bin;
72-
} catch (e) {
73-
outputChannel.error(`Invalid bin path: ${bin}`, e);
74-
}
75-
}
76-
const ext = process.platform === 'win32' ? '.exe' : '';
77-
// NOTE: The `./target/release` path is aligned with the path defined in .github/workflows/release_vscode.yml
78-
return process.env.SERVER_PATH_DEV ?? join(context.extensionPath, `./target/release/oxc_language_server${ext}`);
79-
}
80-
81-
const path = await findBinary();
82-
83-
const run: Executable = runExecutable(path, configService.vsCodeConfig.nodePath);
86+
const run: Executable = runExecutable(binaryPath, configService.vsCodeConfig.nodePath);
8487
const serverOptions: ServerOptions = {
8588
run,
8689
debug: run,
8790
};
8891

89-
outputChannel.info(`Using server binary at: ${path}`);
92+
outputChannel.info(`Using server binary at: ${binaryPath}`);
9093

9194
// see https://github.com/oxc-project/oxc/blob/9b475ad05b750f99762d63094174be6f6fc3c0eb/crates/oxc_linter/src/loader/partial_loader/mod.rs#L17-L20
9295
const supportedExtensions = ['astro', 'cjs', 'cts', 'js', 'jsx', 'mjs', 'mts', 'svelte', 'ts', 'tsx', 'vue'];

0 commit comments

Comments
 (0)