microsoft / TypeScript-Sublime-Plugin

IO wrapper around TypeScript language services, allowing for easy consumption by editor plugins
Apache License 2.0
1.72k stars 235 forks source link

tsconfig's compilerOptions' paths setting ignored #594

Open anchann opened 7 years ago

anchann commented 7 years ago

TypeScript allows to modify module resolution using the paths configuration option on the compilerOptions object. The mechanics of this are described in https://www.typescriptlang.org/docs/handbook/module-resolution.html. This feature is necessary for avoiding the relative path hell by aliasing the root of the current project, and importing files relative to that root.

Currently imports that contain an alias from the paths configuration are reported with the "Cannot find module" error, even though tsc resolves them correctly.

anchann commented 7 years ago

Hmm. I was certain that this has been an issue for the longest time, but just now I restarted Sublime, and I can use go-to-definition on objects imported through the paths alias. So the problem seems to be elsewhere, potentially related to the flakiness reported in #585 which I also experience.

anchann commented 7 years ago

Actually, this keeps happening. After everything runs fine for some time, the imports start going red-underlined, with "cannot find module xxx" on hover. I originally thought this happens when I switch git branches, but it just happened after a fresh sublime restart and no branch switching. How can I help debug this?

anchann commented 7 years ago

Is it possible that there's some limitation against handling multiple distinct typescript projects open in different sublime windows?

zhengbli commented 7 years ago

Hi @anchann, can you try using a fix I created at https://github.com/Microsoft/TypeScript-Sublime-Plugin/issues/585#issuecomment-292655670 And let me know if this solves your issue? If it does I'll try to publish a new version soon.

jmolero commented 7 years ago

I've tried your fix but the problem persists. Seems that's only on the imports that use tsconfig paths.

I enabled logging. I'll try to post a log them next time the problem shows up.

jmolero commented 7 years ago

Keeps happening with the local4.zip fix. I don't see any errors in the log:

2017-08-15 11:51:30,139: 140736961889216: WARNING: TypeScript plugin initialized.

Bamieh commented 6 years ago

I am facing this issue as well

image

I have these configs in the tsconfig file:

"compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@src/*": ["./src/*"],
      "@fixture/*": ["./test/fixture/*"]
    },
    ...
}

Compiling tsc works just fine.

poplarDev commented 5 years ago

+1 for this issue.

yandzee commented 5 years ago

+1

mshibl commented 4 years ago

+1

marquizzo commented 4 years ago

I had a similar problem where in tsconfig.json:

"paths": {
    "~Utils": ["src/utils"]
}

...would let me import from the "utils/index.ts" file, but it would give me an error in Sublime when importing modules in subfolders:

import { XXX } from "~Utils";    // <- Works
import { XXX } from "~Utils/subfolder";    // <- Does not work!

Funny thing is that the compiler wouldn't complain, only Sublime would have problems finding it. However, adding a second alias in tsconfig.json would let me find modules both from "~Utils" and "~Utils/subfolder"

"paths": {
    "~Utils/*": ["src/utils/*"],
    "~Utils": ["src/utils"]
},

now they both work

import { XXX } from "~Utils";    // <- Works
import { XXX } from "~Utils/subfolder";    // <- Also works!

(using Sublime TypeScript plugin 3.7.3)

Cipscis commented 3 years ago

I'm also experiencing this issue. I'm using Sublime Text 4 (build 4113) with the TypeScript plugin v4.3.2.

In my tsconfig.json:

"compilerOptions": {
    "moduleResolution": "node",
    "paths": {
        "@cipscis/csv": ["./src/csv.ts"]
    },

In one of my TypeScript files:

import { stringify, parse } from '@cipscis/csv';

Compiling this works absolutely fine, but within Sublime the '@cipscis/csv' string has a red underline, and hovering over it the error message is:

"Cannot find module '@cipscis/csv'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?"

The workarounds suggested in other comments in this thread haven't helped me. I'm setting up my aliases this way so I can work with a Node.js package using Webpack using the same syntax as if it had been installed via npm.

shyrma commented 3 years ago

I have the same issue, but in my case problem is only present for test files, that is files with extension *.test.ts. I don't know why and have no solution so far. (Sublime Text 4, build 4113)

rgpublic commented 3 years ago

Has anyone ever gotten this to work? I define paths and if I use them e.g. in triple slash directives and hover over them, however, I get: Cannot find type definition file for '@yadayada/test'. So it seems the @yadayada is not even resolved in the first place...?

tonympls commented 1 year ago

Same issue : Module not found and function definitions do not work with path aliases in Sublime but do work with relative path. Sublime issue only; compiles work fine. I'm using Sublime build 4143 and Typescript 4.5.2.

None of these alias variations work.

image image

It does work without using the path alias

image image

configurations:

{
  "extends":"../../tsconfig.json",
  "compilerOptions": {
    "paths": {
      "@idb-db-libx":["../lib/index"],
      "@idb-db-lib*":["../lib/*"],
      "@idb-db-lib-auth":["../lib/auth"],
      "idb-db-lib-auth":["../lib/auth"],
      "lettrywithoutdash":["../lib/auth"],
    },

  }
}

which extends:

{
  "include":["lib/index.ts"],
  "compilerOptions": {
    "target": "ES2019",                       
    "module": "commonjs",                     
    "esModuleInterop": true,                  
    "moduleResolution": "node",               
    "declaration": false,                     
    "sourceMap": true,
    "baseUrl": ".",
    "paths": {
      "@idb/*":["lib/*"],
      "@idb-db-lib":["./db/lib/index.ts"],
      "@idb-main-lib":["lib/index.ts"],
      "@idb-main-lib/*":["lib/*"],
      "@idb-main-lib-web":["lib/_index099"],
    },
...
Carlos-err406 commented 1 year ago

for me adding /* to my paths worked out just fine

this is previous to the fix/workaround image getting this nasty "errors" in vscode image

then i tried @marquizzo 's answer image and image

hope it helps somehow