Skip to content

Commit d520388

Browse files
authored
fix(resolve): make directory package.json check best effort (#14626)
1 parent 2728a31 commit d520388

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

packages/vite/src/node/plugins/resolve.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ import {
5353
const normalizedClientEntry = normalizePath(CLIENT_ENTRY)
5454
const normalizedEnvEntry = normalizePath(ENV_ENTRY)
5555

56+
const ERR_RESOLVE_PACKAGE_ENTRY_FAIL = 'ERR_RESOLVE_PACKAGE_ENTRY_FAIL'
57+
5658
// special id for paths marked with browser: false
5759
// https://github.com/defunctzombie/package-browser-field-spec#ignore-a-module
5860
export const browserExternalId = '__vite-browser-external'
@@ -643,7 +645,9 @@ function tryCleanFsResolve(
643645
return resolvePackageEntry(dirPath, pkg, targetWeb, options)
644646
}
645647
} catch (e) {
646-
if (e.code !== 'ENOENT') throw e
648+
// This check is best effort, so if an entry is not found, skip error for now
649+
if (e.code !== ERR_RESOLVE_PACKAGE_ENTRY_FAIL && e.code !== 'ENOENT')
650+
throw e
647651
}
648652
}
649653

@@ -1095,11 +1099,13 @@ export function resolvePackageEntry(
10951099
}
10961100

10971101
function packageEntryFailure(id: string, details?: string) {
1098-
throw new Error(
1102+
const err: any = new Error(
10991103
`Failed to resolve entry for package "${id}". ` +
11001104
`The package may have incorrect main/module/exports specified in its package.json` +
11011105
(details ? ': ' + details : '.'),
11021106
)
1107+
err.code = ERR_RESOLVE_PACKAGE_ENTRY_FAIL
1108+
throw err
11031109
}
11041110

11051111
function resolveExportsOrImports(

playground/resolve/__tests__/resolve.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
3+
import { fileURLToPath } from 'node:url'
34
import { expect, test } from 'vitest'
4-
import { isBuild, isWindows, page, testDir } from '~utils'
5+
import { isBuild, isWindows, page, testDir, viteTestUrl } from '~utils'
56

67
test('bom import', async () => {
78
expect(await page.textContent('.utf8-bom')).toMatch('[success]')
@@ -202,6 +203,25 @@ test('Resolving from other package with imports field', async () => {
202203
expect(await page.textContent('.imports-pkg-slash')).toMatch('[success]')
203204
})
204205

206+
test('Resolve doesnt interrupt page request with trailing query and .css', async () => {
207+
await page.goto(viteTestUrl + '/?test.css')
208+
expect(await page.locator('vite-error-overlay').count()).toBe(0)
209+
expect(await page.textContent('h1')).toBe('Resolve')
210+
})
211+
212+
test.runIf(!isWindows)(
213+
'Resolve doesnt interrupt page request that clashes with local project package.json',
214+
async () => {
215+
// Sometimes request path may point to a different project's package.json, but for testing
216+
// we point to Vite's own monorepo which always exists, and the package.json is not a library
217+
const pathToViteMonorepoRoot = new URL('../../../', import.meta.url)
218+
const urlPath = fileURLToPath(pathToViteMonorepoRoot).replace(/\/$/, '')
219+
await page.goto(viteTestUrl + urlPath)
220+
expect(await page.locator('vite-error-overlay').count()).toBe(0)
221+
expect(await page.textContent('h1')).toBe('Resolve')
222+
},
223+
)
224+
205225
test.runIf(isBuild)('public dir is not copied', async () => {
206226
expect(
207227
fs.existsSync(path.resolve(testDir, 'dist/should-not-be-copied')),

0 commit comments

Comments
 (0)