Typescript: Issues trying to auto-import across project boundaries

Created on 26 Jan 2019  路  9Comments  路  Source: microsoft/TypeScript


TypeScript Version: 3.2.2, 3.3.0-rc


Search Terms: autoimport, project references, autocomplete

Code
Full minimal repro: https://github.com/MLoughry/typescript-repro

See _src/a/lib/index.ts_

// If another file in the project already has an import from the other project,
//   then attempting to auto-import `bar` will prompt you to import from 'obj/b/lib'
// If no other import is found within the project, no auto-import suggestions are found
console.log(bar);

Expected behavior:
Auto-import should work across project boundaries when an outDir is specified (with or without paths specified in tsconfig.json)

Actual behavior:

  • If there is already a cross-project import within the project: The auto-import attempts to import from the outDir.
    image
  • If there is no other cross-project import within the project: No import suggestions are found.
    image
In Discussion Suggestion

Most helpful comment

So, there are two issues here: one when the project has no other reference to the module, and another when it does.

Now that @sheetalkamat has laid them out, I can appreciate the perf implications of trying to fix the first issue (project has no other reference to module). However, would there be any perf implications to fixing the other issue (auto-import resolves to outDir file rather than source file)? It seems like just a matter of mapping the outDir path back to the source path, or am I mistaken?

All 9 comments

Maybe I'm missing something but I'm not sure how this can work. How would TypeScript know the other project even exists if there wasn't already a locally-sourced import pointing to it?

Project references in the tsconfig. The build can obviously find the code cross-project.

Thanks, I see it now, the references section. See, I knew I was missing something; I didn鈥檛 even realize that was possible!

I agree it definitely should be able to suggest modules from other referenced projects.

This is happening for me with regular node_modules dependencies, too.

See here: https://github.com/Microsoft/vscode/issues/67505

I think the issue here is that unless file is included in your module resolution from referenced project, it is not included in the referring project (unless it was --out project). This helps keeping program smallest and include only files/symbols needed. Auto import needs files already in program as otherwise the symbols are not available. So far we do not include any files that wont be part of your program when you run command line compilation so we don't have to worry about extra errors and discrepancy between ide experience and command line. We might need to change if we want to be able to give suggestions for such.

So, there are two issues here: one when the project has no other reference to the module, and another when it does.

Now that @sheetalkamat has laid them out, I can appreciate the perf implications of trying to fix the first issue (project has no other reference to module). However, would there be any perf implications to fixing the other issue (auto-import resolves to outDir file rather than source file)? It seems like just a matter of mapping the outDir path back to the source path, or am I mistaken?

@MLoughry yes.

A big issue in our source code base as well. One single source tree with lots of tsconfig.json, all extending single tsconfig.base.json with outDir: "build" & composite: true. References everywhere.

In most cases VSC auto-imports from build/ folder. Need to fix imports manually all the time.

@sheetalkamat We are planing to change some of our project to use project references, mostly everything works right, but the fact that the auto import feature picks the paths from the build directory is somewhat annoying.

Are any plans to fix that in the next release or is there a roadmap for it? That way we can maybe wait to migrate our projects or make a new plan. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uber5001 picture uber5001  路  3Comments

manekinekko picture manekinekko  路  3Comments

blendsdk picture blendsdk  路  3Comments

CyrusNajmabadi picture CyrusNajmabadi  路  3Comments

bgrieder picture bgrieder  路  3Comments