Skip to content

Commit 40386f8

Browse files
authored
Fix task rerun (#276823)
1 parent e026a48 commit 40386f8

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

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

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,22 +2172,31 @@ export abstract class AbstractTaskService extends Disposable implements ITaskSer
21722172
if (!this._taskSystem) {
21732173
return;
21742174
}
2175-
const response = await this._taskSystem.terminate(task);
2176-
if (response.success) {
2177-
try {
2178-
// Before restarting, check if the task still exists and get updated version
2179-
const updatedTask = await this._findUpdatedTask(task);
2180-
if (updatedTask) {
2181-
await this.run(updatedTask);
2182-
} else {
2183-
// Task no longer exists, show warning
2184-
this._notificationService.warn(nls.localize('TaskSystem.taskNoLongerExists', 'Task {0} no longer exists or has been modified. Cannot restart.', task.configurationProperties.name));
2185-
}
2186-
} catch {
2187-
// eat the error, we don't care about it here
2175+
2176+
// Check if the task is currently running
2177+
const isTaskRunning = await this.getActiveTasks().then(tasks => tasks.some(t => t.getMapKey() === task.getMapKey()));
2178+
2179+
if (isTaskRunning) {
2180+
// Task is running, terminate it first
2181+
const response = await this._taskSystem.terminate(task);
2182+
if (!response.success) {
2183+
this._notificationService.warn(nls.localize('TaskSystem.restartFailed', 'Failed to terminate and restart task {0}', Types.isString(task) ? task : task.configurationProperties.name));
2184+
return;
21882185
}
2189-
} else {
2190-
this._notificationService.warn(nls.localize('TaskSystem.restartFailed', 'Failed to terminate and restart task {0}', Types.isString(task) ? task : task.configurationProperties.name));
2186+
}
2187+
2188+
// Task is not running or was successfully terminated, now run it
2189+
try {
2190+
// Before restarting, check if the task still exists and get updated version
2191+
const updatedTask = await this._findUpdatedTask(task);
2192+
if (updatedTask) {
2193+
await this.run(updatedTask);
2194+
} else {
2195+
// Task no longer exists, show warning
2196+
this._notificationService.warn(nls.localize('TaskSystem.taskNoLongerExists', 'Task {0} no longer exists or has been modified. Cannot restart.', task.configurationProperties.name));
2197+
}
2198+
} catch {
2199+
// eat the error, we don't care about it here
21912200
}
21922201
}
21932202

@@ -2275,6 +2284,17 @@ export abstract class AbstractTaskService extends Disposable implements ITaskSer
22752284
} else {
22762285
return undefined;
22772286
}
2287+
},
2288+
async (taskKey: string) => {
2289+
// Look up task by its map key across all workspace tasks
2290+
const taskMap = await this._getGroupedTasks();
2291+
const allTasks = taskMap.all();
2292+
for (const task of allTasks) {
2293+
if (task.getMapKey() === taskKey) {
2294+
return task;
2295+
}
2296+
}
2297+
return undefined;
22782298
}
22792299
);
22802300
}
@@ -3209,8 +3229,8 @@ export abstract class AbstractTaskService extends Disposable implements ITaskSer
32093229
}
32103230

32113231

3212-
rerun(terminalInstanceId: number): void {
3213-
const task = this._taskSystem?.getTaskForTerminal(terminalInstanceId);
3232+
async rerun(terminalInstanceId: number): Promise<void> {
3233+
const task = await this._taskSystem?.getTaskForTerminal(terminalInstanceId);
32143234
if (task) {
32153235
this._restart(task);
32163236
} else {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ export class TerminalTaskSystem extends Disposable implements ITaskSystem {
221221
contextKeyService: IContextKeyService,
222222
instantiationService: IInstantiationService,
223223
taskSystemInfoResolver: ITaskSystemInfoResolver,
224+
private _taskLookup: (taskKey: string) => Promise<Task | undefined>,
224225
) {
225226
super();
226227

@@ -1944,13 +1945,20 @@ export class TerminalTaskSystem extends Disposable implements ITaskSystem {
19441945
return 'other';
19451946
}
19461947

1947-
public getTaskForTerminal(instanceId: number): Task | undefined {
1948+
public async getTaskForTerminal(instanceId: number): Promise<Task | undefined> {
1949+
// First check if there's an active task for this terminal
19481950
for (const key in this._activeTasks) {
19491951
const activeTask = this._activeTasks[key];
19501952
if (activeTask.terminal?.instanceId === instanceId) {
19511953
return activeTask.task;
19521954
}
19531955
}
1956+
// If no active task, check the terminals map for the last task that ran in this terminal
1957+
const terminalData = this._terminals[instanceId.toString()];
1958+
if (terminalData?.lastTask) {
1959+
// Look up the task using the callback provided by the task service
1960+
return await this._taskLookup(terminalData.lastTask);
1961+
}
19541962
return undefined;
19551963
}
19561964

src/vs/workbench/contrib/tasks/common/taskSystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export interface ITaskSystem {
152152
revealTask(task: Task): boolean;
153153
customExecutionComplete(task: Task, result: number): Promise<void>;
154154
isTaskVisible(task: Task): boolean;
155-
getTaskForTerminal(instanceId: number): Task | undefined;
155+
getTaskForTerminal(instanceId: number): Promise<Task | undefined>;
156156
getTerminalsForTasks(tasks: SingleOrMany<Task>): URI[] | undefined;
157157
getTaskProblems(instanceId: number): Map<string, { resources: URI[]; markers: IMarkerData[] }> | undefined;
158158
getFirstInstance(task: Task): Task | undefined;

0 commit comments

Comments
 (0)