From 7e482b27fbdb469654c87e11ba72a3986b22caf7 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Mon, 2 Apr 2018 20:58:37 +0200 Subject: [PATCH] fix: `CompilerHost.getSourceFile` is being called for odd filenames besides the one being compiled Ignore falsy file names from `getDefaultLibraryFileName` Closes: #13629 --- Jakefile.js | 1 + src/compiler/program.ts | 7 ++-- src/harness/tsconfig.json | 1 + .../unittests/programNoParseFalsyFileNames.ts | 37 +++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/harness/unittests/programNoParseFalsyFileNames.ts diff --git a/Jakefile.js b/Jakefile.js index f2821a01686..d570a7cfc3d 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -154,6 +154,7 @@ var harnessSources = harnessCoreSources.concat([ "transform.ts", "customTransforms.ts", "programMissingFiles.ts", + "programNoParseFalsyFileNames.ts", "symbolWalker.ts", "languageService.ts", "publicApi.ts", diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 342b697b977..79baae92fa8 100755 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -611,8 +611,9 @@ namespace ts { if (!skipDefaultLib) { // If '--lib' is not specified, include default library file according to '--target' // otherwise, using options specified in '--lib' instead of '--target' default library file - if (!options.lib) { - processRootFile(getDefaultLibraryFileName(), /*isDefaultLib*/ true); + const defaultLibraryFileName = getDefaultLibraryFileName(); + if (!options.lib && defaultLibraryFileName) { + processRootFile(defaultLibraryFileName, /*isDefaultLib*/ true); } else { forEach(options.lib, libFileName => { @@ -1117,7 +1118,7 @@ namespace ts { // otherwise, using options specified in '--lib' instead of '--target' default library file const equalityComparer = host.useCaseSensitiveFileNames() ? equateStringsCaseSensitive : equateStringsCaseInsensitive; if (!options.lib) { - return equalityComparer(file.fileName, getDefaultLibraryFileName()); + return equalityComparer(file.fileName, getDefaultLibraryFileName()); } else { return forEach(options.lib, libFileName => equalityComparer(file.fileName, combinePaths(defaultLibraryPath, libFileName))); diff --git a/src/harness/tsconfig.json b/src/harness/tsconfig.json index 54766ed02ec..c9521979776 100644 --- a/src/harness/tsconfig.json +++ b/src/harness/tsconfig.json @@ -135,6 +135,7 @@ "./unittests/telemetry.ts", "./unittests/languageService.ts", "./unittests/programMissingFiles.ts", + "./unittests/programNoParseFalsyFileNames.ts", "./unittests/publicApi.ts", "./unittests/hostNewLineSupport.ts" ] diff --git a/src/harness/unittests/programNoParseFalsyFileNames.ts b/src/harness/unittests/programNoParseFalsyFileNames.ts new file mode 100644 index 00000000000..3270f6b14be --- /dev/null +++ b/src/harness/unittests/programNoParseFalsyFileNames.ts @@ -0,0 +1,37 @@ +/// +namespace ts { + describe("programNoParseFalsyFileNames", () => { + let program: Program; + + beforeEach(() => { + const testSource = ` + class Foo extends HTMLElement { + bar: string = 'baz'; + }`; + + const host: CompilerHost = { + getSourceFile: (fileName: string, languageVersion: ScriptTarget, _onError?: (message: string) => void) => { + return fileName === "test.ts" ? createSourceFile(fileName, testSource, languageVersion) : undefined; + }, + getDefaultLibFileName: () => "", + writeFile: (_fileName, _content) => { throw new Error("unsupported"); }, + getCurrentDirectory: () => sys.getCurrentDirectory(), + getCanonicalFileName: fileName => sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(), + getNewLine: () => sys.newLine, + useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames, + fileExists: fileName => fileName === "test.ts", + readFile: fileName => fileName === "test.ts" ? testSource : undefined, + resolveModuleNames: (_moduleNames: string[], _containingFile: string) => { throw new Error("unsupported"); }, + getDirectories: _path => { throw new Error("unsupported"); }, + }; + + program = createProgram(["test.ts"], { module: ModuleKind.ES2015 }, host); + }); + + it("should not have missing file paths", () => { + assert(program.getSourceFiles().length === 1, "expected 'getSourceFiles' length to be 1"); + assert(program.getMissingFilePaths().length === 0, "expected 'getMissingFilePaths' length to be 0"); + assert(program.getFileProcessingDiagnostics().getDiagnostics().length === 0, "expected 'getFileProcessingDiagnostics' length to be 0"); + }); + }); +} \ No newline at end of file