-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
fix(optimizer): show error when computeEntries failed
#20079
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,17 +123,17 @@ export function scanImports(environment: ScanEnvironment): { | |
| }> | ||
| } { | ||
| const start = performance.now() | ||
| const deps: Record<string, string> = {} | ||
| const missing: Record<string, string> = {} | ||
| let entries: string[] | ||
|
|
||
| const { config } = environment | ||
|
|
||
| const scanContext = { cancelled: false } | ||
| const esbuildContext: Promise<BuildContext | undefined> = computeEntries( | ||
| environment, | ||
| ).then((computedEntries) => { | ||
| entries = computedEntries | ||
| let esbuildContext: Promise<BuildContext | undefined> | undefined | ||
| async function cancel() { | ||
| scanContext.cancelled = true | ||
| return esbuildContext?.then((context) => context?.cancel()) | ||
| } | ||
|
|
||
| async function scan() { | ||
| const entries = await computeEntries(environment) | ||
| if (!entries.length) { | ||
| if (!config.optimizeDeps.entries && !config.optimizeDeps.include) { | ||
| environment.logger.warn( | ||
|
|
@@ -153,82 +153,73 @@ export function scanImports(environment: ScanEnvironment): { | |
| .map((entry) => `\n ${colors.dim(entry)}`) | ||
| .join('')}`, | ||
| ) | ||
| return prepareEsbuildScanner( | ||
| environment, | ||
| entries, | ||
| deps, | ||
| missing, | ||
| scanContext, | ||
| ) | ||
| }) | ||
| const deps: Record<string, string> = {} | ||
| const missing: Record<string, string> = {} | ||
|
|
||
| let context: BuildContext | undefined | ||
| try { | ||
| esbuildContext = prepareEsbuildScanner( | ||
| environment, | ||
| entries, | ||
| deps, | ||
| missing, | ||
| ) | ||
| context = await esbuildContext | ||
| if (scanContext.cancelled) return | ||
|
|
||
| const result = esbuildContext | ||
| .then((context) => { | ||
| function disposeContext() { | ||
| return context?.dispose().catch((e) => { | ||
| environment.logger.error('Failed to dispose esbuild context', { | ||
| error: e, | ||
| }) | ||
| }) | ||
| } | ||
| if (!context || scanContext.cancelled) { | ||
| disposeContext() | ||
| return { deps: {}, missing: {} } | ||
| } | ||
| return context | ||
| .rebuild() | ||
| .then(() => { | ||
| return { | ||
| // Ensure a fixed order so hashes are stable and improve logs | ||
| deps: orderedDependencies(deps), | ||
| missing, | ||
| } | ||
| }) | ||
| .finally(() => { | ||
| return disposeContext() | ||
| }) | ||
| }) | ||
| .catch(async (e) => { | ||
| if (e.errors && e.message.includes('The build was canceled')) { | ||
| // esbuild logs an error when cancelling, but this is expected so | ||
| // return an empty result instead | ||
| return { deps: {}, missing: {} } | ||
| } | ||
| try { | ||
| await context!.rebuild() | ||
| return { | ||
| // Ensure a fixed order so hashes are stable and improve logs | ||
| deps: orderedDependencies(deps), | ||
| missing, | ||
| } | ||
| } catch (e) { | ||
| if (e.errors && e.message.includes('The build was canceled')) { | ||
| // esbuild logs an error when cancelling, but this is expected so | ||
| // return an empty result instead | ||
| return { deps: {}, missing: {} } | ||
|
||
| } | ||
|
|
||
| const prependMessage = colors.red(`\ | ||
| const prependMessage = colors.red(`\ | ||
| Failed to scan for dependencies from entries: | ||
| ${entries.join('\n')} | ||
|
Member
Author
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. This is the place the The old code was not clear which variable lives at which part of the code. TypeScript is not good with analyzing code flow that uses promise + then + temp variable. |
||
|
|
||
| `) | ||
| if (e.errors) { | ||
| const msgs = await formatMessages(e.errors, { | ||
| kind: 'error', | ||
| color: true, | ||
| }) | ||
| e.message = prependMessage + msgs.join('\n') | ||
| } else { | ||
| e.message = prependMessage + e.message | ||
| } | ||
| throw e | ||
| }) | ||
| .finally(() => { | ||
| if (debug) { | ||
| const duration = (performance.now() - start).toFixed(2) | ||
| const depsStr = | ||
| Object.keys(orderedDependencies(deps)) | ||
| .sort() | ||
| .map((id) => `\n ${colors.cyan(id)} -> ${colors.dim(deps[id])}`) | ||
| .join('') || colors.dim('no dependencies found') | ||
| debug(`Scan completed in ${duration}ms: ${depsStr}`) | ||
| if (e.errors) { | ||
| const msgs = await formatMessages(e.errors, { | ||
| kind: 'error', | ||
| color: true, | ||
| }) | ||
| e.message = prependMessage + msgs.join('\n') | ||
| } else { | ||
| e.message = prependMessage + e.message | ||
| } | ||
| throw e | ||
| } finally { | ||
| if (debug) { | ||
| const duration = (performance.now() - start).toFixed(2) | ||
| const depsStr = | ||
| Object.keys(orderedDependencies(deps)) | ||
| .sort() | ||
| .map((id) => `\n ${colors.cyan(id)} -> ${colors.dim(deps[id])}`) | ||
| .join('') || colors.dim('no dependencies found') | ||
| debug(`Scan completed in ${duration}ms: ${depsStr}`) | ||
| } | ||
| } | ||
| }) | ||
| } finally { | ||
| context?.dispose().catch((e) => { | ||
| environment.logger.error('Failed to dispose esbuild context', { | ||
| error: e, | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
| const result = scan() | ||
|
|
||
| return { | ||
| cancel: async () => { | ||
| scanContext.cancelled = true | ||
| return esbuildContext.then((context) => context?.cancel()) | ||
| }, | ||
| result, | ||
| cancel, | ||
| result: result.then((res) => res ?? { deps: {}, missing: {} }), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -283,10 +274,7 @@ async function prepareEsbuildScanner( | |
| entries: string[], | ||
| deps: Record<string, string>, | ||
| missing: Record<string, string>, | ||
| scanContext: { cancelled: boolean }, | ||
| ): Promise<BuildContext | undefined> { | ||
| if (scanContext.cancelled) return | ||
|
|
||
| ): Promise<BuildContext> { | ||
| const plugin = esbuildScanPlugin(environment, deps, missing, entries) | ||
|
|
||
| const { plugins = [], ...esbuildOptions } = | ||
|
|
||
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.
This part of the change just adds some context to the error. It looks like
