Skip to content

Commit 239ff7d

Browse files
committed
Fixes microsoft#23758: Can start same task twice when adding task to tasks.json in between
1 parent c8ad3f5 commit 239ff7d

1 file changed

Lines changed: 66 additions & 4 deletions

File tree

src/vs/workbench/parts/tasks/common/taskConfiguration.ts

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ function fillProperty<T, K extends keyof T>(target: T, source: T, key: K) {
358358
interface ParseContext {
359359
problemReporter: IProblemReporter;
360360
namedProblemMatchers: IStringDictionary<NamedProblemMatcher>;
361+
uuidMap: UUIDMap;
361362
engine: Tasks.ExecutionEngine;
362363
schemaVersion: Tasks.JsonSchemaVersion;
363364
}
@@ -882,7 +883,7 @@ namespace TaskDescription {
882883
let command: Tasks.CommandConfiguration = CommandConfiguration.from(externalTask, context);
883884
let identifer = Types.isString(externalTask.identifier) ? externalTask.identifier : taskName;
884885
let task: Tasks.Task = {
885-
_id: UUID.generateUuid(),
886+
_id: context.uuidMap.getUUID(taskName),
886887
_source: source,
887888
_label: taskName,
888889
name: taskName,
@@ -1222,11 +1223,65 @@ export interface IProblemReporter extends IProblemReporterBase {
12221223
clearOutput(): void;
12231224
}
12241225

1226+
class UUIDMap {
1227+
1228+
private last: IStringDictionary<string | string[]>;
1229+
private current: IStringDictionary<string | string[]>;
1230+
1231+
constructor() {
1232+
this.current = Object.create(null);
1233+
}
1234+
1235+
public start(): void {
1236+
this.last = this.current;
1237+
this.current = Object.create(null);
1238+
}
1239+
1240+
public getUUID(identifier: string): string {
1241+
let lastValue = this.last[identifier];
1242+
let result: string;
1243+
if (lastValue !== void 0) {
1244+
if (Array.isArray(lastValue)) {
1245+
result = lastValue.shift();
1246+
if (lastValue.length === 0) {
1247+
delete this.last[identifier];
1248+
}
1249+
} else {
1250+
result = lastValue;
1251+
delete this.last[identifier];
1252+
}
1253+
}
1254+
if (result === void 0) {
1255+
result = UUID.generateUuid();
1256+
}
1257+
let currentValue = this.current[identifier];
1258+
if (currentValue === void 0) {
1259+
this.current[identifier] = result;
1260+
} else {
1261+
if (Array.isArray(currentValue)) {
1262+
currentValue.push(result);
1263+
} else {
1264+
let arrayValue: string[] = [currentValue];
1265+
arrayValue.push(result);
1266+
this.current[identifier] = arrayValue;
1267+
}
1268+
}
1269+
return result;
1270+
}
1271+
1272+
public finish(): void {
1273+
this.last = undefined;
1274+
}
1275+
}
1276+
12251277
class ConfigurationParser {
12261278

12271279
private problemReporter: IProblemReporter;
1228-
constructor(problemReporter: IProblemReporter) {
1280+
private uuidMap: UUIDMap;
1281+
1282+
constructor(problemReporter: IProblemReporter, uuidMap: UUIDMap) {
12291283
this.problemReporter = problemReporter;
1284+
this.uuidMap = uuidMap;
12301285
}
12311286

12321287
public run(fileConfig: ExternalTaskRunnerConfiguration): ParseResult {
@@ -1237,6 +1292,7 @@ class ConfigurationParser {
12371292
}
12381293
let context: ParseContext = {
12391294
problemReporter: this.problemReporter,
1295+
uuidMap: this.uuidMap,
12401296
namedProblemMatchers: undefined,
12411297
engine,
12421298
schemaVersion,
@@ -1278,7 +1334,7 @@ class ConfigurationParser {
12781334
let matchers: ProblemMatcher[] = ProblemMatcherConverter.from(fileConfig.problemMatcher, context);;
12791335
let isBackground = fileConfig.isBackground ? !!fileConfig.isBackground : fileConfig.isWatching ? !!fileConfig.isWatching : undefined;
12801336
let task: Tasks.Task = {
1281-
_id: UUID.generateUuid(),
1337+
_id: context.uuidMap.getUUID(globals.command.name),
12821338
_source: TaskDescription.source,
12831339
_label: globals.command.name,
12841340
name: globals.command.name,
@@ -1303,8 +1359,14 @@ class ConfigurationParser {
13031359
}
13041360
}
13051361

1362+
let uuidMap: UUIDMap = new UUIDMap();
13061363
export function parse(configuration: ExternalTaskRunnerConfiguration, logger: IProblemReporter): ParseResult {
1307-
return (new ConfigurationParser(logger)).run(configuration);
1364+
try {
1365+
uuidMap.start();
1366+
return (new ConfigurationParser(logger, uuidMap)).run(configuration);
1367+
} finally {
1368+
uuidMap.finish();
1369+
}
13081370
}
13091371

13101372
export function mergeTasks(target: Tasks.Task, source: Tasks.Task): Tasks.Task {

0 commit comments

Comments
 (0)