Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix: unecessary program updates by removing timeout methods#1693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
274a791
fix: unecessary program updates by removing timeout methods
sheetalkamataecb6bb
fix spelling for CI
bradzacher0510a22
fix: store hash of empty content file to avoid program update
sheetalkamat0351eea
fix: instead of not using timeouts, callback them before getProgram
sheetalkamat054204a
Merge branch 'master' into noTimeouts
sheetalkamat0b0760c
fix: lint errors
sheetalkamat2405fae
fix: use Abstract Builder instead of semantic builder
sheetalkamatd9a5867
fix: handle typescript version for the no timeout fix
sheetalkamat75e9a1a
Merge branch 'master' into noTimeouts
sheetalkamat675baf7
Merge branch 'master' into noTimeouts
sheetalkamatFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
68 changes: 43 additions & 25 deletionspackages/typescript-estree/src/create-program/createWatchProgram.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import debug from 'debug'; | ||
import fs from 'fs'; | ||
import semver from 'semver'; | ||
import * as ts from 'typescript'; | ||
import { Extra } from '../parser-options'; | ||
import { WatchCompilerHostOfConfigFile } from './WatchCompilerHostOfConfigFile'; | ||
@@ -18,7 +19,7 @@ const log = debug('typescript-eslint:typescript-estree:createWatchProgram'); | ||
*/ | ||
const knownWatchProgramMap = new Map< | ||
CanonicalPath, | ||
ts.WatchOfConfigFile<ts.BuilderProgram> | ||
>(); | ||
/** | ||
@@ -226,21 +227,25 @@ function getProgramsForProjects( | ||
return results; | ||
} | ||
const isRunningNoTimeoutFix = semver.satisfies(ts.version, '>=3.9.0-beta', { | ||
includePrerelease: true, | ||
}); | ||
function createWatchProgram( | ||
tsconfigPath: string, | ||
extra: Extra, | ||
): ts.WatchOfConfigFile<ts.BuilderProgram> { | ||
log('Creating watch program for %s.', tsconfigPath); | ||
// create compiler host | ||
const watchCompilerHost = ts.createWatchCompilerHost( | ||
tsconfigPath, | ||
createDefaultCompilerOptionsFromExtra(extra), | ||
ts.sys, | ||
ts.createAbstractBuilder, | ||
diagnosticReporter, | ||
/*reportWatchStatus*/ () => {}, | ||
) as WatchCompilerHostOfConfigFile<ts.BuilderProgram>; | ||
// ensure readFile reads the code being linted instead of the copy on disk | ||
const oldReadFile = watchCompilerHost.readFile; | ||
@@ -250,7 +255,7 @@ function createWatchProgram( | ||
filePath === currentLintOperationState.filePath | ||
? currentLintOperationState.code | ||
: oldReadFile(filePath, encoding); | ||
if (fileContent !== undefined) { | ||
parsedFilesSeenHash.set(filePath, createHash(fileContent)); | ||
} | ||
return fileContent; | ||
@@ -309,25 +314,38 @@ function createWatchProgram( | ||
); | ||
oldOnDirectoryStructureHostCreate(host); | ||
}; | ||
watchCompilerHost.trace = log; | ||
// Since we don't want to asynchronously update program we want to disable timeout methods | ||
// So any changes in the program will be delayed and updated when getProgram is called on watch | ||
let callback: (() => void) | undefined; | ||
sheetalkamat marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if (isRunningNoTimeoutFix) { | ||
watchCompilerHost.setTimeout = undefined; | ||
watchCompilerHost.clearTimeout = undefined; | ||
} else { | ||
log('Running without timeout fix'); | ||
// But because of https://github.com/microsoft/TypeScript/pull/37308 we cannot just set it to undefined | ||
// instead save it and call before getProgram is called | ||
watchCompilerHost.setTimeout = (cb, _ms, ...args): unknown => { | ||
callback = cb.bind(/*this*/ undefined, ...args); | ||
return callback; | ||
}; | ||
watchCompilerHost.clearTimeout = (): void => { | ||
callback = undefined; | ||
}; | ||
} | ||
const watch = ts.createWatchProgram(watchCompilerHost); | ||
if (!isRunningNoTimeoutFix) { | ||
const originalGetProgram = watch.getProgram; | ||
watch.getProgram = (): ts.BuilderProgram => { | ||
if (callback) { | ||
callback(); | ||
} | ||
callback = undefined; | ||
return originalGetProgram.call(watch); | ||
}; | ||
} | ||
return watch; | ||
} | ||
function hasTSConfigChanged(tsconfigPath: CanonicalPath): boolean { | ||
@@ -347,7 +365,7 @@ function hasTSConfigChanged(tsconfigPath: CanonicalPath): boolean { | ||
} | ||
function maybeInvalidateProgram( | ||
existingWatch: ts.WatchOfConfigFile<ts.BuilderProgram>, | ||
filePath: CanonicalPath, | ||
tsconfigPath: CanonicalPath, | ||
): ts.Program | null { | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.