Skip to content

Fix detection of duplicate file change events #1564

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

Merged
merged 1 commit into from
May 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/utils/documentIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export async function indexWorkspaceFolder(wsFolder: vscode.WorkspaceFolder): Pr
// request to 50 concurrent calls to avoid hammering the server
const restRateLimiter = new RateLimiter(50);
// A cache of the last time each file was last changed
const lastFileChangeTimes: Map<string, number> = new Map();
const lastChangeMtimes: Map<string, number> = new Map();
// Index classes and routines that currently exist
vscode.workspace
.findFiles(new vscode.RelativePattern(wsFolder, "{**/*.cls,**/*.mac,**/*.int,**/*.inc}"))
Expand All @@ -178,7 +178,7 @@ export async function indexWorkspaceFolder(wsFolder: vscode.WorkspaceFolder): Pr
const watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(wsFolder, "**/*"));
const debouncedCompile = generateCompileFn();
const debouncedDelete = generateDeleteFn(wsFolder.uri);
const updateIndexAndSyncChanges = async (uri: vscode.Uri): Promise<void> => {
const updateIndexAndSyncChanges = async (uri: vscode.Uri, created = false): Promise<void> => {
if (uri.scheme != wsFolder.uri.scheme) {
// We don't care about virtual files that might be
// part of the workspace folder, like "git" files
Expand All @@ -194,8 +194,22 @@ export async function indexWorkspaceFolder(wsFolder: vscode.WorkspaceFolder): Pr
return;
}
const uriString = uri.toString();
const lastFileChangeTime = lastFileChangeTimes.get(uriString) ?? 0;
lastFileChangeTimes.set(uriString, Date.now());
if (!created) {
const stat = await vscode.workspace.fs.stat(uri).then(undefined, () => {});
if (!stat) {
// If we couldn't get the file's metadata then something is very wrong
touchedByVSCode.delete(uriString);
return;
}
const lastChangeMtime = lastChangeMtimes.get(uriString) ?? 0;
lastChangeMtimes.set(uriString, stat.mtime);
if (stat.mtime == lastChangeMtime) {
// This file change event was triggered on the same version
// of the file as the last event, so ignore this one
touchedByVSCode.delete(uriString);
return;
}
}
if (openLowCodeEditors.has(uriString)) {
// This class is open in a low-code editor, so its name will not change
// and any updates to the class will be handled by that editor
Expand All @@ -210,12 +224,6 @@ export async function indexWorkspaceFolder(wsFolder: vscode.WorkspaceFolder): Pr
touchedByVSCode.delete(uriString);
return;
}
if (lastFileChangeTimes.get(uriString) - lastFileChangeTime < 300) {
// This file change event came too quickly after the last one to be a
// meaningful change triggered by the user, so consider it a duplicate
touchedByVSCode.delete(uriString);
return;
}
const api = new AtelierAPI(uri);
const conf = vscode.workspace.getConfiguration("objectscript", wsFolder);
const syncLocalChanges: string = conf.get("syncLocalChanges");
Expand Down Expand Up @@ -244,7 +252,7 @@ export async function indexWorkspaceFolder(wsFolder: vscode.WorkspaceFolder): Pr
}
};
watcher.onDidChange((uri) => restRateLimiter.call(() => updateIndexAndSyncChanges(uri)));
watcher.onDidCreate((uri) => restRateLimiter.call(() => updateIndexAndSyncChanges(uri)));
watcher.onDidCreate((uri) => restRateLimiter.call(() => updateIndexAndSyncChanges(uri, true)));
watcher.onDidDelete((uri) => {
if (uri.scheme != wsFolder.uri.scheme) {
// We don't care about virtual files that might be
Expand Down