From dc64aa8b139c9e276146ad6a0f93dd09e96df17a Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 23 Oct 2024 16:14:56 +0200 Subject: [PATCH] fix(vitest): don't fail if the working directory starts with a small drive letter Fixes #5772 Fixes #5798 Fixes #5251 --- packages/vite-node/src/utils.ts | 16 ---------------- packages/vitest/src/node/cli/cac.ts | 8 ++++++++ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/packages/vite-node/src/utils.ts b/packages/vite-node/src/utils.ts index a4e5b5bc6586..48a5132253ff 100644 --- a/packages/vite-node/src/utils.ts +++ b/packages/vite-node/src/utils.ts @@ -6,17 +6,6 @@ import { dirname, join, resolve } from 'pathe' export const isWindows = process.platform === 'win32' -const drive = isWindows ? process.cwd()[0] : null -const driveOpposite = drive - ? drive === drive.toUpperCase() - ? drive.toLowerCase() - : drive.toUpperCase() - : null -const driveRegexp = drive ? new RegExp(`(?:^|/@fs/)${drive}(\:[\\/])`) : null -const driveOppositeRegext = driveOpposite - ? new RegExp(`(?:^|/@fs/)${driveOpposite}(\:[\\/])`) - : null - export function slash(str: string) { return str.replace(/\\/g, '/') } @@ -28,11 +17,6 @@ export function normalizeRequestId(id: string, base?: string): string { id = `/${id.slice(base.length)}` } - // keep drive the same as in process cwd - if (driveRegexp && !driveRegexp?.test(id) && driveOppositeRegext?.test(id)) { - id = id.replace(driveOppositeRegext, `${drive}$1`) - } - return id .replace(/^\/@id\/__x00__/, '\0') // virtual modules start with `\0` .replace(/^\/@id\//, '') diff --git a/packages/vitest/src/node/cli/cac.ts b/packages/vitest/src/node/cli/cac.ts index d1c8e78b103d..6992f1b98ad4 100644 --- a/packages/vitest/src/node/cli/cac.ts +++ b/packages/vitest/src/node/cli/cac.ts @@ -259,6 +259,14 @@ function normalizeCliOptions(argv: CliOptions): CliOptions { async function start(mode: VitestRunMode, cliFilters: string[], options: CliOptions): Promise { try { process.title = 'node (vitest)' + // pathe always normalizes the drive letter to be uppercase + // if user runs Vitest from c:/, the built-in ESM loader in tests + // will resolve a different Vitest instance than the one in vite-node + // we can either + // 1. normalize the CWD so it works in the CLI, but might break when API is used directly + // 2. ask "pathe" to not normalize the drive letter + // 2. don't use "pathe" when resolving paths + process.chdir(normalize(process.cwd())) } catch {}