You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Auto imports from referenced project not working until already imported once in project #39778
Cursed?It's likely this is extremely difficult to fix without making something else much, much worseExperience EnhancementNoncontroversial enhancementsSuggestionAn idea for TypeScript
I have an Angular project, and just 1 of the files don't have a proper suggestion support in it.
Same happens when I create a file via plus sign from the side bar.
VSCode doesn't suggest "obvious(necessary)" things, quite the contrary happens:
it suggests everything else what's available, but not what I need and not what really exists in the project.
Also, when I try to import environment object with 'src/environments/environment',
it doesn't seem to know about src folder(project_name/src).
Although it already works this way for all the other files in the project.
Cache cleaning && insiders build didn't help out, no errors in debug console and I couldn't find any useful tsserver logs.
Although I can send if necessary.
Having 3.9.7 TS
Does this issue occur when all extensions are disabled?: Yes
Reacted by Tylor Reynolds, cevek, Stefan, Vitalii Shevchenko, chuckmah, Toliak, Alexander, Can Sahin, Evan Kennedy, Guilherme C. Matuella and 18 more
Each project has // environment line their src/app/app.component.ts, and when I uncomment the line and do command + space to have suggestions list pop up, for 102740-0 I get nothing or allowedNodeEnvironmentFlags function from 'process' package, if npm -i has been ran.
Looks like src/environments/environment.ts and app.component.ts are part of separate tsconfig projects, although both of these project are both referenced by tsconfig.json.
Moving upstream to get the TypeScript team's feedback on this behavior
Project reference imports like using @ namespace only works when there is already an import using that path. Otherwise it defaults to relative paths. Very annoying to have to manually type it out initially everytime
Reacted by Webber Wang, Can Sahin, Evan Kennedy, Shota Archemashvili ( შოთა არჩემაშვილი ), theomessin and Simone Benigni
This is based on a simplification of our own yarn monorepo. We've got a common project that contains stuff that's shared between frontend and backend (the backend project is omitted), and a web project that references it via a @common alias. Each project has its own tsconfig.json. I believe this is pretty similar to the example repo posted above. To see the problem, hop into web/src/Helper.ts and try to use a "Quick Fix" to import square from the common repo.
This example repo doesn't include any of this extra stuff because it suffices to show the issue, but for context, in our real repo web is a CRA-based Webpack app. We run it via yarn tsc --build --watch & react-app-rewired start (we run tsc separate because CRA's setup doesn't understand building projects). We want to import the compiled code in common/dist via an alias (e.g. import { square } from '@common/MyModule').
Our biggest issue is the one mentioned in the comment above - VSCode auto-import suggests relative paths to the original .ts file no matter what. There's a few issues here:
it leads CRA to barf because of the attempt to import a file outside of the project (this works fine with aliases, with some monkey-grease)
we want to be importing the compiled .js file, not the original .ts file
Depending on VSCode settings, it won't even import the correct file.
What I mean by that last point is, if I try it with the "Import Module Specifier" preference set to "shortest", it works (although with a relative path, which is still undesirable):
But if I set "Import Module Specifier" to "non-relative", it curiously gives me:
Which is simply broken - an immediate compilation error.
It's totally possible - and would be great if! - this is just a configuration issue on our part and there's a different/better way to do it.
I found autoimports working quite reliably if top level tsconfig includes all files. It will be unused by any particular file or project having own local configuration, but magically enables autocomplete.
Also almost the opposite advice - do not reference workspace packages from a top-level config. Even for small projects it might take too much time to start language server, so you'll be left with no support as-you-type.
Anton Korzunov (@theKashey) what do you mean by root level tsconfig including all files?
I have created a repro repo and if add "include": ["**/*"] to the root tsconfig I still don't get any autocomplete until first imported
So that the trick you should have your tsconfig-base as your tsconfig and put references aside (tsconfig.references.json) using them only inside particular packages.
I am not sure references are expected to work this way, but it works.
The only way I got this working is to include the referenced projects' source code in include of the project using the references. Not sure if this is how it's intended to work 🤷
Just realized if we include the referenced projects in include it will break the incremental builds! This is really frustrating.
Do you know any workarounds for this Andrew Branch (@andrewbranch)?
For those who want to fix the auto import in composite projects, I have a partial solution for you. I think as this already works out of the box with the current implementation, the TypeScript team might be able to actually leverage this, and fix the issue internally so we don't have to configure all the referenced projects to built files to make types available only for auto-import.
Here is how it all works, you will have to add the built version of the referenced projects where the declaration files are generated in the compilerOptions.typeRoots, this is on top of node_modules/types, and remember adding node_modules/types is important or it will break the global types in your project.
A minimal base configs (that will be extended by the composite projects) look like this:
And then you'll have to build the whole project for the declarations to be available for auto-import to work by running tsc -b app --watch.
This works only partially, and for some reasons the root exported declarations are not available in the auto import. In our example loadConfigs1 is not available which is in the root index.ts but loadConfigs2 is available in the auto-import list.
This should allow the incremental build, and the auto imports in vscode to work properly.
setting VScode setting typescript.preferences.includePackageJsonAutoImports to on. Looking into if I can avoid this or enforce using a typescript plugin.
Cursed?It's likely this is extremely difficult to fix without making something else much, much worseExperience EnhancementNoncontroversial enhancementsSuggestionAn idea for TypeScript
Activity
EKashpersky commentedon Jul 20, 2020
For me it's not a problem even for a session with screen sharing, if anything
mjbvz commentedon Jul 20, 2020
Can you share the project that causes this?
EKashpersky commentedon Jul 20, 2020
Yup!
Working on it
EKashpersky commentedon Jul 20, 2020
Matt Bierner (@mjbvz) here you go
https://github.com/EKashpersky/mentare-102740-0
https://github.com/EKashpersky/mentare-102740-1
Each project has
// environmentline theirsrc/app/app.component.ts, and when I uncomment the line and do command + space to have suggestions list pop up, for 102740-0 I get nothing orallowedNodeEnvironmentFlagsfunction from 'process' package, ifnpm -ihas been ran.Works fine for 102740-1.
mjbvz commentedon Jul 27, 2020
Looks like
src/environments/environment.tsandapp.component.tsare part of separate tsconfig projects, although both of these project are both referenced bytsconfig.json.Moving upstream to get the TypeScript team's feedback on this behavior
[-]Corrupted autocomplete[/-]cevek commentedon Sep 18, 2020
Confirm this issue. My composite projects referenced to each other don't auto import names from other connected projects
zerkalica commentedon Sep 18, 2020
Confirm. Autocomplete not working via references in tsconfig.json
demo: https://github.com/zerkalica/ts-references-autoimport-bug/tree/autoimport-no-ref
Currently autocomplete works only with includes (cpu eating on big projects) and via "types" in package.json.
Autocomplete doesn't works with direct imports (without types in package.json)
webberwang commentedon Oct 6, 2020
Project reference imports like using
@namespace only works when there is already an import using that path. Otherwise it defaults to relative paths. Very annoying to have to manually type it out initially everytime35 remaining items
adamsmasher commentedon Oct 21, 2022
Andrew Branch (@andrewbranch)
Sure, here's a repo that has the issue.
This is based on a simplification of our own yarn monorepo. We've got a
commonproject that contains stuff that's shared between frontend and backend (thebackendproject is omitted), and awebproject that references it via a@commonalias. Each project has its owntsconfig.json. I believe this is pretty similar to the example repo posted above. To see the problem, hop intoweb/src/Helper.tsand try to use a "Quick Fix" to importsquarefrom thecommonrepo.This example repo doesn't include any of this extra stuff because it suffices to show the issue, but for context, in our real repo
webis a CRA-based Webpack app. We run it viayarn tsc --build --watch & react-app-rewired start(we runtscseparate becauseCRA's setup doesn't understand building projects). We want to import the compiled code incommon/distvia an alias (e.g.import { square } from '@common/MyModule').Our biggest issue is the one mentioned in the comment above - VSCode auto-import suggests relative paths to the original
.tsfile no matter what. There's a few issues here:.jsfile, not the original.tsfileWhat I mean by that last point is, if I try it with the "Import Module Specifier" preference set to "shortest", it works (although with a relative path, which is still undesirable):
But if I set "Import Module Specifier" to "non-relative", it curiously gives me:
Which is simply broken - an immediate compilation error.
It's totally possible - and would be great if! - this is just a configuration issue on our part and there's a different/better way to do it.
Thanks for your help!
andrewbranch commentedon Oct 21, 2022
Opened a new issue because that does sound different. Will take a look next week; thanks for the detailed info!
kbrilla commentedon Apr 10, 2023
Any news on this? Monorepos with @nrwl/nx are nightmare to use without suggestions.
RyanCavanaugh commentedon Apr 10, 2023
Nothing has changed since the last update
theKashey commentedon Apr 11, 2023
I found autoimports working quite reliably if top level tsconfig includes all files. It will be unused by any particular file or project having own local configuration, but magically enables autocomplete.
Also almost the opposite advice - do not reference workspace packages from a top-level config. Even for small projects it might take too much time to start language server, so you'll be left with no support as-you-type.
MathieuUrstein commentedon May 12, 2023
Anton Korzunov (@theKashey) what do you mean by root level tsconfig including all files?
I have created a repro repo and if add
"include": ["**/*"]to the roottsconfigI still don't get any autocomplete until first importedtheKashey commentedon May 12, 2023
So that the trick you should have your

tsconfig-baseas yourtsconfigand put references aside (tsconfig.references.json) using them only inside particular packages.I am not sure references are expected to work this way, but it works.
aminpaks commentedon Sep 9, 2023
The only way I got this working is to include the referenced projects' source code in
includeof the project using the references. Not sure if this is how it's intended to work 🤷aminpaks commentedon Sep 20, 2023
Just realized if we include the referenced projects in
includeit will break the incremental builds! This is really frustrating.Do you know any workarounds for this Andrew Branch (@andrewbranch)?
Here is a sample to reproduce the issue.
aminpaks commentedon Sep 20, 2023
For those who want to fix the auto import in composite projects, I have a partial solution for you. I think as this already works out of the box with the current implementation, the TypeScript team might be able to actually leverage this, and fix the issue internally so we don't have to configure all the referenced projects to built files to make types available only for auto-import.
Here is how it all works, you will have to add the built version of the referenced projects where the declaration files are generated in the
compilerOptions.typeRoots, this is on top ofnode_modules/types, and remember addingnode_modules/typesis important or it will break the global types in your project.A minimal base configs (that will be extended by the composite projects) look like this:
{ "extends": "@tsconfig/strictest/tsconfig.json", "compilerOptions": { // required configs "baseUrl": ".", "rootDir": ".", "outDir": "./built/local", "composite": true, "declaration": true, "declarationMap": true, "sourceMap": true, // configure these based on your needs "module": "esnext", "target": "es2016", "moduleResolution": "node", "resolveJsonModule": true, "jsx": "react-jsx", "checkJs": false } }And a project that has references can include the built of
sharedin thetypeRootslike this:{ "extends": "../../tsconfig-base", "compilerOptions": { "typeRoots": [ "../../node_modules/@types", "../../built/local/app/shared", ], }, "include": [ "**/*", "**/*.json", ], "references": [ {"path": "../shared"}, ], }And the shared project configs that was referenced above:
{ "files": [], "include": [], "references": [ {"path": "./main"}, {"path": "./shared"}, ], }And then you'll have to build the whole project for the declarations to be available for auto-import to work by running
tsc -b app --watch.This works only partially, and for some reasons the root exported declarations are not available in the auto import. In our example
loadConfigs1is not available which is in the rootindex.tsbutloadConfigs2is available in the auto-import list.This should allow the incremental build, and the auto imports in vscode to work properly.
dajinchu commentedon Dec 4, 2024
FYI I got auto importing in a monorepo working by:
exportsfield to the shared module. In my case, exporting everything with a glob, which works as of 5.2.2 Offer auto-imports from wildcard exports with AutoImportProvider #54831typescript.preferences.includePackageJsonAutoImportstoon. Looking into if I can avoid this or enforce using a typescript plugin.