diff --git a/README.md b/README.md index e97d576..2d1dfbc 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ These options are resolved relative to the [workspace file](https://code.visuals ### Other Options -- `vitest.packagePath`: The path to a `package.json` file of a Vitest executable (it's usually inside `node_modules`) in case the extension cannot find it. It will be used to resolve Vitest API paths. This should be used as a last resort fix. +- `vitest.vitestPackagePath`: The path to a `package.json` file of a Vitest executable (it's usually inside `node_modules`) in case the extension cannot find it. It will be used to resolve Vitest API paths. This should be used as a last resort fix. - `vitest.nodeEnv`: Environment passed to the runner process in addition to `process.env` - `vitest.debugExclude`: Excludes files matching specified glob patterns from debugging. Default: diff --git a/package.json b/package.json index 5d7fe0c..0e534d7 100644 --- a/package.json +++ b/package.json @@ -85,7 +85,7 @@ "configuration": { "title": "Vitest", "properties": { - "vitest.packagePath": { + "vitest.vitestPackagePath": { "markdownDescription": "The path to the `package.json` file of Vitest executable (it's usually inside `node_modules`) in case the extension cannot find it.", "type": "string", "scope": "resource" diff --git a/src/api/pkg.ts b/src/api/pkg.ts index 8b2d6c3..b9b14eb 100644 --- a/src/api/pkg.ts +++ b/src/api/pkg.ts @@ -55,6 +55,13 @@ function resolveVitestConfig(showWarning: boolean, configOrWorkspaceFile: vscode } const pkg = _require(vitest.vitestPackageJsonPath) + if (pkg.name !== 'vitest') { + vscode.window.showErrorMessage( + `Package was resolved to "${pkg.name}" instead of "vitest". If you are using "vitest.vitestPackagePath", make sure it points to a "vitest" package.`, + ) + delete require.cache[vitest.vitestPackageJsonPath] + return null + } if (!gte(pkg.version, minimumVersion)) { const warning = `Vitest v${pkg.version} is not supported. Vitest v${minimumVersion} or newer is required.` if (showWarning) diff --git a/src/config.ts b/src/config.ts index 4105ea2..655f30d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -14,7 +14,7 @@ export function getConfigValue( return folderConfig.get(key) ?? rootConfig.get(key) ?? defaultValue } -export function getConfig(workspaceFolder?: WorkspaceFolder | vscode.Uri | string) { +export function getConfig(workspaceFolder?: WorkspaceFolder) { let workspace: WorkspaceFolder | vscode.Uri | undefined if (typeof workspaceFolder === 'string') workspace = vscode.Uri.from({ scheme: 'file', path: workspaceFolder }) @@ -37,10 +37,16 @@ export function getConfig(workspaceFolder?: WorkspaceFolder | vscode.Uri | strin const configSearchPatternExclude = get('configSearchPatternExclude', '**/{node_modules,.*}/**')! + const vitestPackagePath = get('vitestPackagePath') + const resolvedVitestPackagePath = workspaceFolder && vitestPackagePath + // eslint-disable-next-line no-template-curly-in-string + ? resolve(workspaceFolder.uri.fsPath, vitestPackagePath.replace('${workspaceFolder}', workspaceFolder.uri.fsPath)) + : vitestPackagePath + return { env: get>('nodeEnv', null), debugExclude: get('debugExclude', []), - packagePath: get('packagePath'), + vitestPackagePath: resolvedVitestPackagePath, workspaceConfig: resolvePath(workspaceConfig), rootConfig: resolvePath(rootConfigFile), configSearchPatternExclude, diff --git a/src/extension.ts b/src/extension.ts index 87542a3..9659475 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -208,7 +208,7 @@ class VitestExtension { this.testController.items.replace([this.loadingTestItem]) const reloadConfigNames = [ - 'vitest.packagePath', + 'vitest.vitestPackagePath', 'vitest.nodeExecutable', 'vitest.workspaceConfig', 'vitest.rootConfig',