Repository navigation
fix(kit,nuxt): pass error as cause when we re-throw - #35387
Conversation
@nuxt/kit
@nuxt/nitro-server
nuxt
@nuxt/rspack-builder
@nuxt/schema
@nuxt/vite-builder
@nuxt/webpack-builder
commit: |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/kit/src/module/install.ts`:
- Around line 346-352: In the module installation error handling block starting
at line 346, when the code matches MODULE_NOT_FOUND or ERR_MODULE_NOT_FOUND, the
nested condition on line 350 only throws if the module is found and doesn't
match nuxtModule. However, if this condition is false (module is undefined or
module includes nuxtModule), execution falls through without throwing, losing
the original error context. Add a fallthrough throw statement after the
conditional block that throws an error with the cause property set to the
original error to ensure the error context is preserved in all MODULE_NOT_FOUND
paths.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 3a1bfb28-ab90-4c1f-a836-2c7472d53d25
📒 Files selected for processing (3)
packages/kit/src/module/define.tspackages/kit/src/module/install.tspackages/nuxt/src/app/components/nuxt-island.ts
| if (code === 'MODULE_NOT_FOUND' || code === 'ERR_MODULE_NOT_FOUND') { | ||
| const module = MissingModuleMatcher.exec((error as Error).message)?.[1] | ||
| // verify that it's missing the nuxt module otherwise it may be a sub dependency of the module itself | ||
| // i.e. module is importing a module that is missing | ||
| if (module && !module.includes(nuxtModule as string)) { | ||
| throw new TypeError(`Error while importing module \`${nuxtModule}\`: ${error}`) | ||
| throw new TypeError(`Error while importing module \`${nuxtModule}\`: ${error}`, { cause: error }) | ||
| } |
There was a problem hiding this comment.
Preserve cause in the MODULE_NOT_FOUND fallthrough path.
When Line 346 matches and the Line 350 condition is false, execution falls through to the generic throw without cause, so the original import error context is still lost.
Suggested patch
} catch (error: unknown) {
const code = (error as Error & { code?: string }).code
if (code === 'ERR_PACKAGE_PATH_NOT_EXPORTED' || code === 'ERR_UNSUPPORTED_DIR_IMPORT' || code === 'ENOTDIR') {
throw new TypeError(`Could not load \`${nuxtModule}\`. Is it installed?`, { cause: error })
}
if (code === 'MODULE_NOT_FOUND' || code === 'ERR_MODULE_NOT_FOUND') {
const module = MissingModuleMatcher.exec((error as Error).message)?.[1]
// verify that it's missing the nuxt module otherwise it may be a sub dependency of the module itself
// i.e. module is importing a module that is missing
if (module && !module.includes(nuxtModule as string)) {
throw new TypeError(`Error while importing module \`${nuxtModule}\`: ${error}`, { cause: error })
}
}
+ throw new TypeError(`Could not load \`${nuxtModule}\`. Is it installed?`, { cause: error })
}
-
- throw new TypeError(`Could not load \`${nuxtModule}\`. Is it installed?`)
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (code === 'MODULE_NOT_FOUND' || code === 'ERR_MODULE_NOT_FOUND') { | |
| const module = MissingModuleMatcher.exec((error as Error).message)?.[1] | |
| // verify that it's missing the nuxt module otherwise it may be a sub dependency of the module itself | |
| // i.e. module is importing a module that is missing | |
| if (module && !module.includes(nuxtModule as string)) { | |
| throw new TypeError(`Error while importing module \`${nuxtModule}\`: ${error}`) | |
| throw new TypeError(`Error while importing module \`${nuxtModule}\`: ${error}`, { cause: error }) | |
| } | |
| } catch (error: unknown) { | |
| const code = (error as Error & { code?: string }).code | |
| if (code === 'ERR_PACKAGE_PATH_NOT_EXPORTED' || code === 'ERR_UNSUPPORTED_DIR_IMPORT' || code === 'ENOTDIR') { | |
| throw new TypeError(`Could not load \`${nuxtModule}\`. Is it installed?`, { cause: error }) | |
| } | |
| if (code === 'MODULE_NOT_FOUND' || code === 'ERR_MODULE_NOT_FOUND') { | |
| const module = MissingModuleMatcher.exec((error as Error).message)?.[1] | |
| // verify that it's missing the nuxt module otherwise it may be a sub dependency of the module itself | |
| // i.e. module is importing a module that is missing | |
| if (module && !module.includes(nuxtModule as string)) { | |
| throw new TypeError(`Error while importing module \`${nuxtModule}\`: ${error}`, { cause: error }) | |
| } | |
| } | |
| throw new TypeError(`Could not load \`${nuxtModule}\`. Is it installed?`, { cause: error }) | |
| } |
🧰 Tools
🪛 OpenGrep (1.22.0)
[ERROR] 347-347: Dynamic command passed to child_process.exec/execSync. Use child_process.execFile or spawn with an argument array instead.
(coderabbit.command-injection.exec-js)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/kit/src/module/install.ts` around lines 346 - 352, In the module
installation error handling block starting at line 346, when the code matches
MODULE_NOT_FOUND or ERR_MODULE_NOT_FOUND, the nested condition on line 350 only
throws if the module is found and doesn't match nuxtModule. However, if this
condition is false (module is undefined or module includes nuxtModule),
execution falls through without throwing, losing the original error context. Add
a fallthrough throw statement after the conditional block that throws an error
with the cause property set to the original error to ensure the error context is
preserved in all MODULE_NOT_FOUND paths.
Merging this PR will degrade performance by 23.78%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ❌ | loadNuxtConfig in the basic test fixture (types) |
16.8 ms | 22.1 ms | -23.78% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing fix/lint-errors (989081e) with main (886e12f)
Footnotes
-
3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
(cherry picked from commit 549949f)
🔗 Linked issue
📚 Description
flagged up in #35369 in eslint config improvements