Skip to content

Conversation

@vladima
Copy link
Contributor

@vladima vladima commented Sep 27, 2015

…te name. Fixes #2192.

The same file can be referenced using different relative names. For example:

 folder1
   |---folder2
   |      |---file1.ts
   |      |---folder3

if folder3 is current directory then file1.ts can be referenced as ../file1.ts or as ../../folder2/file1.ts. Both these names are correct however now we'll record resolutions for both relative and and absolute names only for the first requested file i.e. ../file1.ts. Subsequent request to find source file for ../../folder2/file1.ts will fail to find it for relative name but will succeed to do it for absolute name and this result will be returned without recording association 'relative name' -> 'resolved file'. This means that calling program.getSourceFile('../../folder2/file1.ts') will return undefined though clearly this file is in the program. Solution: when lookup fails for relative name but succeeds for absolute - record association for relative as well.

Also PR reduces nesting in findSourceFile and drops unnecessary calls to CompilerHost.getCannonicalFileName - FileMap will do this internally anyways so there is no need to do it several times

@DanielRosenwasser
Copy link
Member

I recall we made some optimization to avoid recalculating dependencies. Could there be an issue in caching for language service scenarios? For instance, let's say you have files ./A.ts and ./B.ts. It doesn't really matter what's in B.ts, it just matters that A.ts incorrectly references it:

import "B";

Now you introduce node_modules/B.d.ts. Does the import of B suddenly work correctly?

@vladima
Copy link
Contributor Author

vladima commented Sep 29, 2015

@DanielRosenwasser no, it should be fine. We never directly interpret module name in import as file name. The whole process will look like:

  • parse 'A.ts'
  • try to resolve imports in 'A' to actual module files
  • (assuming that current module target is CommonJS) "B" is not relative name so run node resolution process to find module named "B". If it cannot be found then resolution fails and internally we'll record this in internal map on the SourceFile. findSourceFile will be called only if resolution succeeded.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 29, 2015

👍

vladima added a commit that referenced this pull request Sep 29, 2015
record resolution for relative file name if file was found via absolu…
@vladima vladima merged commit bfa390e into master Sep 29, 2015
@vladima vladima deleted the relativeModuleNamesInImports branch September 29, 2015 17:17
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler fails to find external module referenced by two different files

5 participants