@@ -7,8 +7,6 @@ import { configGlob, minimumVersion, workspaceGlob } from '../constants'
77import { log } from '../log'
88import { resolveVitestPackage } from './resolve'
99
10- const _require = require
11-
1210function nonNullable < T > ( value : T | null | undefined ) : value is T {
1311 return value != null
1412}
@@ -32,8 +30,8 @@ export interface VitestPackage {
3230function isVitestInPackageJson ( root : string ) {
3331 const pkgJson = resolve ( dirname ( root ) , 'package.json' )
3432 if ( existsSync ( pkgJson ) ) {
35- const pkg = JSON . parse ( readFileSync ( pkgJson , 'utf-8' ) )
36- return pkg . dependencies ?. vitest || pkg . devDependencies ?. vitest
33+ const pkg = readPkgJson ( pkgJson )
34+ return pkg ? .dependencies ?. vitest || pkg ? .devDependencies ?. vitest
3735 }
3836 return false
3937}
@@ -85,8 +83,8 @@ function resolveVitestConfig(showWarning: boolean, configOrWorkspaceFile: vscode
8583 }
8684 }
8785
88- const pkg = _require ( vitest . vitestPackageJsonPath )
89- if ( ! validateVitestPkg ( showWarning , vitest . vitestPackageJsonPath , pkg ) )
86+ const pkg = readPkgJson ( vitest . vitestPackageJsonPath )
87+ if ( ! pkg || ! validateVitestPkg ( showWarning , vitest . vitestPackageJsonPath , pkg ) )
9088 return null
9189
9290 return {
@@ -123,7 +121,7 @@ function validateVitestPkg(showWarning: boolean, pkgJsonPath: string, pkg: any)
123121export async function resolveVitestPackages ( showWarning : boolean ) : Promise < { configs : VitestPackage [ ] ; workspaces : VitestPackage [ ] } > {
124122 // TODO: update "warned" logic
125123 const [ workspaceConfigs , configs ] = await Promise . all ( [
126- resolveVitestWorkspaceConfigs ( showWarning ) ,
124+ resolveVitestWorkspaceConfigs ( ) ,
127125 resolveVitestConfigs ( showWarning ) ,
128126 ] )
129127 if ( ! workspaceConfigs . meta . length && ! configs . meta . length ) {
@@ -147,8 +145,8 @@ function resolveVitestWorkspacePackages(showWarning: boolean) {
147145 if ( ! vitest )
148146 return
149147
150- const pkg = _require ( vitest . vitestPackageJsonPath )
151- if ( ! validateVitestPkg ( showWarning , vitest . vitestPackageJsonPath , pkg ) ) {
148+ const pkg = readPkgJson ( vitest . vitestPackageJsonPath )
149+ if ( ! pkg || ! validateVitestPkg ( showWarning , vitest . vitestPackageJsonPath , pkg ) ) {
152150 warned = true
153151 return
154152 }
@@ -181,7 +179,7 @@ export async function resolveVitestPackagesViaPackageJson(showWarning: boolean):
181179 let warned = false
182180 const meta : VitestPackage [ ] = [ ]
183181 packages . forEach ( ( pkgPath ) => {
184- const scripts = Object . entries ( _require ( pkgPath . fsPath ) . scripts || { } ) . filter ( ( [ , script ] ) => {
182+ const scripts = Object . entries ( readPkgJson ( pkgPath . fsPath ) ? .scripts || { } ) . filter ( ( [ , script ] ) => {
185183 return typeof script === 'string' && script . startsWith ( 'vitest ' )
186184 } ) as [ string , string ] [ ]
187185
@@ -196,8 +194,8 @@ export async function resolveVitestPackagesViaPackageJson(showWarning: boolean):
196194 if ( ! vitest )
197195 return
198196
199- const pkg = _require ( vitest . vitestPackageJsonPath )
200- if ( ! validateVitestPkg ( showWarning , vitest . vitestPackageJsonPath , pkg ) ) {
197+ const pkg = readPkgJson ( vitest . vitestPackageJsonPath )
198+ if ( ! pkg || ! validateVitestPkg ( showWarning , vitest . vitestPackageJsonPath , pkg ) ) {
201199 warned = true
202200 return
203201 }
@@ -230,7 +228,7 @@ export async function resolveVitestPackagesViaPackageJson(showWarning: boolean):
230228 }
231229}
232230
233- async function resolveVitestWorkspaceConfigs ( showWarning : boolean ) {
231+ async function resolveVitestWorkspaceConfigs ( ) {
234232 const config = getConfig ( )
235233 const userWorkspace = config . workspaceConfig
236234 const rootConfig = config . rootConfig
@@ -246,13 +244,15 @@ async function resolveVitestWorkspaceConfigs(showWarning: boolean) {
246244 ? [ vscode . Uri . file ( userWorkspace ) ]
247245 : await vscode . workspace . findFiles ( workspaceGlob , config . configSearchPatternExclude )
248246
249- let warned = false
250247 if ( vitestWorkspaces . length ) {
251248 // if there is a workspace config, use it as root
252249 const meta = resolvePackagUniquePrefixes ( vitestWorkspaces . map ( ( config ) => {
253- const vitest = resolveVitestConfig ( showWarning , config )
250+ const vitest = resolveVitestConfig ( /* don't show warnings for workspaces because they have limited support */ false , config )
254251 if ( ! vitest ) {
255- warned = true
252+ return null
253+ }
254+ // Version 4 doesn't support workspace files
255+ if ( gte ( vitest . version , '4.0.0' ) ) {
256256 return null
257257 }
258258 return {
@@ -264,12 +264,12 @@ async function resolveVitestWorkspaceConfigs(showWarning: boolean) {
264264
265265 return {
266266 meta,
267- warned,
267+ warned : false ,
268268 }
269269 }
270270 return {
271271 meta : [ ] ,
272- warned,
272+ warned : false ,
273273 }
274274}
275275
@@ -386,3 +386,17 @@ function resolvePackagUniquePrefixes(packages: VitestPackage[]) {
386386
387387 return packages
388388}
389+
390+ function readPkgJson ( path : string ) : null | {
391+ version : string
392+ dependencies ?: Record < string , string >
393+ devDependencies ?: Record < string , string >
394+ scripts ?: Record < string , string >
395+ } {
396+ try {
397+ return JSON . parse ( readFileSync ( path , 'utf-8' ) )
398+ }
399+ catch {
400+ return null
401+ }
402+ }
0 commit comments