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

Typescript Language service not finding symbolic linked (junction) npm module #25312

Closed
SteveALee opened this issue Apr 25, 2017 · 45 comments
Closed
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug javascript JavaScript support issues typescript Typescript support issues upstream Issue identified as 'upstream' component related (exists outside of VS Code)

Comments

@SteveALee
Copy link

SteveALee commented Apr 25, 2017

  • VSCode Version: 1.11.2
  • OS Version: Windows 10 build 16179 (insiders)
  • Shell: cmd

A symbolic link (NTFS junction) to a library package in node_modules breaks code's intellisense ability to find the module, even though building from the command line works fine. This makes node dev a pain when developing a lib and consuming package and don't want to go through the publish / install loop

  1. Create and compile a simple npm package that is a typescript lib (common JS, main and typings set in package.json. This exports foo.
{
    "compilerOptions": {
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "declaration": true,
        "rootDir": "src", 
        "outDir": "lib",
        "target": "es2015",
        "lib": [
            "dom",
            "es2016"
        ],
        "noImplicitAny": false
    },
  "include": [
    "src/**/*"
  ]
}
  1. Create and compile another package consumer with a file that references the lib with an import {foo} from 'lib'

  2. Copy the lib into the consumer's node_modules - as if it had been `npm install'ed

  3. Look at the import line in code - all intellisense is good. lib is found

  4. Delete node_modules\lib

  5. Either manually link by adding a junction or use npm link ..\lib lib is navigable in the code file browser (on left)

  6. Look at the import line in code - 'lib' has red squiggle and hover displays '[ts] Cannot find module 'lib'.'

I can supply all files if required, but the lib is just a single file with a single export and consuming code references it. basic stuff.

@mjbvz mjbvz self-assigned this Apr 25, 2017
@SteveALee SteveALee changed the title Typescript Language service not finding linked npm module Typescript Language service not finding symbilic linked (junction) npm module Apr 25, 2017
@SteveALee SteveALee changed the title Typescript Language service not finding symbilic linked (junction) npm module Typescript Language service not finding symbolic linked (junction) npm module Apr 25, 2017
@ORESoftware
Copy link

ORESoftware commented Jul 2, 2017

As a personal anecdote - I have 6 or 7 TypeScript based projects, all symlinked together locally, and unfortunately I cannot get static typing across them, the way I would if they were actually installed in node_modules instead of symlinked. It's a huge loss. :(

@starkevin
Copy link

Also just hit this problem...

@schotime
Copy link

+1

@jcjolley
Copy link

jcjolley commented Nov 3, 2017

This is an issue for me as well.

@mjbvz mjbvz added bug Issue identified by VS Code Team member as probable bug javascript JavaScript support issues typescript Typescript support issues labels Nov 14, 2017
@d-akara
Copy link

d-akara commented Nov 19, 2017

This is causing problems for me as well with local development.

@Xceno
Copy link

Xceno commented Dec 19, 2017

This only seems to happen in large projects. I've created a small example and intellisense worked well.
You can check it out yourself here: https://github.com/Xceno/ts-modules-test

My other project has a similar setup and absolutely no intellisense 😕

@mjbvz
Copy link
Collaborator

mjbvz commented Jan 3, 2018

@SteveALee Hey sorry this fell off my radar. Are you still running into this issue with typescript@2.7-insiders under the current vscode insiders?

If so, let's move this issue over the the TS repo. Someone over there should know more about the expected behavior for linked modules and if there are any workarounds

@skorunka
Copy link

skorunka commented Jan 6, 2018

Same problem on TS 2.6.2, VSC 1.19.1 :(

@SteveALee
Copy link
Author

@skorunka thanks!

@yss14
Copy link

yss14 commented Mar 14, 2018

Still same problem here on TypeScript 2.7.2 and VSC 1.21.0

@efokschaner
Copy link

Also seem to be having this problem.
I'm using yarn workspaces so I have multiple symlinks in a single top level node_modules above any individual package.
TSC build works fine, but VSCode "PROBLEMS" window is full of "Cannot find module" errors.

Typescript (+ tsdk used by VSCode) 2.8.1
VSCode 1.21.1
Windows 10 Pro version 1709

@kunlongxu
Copy link

Still same problem here on TypeScript 2.8.1 and VSC 1.22.1

@kunlongxu
Copy link

@mjbvz I not find a problem as this. It still exists in the latest version

@bogthe
Copy link

bogthe commented May 10, 2018

@linmodev try restarting the typescript server inside of vscode using the command palette, that seemed to have worked for me.

@niktekusho
Copy link

niktekusho commented May 25, 2018

Here to confirm that @bogthe workaround fixed it for me.
Steps:

  1. CTRL + SHIFT + P & equivalent for MacOS (brings up the command palette)
  2. Search something like "typescript"
  3. Select TypeScript: Restart TS server

@gobengo
Copy link

gobengo commented Jul 17, 2018

"Restart TS server" does not help me here. I created symlink via 'npm link '.

tsc succeeds in my project, but vs code is not happy, and reports "Cannot find module "

I believe npm link and getting vs code typescript type checking is currently not possible because of this. Bummer because there is not a great workaround.

@orlaqp
Copy link

orlaqp commented Jul 20, 2018

Hi @gobengo not sure what steps did you followed but this is what I did and it worked for me (I am on a Mac though):

In source project: npm link
In target project: npm link <package name>

Then: Cmd + Shift + P
Search for: typescript
Selected: Restart TS Server

And everything worked .. not sure if you did it similar

@zbjornson
Copy link

@orlaqp this seems to be specific to Windows' symlinks. (Restarting the TS server has no effect for me.)

@gobengo
Copy link

gobengo commented Jul 23, 2018

@orlaqp @zbjornson Yeah. I'm using Debian on Windows Subsystem for Linux (Windows 10)

@biels
Copy link

biels commented Aug 21, 2018

To everyone here, check out --preserveSymlinks flag. I just found it, I'll see if I can make it work

@SteveALee
Copy link
Author

Why would that not be on by default though? Strange choice. Looking forward to hearing if it works.

@biels
Copy link

biels commented Aug 21, 2018

It worked! Yes, I would find it more useful to have it on by default or at least provide some kind of suggestion when there are errors with symlinks. For example "You got an error with a symlink, you should douvble check that --preserveSymlinks is set to the desired value". User (developer) experience is very important...
Anyways glad it worked, still in love with ts.

@biels
Copy link

biels commented Aug 22, 2018

Actually it didn't :/ It appeared to work because I had set "rootDir": "C:\Users\biels" in tsconfig.json after hours of messing with it. But it is still not behaving as expected.
It still resolves the symlinks and then proceeds to complain about how their canonical paths are outside of rootDir.

The --preserveSymlinks is a flag that switches between resolving the symlink (to its canocical path) vs treating it as a file that contains a reference to a directory.

@biels
Copy link

biels commented Aug 22, 2018

What about treating symlinks as if it were portals (not just shortcuts)? That you could access files using a symlinked path the same way you do with normal ones.
For example you could have:

  • C:
    • project1
      • symlink (to project2)
    • project2
      • src

And then access C:\project1\symlink\src. Notice how src is still under project1 which would appear to be under the rootDir.
The thing is, it's 2018 and I don't think we should still be solving this kinds of problems... At this point there should be support for it at some part of the stack... not that we're creating the first OS in history or anything...

@SteveALee
Copy link
Author

Plus npm uses links explicitly if you have a local version of a dependency module

@cdaringe
Copy link

cdaringe commented Aug 27, 2018

i npm link and/or yarn link monorepos on a daily basis. i can confirm that linked typings files do not get updated realtime, onchange. i'm on OSX, so restarting the TS Server does work, but it's a hack. i assume that the solution path will be the same for all of us.

just adding another data point to the mix.

@bbugh
Copy link

bbugh commented Nov 26, 2018

I just got hit by this bug too, using yarn link on a Mac to try to access a local library I'm working on. I was really surprised that this issue has been around for so long.

If you're doing local library development, here's an ugly hack that will at least allow you to keep moving until you publish on npm:

  1. Make a folder in your source (like vendor)
  2. Sym link your library's dist folder into that folder, like ln -s ../ts-library/dist vendor/ts-library
  3. Set some kind of build watcher for your library so it auto runs rollup (or whatever) whenever you change library code.

VSCode will recognize that sym link, just not the one in node_modules (this issue). It will also update it live - no need to restart the TS Server. All of the links will unfortunately be relative paths, but I can live with that until I publish the library.

It's certainly not ideal, but at least it's workable.

@biels
Copy link

biels commented Nov 27, 2018

Come on please fix this. It can't be that hard... This has been around for too long already.

@jthomerson
Copy link

Same for me as @cdaringe. I:

In ProjectA: npm link
In ProjectB that depends on ProjectA: npm link <package name>

Both projects are open in VSCode. I have a grunt watch task running in the background that is "compiling" ProjectA any time I save a change to it. But switching over to a file in ProjectB, it doesn't pick up the new changes from ProjectA until I restart the TS server. It seems like it's just not watching for changes in node_modules or something.

@DamonOehlman
Copy link

I had a similar issue when working with yarn workspaces (as per @efokschaner) within a WSL environment. After a good bit of searching I came across issue microsoft/WSL#3611 with the following comment:

microsoft/WSL#3611 (comment)

More or less it seems like you can enable developer mode within windows 10 and apparently all your problems go away. Which was true in my situation at least.

@gobengo
Copy link

gobengo commented Dec 20, 2018

@DamonOehlman I tried to no avail. The symlinks work fine in the terminal and stuff, but the typescript checking in vscode always complains about not being able to find the npm linked module.

@jthomerson
Copy link

I should've mentioned I'm on a Mac. If there's more info that could be helpful, let me know and I'll provide it.

@panosfff
Copy link

panosfff commented Dec 20, 2018

Come on guys, this is killing us, symbolic links is bread and butter for efficient development, please get this addressed. It took a while to get node, nodemon, npm/yarn and everybody else on board, vscode is the last hold out.... My experience that this does not work is on macos.

@zbjornson
Copy link

I resolved this by adding "moduleResolution": "node" or "module": "commonjs" to jsconfig.json:

{
  "compilerOptions": {
    "moduleResolution": "node"
  }
}

I'm not sure that will work universally, since the OP has both lines, but it works in two projects for me. Interested to know if it works for others.


Details:

With "compilerOptions.module" = "es6" (the default if your target is anything other than "ES3" or "ES5"), Typescript defaults to "classic" module resolution. (That seems like a gotcha; "classic" is an old method that's discouraged from use, but it appears to be intended per microsoft/TypeScript#11434.)

For reasons I don't understand yet, linked modules don't resolve from VSCode in "classic" mode, although non-linked commonjs modules in node_modules do. (I would expect neither to resolve; the example command below fails with linked or non-linked modules in classic mode.) Specifying one or both of the options above resolves it.

Make a file with

//@ts-check
const foo = require("some-linked-module")

and then run:

> tsc --moduleResolution classic --allowJs --noEmit --traceResolution file.js
======== Resolving module 'prom-client' from 'C:/Users/zachb/git/temp/index.js'. ========
Module resolution kind is not specified, using 'Classic'.
File 'C:/Users/zachb/git/temp/prom-client.ts' does not exist.
File 'C:/Users/zachb/git/temp/prom-client.tsx' does not exist.
File 'C:/Users/zachb/git/temp/prom-client.d.ts' does not exist.
File 'C:/Users/zachb/git/prom-client.ts' does not exist.
File 'C:/Users/zachb/git/prom-client.tsx' does not exist.
File 'C:/Users/zachb/git/prom-client.d.ts' does not exist.
File 'C:/Users/zachb/prom-client.ts' does not exist.
File 'C:/Users/zachb/prom-client.tsx' does not exist.
File 'C:/Users/zachb/prom-client.d.ts' does not exist.
File 'C:/Users/prom-client.ts' does not exist.
File 'C:/Users/prom-client.tsx' does not exist.
File 'C:/Users/prom-client.d.ts' does not exist.
File 'C:/prom-client.ts' does not exist.
File 'C:/prom-client.tsx' does not exist.
File 'C:/prom-client.d.ts' does not exist.
Directory 'C:/Users/zachb/git/temp/node_modules/@types' does not exist, skipping all lookups in it.
Directory 'C:/Users/zachb/git/node_modules/@types' does not exist, skipping all lookups in it.
Directory 'C:/Users/zachb/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/Users/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
File 'C:/Users/zachb/git/temp/prom-client.js' does not exist.
File 'C:/Users/zachb/git/temp/prom-client.jsx' does not exist.
File 'C:/Users/zachb/git/prom-client.js' does not exist.
File 'C:/Users/zachb/git/prom-client.jsx' does not exist.
File 'C:/Users/zachb/prom-client.js' does not exist.
File 'C:/Users/zachb/prom-client.jsx' does not exist.
File 'C:/Users/prom-client.js' does not exist.
File 'C:/Users/prom-client.jsx' does not exist.
File 'C:/prom-client.js' does not exist.
File 'C:/prom-client.jsx' does not exist.
======== Module name 'prom-client' was not resolved. ========
index.js:2:23 - error TS2307: Cannot find module 'prom-client'.

vs.

> tsc --moduleResolution node --allowJs --noEmit --traceResolution file.js
======== Resolving module 'prom-client' from 'C:/Users/zachb/git/temp/index.js'. ========
Module resolution kind is not specified, using 'NodeJs'.
Loading module 'prom-client' from 'node_modules' folder, target file type 'TypeScript'.
'package.json' does not have a 'typings' field.
'package.json' has 'types' field './index.d.ts' that references 'C:/Users/zachb/git/temp/node_modules/prom-client/index.d.ts'.
'package.json' does not have a 'typesVersions' field.
Found 'package.json' at 'C:/Users/zachb/git/temp/node_modules/prom-client/package.json'. Package ID is 'prom-client/index.d.ts@11.1.2'.
File 'C:/Users/zachb/git/temp/node_modules/prom-client.ts' does not exist.
File 'C:/Users/zachb/git/temp/node_modules/prom-client.tsx' does not exist.
File 'C:/Users/zachb/git/temp/node_modules/prom-client.d.ts' does not exist.
'package.json' does not have a 'typings' field.
'package.json' has 'types' field './index.d.ts' that references 'C:/Users/zachb/git/temp/node_modules/prom-client/index.d.ts'.
File 'C:/Users/zachb/git/temp/node_modules/prom-client/index.d.ts' exist - use it as a name resolution result.
Resolving real path for 'C:/Users/zachb/git/temp/node_modules/prom-client/index.d.ts', result 'C:/Users/zachb/git/prom-client/index.d.ts'.
======== Module name 'prom-client' was successfully resolved to 'C:/Users/zachb/git/prom-client/index.d.ts'. ========

Possibly relevant: microsoft/TypeScript#26819 microsoft/TypeScript#18492

@gae123
Copy link

gae123 commented Dec 24, 2018

@zbjornson I appreciate your effort to explain how if we set up our project in a certain way then the problem is worked around, but in most real world, ie non vscode demo, projects there are many conflicting reasons on how to set up the project. Usually the IDE is the last one to be considered. After all, there is an obvious workaround to this issue: not use symbolic links. But there is some other reason, having nothing to do with VSCode that led us to use symlinks in the first place.

In general, if it works from the command line it should work from the IDE. The issue here is that on both windows and unix/macos something that works on the command like does not work from inside the VScode IDE and this is what we want to see addressed.

@suntong
Copy link

suntong commented Jan 1, 2019

As I commented in microsoft/vscode-go#1307 (comment), I suspect that the problem is beyond npm -- MS vscode is incapable of handling symlinks, that is causing problems for nodejs, angular 2, and Go.

@webberwang
Copy link

webberwang commented Mar 10, 2019

Literally spent days figuring out why my mono-repo using work workspace has issues detecting imports from other packages. Still haven't found the ideal solution yet.

Update:
This thread helped me solved the issue microsoft/TypeScript#15479

@linkdesu
Copy link

I found the key to solve this problem!:

  1. Configure declarationDir to './types' .
  2. Add types: 'types' field into package.json .

Then typescript language service will work!

I ran into this problem when I split my package with lerna, and my typescript is Version 3.2.2 .

More infomation: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html

@suntong
Copy link

suntong commented Mar 19, 2019

Hi @linkdesu , was your problem has anything to do with symbolic links, or it was only about package splitting?

@natew
Copy link

natew commented Apr 4, 2019

Just wanted to add 2c, but this is my biggest time waste per-day in VSCode. I didn't know about just restarting TSServer, but either way in a big monorepo I'm often "refreshing" VSCode ~100x a day.

@kalbert312
Copy link

Just wasted about an hour on this as well. Deleted the link, copied pasted the link contents to a regular folder in node_modules, and no longer receiving errors.

@animando
Copy link

I had this issue, was driving me crazy. Tried everything suggested above and nothing worked.
vscode TS server and tsc both had the issue, but ts-node did not.
Resolved it by removing compilerOptions.target=es2016 from tsconfig.json

@Ruud-cb
Copy link

Ruud-cb commented Aug 1, 2019

I don't see a lot of love for plain old Visual Studio 2017, but I will add my 2 cents that it is also a problem in here on typescript 3. Funny though as it is a bit of both:

image

That property is newly added to a library that it is coming from, so it does give a suggestion.

But keeps showing an error:
image.

I still have another problem though, one library has a dependency on another library, so once I have linked all the libraries (including the dependency lib) I get ng build errors because it cannot find the required module within all the other libraries.

All libraries are automatically updated into Project1/ClientApp/dist/[lib-name]
I use ng link to link them to Project2/node_modules/. All [lib-name]'s show up correctly there. In Project1 it works fine with the dependencies between libraries, in Project2 it does not when linked (works of course good when publish and install is done the normal way).

@brendanalexdr
Copy link

brendanalexdr commented Aug 28, 2019

Hi @gobengo not sure what steps did you followed but this is what I did and it worked for me (I am on a Mac though):

In source project: npm link
In target project: npm link <package name>

Then: Cmd + Shift + P
Search for: typescript
Selected: Restart TS Server

And everything worked .. not sure if you did it similar

This worked for me! Using windows.

@mjbvz
Copy link
Collaborator

mjbvz commented Oct 10, 2019

Issues about project structure or resolution are upstream issues from the TypeScript project.

If you are still seeing issues related to these and are using the most recent version of TypeScript in your workspace, please file a bug report upstream. Be sure to include a set of repo steps so that we can investigate

@mjbvz mjbvz closed this as completed Oct 10, 2019
@mjbvz mjbvz added the upstream Issue identified as 'upstream' component related (exists outside of VS Code) label Oct 10, 2019
@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug javascript JavaScript support issues typescript Typescript support issues upstream Issue identified as 'upstream' component related (exists outside of VS Code)
Projects
None yet
Development

No branches or pull requests