Skip to content

No error handling on faulty mapped imports #5813

@muryoh

Description

@muryoh

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:
image

While with an aliased faulty import:
image

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.0

Used 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    p2-nice-to-haveNot breaking anything but nice to have (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions