Vscode: import suggestion for node modules includes entire path

Created on 16 Jun 2018  路  15Comments  路  Source: microsoft/vscode

  • VSCode Version: 1.25.0-insider
  • OS Version: Windows 7 Enterprise

image

Not sure if I should be opening this since this is a part of the insiders build, but since my most recent update, I've noticed that the import suggestions for installed modules like lodash include the entire path to the module, rather than just import something from 'module'. In the screenshot above you can see what I mean, the comment at the top is the result of accepting the suggestion, when it could simply be

import startCase from 'lodash';

EDIT: I've also just noticed that it seems to be suggesting the type rather than the actual function that I need. This may be at the heart of the problem?

Steps to Reproduce:

  1. create project with lodash as dependency (or another package)
  2. write a function or expression that requires an export from the package


Does this issue occur when all extensions are disabled?: Yes

bug javascript upstream verified

Most helpful comment

I'm also running into this same problem, and have a very standard TS project setup. This started happening on 2.9.2

All 15 comments

(Experimental duplicate detection)
Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

now that I'm browsing though other issues, looks related to #52022

@no-stack-dub-sack Did you manually install @types/lodash? How did you install node_modules in the first place?

@mjbvz lodash is a part of the application scaffold I am using at work, so I never installed lodash at all, and I can't see the package.json of the generator that produces the node_modules for me, but I'm guessing @types/lodash is being installed separately from lodash rather than using the built in types since it makes it in to the @types directory. However, in the past (until I updated), the suggestion always produced an import without the complete path — is this the new expected behavior when types are installed separately?

@mjbvz Also, for another example - I just installed another package to the project myself via yarn which has it's own index.d.ts file. When I try to import a type from that package, I get the same behavior - it suggests '../../complete/path/to/node_modules/package' rather than just 'package'

Same as https://github.com/Microsoft/TypeScript/issues/23230

Please share more information about your project structure. Are you opening a loose file or are you opening a folder? Do you have a jsconfig.json? What is the directory structure like? Where is node_modules located and where is the js file located?

@mjbvz Sure:

  • I am opening a folder
  • I do not have a jsconfig.json, all my files are .ts extensions, but the tsconfig does define allowJs as true
  • node_modules is at the project root, where I also have src and types directories
  • src contains client and server directories (this is a full-stack node / TS project)
  • package.json and tsconfig are also at the project root alongside src, node_modules, and types
  • the project builds into a .gitignored dist directory, also at the root level
  • all in all, a pretty standard setup, and again, this behavior is new - in months of using VS Code prior to this (with the same setup / project), the behavior had always been as described above until the 1.25 update

Let me know if you need more info

I'm also running into this same problem, and have a very standard TS project setup. This started happening on 2.9.2

I'm having trouble reproducing this -- could you try and come up with a particular set of files that reproduces the issue? Presumably this can be reproduced with a setup like the following?

node_modules/xxx/index.d.ts

export const xxx: number;

src/a.ts

/// <reference types="xxx" />

xxx // Import fix here

tsconfig.json:

{
    "compilerOptions": {
        <<settings needed to reproduce the issue here>>
    }
}

With just those files (and empty compilerOptions) I'm not reproducing the issue with typescript@next.

@andy-ms The issue exists in a fresh Angular install.

npm install -g @angular/cli
ng new hello-world

and edit app.component.ts or some other file with node_modules import.

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

tsconfig.app.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "types": []
  },
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

@krokofant Thanks! I can only reproduce that on windows for some reason.

Fixed by Microsoft/TypeScript#25364. @mjbvz Could you close this?

Thanks @andy-ms!

So I take it this did _not_ make it into 1.25.1?

Do we have to wait until the September release to see the fix for this regression?

Thanks for any insights.

I'm having this issue for all imports and i'm not using TS at all.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shanalikhan picture shanalikhan  路  3Comments

vsccarl picture vsccarl  路  3Comments

curtw picture curtw  路  3Comments

lukehoban picture lukehoban  路  3Comments

villiv picture villiv  路  3Comments