Skip to content

Commit 3458a13

Browse files
committed
another round of coderabbit
1 parent 097e939 commit 3458a13

9 files changed

Lines changed: 175 additions & 114 deletions

File tree

src/build/dfuExecution.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ import { getIdfTargetFromSdkconfig } from "../workspaceConfig";
2626
import { configureEnvVariables } from "../common/prepareEnv";
2727
import { selectedDFUAdapterId } from "../flash/transports/dfu/helpers";
2828
import { getVirtualEnvPythonPath } from "../pythonManager";
29-
import { addProcessTask, type MaybeIdfTaskExecution } from "../taskManager";
29+
import { addProcessTask, type IdfTaskExecution } from "../taskManager";
3030

3131
export async function appendDfuExecution(
32-
executions: Exclude<MaybeIdfTaskExecution, undefined>[],
32+
executions: IdfTaskExecution[],
3333
workspace: Uri,
3434
captureOutput?: boolean
3535
): Promise<boolean> {
36-
const buildPath = readParameter("idf.buildPath", workspace) as string;
36+
const buildPath = (readParameter("idf.buildPath", workspace) as string)?.trim();
37+
if (!buildPath) {
38+
Logger.warnNotify(
39+
"idf.buildPath is not configured. Set the build directory in ESP-IDF settings before building for DFU."
40+
);
41+
return false;
42+
}
3743
if (!(await pathExists(join(buildPath, "flasher_args.json")))) {
3844
Logger.warnNotify(
3945
"flasher_args.json file is missing from the build directory, can't proceed, please build properly!"

src/build/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ export async function build(
5050
async (_progress, cancelToken, wsFolder) => {
5151
let resolvedFlashType = flashType;
5252
if (!resolvedFlashType) {
53-
resolvedFlashType = readParameter(
54-
"idf.flashType",
55-
wsFolder
56-
) as ESP.FlashType;
53+
const fromConfig = readParameter("idf.flashType", wsFolder);
54+
const raw =
55+
typeof fromConfig === "string" ? fromConfig.trim() : "";
56+
resolvedFlashType = Object.values(ESP.FlashType).includes(
57+
raw as ESP.FlashType
58+
)
59+
? (raw as ESP.FlashType)
60+
: ESP.FlashType.UART;
5761
}
5862
await buildMain(wsFolder.uri, cancelToken, resolvedFlashType, buildType);
5963
}

src/cdtDebugAdapter/debugConfProvider.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,31 @@ import { createNewIdfMonitor } from "../espIdf/monitor/command";
3838
import { ESP } from "../config";
3939
import { buildFlashAndMonitor } from "../buildFlashMonitor";
4040

41+
async function getOrPickWorkspaceFolder(
42+
folder: WorkspaceFolder | undefined
43+
): Promise<WorkspaceFolder> {
44+
if (!folder) {
45+
folder = ESP.GlobalConfiguration.store.getSelectedWorkspaceFolder();
46+
if (!folder) {
47+
folder = await window.showWorkspaceFolderPick({
48+
placeHolder: "Pick a workspace folder to start a debug session.",
49+
});
50+
if (!folder) {
51+
throw new Error("No folder was selected to start debug session");
52+
}
53+
}
54+
}
55+
return folder;
56+
}
57+
4158
export class CDTDebugConfigurationProvider
4259
implements DebugConfigurationProvider {
4360
public async resolveDebugConfigurationWithSubstitutedVariables(
4461
folder: WorkspaceFolder | undefined,
4562
debugConfiguration: DebugConfiguration,
4663
token?: CancellationToken
4764
) {
48-
if (!folder) {
49-
folder = ESP.GlobalConfiguration.store.getSelectedWorkspaceFolder();
50-
if (!folder) {
51-
folder = await window.showWorkspaceFolderPick({
52-
placeHolder: "Pick a workspace folder to start a debug session.",
53-
});
54-
if (!folder) {
55-
throw new Error("No folder was selected to start debug session");
56-
}
57-
}
58-
}
65+
folder = await getOrPickWorkspaceFolder(folder);
5966
const useMonitorWithDebug = readParameter(
6067
"idf.launchMonitorOnDebugSession",
6168
folder
@@ -87,17 +94,7 @@ export class CDTDebugConfigurationProvider
8794
token?: CancellationToken
8895
): Promise<DebugConfiguration | undefined> {
8996
try {
90-
if (!folder) {
91-
folder = ESP.GlobalConfiguration.store.getSelectedWorkspaceFolder();
92-
if (!folder) {
93-
folder = await window.showWorkspaceFolderPick({
94-
placeHolder: "Pick a workspace folder to start a debug session.",
95-
});
96-
if (!folder) {
97-
throw new Error("No folder was selected to start debug session");
98-
}
99-
}
100-
}
97+
folder = await getOrPickWorkspaceFolder(folder);
10198
if (!config.program) {
10299
const elfFilePath = await getProjectElfFilePath(folder.uri);
103100
const elfFileExists = await pathExists(elfFilePath);
@@ -183,10 +180,25 @@ export class CDTDebugConfigurationProvider
183180
esp32c5: 4,
184181
esp32c61: 4,
185182
};
183+
const rawIdfTarget = idfTarget ?? "";
184+
const watchpointNum =
185+
rawIdfTarget !== "" &&
186+
Object.prototype.hasOwnProperty.call(
187+
idfTargetWatchpointMap,
188+
rawIdfTarget
189+
)
190+
? idfTargetWatchpointMap[rawIdfTarget as IdfTarget]
191+
: undefined;
192+
if (watchpointNum === undefined && rawIdfTarget !== "") {
193+
Logger.info(
194+
`Unknown IDF target "${rawIdfTarget}" for CPU hardware watchpoint mapping; using default 2.`,
195+
{ context: "CDTDebugConfigurationProvider" }
196+
);
197+
}
186198
config.initCommands = config.initCommands.map((cmd: string) =>
187199
cmd.replace(
188200
"{IDF_TARGET_CPU_WATCHPOINT_NUM}",
189-
String(idfTargetWatchpointMap[idfTarget as IdfTarget] ?? 2)
201+
String(watchpointNum ?? 2)
190202
)
191203
);
192204
}

src/extension.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,14 @@ export async function activate(context: vscode.ExtensionContext) {
27742774
workspaceFolder,
27752775
conf
27762776
);
2777+
if (!resolvedConf) {
2778+
await vscode.window.showErrorMessage(
2779+
vscode.l10n.t(
2780+
"Could not resolve the gdbtarget debug configuration. Check the ESP-IDF output for details."
2781+
)
2782+
);
2783+
return;
2784+
}
27772785
await vscode.debug.startDebugging(workspaceFolder, resolvedConf);
27782786
return;
27792787
}

src/flash/transports/jtag/flashTclClient.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@
1717
*/
1818

1919
import { TCLClient } from "../../../espIdf/openOcd/tcl/tclClient";
20-
import { IdfTaskExecution } from "../../../taskManager";
20+
import { Logger } from "../../../logger/logger";
2121
import {
22+
CaptureableTaskExecution,
2223
CapturedTaskOutput,
2324
CustomExecutionTaskResult,
2425
} from "../../../taskManager/customExecution";
2526

2627
export function createCapturedExecution(
2728
output: CapturedTaskOutput
28-
): IdfTaskExecution {
29-
return ({
29+
): CaptureableTaskExecution {
30+
return {
3031
getOutput: async () => output,
31-
} as unknown) as IdfTaskExecution;
32+
};
3233
}
3334

3435
/**
@@ -94,6 +95,11 @@ export async function jtagFlash(
9495
};
9596

9697
const onError = (err: unknown) => {
98+
Logger.error(
99+
"jtagFlash OpenOCD TCL error",
100+
err instanceof Error ? err : new Error(String(err)),
101+
"jtagFlash onError"
102+
);
97103
const stderr =
98104
err instanceof Error
99105
? err.message

src/flash/transports/uart/uartFlashCmd.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* limitations under the License.
1717
*/
1818
import { join } from "path";
19-
import { CancellationToken, Uri } from "vscode";
19+
import { CancellationToken, Disposable, Uri } from "vscode";
2020
import { Logger } from "../../../logger/logger";
2121
import {
2222
collectExecutions,
@@ -51,12 +51,18 @@ export async function uartFlashCommandMain(
5151
if (FlashSession.isFlashing) {
5252
throw new Error("ALREADY_FLASHING");
5353
}
54-
cancelToken.onCancellationRequested(() => {
55-
TaskManager.cancelTasks();
56-
FlashSession.isFlashing = false;
57-
});
54+
let ownsFlashSession = false;
55+
let cancelSubscription: Disposable | undefined;
5856
try {
5957
FlashSession.isFlashing = true;
58+
ownsFlashSession = true;
59+
cancelSubscription = cancelToken.onCancellationRequested(() => {
60+
if (!ownsFlashSession) {
61+
return;
62+
}
63+
TaskManager.cancelTasks();
64+
FlashSession.isFlashing = false;
65+
});
6066
const model = await createFlashModel(
6167
flasherArgsJsonPath,
6268
port,
@@ -105,7 +111,10 @@ export async function uartFlashCommandMain(
105111
),
106112
};
107113
} finally {
108-
FlashSession.isFlashing = false;
114+
cancelSubscription?.dispose();
115+
if (ownsFlashSession) {
116+
FlashSession.isFlashing = false;
117+
}
109118
TaskManager.disposeListeners();
110119
}
111120
}

src/langTools/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ export function activateLanguageTool(context: vscode.ExtensionContext) {
275275
}
276276

277277
if (TASK_COMMANDS.has(commandName)) {
278+
if (!continueFlag) {
279+
outputs.unshift(
280+
new vscode.LanguageModelTextPart(
281+
`Command "${commandName}" did not complete successfully.`
282+
)
283+
);
284+
}
278285
for (const execution of taskExecutions) {
279286
if (execution && "getOutput" in execution) {
280287
const output = await (execution as
@@ -316,6 +323,13 @@ export function activateLanguageTool(context: vscode.ExtensionContext) {
316323
),
317324
]);
318325
}
326+
if (errorMessage === "ALREADY_ERASING") {
327+
return new vscode.LanguageModelToolResult([
328+
new vscode.LanguageModelTextPart(
329+
"An erase-flash operation is already in progress."
330+
),
331+
]);
332+
}
319333
if (errorMessage === "NO_DFU_DEVICE_SELECTED") {
320334
return new vscode.LanguageModelToolResult([
321335
new vscode.LanguageModelTextPart("No DFU was selected"),

0 commit comments

Comments
 (0)