Skip to content

Commit

Permalink
fix: reload configs mentioned in the workspace file
Browse files Browse the repository at this point in the history
FIxes #526
  • Loading branch information
sheremet-va committed Nov 15, 2024
1 parent cb48b51 commit 1b2c283
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export class VitestFolderAPI {
return this.meta.process
}

get configs() {
return this.meta.configs
}

get version() {
return this.pkg.version
}
Expand Down Expand Up @@ -261,6 +265,7 @@ export interface ResolvedMeta {
rpc: VitestRPC
process: VitestProcess
pkg: VitestPackage
configs: string[]
handlers: {
onConsoleLog: (listener: VitestEvents['onConsoleLog']) => void
onTaskUpdate: (listener: VitestEvents['onTaskUpdate']) => void
Expand Down
7 changes: 4 additions & 3 deletions src/api/child_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ async function createChildVitestProcess(pkg: VitestPackage) {
}
})

return new Promise<ChildProcess>((resolve, reject) => {
return new Promise<{ process: ChildProcess; configs: string[] }>((resolve, reject) => {
function onMessage(message: WorkerEvent) {
if (message.type === 'debug')
log.worker('info', ...message.args)

if (message.type === 'ready') {
resolve(vitest)
resolve({ process: vitest, configs: message.configs })
}
if (message.type === 'error') {
const error = new Error(`Vitest failed to start: \n${message.error}`)
Expand Down Expand Up @@ -126,7 +126,7 @@ async function createChildVitestProcess(pkg: VitestPackage) {
}

export async function createVitestProcess(pkg: VitestPackage): Promise<ResolvedMeta> {
const vitest = await createChildVitestProcess(pkg)
const { process: vitest, configs } = await createChildVitestProcess(pkg)

log.info('[API]', `${formatPkg(pkg)} child process ${vitest.pid} created`)

Expand All @@ -142,6 +142,7 @@ export async function createVitestProcess(pkg: VitestPackage): Promise<ResolvedM

return {
rpc: api,
configs,
process: new VitestChildProcess(vitest),
handlers,
pkg,
Expand Down
1 change: 1 addition & 0 deletions src/api/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function waitForWsResolvedMeta(
resolve({
rpc: api,
handlers,
configs: message.configs,
process: new VitestWebSocketProcess(Math.random(), wss, ws),
pkg,
})
Expand Down
12 changes: 8 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,21 @@ class VitestExtension {
return
// if new config is created, always check if it should be respected
if (event === 'create') {
this.defineTestProfiles(false)
this.defineTestProfiles(false).catch((err) => {
log.error('Failed to define test profiles after a new config file was created', err)
})
return
}
// otherwise ignore changes to unrelated configs
const filePath = normalize(uri.fsPath)
for (const api of this.api.folderAPIs) {
if (
api.package.configFile === filePath
|| api.package.workspaceFile === filePath
api.package.workspaceFile === filePath
|| api.configs.includes(filePath)
) {
this.defineTestProfiles(false)
this.defineTestProfiles(false).catch((err) => {
log.error('Failed to define test profiles after a new config file was updated', err)
})
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/worker/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ abstract class WorkerEventEmitter {
abstract on(event: string, listener: (...args: any[]) => void): void
abstract off(event: string, listener: (...args: any[]) => void): void

ready() {
this.sendWorkerEvent({ type: 'ready' })
ready(configs: string[]) {
this.sendWorkerEvent({ type: 'ready', configs })
}

error(err: any) {
Expand Down
2 changes: 2 additions & 0 deletions src/worker/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ export async function initVitest(meta: WorkerMeta, options?: UserConfig) {
},
)
reporter.init(vitest)
const configs = vitest.projects.map(p => p.server.config.configFile).filter(c => c != null)
return {
vitest,
reporter,
configs,
meta,
}
}
1 change: 1 addition & 0 deletions src/worker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface WorkerRunnerOptions {

export interface EventReady {
type: 'ready'
configs: string[]
}

export interface EventDebug {
Expand Down
4 changes: 2 additions & 2 deletions src/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ emitter.on('message', async function onMessage(message: any) {
const data = message as WorkerRunnerOptions

try {
const { reporter, vitest } = await initVitest(
const { reporter, vitest, configs } = await initVitest(
data.meta,
data.debug
? {
Expand All @@ -42,7 +42,7 @@ emitter.on('message', async function onMessage(message: any) {
deserialize: v => v8.deserialize(Buffer.from(v) as any),
})
reporter.initRpc(rpc)
emitter.ready()
emitter.ready(configs)
}
catch (err: any) {
emitter.error(err)
Expand Down

0 comments on commit 1b2c283

Please sign in to comment.