From 86aa4a07b8c7afc88e18c92466a91b8ff922e99d Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 25 Jul 2024 12:54:12 +0200 Subject: [PATCH] fix: resolve ~ as homedir in the config --- src/config.ts | 16 ++++++++++------ test/config.test.ts | 10 ++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 test/config.test.ts diff --git a/src/config.ts b/src/config.ts index b471844..50d1f64 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,4 +1,5 @@ -import { dirname, resolve } from 'node:path' +import { homedir } from 'node:os' +import { dirname, isAbsolute, resolve } from 'node:path' import type { WorkspaceConfiguration, WorkspaceFolder } from 'vscode' import * as vscode from 'vscode' @@ -50,20 +51,23 @@ export function getConfig(workspaceFolder?: WorkspaceFolder) { env: get>('nodeEnv', null), debugExclude: get('debugExclude', []), vitestPackagePath: resolvedVitestPackagePath, - workspaceConfig: resolvePath(workspaceConfig), - rootConfig: resolvePath(rootConfigFile), + workspaceConfig: resolveConfigPath(workspaceConfig), + rootConfig: resolveConfigPath(rootConfigFile), configSearchPatternExclude, maximumConfigs: get('maximumConfigs', 3), - nodeExecutable: resolvePath(nodeExecutable), + nodeExecutable: resolveConfigPath(nodeExecutable), disableWorkspaceWarning: get('disableWorkspaceWarning', false), debuggerPort: get('debuggerPort') || undefined, debuggerAddress: get('debuggerAddress', undefined) || undefined, } } -function resolvePath(path: string | undefined) { - if (!path) +export function resolveConfigPath(path: string | undefined) { + if (!path || isAbsolute(path)) return path + if (path.startsWith('~/')) { + return resolve(homedir(), path.slice(2)) + } // if there is a workspace file, then it should be relative to it because // this option cannot be configured on a workspace folder level if (vscode.workspace.workspaceFile) diff --git a/test/config.test.ts b/test/config.test.ts new file mode 100644 index 0000000..78e37af --- /dev/null +++ b/test/config.test.ts @@ -0,0 +1,10 @@ +import { resolve } from 'node:path' +import { homedir } from 'node:os' +import { expect } from 'chai' +import { resolveConfigPath } from '../src/config' + +it('correctly resolves ~', () => { + expect(resolveConfigPath('~/test')).to.equal( + resolve(homedir(), 'test'), + ) +}) -- 2.51.2