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(optimizer): don not call context.rebuild after cancel #12264

Merged
merged 3 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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('error happed during context.dispose', e)
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
})
}
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('error happed during context.dispose', e)
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
})
}
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