Skip to content

Commit

Permalink
fix(optimizer): don not call context.rebuild after cancel (#12264)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
  • Loading branch information
patak-dev and bluwy committed Mar 2, 2023
1 parent 22de84f commit 520d84e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
25 changes: 20 additions & 5 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ export function runOptimizeDeps(
cancel: () => Promise<void>
result: Promise<DepOptimizationResult>
} {
const optimizerContext = { cancelled: false }

const config: ResolvedConfig = {
...resolvedConfig,
command: 'build',
Expand Down Expand Up @@ -536,9 +538,20 @@ export function runOptimizeDeps(
depsInfo,
ssr,
processingCacheDir,
optimizerContext,
)

const result = preparedRun.then(({ context, idToExports }) => {
function disposeContext() {
return context?.dispose().catch((e) => {
config.logger.error('Failed to dispose esbuild context', { error: e })
})
}
if (!context || optimizerContext.cancelled) {
disposeContext()
return createProcessingResult()
}

return context
.rebuild()
.then((result) => {
Expand Down Expand Up @@ -613,9 +626,7 @@ export function runOptimizeDeps(
return createProcessingResult()
})
.finally(() => {
return context.dispose().catch((e) => {
config.logger.error('error happed during context.dispose', e)
})
return disposeContext()
})
})

Expand All @@ -625,8 +636,9 @@ export function runOptimizeDeps(

return {
async cancel() {
optimizerContext.cancelled = true
const { context } = await preparedRun
await context.cancel()
await context?.cancel()
cleanUp()
},
result,
Expand All @@ -638,8 +650,9 @@ async function prepareEsbuildOptimizerRun(
depsInfo: Record<string, OptimizedDepInfo>,
ssr: boolean,
processingCacheDir: string,
optimizerContext: { cancelled: boolean },
): Promise<{
context: BuildContext
context?: BuildContext
idToExports: Record<string, ExportsData>
}> {
const isBuild = resolvedConfig.command === 'build'
Expand Down Expand Up @@ -681,6 +694,8 @@ async function prepareEsbuildOptimizerRun(
flatIdToExports[flatId] = exportsData
}

if (optimizerContext.cancelled) return { context: undefined, idToExports }

// esbuild automatically replaces process.env.NODE_ENV for platform 'browser'
// In lib mode, we need to keep process.env.NODE_ENV untouched, so to at build
// time we replace it by __vite_process_env_NODE_ENV. This placeholder will be
Expand Down
28 changes: 21 additions & 7 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export function scanImports(config: ResolvedConfig): {
const missing: Record<string, string> = {}
let entries: string[]

const scanContext = { cancelled: false }

const esbuildContext: Promise<BuildContext | undefined> = computeEntries(
config,
).then((computedEntries) => {
Expand All @@ -78,14 +80,21 @@ export function scanImports(config: ResolvedConfig): {
}
return
}
if (scanContext.cancelled) return

debug(`Crawling dependencies using entries:\n ${entries.join('\n ')}`)
return prepareEsbuildScanner(config, entries, deps, missing)
return prepareEsbuildScanner(config, entries, deps, missing, scanContext)
})

const result = esbuildContext
.then((context) => {
if (!context) {
function disposeContext() {
return context?.dispose().catch((e) => {
config.logger.error('Failed to dispose esbuild context', { error: e })
})
}
if (!context || scanContext?.cancelled) {
disposeContext()
return { deps: {}, missing: {} }
}
return context
Expand All @@ -98,9 +107,7 @@ export function scanImports(config: ResolvedConfig): {
}
})
.finally(() => {
return context.dispose().catch((e) => {
config.logger.error('error happed during context.dispose', e)
})
return disposeContext()
})
})
.catch(async (e) => {
Expand Down Expand Up @@ -128,7 +135,10 @@ export function scanImports(config: ResolvedConfig): {
})

return {
cancel: () => esbuildContext.then((context) => context?.cancel()),
cancel: async () => {
scanContext.cancelled = true
return esbuildContext.then((context) => context?.cancel())
},
result,
}
}
Expand Down Expand Up @@ -170,8 +180,12 @@ async function prepareEsbuildScanner(
entries: string[],
deps: Record<string, string>,
missing: Record<string, string>,
) {
scanContext?: { cancelled: boolean },
): Promise<BuildContext | undefined> {
const container = await createPluginContainer(config)

if (scanContext?.cancelled) return

const plugin = esbuildScanPlugin(config, container, deps, missing, entries)

const { plugins = [], ...esbuildOptions } =
Expand Down

0 comments on commit 520d84e

Please sign in to comment.