Skip to content
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 7 commits into from Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -13,23 +13,28 @@ import {
} from './service';
import { GlobalSnapshotsManager, SnapshotManager } from './SnapshotManager';

export class LSAndTSDocResolver {
interface LSAndTSDocResolverOptions {
notifyExceedSizeLimit?: () => void;
/**
* True, if used in the context of svelte-check
*/
isSvelteCheck?: boolean;

/**
*
* @param docManager
* @param workspaceUris
* @param configManager
* @param notifyExceedSizeLimit
* @param isSvelteCheck True, if used in the context of svelte-check
* @param tsconfigPath This should only be set via svelte-check. Makes sure all documents are resolved to that tsconfig. Has to be absolute.
* This should only be set via svelte-check. Makes sure all documents are resolved to that tsconfig. Has to be absolute.
*/
tsconfigPath?: string;

onProjectReloaded?: () => void;
watchTsConfig?: boolean
}

export class LSAndTSDocResolver {
constructor(
private readonly docManager: DocumentManager,
private readonly workspaceUris: string[],
private readonly configManager: LSConfigManager,
private readonly notifyExceedSizeLimit?: () => void,
private readonly isSvelteCheck = false,
private readonly tsconfigPath?: string
private readonly options?: LSAndTSDocResolverOptions
) {
const handleDocumentChange = (document: Document) => {
// This refreshes the document in the ts language service
Expand Down Expand Up @@ -65,15 +70,19 @@ export class LSAndTSDocResolver {
};

private globalSnapshotsManager = new GlobalSnapshotsManager();
private extendedConfigCache = new Map<string, ts.ExtendedConfigCacheEntry>();

private get lsDocumentContext(): LanguageServiceDocumentContext {
return {
ambientTypesSource: this.isSvelteCheck ? 'svelte-check' : 'svelte2tsx',
ambientTypesSource: this.options?.isSvelteCheck ? 'svelte-check' : 'svelte2tsx',
createDocument: this.createDocument,
useNewTransformation: this.configManager.getConfig().svelte.useNewTransformation,
transformOnTemplateError: !this.isSvelteCheck,
transformOnTemplateError: !this.options?.isSvelteCheck,
globalSnapshotsManager: this.globalSnapshotsManager,
notifyExceedSizeLimit: this.notifyExceedSizeLimit
notifyExceedSizeLimit: this.options?.notifyExceedSizeLimit,
extendedConfigCache: this.extendedConfigCache,
onProjectReloaded: this.options?.onProjectReloaded,
watchTsConfig: !!this.options?.watchTsConfig
};
}

Expand Down Expand Up @@ -157,8 +166,8 @@ export class LSAndTSDocResolver {
}

async getTSService(filePath?: string): Promise<LanguageServiceContainer> {
if (this.tsconfigPath) {
return getServiceForTsconfig(this.tsconfigPath, this.lsDocumentContext);
if (this.options?.tsconfigPath) {
return getServiceForTsconfig(this.options?.tsconfigPath, this.lsDocumentContext);
}
if (!filePath) {
throw new Error('Cannot call getTSService without filePath and without tsconfigPath');
Expand Down
Empty file.
39 changes: 26 additions & 13 deletions packages/language-server/src/plugins/typescript/SnapshotManager.ts
Expand Up @@ -5,6 +5,8 @@ import { TextDocumentContentChangeEvent } from 'vscode-languageserver';
import { normalizePath } from '../../utils';
import { EventEmitter } from 'events';

type SnapshotChangeHandler = (fileName: string, newDocument: DocumentSnapshot | undefined) => void;

/**
* Every snapshot corresponds to a unique file on disk.
* A snapshot can be part of multiple projects, but for a given file path
Expand Down Expand Up @@ -60,9 +62,13 @@ export class GlobalSnapshotsManager {
}
}

onChange(listener: (fileName: string, newDocument: DocumentSnapshot | undefined) => void) {
onChange(listener: SnapshotChangeHandler) {
this.emitter.on('change', listener);
}

removeChangeListener(listener: SnapshotChangeHandler) {
this.emitter.off('change', listener);
}
}

export interface TsFilesSpec {
Expand Down Expand Up @@ -92,18 +98,21 @@ export class SnapshotManager {
private fileSpec: TsFilesSpec,
private workspaceRoot: string
) {
this.globalSnapshotsManager.onChange((fileName, document) => {
// Only delete/update snapshots, don't add new ones,
// as they could be from another TS service and this
// snapshot manager can't reach this file.
// For these, instead wait on a `get` method invocation
// and set them "manually" in the set/update methods.
if (!document) {
this.documents.delete(fileName);
} else if (this.documents.has(fileName)) {
this.documents.set(fileName, document);
}
});
this.onSnapshotChange = this.onSnapshotChange.bind(this);
this.globalSnapshotsManager.onChange(this.onSnapshotChange);
}

private onSnapshotChange(fileName: string, document: DocumentSnapshot | undefined) {
// Only delete/update snapshots, don't add new ones,
// as they could be from another TS service and this
// snapshot manager can't reach this file.
// For these, instead wait on a `get` method invocation
// and set them "manually" in the set/update methods.
if (!document) {
this.documents.delete(fileName);
} else if (this.documents.has(fileName)) {
this.documents.set(fileName, document);
}
}

updateProjectFiles(): void {
Expand Down Expand Up @@ -191,6 +200,10 @@ export class SnapshotManager {
);
}
}

dispose() {
this.globalSnapshotsManager.removeChangeListener(this.onSnapshotChange);
}
}

export const ignoredBuildDirectories = ['__sapper__', '.svelte-kit'];
140 changes: 132 additions & 8 deletions packages/language-server/src/plugins/typescript/service.ts
Expand Up @@ -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
*/
Expand All @@ -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.
Expand All @@ -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(
Expand Down Expand Up @@ -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) {
Copy link
Member

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 expose extendedConfigPaths

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);
Copy link
Member

Choose a reason for hiding this comment

The 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 getService call will do this. Could you add a comment here and in watchConfigCallback about this?

});
}

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(
Expand Down Expand Up @@ -172,9 +267,10 @@ async function createLanguageService(
typingsNamespace: raw?.svelteOptions?.namespace || 'svelteHTML'
};

docContext.globalSnapshotsManager.onChange(() => {
const onSnapshotChange = () => {
projectVersion++;
});
};
docContext.globalSnapshotsManager.onChange(onSnapshotChange);

reduceLanguageServiceCapabilityIfFileSizeTooBig();

Expand All @@ -188,7 +284,9 @@ async function createLanguageService(
updateTsOrJsFile,
hasFile,
fileBelongsToProject,
snapshotManager
snapshotManager,
extendedConfigPaths,
dispose
};

function deleteSnapshot(filePath: string): void {
Expand Down Expand Up @@ -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,
Expand All @@ -335,7 +451,8 @@ async function createLanguageService(
ts.ScriptKind.Deferred ??
(docContext.useNewTransformation ? ts.ScriptKind.TS : ts.ScriptKind.TSX)
}
]
],
cacheMonitorProxy
);

const compilerOptions: ts.CompilerOptions = {
Expand Down Expand Up @@ -391,7 +508,8 @@ async function createLanguageService(
return {
...parsedConfig,
fileNames: parsedConfig.fileNames.map(normalizePath),
options: compilerOptions
options: compilerOptions,
extendedConfigPaths
};
}

Expand Down Expand Up @@ -429,6 +547,12 @@ async function createLanguageService(
docContext.notifyExceedSizeLimit?.();
}
}

function dispose() {
languageService.dispose();
snapshotManager.dispose();
docContext.globalSnapshotsManager.removeChangeListener(onSnapshotChange);
}
}

/**
Expand Down
11 changes: 5 additions & 6 deletions packages/language-server/src/server.ts
Expand Up @@ -161,12 +161,11 @@ export function startServer(options?: LSOptions) {
pluginHost.register(
new TypeScriptPlugin(
configManager,
new LSAndTSDocResolver(
docManager,
workspaceUris.map(normalizeUri),
configManager,
notifyTsServiceExceedSizeLimit
)
new LSAndTSDocResolver(docManager, workspaceUris.map(normalizeUri), configManager, {
notifyExceedSizeLimit: notifyTsServiceExceedSizeLimit,
onProjectReloaded: updateAllDiagnostics,
watchTsConfig: true
})
)
);

Expand Down