From c9280292c0c79083e951bf99df987e3fcd59a438 Mon Sep 17 00:00:00 2001 From: "voidzero-bot[bot]" <348775505+voidzero-bot[bot]@users.noreply.github.com> Date: Sat, 28 Nov 2026 18:21:54 +0000 Subject: [PATCH] fix(cli): never return undefined from lazyPlugins to keep defineConfig compatible with exactOptionalPropertyTypes --- packages/cli/src/__tests__/index.spec.ts | 42 ++++++++++++++++-------- packages/cli/src/define-config.ts | 14 ++++---- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/packages/cli/src/__tests__/index.spec.ts b/packages/cli/src/__tests__/index.spec.ts index 64ecaf7350..caaeab1a0e 100644 --- a/packages/cli/src/__tests__/index.spec.ts +++ b/packages/cli/src/__tests__/index.spec.ts @@ -29,23 +29,25 @@ test('lazyPlugins executes the callback by default', () => { expect(result).toEqual([{ name: 'test' }]); }); -test('lazyPlugins returns undefined during a config-metadata resolution', () => { +test('lazyPlugins returns an empty array during a config-metadata resolution', () => { process.env.VP_RESOLVING_CONFIG_METADATA = '1'; const cb = vi.fn<() => Array>(() => [{ name: 'my-plugin' }]); const result = lazyPlugins(cb); - expect(result).toBeUndefined(); + expect(result).toEqual([]); expect(cb).not.toHaveBeenCalled(); }); test('withConfigMetadataResolution skips plugins during the resolution and restores after', async () => { const cb = vi.fn<() => Array>(() => [{ name: 'my-plugin' }]); - let during: ReturnType; + // Initialized so the assignment inside the async closure satisfies + // definite-assignment analysis now that the return type excludes `undefined`. + let during: ReturnType = []; const returned = await withConfigMetadataResolution(async () => { during = lazyPlugins(cb); return 'result'; }); expect(returned).toBe('result'); - expect(during).toBeUndefined(); + expect(during).toEqual([]); expect(cb).not.toHaveBeenCalled(); // marker cleared after → plugins load again expect(lazyPlugins(() => [{ name: 'after' }])).toEqual([{ name: 'after' }]); @@ -73,7 +75,7 @@ test('withConfigMetadataResolution keeps the marker set across overlapping resol const second = withConfigMetadataResolution(async () => 'second'); expect(await second).toBe('second'); // first is still in flight → lazyPlugins must still skip - expect(lazyPlugins(() => [{ name: 'plugin' }])).toBeUndefined(); + expect(lazyPlugins(() => [{ name: 'plugin' }])).toEqual([]); releaseFirst(); expect(await first).toBe('first'); // both done → marker cleared, plugins load again @@ -86,21 +88,21 @@ test('lazyPlugins supports async callback', async () => { return [plugin]; }); // Async factory wraps the promise in an array for Vite's asyncFlatten - expect(await Promise.all(result!)).toEqual([[{ name: 'async-plugin' }]]); + expect(await Promise.all(result)).toEqual([[{ name: 'async-plugin' }]]); }); -test('lazyPlugins returns undefined for async callback during metadata resolution', () => { +test('lazyPlugins returns an empty array for async callback during metadata resolution', () => { process.env.VP_RESOLVING_CONFIG_METADATA = '1'; const result = lazyPlugins(async () => { return [{ name: 'async-plugin' }]; }); - expect(result).toBeUndefined(); + expect(result).toEqual([]); }); test('lazyPlugins wraps sync function returning a Promise into array', async () => { // A sync function that returns a Promise (not an async function) — same handling as async const result = lazyPlugins(() => Promise.resolve([{ name: 'sync-promise-plugin' }])); - expect(await Promise.all(result!)).toEqual([[{ name: 'sync-promise-plugin' }]]); + expect(await Promise.all(result)).toEqual([[{ name: 'sync-promise-plugin' }]]); }); // defineConfig auto-injects four internal plugins before user-supplied @@ -128,8 +130,20 @@ const userPlugins = (plugins: unknown): Array => { // lazyPlugins type compatibility tests — these verify at compile time that // lazyPlugins return types satisfy Vite's plugins?: PluginOption[] field. +// Regression test for gh-3597: under `exactOptionalPropertyTypes`, Vite's +// optional `plugins?: PluginOption[]` property rejects an explicit `undefined`, +// so the public `lazyPlugins` return type must never include `undefined`. +// When it does, this annotation resolves to `never` and fails to compile. +const lazyPluginsReturnExcludesUndefined: [undefined] extends [ReturnType] + ? never + : true = true; + +test('lazyPlugins return type never includes undefined (exactOptionalPropertyTypes)', () => { + expect(lazyPluginsReturnExcludesUndefined).toBe(true); +}); + test('lazyPlugins sync return type satisfies plugins field', () => { - // Must compile: plugins accepts PluginOption[] | undefined + // Must compile: plugins accepts PluginOption[] const config = defineConfig({ plugins: lazyPlugins(() => [{ name: 'sync-type-test' }]), }); @@ -137,7 +151,7 @@ test('lazyPlugins sync return type satisfies plugins field', () => { }); test('lazyPlugins async return type satisfies plugins field', async () => { - // Must compile: async overload returns PluginOption[] | undefined, not Promise + // Must compile: async overload returns PluginOption[], not Promise const config = defineConfig({ plugins: lazyPlugins(async () => { return [{ name: 'async-type-test' }]; @@ -146,13 +160,13 @@ test('lazyPlugins async return type satisfies plugins field', async () => { expect(await Promise.all(userPlugins(config.plugins))).toEqual([[{ name: 'async-type-test' }]]); }); -test('lazyPlugins undefined return satisfies plugins field', () => { +test('lazyPlugins empty-array return during metadata resolution satisfies plugins field', () => { process.env.VP_RESOLVING_CONFIG_METADATA = '1'; - // Must compile: undefined is accepted by plugins?: PluginOption[] + // Must compile: the empty array is accepted by plugins?: PluginOption[] const config = defineConfig({ plugins: lazyPlugins(() => [{ name: 'skipped' }]), }); - // lazyPlugins returns undefined, but defineConfig still injects its rewrite plugin. + // lazyPlugins returns an empty array, but defineConfig still injects its rewrite plugin. expect(userPlugins(config.plugins).length).toBe(0); }); diff --git a/packages/cli/src/define-config.ts b/packages/cli/src/define-config.ts index 504453d6da..3a0b66081c 100644 --- a/packages/cli/src/define-config.ts +++ b/packages/cli/src/define-config.ts @@ -1145,15 +1145,17 @@ export async function resolveConfigFromFile( ); } -export function lazyPlugins(cb: () => Array): Array | undefined; -export function lazyPlugins( - cb: () => Promise>, -): Array | undefined; +export function lazyPlugins(cb: () => Array): Array; +export function lazyPlugins(cb: () => Promise>): Array; export function lazyPlugins( cb: () => Array | Promise>, -): Array | undefined { +): Array { if (process.env[CONFIG_METADATA_ENV] === '1') { - return undefined; + // Vite's `plugins?: PluginOption[]` is optional without `undefined`, so + // returning an empty array (instead of `undefined`) keeps + // `defineConfig({ plugins: lazyPlugins(...) })` type-checking under + // `exactOptionalPropertyTypes`. + return []; } const result = cb(); if (types.isPromise(result)) {