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(types): narrow down the return type of defineConfig #13792

Merged

Conversation

haoqunjiang
Copy link
Member

Description

Fixes compatibility with mergeConfig, as reported in vuejs/create-vue#313

Before the change, defineConfig would return UserConfigExport, which is a union type. This would cause mergeConfig to fail, as it doesn't accept the UserConfigFn type in the union. (#13135)

After the change, defineConfig returns the same type that it receives, so it would exclude UserConfigFn from the return type whenever possible.

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

Fixes compatibility with `mergeConfig`, as reported in vuejs/create-vue#313

Before the change, `defineConfig` would return `UserConfigExport`, which
is a union type. This would cause `mergeConfig` to fail, as it doesn't
accept the `UserConfigFn` type in the union. (vitejs#13135)

After the change, `defineConfig` returns the same type that it receives,
so it would exclude `UserConfigFn` from the return type whenever
possible.
@stackblitz
Copy link

stackblitz bot commented Jul 11, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@patak-dev
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented Jul 11, 2023

📝 Ran ecosystem CI: Open

suite result
analogjs ✅ success
astro ✅ success
histoire ❌ failure
iles ❌ failure
ladle ✅ success
laravel ✅ success
marko ✅ success
nuxt ✅ success
nx ✅ success
previewjs ✅ success
qwik ❌ failure
rakkas ✅ success
sveltekit ✅ success
unocss ✅ success
vite-plugin-pwa ✅ success
vite-plugin-ssr ✅ success
vite-plugin-react ✅ success
vite-plugin-react-pages ✅ success
vite-plugin-react-swc ✅ success
vite-plugin-svelte ✅ success
vite-plugin-vue ✅ success
vite-setup-catalogue ✅ success
vitepress ✅ success
vitest ✅ success

@haoqunjiang
Copy link
Member Author

haoqunjiang commented Jul 11, 2023

Oops. So many broken tests… 🤦‍♂️

I think the @ts-expect-error failures in this repo are fine, because I have actually improved the type to make them unnecessary. I'll just remove these comments in the tests.
But I'm not sure about the Qwik failures. Will have a closer look tomorrow.

@patak-dev
Copy link
Member

Only Qwik is failing compared to main. Iles and Histoire are known issues.

@haoqunjiang
Copy link
Member Author

haoqunjiang commented Jul 12, 2023

Update: I can confirm this is affected by the ["strictFunctionTypes"](https://www.typescriptlang.org/tsconfig#strictFunctionTypes) configuration.

No tsconfig is configured for the config.spec.ts file, therefore it's not under strict mode, thus the local tests missed the bug. Seems config.spec.ts can also catch it with an as casting

@bluwy
Copy link
Member

bluwy commented Jul 12, 2023

FWIW this was also attempted at #12021 but reverted in #12077 also because of Qwik (and VitePress).

@haoqunjiang
Copy link
Member Author

Further tests show that the as any casting in the Qwik config may be the culprit. https://github.com/BuilderIO/qwik/blob/190f0bcd7593745b58aef07054547551253d087b/packages/qwik-worker/vite.config.ts#L42
Removing that fixes the error.

I also managed to use the in out operator to fix the issue without modifying the downstream code, but ended up breaking some other use cases… But maybe it could work with more overloads. Let me try again.

Based on vitejs#13792 but with slightly more verbose function style
configuration types.
We can make the generic version pass the tests with some hack, but it
is too permissive that it would allow users to pass in unknown fields.
The overload version is much more direct and simpler.

Thanks @xiaoxiangmoe for helping me debug this.
@haoqunjiang
Copy link
Member Author

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented Jul 12, 2023

📝 Ran ecosystem CI: Open

suite result
analogjs ✅ success
astro ✅ success
histoire ❌ failure
iles ❌ failure
ladle ✅ success
laravel ✅ success
marko ✅ success
nuxt ✅ success
nx ✅ success
previewjs ✅ success
qwik ✅ success
rakkas ✅ success
sveltekit ✅ success
unocss ✅ success
vite-plugin-pwa ✅ success
vite-plugin-ssr ✅ success
vite-plugin-react ✅ success
vite-plugin-react-pages ✅ success
vite-plugin-react-swc ✅ success
vite-plugin-svelte ✅ success
vite-plugin-vue ✅ success
vite-setup-catalogue ✅ success
vitepress ✅ success
vitest ✅ success

@haoqunjiang haoqunjiang added regression The issue only appears after a new release p2-edge-case Bug, but has workaround or limited in scope (priority) labels Jul 17, 2023
@patak-dev patak-dev merged commit c971f26 into vitejs:main Jul 17, 2023
haoqunjiang added a commit to haoqunjiang/vitest that referenced this pull request Jul 24, 2023
Ported from vitejs/vite#13792

Fully fixes issues like vuejs/create-vue#317
and vuejs/create-vue#313

For an easy reproduction, please refer to https://github.com/sodatea/viteste-define-config-bug-repro

I didn't include a test in this PR because:

1. The reproduction is a bit complicated, it only fails on the very
latest Vite versions, but updating Vite would mess up the lockfile.
2. By not updating the dependencies, we can make sure that this fix is
compatible with older versions of Vite, too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority) regression The issue only appears after a new release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants