diff --git a/src/vs/workbench/contrib/tasks/browser/terminalTaskSystem.ts b/src/vs/workbench/contrib/tasks/browser/terminalTaskSystem.ts index 8560e38f89576b..92d24e5a7fb528 100644 --- a/src/vs/workbench/contrib/tasks/browser/terminalTaskSystem.ts +++ b/src/vs/workbench/contrib/tasks/browser/terminalTaskSystem.ts @@ -980,10 +980,13 @@ export class TerminalTaskSystem extends Disposable implements ITaskSystem { promise = new Promise((resolve, reject) => { const boundTerminal = terminal!; - const onExit = terminal!.onExit((terminalLaunchResult) => { + const onExit = terminal!.onExit(async (terminalLaunchResult) => { const exitCode = typeof terminalLaunchResult === 'number' ? terminalLaunchResult : terminalLaunchResult?.code; onData?.dispose(); onExit.dispose(); + // Wait for the problems of the last lines before the task is + // released, so that a new run can't be affected by this one. + await watchingProblemMatcher.done(); const key = task.getMapKey(); if (this._busyTasks[mapKey]) { delete this._busyTasks[mapKey]; @@ -1016,7 +1019,6 @@ export class TerminalTaskSystem extends Disposable implements ITaskSystem { // There is nothing else to do here. } } - watchingProblemMatcher.done(); watchingProblemMatcher.dispose(); if (!processStartedSignaled) { this._fireTaskEvent(TaskEvent.processStarted(task, terminal!.instanceId, terminal!.processId!)); diff --git a/src/vs/workbench/contrib/tasks/common/problemCollectors.ts b/src/vs/workbench/contrib/tasks/common/problemCollectors.ts index fb4a1f35e0ad5e..d46dff612be862 100644 --- a/src/vs/workbench/contrib/tasks/common/problemCollectors.ts +++ b/src/vs/workbench/contrib/tasks/common/problemCollectors.ts @@ -352,6 +352,15 @@ export abstract class AbstractProblemCollector extends Disposable implements IDi this.reportMarkers(); this.cleanAllMarkers(); } + + /** + * Resolves once the lines passed to `processLine` so far have been + * processed. Lines are processed asynchronously, one after the other. + */ + protected linesProcessed(): Promise { + const ignore = () => { }; + return this.tail ? this.tail.then(ignore, ignore) : Promise.resolve(); + } } export const enum ProblemHandlingStrategy { @@ -596,10 +605,17 @@ export class WatchingProblemCollector extends AbstractProblemCollector implement } } - public override done(): void { + /** + * Reports the markers and cleans up the markers of the previous run. A + * background task can exit right after printing its output, before its last + * lines have been processed, so this waits for them first. The returned + * promise resolves once the markers have been reported. + */ + public override async done(): Promise { [...this.applyToByOwner.keys()].forEach(owner => { this.recordResourcesToClean(owner); }); + await this.linesProcessed(); super.done(); } diff --git a/src/vs/workbench/contrib/tasks/test/common/problemCollectors.test.ts b/src/vs/workbench/contrib/tasks/test/common/problemCollectors.test.ts index f5ef07f886b208..188a8763b0ab8c 100644 --- a/src/vs/workbench/contrib/tasks/test/common/problemCollectors.test.ts +++ b/src/vs/workbench/contrib/tasks/test/common/problemCollectors.test.ts @@ -12,7 +12,7 @@ import { runWithFakedTimers } from '../../../../../base/test/common/timeTravelSc import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js'; import { ITextModel } from '../../../../../editor/common/model.js'; import { IModelService } from '../../../../../editor/common/services/model.js'; -import { IMarker, IMarkerService } from '../../../../../platform/markers/common/markers.js'; +import { IMarker, IMarkerData, IMarkerService } from '../../../../../platform/markers/common/markers.js'; import { MarkerService } from '../../../../../platform/markers/common/markerService.js'; import { ProblemCollectorEventKind, WatchingProblemCollector } from '../../common/problemCollectors.js'; import { ApplyToKind, FileLocationKind, ProblemMatcher } from '../../common/problemMatcher.js'; @@ -115,4 +115,55 @@ suite('ProblemCollectors', () => { assert.deepStrictEqual(matchCounts, [1, 2, 3]); })); + + test('reports markers of lines still being processed when done is called', () => runWithFakedTimers({ useFakeTimers: true }, async () => { + const modelService = new class extends mock() { + override readonly onModelAdded = Event.None; + override readonly onModelRemoved = Event.None; + override getModels(): ITextModel[] { return []; } + }; + + const delivered = new Map(); + const markerService = new class extends mock() { + override readonly onMarkerChanged = Event.None; + override read() { return []; } + override changeOne(_owner: string, resource: URI, markers: IMarkerData[]) { + delivered.set(resource.path, markers.map(marker => marker.message)); + } + override remove() { } + }; + + const problemMatcher: ProblemMatcher = { + owner: 'test', + applyTo: ApplyToKind.allDocuments, + fileLocation: FileLocationKind.Absolute, + pattern: { regexp: /^(.*):(\d+):\s+([^:]+):\s+(.*)$/, file: 1, line: 2, severity: 3, message: 4 }, + watching: { + activeOnStart: true, + beginsPattern: { regexp: /^Start$/ }, + endsPattern: { regexp: /^End$/ } + } + }; + const collector = store.add(new WatchingProblemCollector([problemMatcher], markerService, modelService)); + collector.aboutToStart(); + + // The task process exits right after printing its output, before the + // end pattern is seen, while the lines are still being processed. + for (const line of [ + 'Start', + '/file1:150: error: problem1', + '/file1:825: error: problem2', + '/file2:468: error: problem3', + '/file2:550: error: problem4', + ]) { + collector.processLine(line); + } + // The markers are reported by the time the returned promise resolves. + await collector.done(); + + assert.deepStrictEqual([...delivered.entries()].sort(), [ + ['/file1', ['problem1', 'problem2']], + ['/file2', ['problem3', 'problem4']], + ]); + })); });