Skip to content

Commit e53646a

Browse files
committed
harden the implementation of Program.getSourceFile to handle all relative file names correctly
1 parent dae5a62 commit e53646a

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/compiler/program.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,9 @@ namespace ts {
568568
}
569569

570570
function getSourceFile(fileName: string) {
571-
return filesByName.get(fileName);
571+
// first try to use file name as is to find file
572+
// then try to convert relative file name to absolute and use it to retrieve source file
573+
return filesByName.get(fileName) || filesByName.get(getNormalizedAbsolutePath(fileName, host.getCurrentDirectory()));
572574
}
573575

574576
function getDiagnosticsHelper(

tests/cases/unittests/moduleResolution.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ export = C;
208208
assert.equal(syntacticDiagnostics.length, 0, `expect no syntactic diagnostics, got: ${JSON.stringify(syntacticDiagnostics.map(diagnosticToString))}`);
209209
const semanticDiagnostics = program.getSemanticDiagnostics();
210210
assert.equal(semanticDiagnostics.length, 0, `expect no semantic diagnostics, got: ${JSON.stringify(semanticDiagnostics.map(diagnosticToString))}`);
211+
212+
// try to get file using a relative name
213+
const fileC = program.getSourceFile("../../../c/third/class_c.ts");
214+
assert.isTrue(fileC !== undefined, `expected to get file by relative name, got ${fileC}`);
211215
});
212216

213217
function diagnosticToString(diagnostic: Diagnostic) {

0 commit comments

Comments
 (0)