Skip to content

Commit

Permalink
Avoid race condition when emitting implicit entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 20, 2023
1 parent aa5a27b commit 265836d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
3 changes: 1 addition & 2 deletions browser/src/parseAstAsync.ts
Expand Up @@ -2,8 +2,7 @@ import type { AstNode, ParseAst } from '../../src/rollup/types';
import { parseAst } from '../../src/utils/parseAst';

// We could also do things in a web worker here, but that would make bundling
// the
// browser build much more complicated
// the browser build much more complicated
const parseAstAsync = async (
code: Parameters<ParseAst>[0],
options?: Parameters<ParseAst>[1]
Expand Down
20 changes: 14 additions & 6 deletions src/ModuleLoader.ts
Expand Up @@ -245,17 +245,23 @@ export class ModuleLoader {
async entryModule => {
addChunkNamesToModule(entryModule, unresolvedModule, false, chunkNamePriority);
if (!entryModule.info.isEntry) {
this.implicitEntryModules.add(entryModule);
const implicitlyLoadedAfterModules = await Promise.all(
implicitlyLoadedAfter.map(id =>
this.loadEntryModule(id, false, unresolvedModule.importer, entryModule.id)
)
);
for (const module of implicitlyLoadedAfterModules) {
entryModule.implicitlyLoadedAfter.add(module);
}
for (const dependant of entryModule.implicitlyLoadedAfter) {
dependant.implicitlyLoadedBefore.add(entryModule);
// We need to check again if this is still an entry module as these
// changes need to be performed atomically to avoid race conditions
// if the same module is re-emitted as an entry module.
// The inverse changes happen in "handleExistingModule"
if (!entryModule.info.isEntry) {
this.implicitEntryModules.add(entryModule);
for (const module of implicitlyLoadedAfterModules) {
entryModule.implicitlyLoadedAfter.add(module);
}
for (const dependant of entryModule.implicitlyLoadedAfter) {
dependant.implicitlyLoadedBefore.add(entryModule);
}
}
}
return entryModule;
Expand Down Expand Up @@ -615,6 +621,8 @@ export class ModuleLoader {
: loadPromise;
}
if (isEntry) {
// This reverts the changes in addEntryWithImplicitDependants and needs to
// be performed atomically
module.info.isEntry = true;
this.implicitEntryModules.delete(module);
for (const dependant of module.implicitlyLoadedAfter) {
Expand Down

0 comments on commit 265836d

Please sign in to comment.