From 8622dca21eff825b80aab4fb0f0a1230b5475c85 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 6 Apr 2020 23:58:35 -0400 Subject: [PATCH 1/3] tweak issue #884 regression test so it triggers on master --- tests/issue-884/index-2.ts | 5 +++++ tests/issue-884/index-3.ts | 5 +++++ tests/issue-884/index.ts | 4 ++++ 3 files changed, 14 insertions(+) create mode 100644 tests/issue-884/index-2.ts create mode 100644 tests/issue-884/index-3.ts diff --git a/tests/issue-884/index-2.ts b/tests/issue-884/index-2.ts new file mode 100644 index 000000000..84dc7d9a6 --- /dev/null +++ b/tests/issue-884/index-2.ts @@ -0,0 +1,5 @@ +const timeout = setTimeout(() => {}, 0); + +if (timeout.unref) { + timeout.unref(); +} diff --git a/tests/issue-884/index-3.ts b/tests/issue-884/index-3.ts new file mode 100644 index 000000000..84dc7d9a6 --- /dev/null +++ b/tests/issue-884/index-3.ts @@ -0,0 +1,5 @@ +const timeout = setTimeout(() => {}, 0); + +if (timeout.unref) { + timeout.unref(); +} diff --git a/tests/issue-884/index.ts b/tests/issue-884/index.ts index 84dc7d9a6..eb4afa216 100644 --- a/tests/issue-884/index.ts +++ b/tests/issue-884/index.ts @@ -1,5 +1,9 @@ +import './index-2'; + const timeout = setTimeout(() => {}, 0); if (timeout.unref) { timeout.unref(); } + +require('./index-3'); From 3d9cb553da1f1d8fde33e9e3496c66240fcc7be5 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Tue, 7 Apr 2020 00:08:24 -0400 Subject: [PATCH 2/3] Simplify while stil reproducing the issue --- tests/issue-884/index-2.ts | 2 ++ tests/issue-884/index-3.ts | 5 ----- tests/issue-884/index.ts | 12 +++--------- 3 files changed, 5 insertions(+), 14 deletions(-) delete mode 100644 tests/issue-884/index-3.ts diff --git a/tests/issue-884/index-2.ts b/tests/issue-884/index-2.ts index 84dc7d9a6..ddacd3c42 100644 --- a/tests/issue-884/index-2.ts +++ b/tests/issue-884/index-2.ts @@ -1,3 +1,5 @@ +export {}; + const timeout = setTimeout(() => {}, 0); if (timeout.unref) { diff --git a/tests/issue-884/index-3.ts b/tests/issue-884/index-3.ts deleted file mode 100644 index 84dc7d9a6..000000000 --- a/tests/issue-884/index-3.ts +++ /dev/null @@ -1,5 +0,0 @@ -const timeout = setTimeout(() => {}, 0); - -if (timeout.unref) { - timeout.unref(); -} diff --git a/tests/issue-884/index.ts b/tests/issue-884/index.ts index eb4afa216..8c23188df 100644 --- a/tests/issue-884/index.ts +++ b/tests/issue-884/index.ts @@ -1,9 +1,3 @@ -import './index-2'; - -const timeout = setTimeout(() => {}, 0); - -if (timeout.unref) { - timeout.unref(); -} - -require('./index-3'); +// 2x index files required so that memory cache is populated with all build-in lib and @types +// declarations *before* this require() call. +require('./index-2'); From 7eb0c30c70b046be5b503833101cf0ade45ec050 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Tue, 7 Apr 2020 22:21:57 -0400 Subject: [PATCH 3/3] getScriptFileNames returns rootFileNames, not everything from memory cache --- src/index.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index e3d293a53..18d5799ed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -453,13 +453,13 @@ export function create (rawOptions: CreateOptions = {}): Register { // Use full language services when the fast option is disabled. if (!transpileOnly) { const fileContents = new Map() - const rootFileNames = config.fileNames.slice() + const rootFileNames = new Set(config.fileNames) const cachedReadFile = cachedLookup(debugFn('readFile', readFile)) // Use language services by default (TODO: invert next major version). if (!options.compilerHost) { let projectVersion = 1 - const fileVersions = new Map(rootFileNames.map(fileName => [fileName, 0])) + const fileVersions = new Map(Array.from(rootFileNames).map(fileName => [fileName, 0])) const getCustomTransformers = () => { if (typeof transformers === 'function') { @@ -473,7 +473,7 @@ export function create (rawOptions: CreateOptions = {}): Register { // Create the compiler host for type checking. const serviceHost: _ts.LanguageServiceHost = { getProjectVersion: () => String(projectVersion), - getScriptFileNames: () => Array.from(fileVersions.keys()), + getScriptFileNames: () => Array.from(rootFileNames), getScriptVersion: (fileName: string) => { const version = fileVersions.get(fileName) return version ? version.toString() : '' @@ -509,9 +509,12 @@ export function create (rawOptions: CreateOptions = {}): Register { const service = ts.createLanguageService(serviceHost, registry) const updateMemoryCache = (contents: string, fileName: string) => { - // Add to `rootFiles` when discovered for the first time. - if (!fileVersions.has(fileName)) { - rootFileNames.push(fileName) + // Add to `rootFiles` if not already there + // This is necessary to force TS to emit output + if (!rootFileNames.has(fileName)) { + rootFileNames.add(fileName) + // Increment project version for every change to rootFileNames. + projectVersion++ } const previousVersion = fileVersions.get(fileName) || 0 @@ -613,14 +616,14 @@ export function create (rawOptions: CreateOptions = {}): Register { // Fallback for older TypeScript releases without incremental API. let builderProgram = ts.createIncrementalProgram ? ts.createIncrementalProgram({ - rootNames: rootFileNames.slice(), + rootNames: Array.from(rootFileNames), options: config.options, host: host, configFileParsingDiagnostics: config.errors, projectReferences: config.projectReferences }) : ts.createEmitAndSemanticDiagnosticsBuilderProgram( - rootFileNames.slice(), + Array.from(rootFileNames), config.options, host, undefined, @@ -641,13 +644,13 @@ export function create (rawOptions: CreateOptions = {}): Register { // Add to `rootFiles` when discovered by compiler for the first time. if (sourceFile === undefined) { - rootFileNames.push(fileName) + rootFileNames.add(fileName) } // Update program when file changes. if (sourceFile === undefined || sourceFile.text !== contents) { builderProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram( - rootFileNames.slice(), + Array.from(rootFileNames), config.options, host, builderProgram,