From 85dc0195f53f140d7c1de2f62fec15ad990b1e5c Mon Sep 17 00:00:00 2001 From: Qingyu Wang <40660121+colinaaa@users.noreply.github.com> Date: Wed, 11 Jun 2025 20:39:44 +0800 Subject: [PATCH] fix(cli): use absolute path environment on Windows (#8105) --- packages/vitest/src/node/cli/cli-api.ts | 4 ++-- test/cli/test/run-custom-env.test.ts | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index e6944c4d1..606848703 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -5,7 +5,7 @@ import type { TestModule, TestSuite } from '../reporters/reported-tasks' import type { TestSpecification } from '../spec' import type { UserConfig, VitestEnvironment, VitestRunMode } from '../types/config' import { mkdirSync, writeFileSync } from 'node:fs' -import { dirname, relative, resolve } from 'pathe' +import { dirname, isAbsolute, relative, resolve } from 'pathe' import { CoverageProviderMap } from '../../utils/coverage' import { createVitest } from '../create' import { FilesNotFoundError, GitNotFoundError, IncludeTaskLocationDisabledError, LocationFilterFileNotFoundError, RangeLocationFilterProvidedError } from '../errors' @@ -317,7 +317,7 @@ function getEnvPackageName(env: VitestEnvironment) { if (env in envPackageNames) { return (envPackageNames as any)[env] } - if (env[0] === '.' || env[0] === '/') { + if (env[0] === '.' || isAbsolute(env)) { return null } return `vitest-environment-${env}` diff --git a/test/cli/test/run-custom-env.test.ts b/test/cli/test/run-custom-env.test.ts index a25216715..382127c85 100644 --- a/test/cli/test/run-custom-env.test.ts +++ b/test/cli/test/run-custom-env.test.ts @@ -1,3 +1,4 @@ +import { createRequire } from 'node:module' import { expect, test } from 'vitest' import { runVitest } from '../../test-utils' @@ -17,3 +18,21 @@ test('correctly runs tests if custom env is a file', async () => { expect(stderr).toBe('') expect(exitCode).toBe(0) }) + +test('correctly runs tests if custom env is an absolute path', async () => { + const require = createRequire(import.meta.url) + + const { stderr, exitCode } = await runVitest({ + root: './fixtures/custom-file-env', + config: false, + environment: require.resolve('vitest-environment-custom'), + environmentOptions: { + custom: { + option: 'custom-option', + }, + }, + }) + + expect(stderr).toBe('') + expect(exitCode).toBe(0) +}) -- 2.51.2