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

Only fire workspace task events for effective scope #10335

Merged
merged 1 commit into from
Oct 28, 2021

Conversation

colin-grant-work
Copy link
Contributor

@colin-grant-work colin-grant-work commented Oct 27, 2021

What it does

Fixes a minor, intermittent bug that causes tasks in folder scope to be double listed as occurring in both folder and workspace scope in an unsaved (single-root) workspace.

How to test

The bug depends on a race condition, but the basic issue is this:

  • At lines 237-241 below, we enforce that we only fire events for workspace scope if the workspace is saved.
  • At line 243, we fire an event for workspace scope unconditionally, which contradicts our intent from 237-241.

protected updateWorkspaceModel(): void {
const newDelegate = this.workspaceService.saved ? this.workspacePreferences : this.folderPreferences;
const effectiveScope = this.workspaceService.saved ? TaskScope.Workspace : this.workspaceService.tryGetRoots()[0]?.resource.toString();
if (newDelegate !== this.workspaceDelegate) {
this.workspaceDelegate = newDelegate;
this.toDisposeOnDelegateChange.dispose();
const workspaceModel = new TaskConfigurationModel(effectiveScope, newDelegate);
this.toDisposeOnDelegateChange.push(workspaceModel);
// If the delegate is the folder preference provider, its events will be relayed via the folder scope models.
if (newDelegate === this.workspacePreferences) {
this.toDisposeOnDelegateChange.push(workspaceModel.onDidChange(() => {
this.onDidChangeTaskConfigEmitter.fire({ scope: TaskScope.Workspace, type: FileChangeType.UPDATED });
}));
}
this.models.set(TaskScope.Workspace, workspaceModel);
this.onDidChangeTaskConfigEmitter.fire({ scope: TaskScope.Workspace, type: FileChangeType.UPDATED });
}
}
}

When we fire those events, this listener

this.taskConfigurationManager.onDidChangeTaskConfig(async change => {
try {
await this.onDidTaskFileChange([change]);
if (this.client) {
this.client.taskConfigurationChanged(this.getTaskLabels());
}
} catch (err) {
console.error(err);
}
})
);

kicks in and populates a list of tasks by scope. We always fire events for folder scope, which is why we only want to fire them for workspace scope if it's different.

The race condition comes in because if the preference provider for newDelegate happens not to have read its configuration when the event on line 243 is fired, then the listener finds no tasks for that scope, and so we don't see any duplicates. If the preference provider has read its configuration, then we get the same tasks listed in both folder and workspace scope.

If you really want to reproduce the bug, replace line 243 with

newDelegate.ready.then(() => this.onDidChangeTaskConfigEmitter.fire({ scope: TaskScope.Workspace, type: FileChangeType.UPDATED }));

which will guarantee that the configuration has been read then

  1. Open a single-root, unsaved workspace with one or more tasks defined in a tasks.json
  2. Open the Terminal menu and select 'Run task...'
  3. Observe that the tasks defined in the tasks.json are all listed twice, once in workspace scope, once in folder scope.

Review checklist

Reminder for reviewers

Signed-off-by: Colin Grant [email protected]

@colin-grant-work colin-grant-work added the tasks issues related to the task system label Oct 27, 2021
Copy link
Member

@vince-fugnitto vince-fugnitto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍

I confirmed the bug exists on master (using the method to reproduce it consistently)

image

and that the pull-request addresses the problem:

image

@colin-grant-work colin-grant-work merged commit 4b06fef into master Oct 28, 2021
@colin-grant-work colin-grant-work deleted the bugfix/workspace-folder-task-duplication branch October 28, 2021 16:32
@github-actions github-actions bot added this to the 1.19.0 milestone Oct 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tasks issues related to the task system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants