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: warn on missing optional peer deps during build #15737

Conversation

hi-ogawa
Copy link
Collaborator

@hi-ogawa hi-ogawa commented Jan 29, 2024

Description

I wrote my findings in #15733 (comment) and it looks reasonable to treat this case similar to node builtin and log warning to notify users about potential issues with their build #12616

The node builtin warning happens during resolveId hook, but for missing peer dep, I added a warning during load hook since it looks simpler to implement in this way and probably there's no substantial difference for users.

I haven't added a test but verified manually on playground by:

$ pnpm -C playground/optimize-deps build

> @vitejs/test-optimize-deps@0.0.0 build /home/hiroshi/code/others/vite/playground/optimize-deps
> vite build

...
[plugin:vite:resolve] Could not resolve "foobar/baz" imported by "@vitejs/test-dep-with-optional-peer-dep-submodule". Is it installed?
[plugin:vite:resolve] Could not resolve "foobar" imported by "@vitejs/test-dep-with-optional-peer-dep". Is it installed?
...

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, especially the Pull Request Guidelines.
  • 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).
  • Update the corresponding documentation if needed.
  • Ideally, include relevant tests that fail without this PR but pass with it.

Copy link

stackblitz bot commented Jan 29, 2024

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

@hi-ogawa hi-ogawa marked this pull request as ready for review January 29, 2024 03:26
@hi-ogawa
Copy link
Collaborator Author

hi-ogawa commented Jan 29, 2024

Hmm, It looks like load-time warning still shows up even if optional dep import is tree-shaken completely. I checked this by commenting out callPeerDep here and there is still a warning Could not resolve "foobar" ...:

<script type="module">
import {
callItself,
callPeerDep,
} from '@vitejs/test-dep-with-optional-peer-dep'
text('.dep-with-optional-peer-dep', callItself())
// expect error as optional peer dep not installed
callPeerDep()
</script>

This means that the warning will likely to make false-positive, so this approach is probably not good (even though it works somewhat similar to current node builtin warning).
I would love to hear what others think.


One more thing to add: I found rollup also makes false-positive warning saying "Unresolved dependencies" even if it ends up tree-shaken. So the warning might not be an issue in practice?

https://stackblitz.com/edit/github-caz4hb?file=src%2Findex.js

@bluwy
Copy link
Member

bluwy commented Jan 29, 2024

This looks like a good addition to me, but yeah the false warnings are not quite ideal. It might be related to #14145 and #14102 perhaps.

I'm not sure if it's easy to fix those, but I suppose one other way to fix this is to inject a warning in runtime instead (adjacent to export default {}) and make sure they still get treeshaken still.

Curious to hear what others think too, but anyways since uninstalled peer dependencies are also rare, it might not be too bad too.

@hi-ogawa
Copy link
Collaborator Author

hi-ogawa commented Jan 29, 2024

one other way to fix this is to inject a warning in runtime instead (adjacent to export default {}) and make sure they still get treeshaken still.

That's interesting. I haven't thought about that, but that should equally help users to notice the issue. Let me explore that approach.
(EDIT: it might not be really equal except the OP's issue with preact prerendering. Preact prerendering is somewhat special case where they run browser build in the same terminal, but in other cases, the log will appear only when running app on browser.)

@hi-ogawa
Copy link
Collaborator Author

Let me close this for now as I haven't looked into this for a while.

@hi-ogawa hi-ogawa closed this Apr 28, 2024
@hi-ogawa hi-ogawa deleted the fix-warn-on-missing-optional-peer-dep-build branch April 28, 2024 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants