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: wrongly resolve to optimized doppelganger #11290

Merged
merged 7 commits into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 16 additions & 18 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -872,18 +872,27 @@ export async function tryOptimizedResolve(
// We should be able to remove this in the future
await depsOptimizer.scanProcessing

// resolvedSrc is what the importer wants
// we can return a optimizedDep only if optimizedDep.src === resolvedSrc
// https://github.com/vitejs/vite/pull/11290
let resolvedSrc: string | undefined
try {
if (importer)
// this may throw errors if unable to resolve, e.g. aliased id
resolvedSrc = normalizePath(resolveFrom(id, path.dirname(importer)))
} catch {
// this is best-effort only so swallow errors
}
if (!resolvedSrc) return

const metadata = depsOptimizer.metadata

const depInfo = optimizedDepInfoFromId(metadata, id)
if (depInfo) {
// check if the found optimizedDep comes from the resolvedSrc
if (depInfo && depInfo.src === resolvedSrc) {
csr632 marked this conversation as resolved.
Show resolved Hide resolved
return depsOptimizer.getOptimizedDepId(depInfo)
}

if (!importer) return

// further check if id is imported by nested dependency
let resolvedSrc: string | undefined

for (const optimizedData of metadata.depInfoList) {
if (!optimizedData.src) continue // Ignore chunks

Expand All @@ -894,18 +903,7 @@ export async function tryOptimizedResolve(
// this narrows the need to do a full resolve
if (!pkgPath.endsWith(id)) continue

// lazily initialize resolvedSrc
if (resolvedSrc == null) {
try {
// this may throw errors if unable to resolve, e.g. aliased id
resolvedSrc = normalizePath(resolveFrom(id, path.dirname(importer)))
csr632 marked this conversation as resolved.
Show resolved Hide resolved
} catch {
// this is best-effort only so swallow errors
break
}
}

// match by src to correctly identify if id belongs to nested dependency
// check if the found optimizedDep comes from the resolvedSrc
if (optimizedData.src === resolvedSrc) {
return depsOptimizer.getOptimizedDepId(optimizedData)
}
Expand Down
@@ -1,7 +1,7 @@
import { expect, test } from 'vitest'
import { page } from '~utils'

test.todo('resolve-optimized-dup-deps', async () => {
test('resolve-optimized-dup-deps', async () => {
expect(await page.textContent('.a')).toBe('test-package-a:test-package-b-v2')
expect(await page.textContent('.b')).toBe('test-package-b-v1')
})