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

Can't import interface / type and use them with defineProps "failed to resolve import source" #2048

Closed
Y0me opened this issue Jun 1, 2023 · 2 comments · Fixed by vuejs/core#9446
Labels

Comments

@Y0me
Copy link

Y0me commented Jun 1, 2023

Version

17.2.1

Reproduction link

https://github.com/Y0me/defineProps

Steps to reproduce

npm run dev

What is actually happening?

I'm trying to use the new import type feature in vue 3.3 to defineProps with imported type/interface.

Example

<script setup lang="ts">
import type {IHelloWorld} from "./type";

const props = defineProps<IHelloWorld>()
// const props = defineProps<{props: IHelloWorld}>()

</script>

When I compile the project, I get the following errors :

ERROR in ./src/app.vue?vue&type=script&setup=true&lang=ts (./node_modules/esbuild-loader/dist/index.cjs??clonedRuleSet-1!./node_modules/vue-loader/dist/index.js??ruleSet[1].rules[4].use[0]!./src/app.vue?vue&type=script&setup=true&lang=ts)
Module Error (from ./node_modules/vue-loader/dist/index.js):
[@vue/compiler-sfc] Failed to resolve import source "./type".

C:\Repository\defineProps\src\app.vue
2  |  import type {IHelloWorld} from "./type";
3  |
4  |  const props = defineProps<IHelloWorld>()
   |                            ^^^^^^^^^^^
5  |  // const props = defineProps<{props: IHelloWorld}>()
6  |
Error: [@vue/compiler-sfc] Failed to resolve import source "./type".

C:\Repository\defineProps\src\app.vue
2  |  import type {IHelloWorld} from "./type";
3  |
4  |  const props = defineProps<IHelloWorld>()
   |                            ^^^^^^^^^^^
5  |  // const props = defineProps<{props: IHelloWorld}>()
6  |
    at ScriptCompileContext.error (C:\Repository\defineProps\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:15841:11)
    at importSourceToScope (C:\Repository\defineProps\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:18428:16)
    at resolveTypeFromImport (C:\Repository\defineProps\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:18393:23)
    at innerResolveTypeReference (C:\Repository\defineProps\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:18299:14)
    at resolveTypeReference (C:\Repository\defineProps\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:18288:36)
    at innerResolveTypeElements (C:\Repository\defineProps\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:17945:24)
    at resolveTypeElements (C:\Repository\defineProps\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:17902:35)
    at resolveRuntimePropsFromType (C:\Repository\defineProps\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:19312:20)
    at genRuntimePropsFromTypes (C:\Repository\defineProps\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:19288:17)
    at genRuntimeProps (C:\Repository\defineProps\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:19278:18)
 @ ./src/app.vue?vue&type=script&setup=true&lang=ts 1:0-205 1:0-205 1:206-400 1:206-400
 @ ./src/app.vue 1:0-65 2:0-60 2:0-60 5:49-55

ERROR in ./src/app.vue?vue&type=script&setup=true&lang=ts (./node_modules/esbuild-loader/dist/index.cjs??clonedRuleSet-1!./node_modules/vue-loader/dist/index.js??ruleSet[1].rules[4].use[0]!./src/app.vue?vue&type=script&setup=true&lang=ts)
Module build failed (from ./node_modules/vue-loader/dist/index.js):
TypeError: Cannot read properties of null (reading 'content')
    at selectBlock (C:\Repository\defineProps\node_modules\vue-loader\dist\select.js:23:45)
    at Object.loader (C:\Repository\defineProps\node_modules\vue-loader\dist\index.js:92:41)
 @ ./src/app.vue?vue&type=script&setup=true&lang=ts 1:0-205 1:0-205 1:206-400 1:206-400
 @ ./src/app.vue 1:0-65 2:0-60 2:0-60 5:49-55

webpack 5.85.0 compiled with 2 errors in 215 ms

If I wrap the interface, the build succeeds.

const props = defineProps<{props: IHelloWorld}>()

What is expected?

I guess a success webpack build.

Thanks !

@JosephAnson
Copy link

Currently this is causing me the same issues as well, I believe it's same issue as described as above, only experiencing it with jest however. Wrapping the interface with another interface seems to fix the issue, however that'll change the way you need to interact with the component by quite a bit.

defineProps<{props: IHelloWorld}>()

Error received is the same mentioned above:

  TypeError: Cannot read properties of undefined (reading 'sys')
      at resolveFS (node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:18372:35)
      at importSourceToScope (node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:18397:14)
      at resolveTypeFromImport (node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:18393:23)
      at innerResolveTypeReference (node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:18299:14)
      at resolveTypeReference (node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:18288:36)
      at innerResolveTypeElements (node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:17945:24)
      at resolveTypeElements (node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:17902:35)
      at resolveRuntimePropsFromType (node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:19312:20)
      at genRuntimePropsFromTypes (node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:19288:17)
      at genRuntimeProps (node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:19278:18)
      at compileScript (node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:20383:21)
      at processTemplate (node_modules/@vue/vue3-jest/lib/process.js:94:31)
      at Object.module.exports [as process] (node_modules/@vue/vue3-jest/lib/process.js:166:26)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:545:31)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:674:40)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:726:19)
      at Object.<anonymous> (src/components/Base/Button.spec.ts:4:1)

@Huntedpix
Copy link

Hello !

I'm experiencing the same exact same issue than Y0me.

haoqunjiang added a commit to haoqunjiang/core that referenced this issue Oct 20, 2023
Closes vuejs#8671
Closes vuejs/vue-loader#2048

`dirname` is the safest way to get a directory name from a path string.

As for this specific bug, it's because
`path.posix.join(AnyWindowsPath, '..')` returns `.`.

I'm not sure whether there will be unexpected regression if I modify the
`joinPaths` utility, so I changed the smallest possible bit of code to
fix this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants