Skip to content

Commit 8ac9a4d

Browse files
authored
feat(vscode): support custom rstest package path (#679)
1 parent 374292e commit 8ac9a4d

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

packages/vscode/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
"debug"
4949
],
5050
"default": "default"
51+
},
52+
"rstest.rstestPackagePath": {
53+
"markdownDescription": "The path to a `package.json` file of a Rstest executable (it's usually inside `node_modules`) in case the extension cannot find it. It will be used to resolve Rstest API paths. This should be used as a last resort fix. Supports `${workspaceFolder}` placeholder.",
54+
"scope": "resource",
55+
"type": "string"
5156
}
5257
}
5358
}

packages/vscode/src/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export type ExtensionConfig = {
1010
testFileGlobPattern: string[];
1111
// Controls verbosity of extension logging routed to the Output channel.
1212
logLevel: LogLevel;
13+
// The path to a package.json file of a Rstest executable.
14+
// Used as a last resort if the extension cannot auto-detect @rstest/core.
15+
rstestPackagePath?: string;
1316
};
1417

1518
export const defaultConfig: ExtensionConfig = {
@@ -44,6 +47,13 @@ export function getConfigValue<K extends keyof ExtensionConfig>(
4447
return (isLogLevel(v) ? v : defaultConfig[key]) as ExtensionConfig[K];
4548
}
4649

50+
if (key === 'rstestPackagePath') {
51+
const v = value as unknown;
52+
return (
53+
typeof v === 'string' && v.trim().length > 0 ? v : undefined
54+
) as ExtensionConfig[K];
55+
}
56+
4757
return value as ExtensionConfig[K];
4858
}
4959

@@ -60,5 +70,6 @@ export function getConfig(folder?: vscode.WorkspaceFolder): ExtensionConfig {
6070
return {
6171
testFileGlobPattern: getConfigValue('testFileGlobPattern', folder),
6272
logLevel: getConfigValue('logLevel', folder),
73+
rstestPackagePath: getConfigValue('rstestPackagePath', folder),
6374
} satisfies ExtensionConfig;
6475
}

packages/vscode/src/master.ts

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { spawn } from 'node:child_process';
22
import { createServer } from 'node:http';
3-
import path from 'node:path';
3+
import path, { dirname } from 'node:path';
44
import getPort from 'get-port';
55
import vscode from 'vscode';
66
import type { WebSocket } from 'ws';
77
import { WebSocketServer } from 'ws';
8+
import { getConfigValue } from './config';
89
import { logger } from './logger';
910
import type {
1011
WorkerEvent,
@@ -26,17 +27,54 @@ export class RstestApi {
2627
// TODO: use 0 temporarily.
2728
const workspace = vscode.workspace.workspaceFolders?.[0];
2829
if (!workspace) {
29-
vscode.window.showErrorMessage('No workspace found');
3030
throw new Error('No workspace found');
3131
}
32-
const nodeExport = require.resolve('@rstest/core', {
33-
paths: [workspace.uri.fsPath],
34-
});
32+
33+
// Check if user configured a custom package path (last resort fix)
34+
let configuredPackagePath = getConfigValue(
35+
'rstestPackagePath',
36+
workspace,
37+
);
38+
39+
if (configuredPackagePath) {
40+
// Support ${workspaceFolder} placeholder
41+
configuredPackagePath = configuredPackagePath.replace(
42+
// biome-ignore lint: This is a VS Code config placeholder string
43+
'${workspaceFolder}',
44+
workspace.uri.fsPath,
45+
);
46+
// Validate that the path points to package.json
47+
if (!configuredPackagePath.endsWith('package.json')) {
48+
const errorMessage = `"rstest.rstestPackagePath" must point to a package.json file, instead got: ${configuredPackagePath}`;
49+
throw new Error(errorMessage);
50+
}
51+
52+
// User provided a custom path to package.json
53+
configuredPackagePath = path.isAbsolute(configuredPackagePath)
54+
? configuredPackagePath
55+
: path.resolve(workspace.uri.fsPath, configuredPackagePath);
56+
57+
logger.debug(
58+
'Using configured rstestPackagePath:',
59+
configuredPackagePath,
60+
);
61+
}
62+
63+
const nodeExport = require.resolve(
64+
configuredPackagePath ? dirname(configuredPackagePath) : '@rstest/core',
65+
{
66+
paths: [workspace.uri.fsPath],
67+
},
68+
);
69+
3570
let corePackageJsonPath: string;
3671
try {
37-
corePackageJsonPath = require.resolve('@rstest/core/package.json', {
38-
paths: [workspace.uri.fsPath],
39-
});
72+
corePackageJsonPath = require.resolve(
73+
configuredPackagePath || '@rstest/core/package.json',
74+
{
75+
paths: [workspace.uri.fsPath],
76+
},
77+
);
4078
} catch (e) {
4179
vscode.window.showErrorMessage(
4280
'Failed to resolve @rstest/core/package.json. Please upgrade @rstest/core to the latest version.',

0 commit comments

Comments
 (0)