Skip to content

Commit 9722b07

Browse files
authored
fix(optimizer): scanner should resolve input from root (#22769)
1 parent 7e18bf8 commit 9722b07

3 files changed

Lines changed: 47 additions & 9 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as vue from 'vue'
2+
3+
export default vue

packages/vite/src/node/__tests__/scan.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,44 @@ test('scan import.meta.glob respects rolldown transform jsx options', async (ctx
218218
expect(scanResult.deps).not.toHaveProperty('react/jsx-runtime')
219219
})
220220

221+
// regression test for https://github.com/vitejs/vite/issues/22752
222+
// `resolveRolldownOptions` resolves `build.rolldownOptions.input` relative to the root,
223+
// so the scanner needs to resolve a relative bare entry from the root
224+
test('scan resolves build.rolldownOptions.input relative to the root', async (ctx) => {
225+
const server = await createServer({
226+
configFile: false,
227+
logLevel: 'error',
228+
// root differs from process.cwd(); the entry lives at `<root>/entry-client.tsx`
229+
root: path.join(import.meta.dirname, 'fixtures', 'scan-build-input', 'src'),
230+
environments: {
231+
client: {
232+
build: {
233+
rolldownOptions: {
234+
input: 'entry-client.tsx',
235+
},
236+
},
237+
},
238+
},
239+
optimizeDeps: {
240+
force: true,
241+
noDiscovery: false,
242+
},
243+
})
244+
ctx.onTestFinished(() => server.close())
245+
246+
const { cancel, result } = scanImports(
247+
devToScanEnvironment(server.environments.client),
248+
)
249+
ctx.onTestFinished(cancel)
250+
251+
await expect(result).resolves.toMatchObject({
252+
deps: {
253+
vue: expect.any(String),
254+
},
255+
missing: {},
256+
})
257+
})
258+
221259
test('scan import.meta.glob package imports patterns', async (ctx) => {
222260
const server = await createServer({
223261
configFile: false,

packages/vite/src/node/optimizer/scan.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,13 @@ async function computeEntries(environment: ScanEnvironment) {
202202
entries = await globEntries(explicitEntryPatterns, environment)
203203
} else if (buildInput) {
204204
const resolvePath = async (p: string) => {
205-
// rollup resolves the input from process.cwd()
205+
// `build.rollupOptions.input` is resolved from the root (not `process.cwd()`)
206+
// by the build, so resolve it from the root here too by not passing an importer.
206207
const id = (
207-
await environment.pluginContainer.resolveId(
208-
p,
209-
path.join(process.cwd(), '*'),
210-
{
211-
isEntry: true,
212-
scan: true,
213-
},
214-
)
208+
await environment.pluginContainer.resolveId(p, undefined, {
209+
isEntry: true,
210+
scan: true,
211+
})
215212
)?.id
216213
if (id === undefined) {
217214
throw new Error(

0 commit comments

Comments
 (0)