Language-tools: Alias is not resolved

Created on 20 Aug 2020  Β·  10Comments  Β·  Source: sveltejs/language-tools

Describe the bug
I used import alias @/ and it worked. Then i switched to Vite and it supports only root aliases. So i switched my code to use /@/ aliases. It compiles and works in browser, but gives me an error in vscode:

image

To Reproduce
Use /@/ import

Expected behavior
Everything works

System (please complete the following information):

  • OS: Ubuntu
  • IDE: VSCode
  • Plugin/Package:"Svelte for VSCode"
Fixed bug question

Most helpful comment

The problem is inside our module-loader, which delegates to the normal TS resolution because the check for "is absolute path" is true (file paths starting with / are absolute in linux). I think we need additional checks for "is this prefix part of tsconfig paths".

All 10 comments

You'll also have to configure the alias for typescript though tsconfig/jsconfig.json

https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

I'm also facing the same issue which tsc can compile it just fine. However, tools like svelte-check won't be able to resolve the alias path correctly and throw errors like OP. It seems like there's a bug related to language-tools not able to resolve tsconfig.json in the project folder correctly?

Screenshot 2020-08-21 at 13 06 57

That error is because we have used some special property injected by svelte2tsx to do component type-check. If we can't find the actual file we can't transform the file using svelte2tsx.

@cayter where do you put your tsconfig?

@jasonlyu123 Below is my project folder structure which happens to be a monorepo for both backend/frontend:

.
β”œβ”€β”€ Makefile
β”œβ”€β”€ README.md
β”œβ”€β”€ database
β”‚   └── database.rules.json
β”œβ”€β”€ firebase.json
β”œβ”€β”€ firestore
β”‚   β”œβ”€β”€ firestore.indexes.json
β”‚   └── firestore.rules
β”œβ”€β”€ functions // backend code
β”‚   β”œβ”€β”€ package-lock.json
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ src
β”‚   β”‚   └── index.ts
β”‚   └── tsconfig.json
β”œβ”€β”€ hosting // frontend PWA code
β”‚   β”œβ”€β”€ ...
β”‚   β”œβ”€β”€ src
β”‚   β”‚   β”œβ”€β”€ components
β”‚   β”‚   β”‚   β”œβ”€β”€ App.svelte
β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   β”œβ”€β”€ images
β”‚   β”‚   β”‚   β”œβ”€β”€ cover.png
β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   β”œβ”€β”€ index.css
β”‚   β”‚   β”œβ”€β”€ index.ts
β”‚   β”‚   β”œβ”€β”€ pages
β”‚   β”‚   β”‚   β”œβ”€β”€ Auth
β”‚   β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   β”‚   β”œβ”€β”€ Error
β”‚   β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   β”‚   β”œβ”€β”€ Home
β”‚   β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   β”‚   └── User
β”‚   β”‚   β”‚       └── ...
β”‚   β”‚   └── stores
β”‚   β”‚       └── ...
β”‚   β”œβ”€β”€ svelte.config.js
β”‚   β”œβ”€β”€ tailwind.config.js
β”‚   β”œβ”€β”€ tsconfig.json
β”‚   β”œβ”€β”€ vite.config.ts
β”‚   └── workbox.config.js
β”œβ”€β”€ package-lock.json
└── package.json

hosting/tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "baseUrl": "src",
    "declaration": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "importHelpers": true,
    "importsNotUsedAsValues": "error",
    "isolatedModules": true,
    "lib": ["dom", "esnext", "es6"],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "rootDir": "src",
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "strictBindCallApply": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictNullChecks": true,
    "target": "esnext",
    "types": ["jest", "node", "svelte"],
    "paths": {
      "/@/*": ["*"]
    }
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules/*", "public/*"]
}

If we can't find the actual file we can't transform the file using svelte2tsx.

Yep, this is relevant to alias path resolving defined in the project's tsconfig.json is not being honoured. So, if we change it to relative path instead of using alias path, the vscode extension and svelte-check will not throw any error anymore which you can see from OP's screenshot for the ComboBox component.

Can you check the output channel of vscode, chose svelte in the dropdown on the right. And see if you have something like this
Initialize new ts service at c:/Current Projects/svelte-app/tsconfig.json and post the log here.

I can reproduce this. For some reason getDefinitionAndBoundSpan can deal with TS/JS files imported from such locations, but not with .svelte files.

The problem is inside our module-loader, which delegates to the normal TS resolution because the check for "is absolute path" is true (file paths starting with / are absolute in linux). I think we need additional checks for "is this prefix part of tsconfig paths".

OP actually said it works with @/ and I missed it. Sorry πŸ˜…

I forgot to say how awesome you guys are. Thank you for quick response

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brunnerh picture brunnerh  Β·  3Comments

JAD3N picture JAD3N  Β·  5Comments

opensas picture opensas  Β·  4Comments

johanbissemattsson picture johanbissemattsson  Β·  4Comments

scippio picture scippio  Β·  3Comments