Subscribe on changes!

v3.3: compiler-sfc fails to resolve source when using path mapping

avatar
Apr 23rd 2023

Vue version

3.3.0-beta.1

Link to minimal reproduction

https://stackblitz.com/edit/node-hrwual?file=repro/src/components/HelloWorld.vue

Steps to reproduce

Consider a component that uses an interface imported with path mapping (as the setup of a project generated with create-vue allows to do):

<script setup lang="ts">
import { User } from '@/User';
defineProps<{
  msg: string;
  user?: User;
}>();
</script>

What is expected?

The compiler should find the file, as if it was imported with:

import { User } from '../User';

What is actually happening?

When bumping from v3.2.47 to v3.3.0-beta.1, the compiler throws (with pnpm dev or pnpm build):

[@vue/compiler-sfc] Failed to resolve import source "@/User".

/home/projects/node-hrwual/repro/src/components/HelloWorld.vue
3  |  defineProps<{
4  |    msg: string;
5  |    user?: User;
   |           ^^^^
6  |  }>();
7  |  </script>
16:04:34 [vite] Internal server error: [@vue/compiler-sfc] Failed to resolve import source "@/User".

System Info

System:
    OS: macOS 13.3.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 4.89 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.17.0 - ~/.volta/tools/image/node/16.17.0/bin/node
    Yarn: 1.22.17 - ~/.volta/tools/image/yarn/1.22.17/bin/yarn
    npm: 8.19.3 - ~/.volta/tools/image/npm/8.19.3/bin/npm
  Browsers:
    Chrome: 112.0.5615.137
    Firefox: 111.0.1
    Safari: 16.4
  npmPackages:
    vue: 3.3.0-beta.1 => 3.3.0-beta.1

Any additional comments?

No response

avatar
Apr 23rd 2023

It seems that it does not work because you're using project references. If you add paths in your tsconfig.json it will work:

{
  "compilerOptions": {
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "files": [],
  "references": [
    {
      "path": "./tsconfig.node.json"
    },
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.vitest.json"
    }
  ]
}
avatar
Apr 25th 2023

FYI this specific case should fallback to skipping runtime check without an error with latest main branch after 2d9f6f92 - that said project references are indeed not supported at the moment.

avatar
May 24th 2023

I get this error (only during build) when importing a type with relative path eg:

'../types/myfile' ❌

when I use project root import, it resolves without errors

'@/types/myfile' ✅

I should note that this error arises when I import a file to use in defineProps<MyType>() since we can do that now which is awesome 🙂

I fixed it with alias import, but I think it should be fixed for relative imports too

avatar
May 24th 2023

@gVguy relative import is already supported. Could you provide a runnable minimal reproduction?

avatar
May 30th 2023

I get this error (only during build) when importing a type with relative path eg:

'../types/myfile' ❌

when I use project root import, it resolves without errors

'@/types/myfile' ✅

I should note that this error arises when I import a file to use in defineProps<MyType>() since we can do that now which is awesome 🙂

I fixed it with alias import, but I think it should be fixed for relative imports too

@gVguy I am having the same issue, also only in an actual build and also only when importing a type to use in defineProps. Interestingly it only occurs on windows build agents, MacOS and linux are fine

avatar
May 30th 2023

Interestingly it only occurs on windows build agents, MacOS

Can't confirm this, for me it occured on MacOS

avatar
Aug 16th 2023

@gVguy Thank you, That helps me a lot.