Skip to content
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

fix(resolve): make directory package.json check best effort #14626

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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