-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Description
Describe the bug
When a module's path is aliased, it will always be resolved even if it doesn't exist, leading to more obscure errors later on
ie, with a non-aliased faulty import:

While with an aliased faulty import:

This is caused by the way the @rollup/plugin-alias plugin behaves at resolution time - it falls back to the mapped import even if it could not be resolved
The very same plugin exposes a way to change that behavior through customresolver however this particular configuration is not exposed in the Vite configuration
I imagine it would be better for this to work without specific configuration, in which case Vite could set this customResolver itself?
I believe it could be something like
this: PluginContext,
updatedId: string,
importer: string | undefined
): Promise<ResolvedId | null> {
return this.resolve(updatedId, importer, { skipSelf: true });
},
Reproduction
Here's a minimal reproduction environment:
https://stackblitz.com/edit/vite-5ejgwh?file=main.js
Import line 7 is aliased and faulty, while the one number 4 isn't aliased but still faulty
System Info
❯ npx envinfo --system --npmPackages '{vite,@vitejs/*}' --binaries --browsers
System:
OS: Linux undefined
CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: Unknown - /bin/jsh
Binaries:
Node: 14.16.0 - /usr/local/bin/node
Yarn: 1.22.10 - /bin/yarn
npm: 7.17.0 - /bin/npm
npmPackages:
vite: ^2.6.0 => 2.6.0Used Package Manager
npm
Logs
https://vite-5ejgwh--3000.local.webcontainer.io/@id/mapped/another2.js net::ERR_ABORTED 404 (Not Found)
Instead of getting a proper
```blitz.19c34b6682252415afe6a0280c63a7c091453a84.js:11 6:23:05 PM [vite] Internal server error: Failed to resolve import "./mapped/another2.js" from "main.js". Does the file exist? Plugin: vite:import-analysis File: /home/projects/vite-5ejgwh/main.js 2 | 3 | // Imported directly: fails with a detailed error message 4 | import './mapped/another2.js'; | ^ 5 | 6 | // Fails with a more obscure error message at formatError (/home/projects/vite-5ejgwh/node_modules/vite/dist/node/chunks/dep-a8e01c9f.js:50383:46)```Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.