Skip to content

Commit 76b88da

Browse files
committed
feat(editor): support relative path for oxc.path.server
1 parent 629cfae commit 76b88da

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

editors/vscode/client/ConfigService.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as path from 'node:path';
12
import { ConfigurationChangeEvent, Uri, workspace, WorkspaceFolder } from 'vscode';
23
import { IDisposable } from './types';
34
import { VSCodeConfig } from './VSCodeConfig';
@@ -59,6 +60,24 @@ export class ConfigService implements IDisposable {
5960
return false;
6061
}
6162

63+
public async getUserServerBinPath(): Promise<string | undefined> {
64+
let bin = this.vsCodeConfig.binPath;
65+
if (!bin) {
66+
return;
67+
}
68+
69+
if (!path.isAbsolute(bin)) {
70+
// if the path is not absolute, resolve it to the first workspace folder
71+
let cwd = this.workspaceConfigs.keys().next().value;
72+
if (!cwd) {
73+
return;
74+
}
75+
bin = path.normalize(path.join(cwd, bin));
76+
}
77+
78+
return bin;
79+
}
80+
6281
private async onVscodeConfigChange(event: ConfigurationChangeEvent): Promise<void> {
6382
let isConfigChanged = false;
6483

editors/vscode/client/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export async function activate(context: ExtensionContext) {
138138
);
139139

140140
async function findBinary(): Promise<string> {
141-
let bin = configService.vsCodeConfig.binPath;
141+
let bin = await configService.getUserServerBinPath();
142142
if (bin) {
143143
try {
144144
await fsPromises.access(bin);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { strictEqual } from 'assert';
2+
import { workspace } from 'vscode';
3+
import { ConfigService } from '../client/ConfigService.js';
4+
import { testSingleFolderMode, WORKSPACE_FOLDER } from './test-helpers.js';
5+
6+
const conf = workspace.getConfiguration('oxc');
7+
8+
suite('ConfigService', () => {
9+
setup(async () => {
10+
const keys = ['path.server'];
11+
12+
await Promise.all(keys.map(key => conf.update(key, undefined)));
13+
});
14+
15+
teardown(async () => {
16+
const keys = ['path.server'];
17+
18+
await Promise.all(keys.map(key => conf.update(key, undefined)));
19+
});
20+
21+
testSingleFolderMode('resolves relative server path with workspace folder', async () => {
22+
const service = new ConfigService();
23+
const nonDefinedServerPath = await service.getUserServerBinPath();
24+
25+
strictEqual(nonDefinedServerPath, undefined);
26+
27+
await conf.update('path.server', '/absolute/binary');
28+
const absoluteServerPath = await service.getUserServerBinPath();
29+
30+
strictEqual(absoluteServerPath, '/absolute/binary');
31+
32+
await conf.update('path.server', './relative/binary');
33+
const relativeServerPath = await service.getUserServerBinPath();
34+
35+
strictEqual(relativeServerPath, WORKSPACE_FOLDER.uri.path + '/relative/binary');
36+
});
37+
});

0 commit comments

Comments
 (0)