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 5 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)
})
25 changes: 14 additions & 11 deletions packages/vite-node/src/client.ts
Expand Up @@ -61,23 +61,23 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
update(fsPath: string, mod: Partial<ModuleCache>) {
fsPath = this.normalizePath(fsPath)
if (!super.has(fsPath))
super.set(fsPath, mod)
this.setByModuleId(fsPath, mod)
else
Object.assign(super.get(fsPath) as ModuleCache, mod)
return this
}

setByModuleId(modulePath: string, mod: ModuleCache) {
return super.set(modulePath, mod)
setByModuleId(modulePath: string, mod: Partial<ModuleCache>) {
return super.set(modulePath, { imports: new Set(), importers: new Set(), ...mod })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not create a new object here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of .set({}) inside of .get() we can just provide { imports: new Set(), importers: new Set() }?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I will change this

}

set(fsPath: string, mod: ModuleCache) {
set(fsPath: string, mod: Partial<ModuleCache>) {
return this.setByModuleId(this.normalizePath(fsPath), mod)
}

getByModuleId(modulePath: string): ModuleCache {
if (!super.has(modulePath))
super.set(modulePath, {})
this.setByModuleId(modulePath, {})
return super.get(modulePath)!
}

Expand All @@ -98,7 +98,7 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
delete mod.resolving
delete mod.promise
delete mod.exports
mod.importers?.clear()
mod.importers.clear()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imports should also be cleared now

return true
}

Expand Down Expand Up @@ -129,7 +129,7 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
continue
invalidated.add(id)
const subIds = Array.from(super.entries())
.filter(([,mod]) => mod.importers?.has(id))
.filter(([,mod]) => mod.importers.has(id))
.map(([key]) => key)
subIds.length && this.invalidateSubDepTree(subIds, invalidated)
super.delete(id)
Expand Down Expand Up @@ -185,16 +185,15 @@ export class ViteNodeRunner {
const importee = callstack[callstack.length - 1]

const mod = this.moduleCache.get(fsPath)
const { imports, importers } = mod

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

const getStack = () => `stack:\n${[...callstack, fsPath].reverse().map(p => ` - ${p}`).join('\n')}`

// check circular dependency
if (callstack.includes(fsPath) || callstack.some(c => this.moduleCache.get(c).importers?.has(fsPath))) {
if (callstack.includes(fsPath) || Array.from(imports.values()).some(i => importers.has(i))) {
if (mod.exports)
return mod.exports
}
Expand Down Expand Up @@ -269,6 +268,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.add(moduleId)
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
mod.imports.add(depFsPath)

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

Expand Down
3 changes: 2 additions & 1 deletion packages/vite-node/src/types.ts
Expand Up @@ -65,7 +65,8 @@ export interface ModuleCache {
/**
* Module ids that imports this module
*/
importers?: Set<string>
importers: Set<string>
imports: Set<string>
}

export interface ViteNodeRunnerOptions {
Expand Down