Skip to content

Commit

Permalink
fix(resolve): make directory package.json check best effort (#14626)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Oct 23, 2023
1 parent 2728a31 commit d520388
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import {
const normalizedClientEntry = normalizePath(CLIENT_ENTRY)
const normalizedEnvEntry = normalizePath(ENV_ENTRY)

const ERR_RESOLVE_PACKAGE_ENTRY_FAIL = 'ERR_RESOLVE_PACKAGE_ENTRY_FAIL'

// special id for paths marked with browser: false
// https://github.com/defunctzombie/package-browser-field-spec#ignore-a-module
export const browserExternalId = '__vite-browser-external'
Expand Down Expand Up @@ -643,7 +645,9 @@ function tryCleanFsResolve(
return resolvePackageEntry(dirPath, pkg, targetWeb, options)
}
} catch (e) {
if (e.code !== 'ENOENT') throw e
// This check is best effort, so if an entry is not found, skip error for now
if (e.code !== ERR_RESOLVE_PACKAGE_ENTRY_FAIL && e.code !== 'ENOENT')
throw e
}
}

Expand Down Expand Up @@ -1095,11 +1099,13 @@ export function resolvePackageEntry(
}

function packageEntryFailure(id: string, details?: string) {
throw new Error(
const err: any = new Error(
`Failed to resolve entry for package "${id}". ` +
`The package may have incorrect main/module/exports specified in its package.json` +
(details ? ': ' + details : '.'),
)
err.code = ERR_RESOLVE_PACKAGE_ENTRY_FAIL
throw err
}

function resolveExportsOrImports(
Expand Down
22 changes: 21 additions & 1 deletion playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { expect, test } from 'vitest'
import { isBuild, isWindows, page, testDir } from '~utils'
import { isBuild, isWindows, page, testDir, viteTestUrl } from '~utils'

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

test('Resolve doesnt interrupt page request with trailing query and .css', async () => {
await page.goto(viteTestUrl + '/?test.css')
expect(await page.locator('vite-error-overlay').count()).toBe(0)
expect(await page.textContent('h1')).toBe('Resolve')
})

test.runIf(!isWindows)(
'Resolve doesnt interrupt page request that clashes with local project package.json',
async () => {
// Sometimes request path may point to a different project's package.json, but for testing
// we point to Vite's own monorepo which always exists, and the package.json is not a library
const pathToViteMonorepoRoot = new URL('../../../', import.meta.url)
const urlPath = fileURLToPath(pathToViteMonorepoRoot).replace(/\/$/, '')
await page.goto(viteTestUrl + urlPath)
expect(await page.locator('vite-error-overlay').count()).toBe(0)
expect(await page.textContent('h1')).toBe('Resolve')
},
)

test.runIf(isBuild)('public dir is not copied', async () => {
expect(
fs.existsSync(path.resolve(testDir, 'dist/should-not-be-copied')),
Expand Down

0 comments on commit d520388

Please sign in to comment.