TSServer import path autocomplete reads incorrect directories when a file is not a direct child of a root directory #55232
Labels
Needs Investigation
This issue needs a team member to investigate its status.
Bug Report
My project has the following (simplified) structure:
where
rootDirs
is configured to read from bothsrc/
andbuild/dts
. In other words, I'm codegening some extra.d.ts
files during build time which I can then import in my typescript code.When tsserver computes autocomplete for an import path in the
a.ts
file, it correctly returns the relevant files:However, when this is done inside of a directory (in this example one called
tmp/
, but this works in general), tsserver reads the/tmp
system directory instead of thesrc/tmp
folder:TSServer is still searching the current script directory (in this case
src/tmp
), but instead of searchingbuild/dts/tmp
it then also searches/tmp
. This is probably why this bug went unnoticed for such a long time - it mostly affects imports of "extra files" that are not in source already.I traced this behaviour to the following code:
TypeScript/src/services/stringCompletions.ts
Line 633 in b87e301
If
scriptDirectory
is the same asrootDirectory
,relativeDirectory
is computed to""
, andrelativeDirectory
is correctly appended to the root directories. This results in TSServer correctly searching bothsrc/
andbuild/dst
.However, if
scriptDirectory
(in our example/Volumes/git/app/src/tmp
) is a child ofrootDirectory
(/Volumes/git/app/src
), this line will result inrelativeDirectory
being/tmp
instead of/tmp
, causingcombinePaths
a few lines down to output/tmp
. This results in TSServer incorrectly searchingsrc/tmp
and/tmp
(instead ofbuild/dts/tmp
). This is effectively an off-by-one error because we're accidentally grabbing a leading slash.🔎 Search Terms
tsserver, getPathCompletionsAtPosition, import path, rootDirs
🕗 Version & Regression Information
As far as I can tell from the code this bug has always existed.
The text was updated successfully, but these errors were encountered: