Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite-node): circular import stuck #3480

Merged
merged 9 commits into from Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/mocks/src/main.js
@@ -1,5 +1,10 @@
import { funcA } from './A'
import { funcB } from './B'

export function main() {
return funcA()
}

export function mainB() {
return funcB()
}
11 changes: 10 additions & 1 deletion examples/mocks/test/circular.spec.ts
@@ -1,18 +1,27 @@
import { expect, test, vi } from 'vitest'
import { main } from '../src/main.js'
import { main, mainB } from '../src/main.js'
import x from '../src/export-default-circle-index'

vi.mock('../src/A', async () => ({
...(await vi.importActual<any>('../src/A')),
funcA: () => 'mockedA',
}))

vi.mock('../src/B', async () => ({
...(await vi.importActual<any>('../src/B')),
funcB: () => 'mockedB',
}))

vi.mock('../src/export-default-circle-b')

test('can import actual inside mock factory', () => {
expect(main()).toBe('mockedA')
})

test('can import in top level and inside mock factory', () => {
expect(mainB()).toBe('mockedB')
})

test('can mock a circular dependency', () => {
expect(x()).toBe(undefined)
})
12 changes: 9 additions & 3 deletions packages/vite-node/src/client.ts
Expand Up @@ -186,8 +186,7 @@ export class ViteNodeRunner {

const mod = this.moduleCache.get(fsPath)

if (!mod.importers)
mod.importers = new Set()
mod.importers ??= new Set()
if (importee)
mod.importers.add(importee)

Expand All @@ -205,8 +204,11 @@ export class ViteNodeRunner {

try {
// cached module
if (mod.promise)
if (mod.promise) {
if (Array.from(mod.imports?.values() || []).some(i => mod.importers!.has(i)))
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
return mod.exports
return await mod.promise
}

const promise = this.directRequest(id, fsPath, callstack)
Object.assign(mod, { promise, evaluated: false })
Expand Down Expand Up @@ -269,6 +271,10 @@ export class ViteNodeRunner {

const request = async (dep: string) => {
const [id, depFsPath] = await this.resolveUrl(`${dep}`, fsPath)
const depMod = this.moduleCache.getByModuleId(depFsPath)
;(depMod.importers ??= new Set()).add(moduleId)
;(mod.imports ??= new Set()).add(depFsPath)
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved

return this.dependencyRequest(id, depFsPath, callstack)
}

Expand Down
1 change: 1 addition & 0 deletions packages/vite-node/src/types.ts
Expand Up @@ -66,6 +66,7 @@ export interface ModuleCache {
* Module ids that imports this module
*/
importers?: Set<string>
imports?: Set<string>
}

export interface ViteNodeRunnerOptions {
Expand Down