Skip to content

Commit 534081c

Browse files
committed
Fixed an issue when the "Upload & Monitor" task selects the wrong environment // Issue platformio#2623
1 parent 9b0ac41 commit 534081c

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/project/tasks.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class ProjectTaskManager {
2626

2727
this._sid = Math.random();
2828
this._refreshTimeout = undefined;
29-
this._restoreOnDidEndTask = undefined;
29+
this._ranTask = undefined;
3030
this._tasksToRestore = [];
3131
this._sbPortSwitcher = undefined;
3232
this._customPort = getProjectItemState(projectDir, 'customPort');
@@ -150,35 +150,36 @@ export default class ProjectTaskManager {
150150
}
151151

152152
runTask(task) {
153-
this._restoreOnDidEndTask = undefined;
153+
this._ranTask = task;
154154
this._tasksToRestore = [];
155-
this._autoCloseSerialMonitor(task);
155+
this._autoCloseSerialMonitor();
156156
// use string-based task defination for Win 7 // issue #3481
157157
vscode.commands.executeCommand(
158158
'workbench.action.tasks.runTask',
159159
`${ProjectTaskManager.PROVIDER_TYPE}: ${task.id}`
160160
);
161161
}
162162

163-
_autoCloseSerialMonitor(task) {
163+
_autoCloseSerialMonitor() {
164164
const closeMonitorConds = [
165165
extension.getConfiguration('autoCloseSerialMonitor'),
166-
['upload', 'test'].some((arg) => task.args.includes(arg)),
166+
['upload', 'test'].some((arg) => this._ranTask.args.includes(arg)),
167167
];
168168
if (!closeMonitorConds.every((value) => value)) {
169169
return;
170170
}
171-
this._restoreOnDidEndTask = task;
172171
vscode.tasks.taskExecutions.forEach((event) => {
172+
const isCurrentEvent = this.areTasksEqual(this._ranTask, event.task);
173173
const skipConds = [
174174
// skip non-PlatformIO task
175175
event.task.definition.type !== ProjectTaskManager.PROVIDER_TYPE,
176176
!event.task.execution.args.includes('monitor'),
177+
this.isMonitorAndUploadTask(event.task) && !isCurrentEvent,
177178
];
178179
if (skipConds.some((value) => value)) {
179180
return;
180181
}
181-
if (!this.isMonitorAndUploadTask(event.task)) {
182+
if (!isCurrentEvent) {
182183
this._tasksToRestore.push(event.task);
183184
}
184185
event.terminate();
@@ -187,15 +188,15 @@ export default class ProjectTaskManager {
187188

188189
onDidEndTaskProcess(event) {
189190
const skipConds = [
190-
!this._restoreOnDidEndTask,
191-
event.execution.task.definition.type !== ProjectTaskManager.PROVIDER_TYPE,
191+
!this._ranTask,
192+
!this.areTasksEqual(this._ranTask, event.execution.task),
192193
event.exitCode !== 0,
193-
this.areTasksEqual(this._restoreOnDidEndTask, event.execution.task),
194+
!this._tasksToRestore.length,
194195
];
195196
if (skipConds.some((value) => value)) {
196197
return;
197198
}
198-
this._restoreOnDidEndTask = undefined;
199+
this._ranTask = undefined;
199200
setTimeout(() => {
200201
while (this._tasksToRestore.length) {
201202
vscode.tasks.executeTask(this._tasksToRestore.pop());
@@ -212,9 +213,12 @@ export default class ProjectTaskManager {
212213
if (!task1 || !task2) {
213214
return task1 === task2;
214215
}
215-
const args1 = task1.args || task1.execution.args;
216-
const args2 = task2.args || task2.execution.args;
217-
return args1 === args2;
216+
const args1 = task1.args || task1.execution.args || [];
217+
const args2 = task2.args || task2.execution.args || [];
218+
return (
219+
args1.length === args2.length &&
220+
args1.every((value, index) => value === args2[index])
221+
);
218222
}
219223

220224
registerTaskBasedCommands(tasks) {

0 commit comments

Comments
 (0)