Skip to content

Commit 838bd55

Browse files
authored
Make #884 regression test repro correctly; also fix the bug (#999)
* tweak issue #884 regression test so it triggers on master * Simplify while stil reproducing the issue * getScriptFileNames returns rootFileNames, not everything from memory cache
1 parent d989d4c commit 838bd55

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/index.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,13 @@ export function create (rawOptions: CreateOptions = {}): Register {
477477
// Use full language services when the fast option is disabled.
478478
if (!transpileOnly) {
479479
const fileContents = new Map<string, string>()
480-
const rootFileNames = config.fileNames.slice()
480+
const rootFileNames = new Set(config.fileNames)
481481
const cachedReadFile = cachedLookup(debugFn('readFile', readFile))
482482

483483
// Use language services by default (TODO: invert next major version).
484484
if (!options.compilerHost) {
485485
let projectVersion = 1
486-
const fileVersions = new Map(rootFileNames.map(fileName => [fileName, 0]))
486+
const fileVersions = new Map(Array.from(rootFileNames).map(fileName => [fileName, 0]))
487487

488488
const getCustomTransformers = () => {
489489
if (typeof transformers === 'function') {
@@ -497,7 +497,7 @@ export function create (rawOptions: CreateOptions = {}): Register {
497497
// Create the compiler host for type checking.
498498
const serviceHost: _ts.LanguageServiceHost = {
499499
getProjectVersion: () => String(projectVersion),
500-
getScriptFileNames: () => Array.from(fileVersions.keys()),
500+
getScriptFileNames: () => Array.from(rootFileNames),
501501
getScriptVersion: (fileName: string) => {
502502
const version = fileVersions.get(fileName)
503503
return version ? version.toString() : ''
@@ -533,9 +533,12 @@ export function create (rawOptions: CreateOptions = {}): Register {
533533
const service = ts.createLanguageService(serviceHost, registry)
534534

535535
const updateMemoryCache = (contents: string, fileName: string) => {
536-
// Add to `rootFiles` when discovered for the first time.
537-
if (!fileVersions.has(fileName)) {
538-
rootFileNames.push(fileName)
536+
// Add to `rootFiles` if not already there
537+
// This is necessary to force TS to emit output
538+
if (!rootFileNames.has(fileName)) {
539+
rootFileNames.add(fileName)
540+
// Increment project version for every change to rootFileNames.
541+
projectVersion++
539542
}
540543

541544
const previousVersion = fileVersions.get(fileName) || 0
@@ -637,14 +640,14 @@ export function create (rawOptions: CreateOptions = {}): Register {
637640
// Fallback for older TypeScript releases without incremental API.
638641
let builderProgram = ts.createIncrementalProgram
639642
? ts.createIncrementalProgram({
640-
rootNames: rootFileNames.slice(),
643+
rootNames: Array.from(rootFileNames),
641644
options: config.options,
642645
host: host,
643646
configFileParsingDiagnostics: config.errors,
644647
projectReferences: config.projectReferences
645648
})
646649
: ts.createEmitAndSemanticDiagnosticsBuilderProgram(
647-
rootFileNames.slice(),
650+
Array.from(rootFileNames),
648651
config.options,
649652
host,
650653
undefined,
@@ -665,13 +668,13 @@ export function create (rawOptions: CreateOptions = {}): Register {
665668

666669
// Add to `rootFiles` when discovered by compiler for the first time.
667670
if (sourceFile === undefined) {
668-
rootFileNames.push(fileName)
671+
rootFileNames.add(fileName)
669672
}
670673

671674
// Update program when file changes.
672675
if (sourceFile === undefined || sourceFile.text !== contents) {
673676
builderProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram(
674-
rootFileNames.slice(),
677+
Array.from(rootFileNames),
675678
config.options,
676679
host,
677680
builderProgram,

tests/issue-884/index-2.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export {};
2+
3+
const timeout = setTimeout(() => {}, 0);
4+
5+
if (timeout.unref) {
6+
timeout.unref();
7+
}

tests/issue-884/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const timeout = setTimeout(() => {}, 0);
2-
3-
if (timeout.unref) {
4-
timeout.unref();
5-
}
1+
// 2x index files required so that memory cache is populated with all build-in lib and @types
2+
// declarations *before* this require() call.
3+
require('./index-2');

0 commit comments

Comments
 (0)