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 3 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
34 changes: 16 additions & 18 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
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
const basedir = importer ? path.dirname(importer) : process.cwd()
try {
// this may throw errors if unable to resolve, e.g. aliased id
resolvedSrc = normalizePath(resolveFrom(id, basedir))
} catch {
// this is best-effort only so swallow errors
}
if (!resolvedSrc) return
csr632 marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'
import { page } from '~utils'

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')
})
17 changes: 17 additions & 0 deletions playground/resolve-optimized-dup-deps/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h2>direct dependency A</h2>
<pre class="a"></pre>

<h2>direct dependency B</h2>
<pre class="b"></pre>

<script type="module">
import A from '@vitejs/test-resolve-optimized-dup-deps-package-a'
import B from '@vitejs/test-resolve-optimized-dup-deps-package-b'

text('.a', A)
text('.b', B)

function text(sel, text) {
document.querySelector(sel).textContent = text
}
</script>
6 changes: 6 additions & 0 deletions playground/resolve-optimized-dup-deps/package-a/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import b from '@vitejs/test-resolve-optimized-dup-deps-package-b'

// should get test-package-a:test-package-b-v2
const result = 'test-package-a:' + b

export default result
9 changes: 9 additions & 0 deletions playground/resolve-optimized-dup-deps/package-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@vitejs/test-resolve-optimized-dup-deps-package-a",
"private": true,
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"@vitejs/test-resolve-optimized-dup-deps-package-b": "file:../package-b-v2"
}
}
3 changes: 3 additions & 0 deletions playground/resolve-optimized-dup-deps/package-b-v1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// test-package-b-v1 is install and imported by user
// it is written in cjs and should be optimized
module.exports = 'test-package-b-v1'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@vitejs/test-resolve-optimized-dup-deps-package-b",
"private": true,
"version": "1.0.0",
"main": "index.js"
}
3 changes: 3 additions & 0 deletions playground/resolve-optimized-dup-deps/package-b-v2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// test-package-b-v2 is install and imported by test-package-a
// it is written in esm. it is not optimized
export default 'test-package-b-v2'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@vitejs/test-resolve-optimized-dup-deps-package-b",
"private": true,
"version": "2.0.0",
"main": "index.js"
}
15 changes: 15 additions & 0 deletions playground/resolve-optimized-dup-deps/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@vitejs/test-resolve-optimized-dup-deps",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"@vitejs/test-resolve-optimized-dup-deps-package-a": "file:./package-a",
"@vitejs/test-resolve-optimized-dup-deps-package-b": "file:./package-b-v1"
}
}
8 changes: 8 additions & 0 deletions playground/resolve-optimized-dup-deps/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @type {import('vite').UserConfig}
*/
module.exports = {
optimizeDeps: {
exclude: ['@vitejs/test-resolve-optimized-dup-deps-package-a'],
},
}
40 changes: 40 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.