Skip to content

Commit 65e7b5f

Browse files
authored
improve trust handling in tasks (microsoft#160450)
improve trust handling in tasks (microsoft#160362)
1 parent d6fd784 commit 65e7b5f

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/vs/workbench/contrib/tasks/browser/abstractTaskService.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import { TerminalTaskSystem } from './terminalTaskSystem';
5858
import { IQuickInputService, IQuickPick, IQuickPickItem, IQuickPickSeparator, QuickPickInput } from 'vs/platform/quickinput/common/quickInput';
5959

6060
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
61-
import { RunAutomaticTasks } from 'vs/workbench/contrib/tasks/browser/runAutomaticTasks';
6261
import { TaskDefinitionRegistry } from 'vs/workbench/contrib/tasks/common/taskDefinitionRegistry';
6362

6463
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
@@ -1226,10 +1225,6 @@ export abstract class AbstractTaskService extends Disposable implements ITaskSer
12261225
} else {
12271226
executeTaskResult = await this._executeTask(task, resolver, runSource);
12281227
}
1229-
if (runSource === TaskRunSource.User) {
1230-
const workspaceTasks = await this.getWorkspaceTasks();
1231-
RunAutomaticTasks.runWithPermission(this, this._storageService, this._notificationService, this._workspaceTrustManagementService, this._openerService, this._configurationService, workspaceTasks);
1232-
}
12331228
return executeTaskResult;
12341229
} catch (error) {
12351230
this._handleError(error);

src/vs/workbench/contrib/tasks/browser/runAutomaticTasks.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const HAS_PROMPTED_FOR_AUTOMATIC_TASKS = 'task.hasPromptedForAutomaticTasks';
2525
const ALLOW_AUTOMATIC_TASKS = 'task.allowAutomaticTasks';
2626

2727
export class RunAutomaticTasks extends Disposable implements IWorkbenchContribution {
28+
private _hasRunTasks: boolean = false;
2829
constructor(
2930
@ITaskService private readonly _taskService: ITaskService,
3031
@IConfigurationService private readonly _configurationService: IConfigurationService,
@@ -34,23 +35,33 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
3435
@IOpenerService private readonly _openerService: IOpenerService,
3536
@INotificationService private readonly _notificationService: INotificationService) {
3637
super();
37-
this._tryRunTasks();
38+
if (this._workspaceTrustManagementService.isWorkspaceTrusted()) {
39+
this._tryRunTasks();
40+
}
41+
this._register(this._workspaceTrustManagementService.onDidChangeTrust(async trusted => {
42+
if (trusted) {
43+
await this._tryRunTasks();
44+
}
45+
}));
3846
}
3947

4048
private async _tryRunTasks() {
49+
if (this._hasRunTasks || this._configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'off') {
50+
return;
51+
}
52+
this._hasRunTasks = true;
4153
this._logService.trace('RunAutomaticTasks: Trying to run tasks.');
4254
// Wait until we have task system info (the extension host and workspace folders are available).
4355
if (!this._taskService.hasTaskSystemInfo) {
4456
this._logService.trace('RunAutomaticTasks: Awaiting task system info.');
4557
await Event.toPromise(Event.once(this._taskService.onDidChangeTaskSystemInfo));
4658
}
47-
4859
const workspaceTasks = await this._taskService.getWorkspaceTasks(TaskRunSource.FolderOpen);
4960
this._logService.trace(`RunAutomaticTasks: Found ${workspaceTasks.size} automatic tasks`);
50-
await RunAutomaticTasks.runWithPermission(this._taskService, this._storageService, this._notificationService, this._workspaceTrustManagementService, this._openerService, this._configurationService, workspaceTasks);
61+
await this._runWithPermission(this._taskService, this._storageService, this._notificationService, this._workspaceTrustManagementService, this._openerService, this._configurationService, workspaceTasks);
5162
}
5263

53-
private static _runTasks(taskService: ITaskService, tasks: Array<Task | Promise<Task | undefined>>) {
64+
private _runTasks(taskService: ITaskService, tasks: Array<Task | Promise<Task | undefined>>) {
5465
tasks.forEach(task => {
5566
if (task instanceof Promise) {
5667
task.then(promiseResult => {
@@ -64,7 +75,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
6475
});
6576
}
6677

67-
private static _getTaskSource(source: TaskSource): URI | undefined {
78+
private _getTaskSource(source: TaskSource): URI | undefined {
6879
const taskKind = TaskSourceKind.toConfigurationTarget(source.kind);
6980
switch (taskKind) {
7081
case ConfigurationTarget.WORKSPACE_FOLDER: {
@@ -77,7 +88,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
7788
return undefined;
7889
}
7990

80-
private static _findAutoTasks(taskService: ITaskService, workspaceTaskResult: Map<string, IWorkspaceFolderTaskResult>): { tasks: Array<Task | Promise<Task | undefined>>; taskNames: Array<string>; locations: Map<string, URI> } {
91+
private _findAutoTasks(taskService: ITaskService, workspaceTaskResult: Map<string, IWorkspaceFolderTaskResult>): { tasks: Array<Task | Promise<Task | undefined>>; taskNames: Array<string>; locations: Map<string, URI> } {
8192
const tasks = new Array<Task | Promise<Task | undefined>>();
8293
const taskNames = new Array<string>();
8394
const locations = new Map<string, URI>();
@@ -89,7 +100,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
89100
if (task.runOptions.runOn === RunOnOptions.folderOpen) {
90101
tasks.push(task);
91102
taskNames.push(task._label);
92-
const location = RunAutomaticTasks._getTaskSource(task._source);
103+
const location = this._getTaskSource(task._source);
93104
if (location) {
94105
locations.set(location.fsPath, location);
95106
}
@@ -107,7 +118,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
107118
} else {
108119
taskNames.push(configuredTask.configures.task);
109120
}
110-
const location = RunAutomaticTasks._getTaskSource(configuredTask._source);
121+
const location = this._getTaskSource(configuredTask._source);
111122
if (location) {
112123
locations.set(location.fsPath, location);
113124
}
@@ -119,35 +130,30 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
119130
return { tasks, taskNames, locations };
120131
}
121132

122-
public static async runWithPermission(taskService: ITaskService, storageService: IStorageService, notificationService: INotificationService, workspaceTrustManagementService: IWorkspaceTrustManagementService,
133+
private async _runWithPermission(taskService: ITaskService, storageService: IStorageService, notificationService: INotificationService, workspaceTrustManagementService: IWorkspaceTrustManagementService,
123134
openerService: IOpenerService, configurationService: IConfigurationService, workspaceTaskResult: Map<string, IWorkspaceFolderTaskResult>) {
124-
const isWorkspaceTrusted = workspaceTrustManagementService.isWorkspaceTrusted;
125-
if (!isWorkspaceTrusted || configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'off') {
126-
return;
127-
}
128135

129136
const hasShownPromptForAutomaticTasks = storageService.getBoolean(HAS_PROMPTED_FOR_AUTOMATIC_TASKS, StorageScope.WORKSPACE, false);
130-
const { tasks, taskNames, locations } = RunAutomaticTasks._findAutoTasks(taskService, workspaceTaskResult);
137+
const { tasks, taskNames, locations } = this._findAutoTasks(taskService, workspaceTaskResult);
131138

132139
if (taskNames.length === 0) {
133140
return;
134141
}
135-
136142
if (configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'on') {
137-
RunAutomaticTasks._runTasks(taskService, tasks);
143+
this._runTasks(taskService, tasks);
138144
} else if (configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'auto' && !hasShownPromptForAutomaticTasks) {
139145
// by default, only prompt once per folder
140146
// otherwise, this can be configured via the setting
141147
this._showPrompt(notificationService, storageService, openerService, configurationService, taskNames, locations).then(allow => {
142148
if (allow) {
143149
storageService.store(HAS_PROMPTED_FOR_AUTOMATIC_TASKS, true, StorageScope.WORKSPACE, StorageTarget.USER);
144-
RunAutomaticTasks._runTasks(taskService, tasks);
150+
this._runTasks(taskService, tasks);
145151
}
146152
});
147153
}
148154
}
149155

150-
private static _showPrompt(notificationService: INotificationService, storageService: IStorageService,
156+
private _showPrompt(notificationService: INotificationService, storageService: IStorageService,
151157
openerService: IOpenerService, configurationService: IConfigurationService, taskNames: Array<string>, locations: Map<string, URI>): Promise<boolean> {
152158
return new Promise<boolean>(resolve => {
153159
notificationService.prompt(Severity.Info, nls.localize('tasks.run.allowAutomatic',

0 commit comments

Comments
 (0)