fix(kit): ensure nuxt is loaded from cwd rather than parent dir (#30910)

This commit is contained in:
Connor Pearson 2025-02-13 23:47:39 +01:00 committed by Daniel Roe
parent 0161985c0f
commit 9ba51dba6f
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
2 changed files with 38 additions and 4 deletions

View File

@ -1,6 +1,7 @@
import { pathToFileURL } from 'node:url'
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
import type { Nuxt } from '@nuxt/schema'
import { withTrailingSlash } from 'ufo'
import { importModule, tryImportModule } from '../internal/esm'
import { runWithNuxtContext } from '../context'
import type { LoadNuxtConfigOptions } from './config'
@ -27,8 +28,10 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
// Apply dev as config override
opts.overrides.dev = !!opts.dev
const rootDir = withTrailingSlash(pathToFileURL(opts.cwd!).href)
const nearestNuxtPkg = await Promise.all(['nuxt-nightly', 'nuxt3', 'nuxt', 'nuxt-edge']
.map(pkg => resolvePackageJSON(pkg, { url: opts.cwd }).catch(() => null)))
.map(pkg => resolvePackageJSON(pkg, { url: rootDir }).catch(() => null)))
.then(r => (r.filter(Boolean) as string[]).sort((a, b) => b.length - a.length)[0])
if (!nearestNuxtPkg) {
throw new Error(`Cannot find any nuxt version from ${opts.cwd}`)
@ -36,8 +39,6 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
const pkg = await readPackageJSON(nearestNuxtPkg)
const majorVersion = pkg.version ? Number.parseInt(pkg.version.split('.')[0]!) : ''
const rootDir = pathToFileURL(opts.cwd || process.cwd()).href
// Nuxt 3
if (majorVersion && majorVersion >= 3) {
const { loadNuxt } = await importModule<typeof import('nuxt')>((pkg as any)._name || pkg.name, { paths: rootDir })
@ -72,7 +73,7 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
}
export async function buildNuxt (nuxt: Nuxt): Promise<any> {
const rootDir = pathToFileURL(nuxt.options.rootDir).href
const rootDir = withTrailingSlash(pathToFileURL(nuxt.options.rootDir).href)
// Nuxt 3
if (nuxt.options._majorVersion === 3) {

View File

@ -0,0 +1,33 @@
import { fileURLToPath } from 'node:url'
import { mkdir, rm, writeFile } from 'node:fs/promises'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { join, normalize } from 'pathe'
import { withoutTrailingSlash } from 'ufo'
import { x } from 'tinyexec'
import { loadNuxt } from '../src'
const repoRoot = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../', import.meta.url))))
describe('loadNuxt', () => {
const tempDir = join(repoRoot, 'temp')
beforeAll(async () => {
await mkdir(join(tempDir, 'nuxt'), { recursive: true })
await writeFile(join(tempDir, 'package.json'), '{"dependencies":{"nuxt":"file:./nuxt"}}')
await writeFile(join(tempDir, 'nuxt', 'package.json'), '{"name":"nuxt","type":"module","exports":{".":"./index.js"}}')
await writeFile(join(tempDir, 'nuxt', 'index.js'), 'export const loadNuxt = (opts) => ({ name: "it me" })')
await x('npm', ['install'], { nodeOptions: { cwd: tempDir } })
})
afterAll(async () => {
await rm(tempDir, { recursive: true, force: true })
})
it('respects correct directory', async () => {
const nuxt = await loadNuxt({ cwd: tempDir })
expect(nuxt).toStrictEqual({
name: 'it me',
})
})
})