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
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6c96857
Revert "revert: "fix(vite-node): circular import stuck" #3418 (#3479)"
Dunqing 9616e32
fix(vite-node): circular import stuck
Dunqing e7f6d96
feat: new Set automatically when set module
Dunqing 90ef4c4
feat: simply check circular
Dunqing 3ce86a4
test: fix ci
Dunqing 0acdd55
fix: should clear imports
Dunqing 61d3414
feat: only exists in get
Dunqing a5f7282
ci: empty
Dunqing 1df3371
fix: get failed
Dunqing File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { funcA } from './A' | ||
import { funcB } from './B' | ||
|
||
export function main() { | ||
return funcA() | ||
} | ||
|
||
export function mainB() { | ||
return funcB() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }) | ||
} | ||
|
||
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)! | ||
} | ||
|
||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imports should also be cleared now |
||
return true | ||
} | ||
|
||
|
@@ -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) | ||
|
@@ -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 | ||
} | ||
|
@@ -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) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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() }
?There was a problem hiding this comment.
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