Skip to content

Commit

Permalink
(fix) check modified time for tsconfig watching (#1973)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed Apr 4, 2023
1 parent ac82729 commit 622c02e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
49 changes: 47 additions & 2 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const serviceSizeMap = new FileMap<number>();
const configWatchers = new FileMap<ts.FileWatcher>();
const extendedConfigWatchers = new FileMap<ts.FileWatcher>();
const extendedConfigToTsConfigPath = new FileMap<FileSet>();
const configFileModifiedTime = new FileMap<Date | undefined>();
const configFileForOpenFiles = new FileMap<string>();
const pendingReloads = new FileSet();

Expand Down Expand Up @@ -559,6 +560,7 @@ async function createLanguageService(
}

if (!configWatchers.has(tsconfigPath) && tsconfigPath) {
configFileModifiedTime.set(tsconfigPath, tsSystem.getModifiedTime?.(tsconfigPath));
configWatchers.set(
tsconfigPath,
// for some reason setting the polling interval is necessary, else some error in TS is thrown
Expand All @@ -571,6 +573,7 @@ async function createLanguageService(
continue;
}

configFileModifiedTime.set(config, tsSystem.getModifiedTime?.(config));
extendedConfigWatchers.set(
config,
// for some reason setting the polling interval is necessary, else some error in TS is thrown
Expand All @@ -579,7 +582,18 @@ async function createLanguageService(
}
}

async function watchConfigCallback(fileName: string, kind: ts.FileWatcherEventKind) {
async function watchConfigCallback(
fileName: string,
kind: ts.FileWatcherEventKind,
modifiedTime: Date | undefined
) {
if (
kind === ts.FileWatcherEventKind.Changed &&
!configFileModified(fileName, modifiedTime ?? tsSystem.getModifiedTime?.(fileName))
) {
return;
}

dispose();

if (kind === ts.FileWatcherEventKind.Changed) {
Expand Down Expand Up @@ -651,7 +665,21 @@ function exceedsTotalSizeLimitForNonTsFiles(
* So that GC won't drop it and cause memory leaks
*/
function createWatchExtendedConfigCallback(docContext: LanguageServiceDocumentContext) {
return async (fileName: string) => {
return async (
fileName: string,
kind: ts.FileWatcherEventKind,
modifiedTime: Date | undefined
) => {
if (
kind === ts.FileWatcherEventKind.Changed &&
!configFileModified(
fileName,
modifiedTime ?? docContext.tsSystem.getModifiedTime?.(fileName)
)
) {
return;
}

docContext.extendedConfigCache.delete(fileName);

const promises = Array.from(extendedConfigToTsConfigPath.get(fileName) ?? []).map(
Expand All @@ -667,6 +695,23 @@ function createWatchExtendedConfigCallback(docContext: LanguageServiceDocumentCo
};
}

/**
* check if file content is modified instead of attributes changed
*/
function configFileModified(fileName: string, modifiedTime: Date | undefined) {
const previousModifiedTime = configFileModifiedTime.get(fileName);
if (!modifiedTime || !previousModifiedTime) {
return true;
}

if (previousModifiedTime >= modifiedTime) {
return false;
}

configFileModifiedTime.set(fileName, modifiedTime);
return true;
}

/**
* schedule to the service reload to the next time the
* service in requested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function createVirtualTsSystem(currentDirectory: string): ts.System {
const watchers = new FileMap<ts.FileWatcherCallback[]>();
const watchTimeout = new FileMap<Array<ReturnType<typeof setTimeout>>>();
const getCanonicalFileName = createGetCanonicalFileName(ts.sys.useCaseSensitiveFileNames);
const modifiedTime = new FileMap<Date>();

function toAbsolute(path: string) {
return isAbsolute(path) ? path : join(currentDirectory, path);
Expand All @@ -27,6 +28,7 @@ export function createVirtualTsSystem(currentDirectory: string): ts.System {
const normalizedPath = normalizePath(toAbsolute(path));
const existsBefore = virtualFs.has(normalizedPath);
virtualFs.set(normalizedPath, data);
modifiedTime.set(normalizedPath, new Date());
triggerWatch(
normalizedPath,
existsBefore ? ts.FileWatcherEventKind.Changed : ts.FileWatcherEventKind.Created
Expand Down Expand Up @@ -82,6 +84,9 @@ export function createVirtualTsSystem(currentDirectory: string): ts.System {
}
}
};
},
getModifiedTime(path) {
return modifiedTime.get(normalizePath(toAbsolute(path)));
}
};

Expand Down

0 comments on commit 622c02e

Please sign in to comment.