From 2659b4d6d71af5ccb276269b38b94e5bf91d0c72 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 2 Oct 2024 17:44:37 +0200 Subject: [PATCH] fix: use vscode stat API instead of Node.js --- src/watcher.ts | 24 ++++++++++++++++++------ src/worker/vitest.ts | 28 ++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/watcher.ts b/src/watcher.ts index c68777a..5b373db 100644 --- a/src/watcher.ts +++ b/src/watcher.ts @@ -1,4 +1,3 @@ -import { stat } from 'node:fs/promises' import { relative } from 'node:path' import * as vscode from 'vscode' import { normalize } from 'pathe' @@ -39,7 +38,7 @@ export class ExtensionWatcher extends vscode.Disposable { watcher.onDidChange(async (file) => { const filepath = normalize(file.fsPath) - if (await this.shouldIgnoreFile(filepath)) { + if (await this.shouldIgnoreFile(file)) { return } log.verbose?.('[VSCODE] File changed:', relative(api.workspaceFolder.uri.fsPath, file.fsPath)) @@ -48,7 +47,7 @@ export class ExtensionWatcher extends vscode.Disposable { watcher.onDidCreate(async (file) => { const filepath = normalize(file.fsPath) - if (await this.shouldIgnoreFile(filepath)) { + if (await this.shouldIgnoreFile(file)) { return } log.verbose?.('[VSCODE] File created:', relative(api.workspaceFolder.uri.fsPath, file.fsPath)) @@ -56,8 +55,21 @@ export class ExtensionWatcher extends vscode.Disposable { }) } - private async shouldIgnoreFile(filepath: string) { - const stats = await stat(filepath).catch(() => null) - return !stats || stats.isDirectory() || mm.isMatch(filepath, this.ignorePattern) + private async shouldIgnoreFile(file: vscode.Uri) { + try { + const stats = await vscode.workspace.fs.stat(file) + if ( + // if not a file + stats.type !== vscode.FileType.File + // if not a symlinked file + && stats.type !== (vscode.FileType.File | vscode.FileType.SymbolicLink) + ) { + return false + } + return mm.isMatch(file.fsPath, this.ignorePattern) + } + catch { + return false + } } } diff --git a/src/worker/vitest.ts b/src/worker/vitest.ts index 470781a..cf5066d 100644 --- a/src/worker/vitest.ts +++ b/src/worker/vitest.ts @@ -1,5 +1,7 @@ import { readFileSync } from 'node:fs' import type { Vitest as VitestCore, WorkspaceProject } from 'vitest/node' +import { relative } from 'pathe' +import mm from 'micromatch' import type { VitestMethods } from '../api/rpc' import { VitestWatcher } from './watcher' import { VitestCoverage } from './coverage' @@ -195,9 +197,13 @@ export class Vitest implements VitestMethods { for (const file of files) { this.updateLastChanged(file) - const content = readFileSync(file, 'utf-8') + let content: string | null = null for (const project of this.ctx.projects) { - if (await project.isTargetFile(file, content)) { + if (this.isTestFile( + project, + file, + () => content ?? (content = readFileSync(file, 'utf-8')), + )) { testFiles.push(file) project.testFilesList?.push(file) this.ctx.changedTests.add(file) @@ -212,6 +218,24 @@ export class Vitest implements VitestMethods { } } + isTestFile(project: WorkspaceProject, file: string, getContent: () => string) { + const relativeId = relative(project.config.dir || project.config.root, file) + if (mm.isMatch(relativeId, project.config.exclude)) { + return false + } + if (mm.isMatch(relativeId, project.config.include)) { + return true + } + if ( + project.config.includeSource?.length + && mm.isMatch(relativeId, project.config.includeSource) + ) { + const source = getContent() + return source.includes('import.meta.vitest') + } + return false + } + unwatchTests() { return this.watcher.stopTracking() } -- 2.51.2