-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(nuxt): Detect Nitro version via the app's Nuxt dependency chain #24019
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
Merged
+133
−10
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import * as fs from 'fs'; | ||
| import * as os from 'os'; | ||
| import * as path from 'path'; | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
| import { getNitroMajorVersion } from '../../src/vite/utils'; | ||
|
|
||
| // Real filesystem fixtures instead of mocks: the bug this guards against lives in | ||
| // module resolution walking up the directory tree, which mocks cannot reproduce. | ||
| let monorepoRoot: string; | ||
|
|
||
| function writePackage(dir: string, packageJson: Record<string, unknown>): void { | ||
| fs.mkdirSync(dir, { recursive: true }); | ||
| fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({ main: 'index.js', ...packageJson })); | ||
| fs.writeFileSync(path.join(dir, 'index.js'), ''); | ||
| } | ||
|
|
||
| function createApp(appName: string, packages: Record<string, Record<string, unknown>>): string { | ||
| const appDir = path.join(monorepoRoot, 'apps', appName); | ||
| fs.mkdirSync(appDir, { recursive: true }); | ||
| for (const [name, packageJson] of Object.entries(packages)) { | ||
| writePackage(path.join(appDir, 'node_modules', name), packageJson); | ||
| } | ||
| return appDir; | ||
| } | ||
|
|
||
| beforeAll(() => { | ||
| monorepoRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'sentry-nitro-version-')); | ||
| // An unrelated Nitro v3 above the apps, like a monorepo root devDependency | ||
| writePackage(path.join(monorepoRoot, 'node_modules', 'nitro'), { name: 'nitro', version: '3.0.0-beta.1' }); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| fs.rmSync(monorepoRoot, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe('getNitroMajorVersion', () => { | ||
| it('detects v2 when nuxt depends on nitropack directly (Nuxt 3 / <=4.1), ignoring a nitro v3 higher up the tree', async () => { | ||
| const appDir = createApp('nuxt-4-old', { | ||
| nuxt: { name: 'nuxt', version: '4.1.0', dependencies: { nitropack: '^2.12.0' } }, | ||
| nitropack: { name: 'nitropack', version: '2.12.0' }, | ||
| }); | ||
|
|
||
| await expect(getNitroMajorVersion(appDir)).resolves.toBe(2); | ||
| }); | ||
|
|
||
| it('detects v2 through @nuxt/nitro-server when it depends on nitropack (Nuxt >=3.21 stable)', async () => { | ||
| const appDir = createApp('nuxt-4-stable', { | ||
| nuxt: { name: 'nuxt', version: '4.5.2', dependencies: { '@nuxt/nitro-server': '4.5.2' } }, | ||
| '@nuxt/nitro-server': { name: '@nuxt/nitro-server', version: '4.5.2', dependencies: { nitropack: '^2.13.4' } }, | ||
| nitropack: { name: 'nitropack', version: '2.13.4' }, | ||
| }); | ||
|
|
||
| await expect(getNitroMajorVersion(appDir)).resolves.toBe(2); | ||
| }); | ||
|
|
||
| it('detects v3 through @nuxt/nitro-server when it depends on nitro (Nuxt 5)', async () => { | ||
| const appDir = createApp('nuxt-5', { | ||
| nuxt: { | ||
| name: 'nuxt', | ||
| version: '5.0.0', | ||
| dependencies: { '@nuxt/nitro-server': 'npm:@nuxt/nitro-server-nightly' }, | ||
| }, | ||
| '@nuxt/nitro-server': { | ||
| name: '@nuxt/nitro-server-nightly', | ||
| version: '5.0.0-nightly', | ||
| dependencies: { nitro: '^3.0.0-beta' }, | ||
| }, | ||
| nitro: { name: 'nitro', version: '3.0.0-beta.2' }, | ||
| }); | ||
|
|
||
| await expect(getNitroMajorVersion(appDir)).resolves.toBe(3); | ||
| }); | ||
|
|
||
| it('detects v3 when nuxt depends on nitro directly, without @nuxt/nitro-server', async () => { | ||
| const appDir = createApp('nuxt-direct-nitro', { | ||
| nuxt: { name: 'nuxt', version: '5.1.0', dependencies: { nitro: '^3.1.0' } }, | ||
| nitro: { name: 'nitro', version: '3.1.0' }, | ||
| }); | ||
|
|
||
| await expect(getNitroMajorVersion(appDir)).resolves.toBe(3); | ||
| }); | ||
|
|
||
| it('falls back to v3 when the declared nitro package has no readable version (stub package)', async () => { | ||
| const appDir = createApp('nuxt-5-stub', { | ||
| nuxt: { | ||
| name: 'nuxt', | ||
| version: '5.0.0', | ||
| dependencies: { '@nuxt/nitro-server': 'npm:@nuxt/nitro-server-nightly' }, | ||
| }, | ||
| '@nuxt/nitro-server': { | ||
| name: '@nuxt/nitro-server-nightly', | ||
| version: '5.0.0-nightly', | ||
| dependencies: { nitro: '^3.0.0-beta' }, | ||
| }, | ||
| nitro: { name: 'nitro' }, | ||
| }); | ||
|
|
||
| await expect(getNitroMajorVersion(appDir)).resolves.toBe(3); | ||
| }); | ||
|
|
||
| it('defaults to v2 when nuxt cannot be resolved', async () => { | ||
| const appDir = path.join(monorepoRoot, 'apps', 'no-nuxt'); | ||
| fs.mkdirSync(appDir, { recursive: true }); | ||
|
|
||
| await expect(getNitroMajorVersion(appDir)).resolves.toBe(2); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.