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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(feat) watch tsconfig and extended tsconfig #1535
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5216b9f
first crack
jasonlyu123 1bae9e5
update diagnotics when reloaded
jasonlyu123 25f4146
svelte check watch tsconfig
jasonlyu123 92bbea2
space
jasonlyu123 a22e283
move watch functions into createLanguageService
jasonlyu123 eb1fd34
move the watch callback out
jasonlyu123 361ec4e
test and actually call the onProjectReloaded hook
jasonlyu123 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Empty file.
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -18,6 +18,7 @@ import { ensureRealSvelteFilePath, findTsConfigPath, hasTsExtensions } from './u | |
export interface LanguageServiceContainer { | ||
readonly tsconfigPath: string; | ||
readonly compilerOptions: ts.CompilerOptions; | ||
readonly extendedConfigPaths: Set<string>; | ||
/** | ||
* @internal Public for tests only | ||
*/ | ||
|
@@ -37,11 +38,17 @@ export interface LanguageServiceContainer { | |
* Only works for TS versions that have ScriptKind.Deferred | ||
*/ | ||
fileBelongsToProject(filePath: string): boolean; | ||
|
||
dispose(): void; | ||
} | ||
|
||
const maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; // 20 MB | ||
const services = new Map<string, Promise<LanguageServiceContainer>>(); | ||
const serviceSizeMap: Map<string, number> = new Map(); | ||
const serviceSizeMap = new Map<string, number>(); | ||
const configWatchers = new Map<string, ts.FileWatcher>(); | ||
const extendedConfigWatchers = new Map<string, ts.FileWatcher>(); | ||
const extendedConfigToTsConfigPath = new Map<string, Set<string>>(); | ||
const pendingReloads = new Set<string>(); | ||
|
||
/** | ||
* For testing only: Reset the cache for services. | ||
|
@@ -60,6 +67,9 @@ export interface LanguageServiceDocumentContext { | |
createDocument: (fileName: string, content: string) => Document; | ||
globalSnapshotsManager: GlobalSnapshotsManager; | ||
notifyExceedSizeLimit: (() => void) | undefined; | ||
extendedConfigCache: Map<string, ts.ExtendedConfigCacheEntry>; | ||
onProjectReloaded: (() => void) | undefined; | ||
watchTsConfig: boolean; | ||
} | ||
|
||
export async function getService( | ||
|
@@ -91,22 +101,107 @@ export async function getServiceForTsconfig( | |
if (services.has(tsconfigPath)) { | ||
service = await services.get(tsconfigPath)!; | ||
} else { | ||
Logger.log('Initialize new ts service at ', tsconfigPath); | ||
const reloading = pendingReloads.has(tsconfigPath); | ||
|
||
if (reloading) { | ||
Logger.log('Reloading ts service at ', tsconfigPath, ' due to config updated'); | ||
} else { | ||
Logger.log('Initialize new ts service at ', tsconfigPath); | ||
} | ||
|
||
pendingReloads.delete(tsconfigPath); | ||
const newService = createLanguageService(tsconfigPath, docContext); | ||
services.set(tsconfigPath, newService); | ||
service = await newService; | ||
|
||
updateExtendedConfigDependents(service); | ||
watchConfigFile(service, docContext); | ||
} | ||
|
||
return service; | ||
} | ||
|
||
function updateExtendedConfigDependents(service: LanguageServiceContainer) { | ||
service.extendedConfigPaths.forEach((extendedConfig) => { | ||
let dependedTsConfig = extendedConfigToTsConfigPath.get(extendedConfig); | ||
if (!dependedTsConfig) { | ||
dependedTsConfig = new Set(); | ||
extendedConfigToTsConfigPath.set(extendedConfig, dependedTsConfig); | ||
} | ||
|
||
dependedTsConfig.add(service.tsconfigPath); | ||
}); | ||
} | ||
|
||
function watchConfigFile(ls: LanguageServiceContainer, docContext: LanguageServiceDocumentContext) { | ||
if (!ts.sys.watchFile || !docContext.watchTsConfig) { | ||
return; | ||
} | ||
|
||
if (!configWatchers.has(ls.tsconfigPath) && ls.tsconfigPath) { | ||
configWatchers.set( | ||
ls.tsconfigPath, | ||
ts.sys.watchFile(ls.tsconfigPath, (filename, kind) => | ||
watchConfigCallback(filename, kind, docContext) | ||
) | ||
); | ||
} | ||
|
||
for (const config of ls.extendedConfigPaths) { | ||
if (extendedConfigWatchers.has(config)) { | ||
continue; | ||
} | ||
extendedConfigWatchers.set( | ||
config, | ||
ts.sys.watchFile(config, (filename, kind) => | ||
watchExtendedConfigCallback(filename, kind, docContext) | ||
) | ||
); | ||
} | ||
} | ||
|
||
async function watchExtendedConfigCallback( | ||
extendedConfig: string, | ||
kind: ts.FileWatcherEventKind, | ||
docContext: LanguageServiceDocumentContext | ||
) { | ||
docContext.extendedConfigCache.delete(extendedConfig); | ||
|
||
extendedConfigToTsConfigPath.get(extendedConfig)?.forEach((config) => { | ||
services.delete(config); | ||
pendingReloads.add(config); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was at first confused why this is enough, until I realized that you don't need to explicitly restart the server because the next |
||
}); | ||
} | ||
|
||
async function watchConfigCallback( | ||
fileName: string, | ||
kind: ts.FileWatcherEventKind, | ||
docContext: LanguageServiceDocumentContext | ||
) { | ||
const oldService = services.get(fileName); | ||
services.delete(fileName); | ||
docContext.extendedConfigCache.delete(fileName); | ||
(await oldService)?.dispose(); | ||
pendingReloads.add(fileName); | ||
|
||
if (kind === ts.FileWatcherEventKind.Deleted && !extendedConfigToTsConfigPath.has(fileName)) { | ||
configWatchers.get(fileName)?.close(); | ||
configWatchers.delete(fileName); | ||
} | ||
} | ||
|
||
async function createLanguageService( | ||
tsconfigPath: string, | ||
docContext: LanguageServiceDocumentContext | ||
): Promise<LanguageServiceContainer> { | ||
const workspacePath = tsconfigPath ? dirname(tsconfigPath) : ''; | ||
|
||
const { options: compilerOptions, fileNames: files, raw } = getParsedConfig(); | ||
const { | ||
options: compilerOptions, | ||
fileNames: files, | ||
raw, | ||
extendedConfigPaths | ||
} = getParsedConfig(); | ||
// raw is the tsconfig merged with extending config | ||
// see: https://github.com/microsoft/TypeScript/blob/08e4f369fbb2a5f0c30dee973618d65e6f7f09f8/src/compiler/commandLineParser.ts#L2537 | ||
const snapshotManager = new SnapshotManager( | ||
|
@@ -172,9 +267,10 @@ async function createLanguageService( | |
typingsNamespace: raw?.svelteOptions?.namespace || 'svelteHTML' | ||
}; | ||
|
||
docContext.globalSnapshotsManager.onChange(() => { | ||
const onSnapshotChange = () => { | ||
projectVersion++; | ||
}); | ||
}; | ||
docContext.globalSnapshotsManager.onChange(onSnapshotChange); | ||
|
||
reduceLanguageServiceCapabilityIfFileSizeTooBig(); | ||
|
||
|
@@ -188,7 +284,9 @@ async function createLanguageService( | |
updateTsOrJsFile, | ||
hasFile, | ||
fileBelongsToProject, | ||
snapshotManager | ||
snapshotManager, | ||
extendedConfigPaths, | ||
dispose | ||
}; | ||
|
||
function deleteSnapshot(filePath: string): void { | ||
|
@@ -317,6 +415,24 @@ async function createLanguageService( | |
); | ||
} | ||
|
||
const extendedConfigPaths = new Set<string>(); | ||
const { extendedConfigCache } = docContext; | ||
const cacheMonitorProxy = { | ||
...docContext.extendedConfigCache, | ||
get(key: string) { | ||
extendedConfigPaths.add(key); | ||
return extendedConfigCache.get(key); | ||
}, | ||
has(key: string) { | ||
extendedConfigPaths.add(key); | ||
return extendedConfigCache.has(key); | ||
}, | ||
set(key: string, value: ts.ExtendedConfigCacheEntry) { | ||
extendedConfigPaths.add(key); | ||
return extendedConfigCache.set(key, value); | ||
} | ||
}; | ||
|
||
const parsedConfig = ts.parseJsonConfigFileContent( | ||
configJson, | ||
ts.sys, | ||
|
@@ -335,7 +451,8 @@ async function createLanguageService( | |
ts.ScriptKind.Deferred ?? | ||
(docContext.useNewTransformation ? ts.ScriptKind.TS : ts.ScriptKind.TSX) | ||
} | ||
] | ||
], | ||
cacheMonitorProxy | ||
); | ||
|
||
const compilerOptions: ts.CompilerOptions = { | ||
|
@@ -391,7 +508,8 @@ async function createLanguageService( | |
return { | ||
...parsedConfig, | ||
fileNames: parsedConfig.fileNames.map(normalizePath), | ||
options: compilerOptions | ||
options: compilerOptions, | ||
extendedConfigPaths | ||
}; | ||
} | ||
|
||
|
@@ -429,6 +547,12 @@ async function createLanguageService( | |
docContext.notifyExceedSizeLimit?.(); | ||
} | ||
} | ||
|
||
function dispose() { | ||
languageService.dispose(); | ||
snapshotManager.dispose(); | ||
docContext.globalSnapshotsManager.removeChangeListener(onSnapshotChange); | ||
} | ||
} | ||
|
||
/** | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code style: I would prefer it to have these added top level functions (at least the first two) inside
createLanguageService
. This would remove the need to exposeextendedConfigPaths