Repository navigation
fix(nitro): resolve paths pointing at bare module specifiers - #36011
Conversation
@nuxt/kit
@nuxt/nitro-server
nuxt
@nuxt/rspack-builder
@nuxt/schema
@nuxt/vite-builder
@nuxt/webpack-builder
commit: |
WalkthroughNitro alias generation now checks whether an alias target exists relative to Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
packages/nuxt/test/load-nuxt.test.ts (1)
193-209: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick winAdd a regression case for an unresolved bare specifier.
The test covers only successful
defuresolution. It does not protect the required failure path. Add a deliberately missing alias and assert thattsConfigPathsretains the original module specifier.Suggested regression case
nuxt.options.nitro.alias ||= {} nuxt.options.nitro.alias['`#probe/defu`'] = 'defu' + nuxt.options.nitro.alias['`#probe/missing`'] = '__nuxt_missing_alias_probe__' ... expect(tsConfigPaths['`#probe/defu`']?.[0]?.replace(/\\/g, '/')).toMatch(/node_modules\/defu\//) + expect(tsConfigPaths['`#probe/missing`']).toEqual(['__nuxt_missing_alias_probe__'])Based on the PR objectives, the test should cover both successful resolution and preservation after resolution failure.
🤖 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/nuxt/test/load-nuxt.test.ts` around lines 193 - 209, Add a second Nitro alias in the existing `loadNuxt` regression test that points to a deliberately nonexistent bare module specifier, then assert the corresponding `tsConfigPaths` entry remains that original specifier when resolution fails. Keep the existing `defu` assertion to cover successful resolution, so the test validates both paths.
🤖 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/nitro-server/src/index.ts`:
- Around line 801-817: Update the alias resolution flow around resolveModulePath
so unresolved aliases retain aliases[alias]! as absolutePath instead of the
initial basePath-resolved value. Only assign resolvedModule to absolutePath
after its stat succeeds; otherwise fall back to the original module specifier
before populating tsConfig.compilerOptions.paths[alias].
In `@packages/nuxt/test/load-nuxt.test.ts`:
- Around line 193-211: Wrap the assertion in the nitro alias test with a
try/finally block so the Nuxt instance created by loadNuxt is always closed.
Keep the existing assertion unchanged in the try block and move await
nuxt.close() into finally.
---
Nitpick comments:
In `@packages/nuxt/test/load-nuxt.test.ts`:
- Around line 193-209: Add a second Nitro alias in the existing `loadNuxt`
regression test that points to a deliberately nonexistent bare module specifier,
then assert the corresponding `tsConfigPaths` entry remains that original
specifier when resolution fails. Keep the existing `defu` assertion to cover
successful resolution, so the test validates both paths.
🪄 Autofix
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 Plus
Run ID: bbe5c389-3583-4fd4-aceb-edecc556fd99
📒 Files selected for processing (2)
packages/nitro-server/src/index.tspackages/nuxt/test/load-nuxt.test.ts
| let absolutePath = resolve(basePath, aliases[alias]!) | ||
| let stats = await fsp.stat(absolutePath).catch(() => null /* file does not exist */) | ||
| if (!stats) { | ||
| const resolvedModule = resolveModulePath(aliases[alias]!, { | ||
| try: true, | ||
| from: importPaths, | ||
| extensions: [...nuxt.options.extensions, '.d.ts', '.d.mts', '.d.cts'], | ||
| }) | ||
| if (resolvedModule) { | ||
| absolutePath = resolvedModule | ||
| stats = await fsp.stat(resolvedModule).catch(() => null) | ||
| } | ||
| } | ||
|
|
||
| const isDirectory = aliases[alias]!.endsWith('/') || stats?.isDirectory() | ||
| // note - nitro will check + remove the file extension as required | ||
| tsConfig.compilerOptions.paths[alias] = [absolutePath] |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Preserve the original module specifier when resolution fails.
When resolveModulePath returns no result, absolutePath still contains resolve(basePath, aliases[alias]!). An alias such as #probe/missing: '__nuxt_missing_alias_probe__' is therefore emitted as a .nuxt path instead of the original module specifier. This can cause TS2307 or silently degrade imported types to any.
Set absolutePath to aliases[alias]! when no module resolves. Only use resolvedModule after its stat succeeds.
Suggested fallback
if (resolvedModule) {
- absolutePath = resolvedModule
- stats = await fsp.stat(resolvedModule).catch(() => null)
+ const resolvedStats = await fsp.stat(resolvedModule).catch(() => null)
+ if (resolvedStats) {
+ absolutePath = resolvedModule
+ stats = resolvedStats
+ }
+ }
+ if (!stats) {
+ absolutePath = aliases[alias]!
}Based on the PR objectives, unresolved aliases must retain their original module specifier.
📝 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.
| let absolutePath = resolve(basePath, aliases[alias]!) | |
| let stats = await fsp.stat(absolutePath).catch(() => null /* file does not exist */) | |
| if (!stats) { | |
| const resolvedModule = resolveModulePath(aliases[alias]!, { | |
| try: true, | |
| from: importPaths, | |
| extensions: [...nuxt.options.extensions, '.d.ts', '.d.mts', '.d.cts'], | |
| }) | |
| if (resolvedModule) { | |
| absolutePath = resolvedModule | |
| stats = await fsp.stat(resolvedModule).catch(() => null) | |
| } | |
| } | |
| const isDirectory = aliases[alias]!.endsWith('/') || stats?.isDirectory() | |
| // note - nitro will check + remove the file extension as required | |
| tsConfig.compilerOptions.paths[alias] = [absolutePath] | |
| let absolutePath = resolve(basePath, aliases[alias]!) | |
| let stats = await fsp.stat(absolutePath).catch(() => null /* file does not exist */) | |
| if (!stats) { | |
| const resolvedModule = resolveModulePath(aliases[alias]!, { | |
| try: true, | |
| from: importPaths, | |
| extensions: [...nuxt.options.extensions, '.d.ts', '.d.mts', '.d.cts'], | |
| }) | |
| if (resolvedModule) { | |
| const resolvedStats = await fsp.stat(resolvedModule).catch(() => null) | |
| if (resolvedStats) { | |
| absolutePath = resolvedModule | |
| stats = resolvedStats | |
| } | |
| } | |
| if (!stats) { | |
| absolutePath = aliases[alias]! | |
| } | |
| } | |
| const isDirectory = aliases[alias]!.endsWith('/') || stats?.isDirectory() | |
| // note - nitro will check + remove the file extension as required | |
| tsConfig.compilerOptions.paths[alias] = [absolutePath] |
🤖 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/nitro-server/src/index.ts` around lines 801 - 817, Update the alias
resolution flow around resolveModulePath so unresolved aliases retain
aliases[alias]! as absolutePath instead of the initial basePath-resolved value.
Only assign resolvedModule to absolutePath after its stat succeeds; otherwise
fall back to the original module specifier before populating
tsConfig.compilerOptions.paths[alias].
| it('resolves nitro aliases pointing at bare module specifiers', async () => { | ||
| const nuxt = await loadNuxt({ | ||
| cwd: repoRoot, | ||
| ready: true, | ||
| overrides: { | ||
| modules: [ | ||
| (_options, nuxt) => { | ||
| nuxt.options.nitro.alias ||= {} | ||
| nuxt.options.nitro.alias['#probe/defu'] = 'defu' | ||
| }, | ||
| ], | ||
| }, | ||
| }) | ||
|
|
||
| const tsConfigPaths = (nuxt as any)._nitro?.options.typescript?.tsConfig?.compilerOptions?.paths ?? {} | ||
|
|
||
| expect(tsConfigPaths['#probe/defu']?.[0]?.replace(/\\/g, '/')).toMatch(/node_modules\/defu\//) | ||
|
|
||
| await nuxt.close() |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Close the Nuxt instance in a finally block.
await nuxt.close() runs only if the assertion succeeds. If the assertion fails, the ready: true load can leave resources open and affect later tests. Wrap the assertion in try/finally.
Suggested cleanup
- const tsConfigPaths = (nuxt as any)._nitro?.options.typescript?.tsConfig?.compilerOptions?.paths ?? {}
-
- expect(tsConfigPaths['`#probe/defu`']?.[0]?.replace(/\\/g, '/')).toMatch(/node_modules\/defu\//)
-
- await nuxt.close()
+ try {
+ const tsConfigPaths = (nuxt as any)._nitro?.options.typescript?.tsConfig?.compilerOptions?.paths ?? {}
+ expect(tsConfigPaths['`#probe/defu`']?.[0]?.replace(/\\/g, '/')).toMatch(/node_modules\/defu\//)
+ } finally {
+ await nuxt.close()
+ }📝 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.
| it('resolves nitro aliases pointing at bare module specifiers', async () => { | |
| const nuxt = await loadNuxt({ | |
| cwd: repoRoot, | |
| ready: true, | |
| overrides: { | |
| modules: [ | |
| (_options, nuxt) => { | |
| nuxt.options.nitro.alias ||= {} | |
| nuxt.options.nitro.alias['#probe/defu'] = 'defu' | |
| }, | |
| ], | |
| }, | |
| }) | |
| const tsConfigPaths = (nuxt as any)._nitro?.options.typescript?.tsConfig?.compilerOptions?.paths ?? {} | |
| expect(tsConfigPaths['#probe/defu']?.[0]?.replace(/\\/g, '/')).toMatch(/node_modules\/defu\//) | |
| await nuxt.close() | |
| it('resolves nitro aliases pointing at bare module specifiers', async () => { | |
| const nuxt = await loadNuxt({ | |
| cwd: repoRoot, | |
| ready: true, | |
| overrides: { | |
| modules: [ | |
| (_options, nuxt) => { | |
| nuxt.options.nitro.alias ||= {} | |
| nuxt.options.nitro.alias['`#probe/defu`'] = 'defu' | |
| }, | |
| ], | |
| }, | |
| }) | |
| try { | |
| const tsConfigPaths = (nuxt as any)._nitro?.options.typescript?.tsConfig?.compilerOptions?.paths ?? {} | |
| expect(tsConfigPaths['`#probe/defu`']?.[0]?.replace(/\\/g, '/')).toMatch(/node_modules\/defu\//) | |
| } finally { | |
| await nuxt.close() | |
| } |
🤖 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/nuxt/test/load-nuxt.test.ts` around lines 193 - 211, Wrap the
assertion in the nitro alias test with a try/finally block so the Nuxt instance
created by loadNuxt is always closed. Keep the existing assertion unchanged in
the try block and move await nuxt.close() into finally.
Merging this PR will not alter performance
Comparing Footnotes
|
🔗 Linked issue
resolves #36003
📚 Description
this applies similar handling to the handling we do in
@nuxt/kitfor writing templates