From 970cab9d520a30d5a027ab5df971466262487104 Mon Sep 17 00:00:00 2001 From: Radu Date: Thu, 20 Nov 2025 14:55:54 +0200 Subject: [PATCH 1/5] Refactor setIdfTarget to manage customExtraVars more efficiently. Moved the declaration of customExtraVars to a single location and added logic to clear OPENOCD_USB_ADAPTER_LOCATION when no board location is available or when switching to a non-connected target. --- src/espIdf/setTarget/index.ts | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/espIdf/setTarget/index.ts b/src/espIdf/setTarget/index.ts index 3a55d37fb..48d48dcce 100644 --- a/src/espIdf/setTarget/index.ts +++ b/src/espIdf/setTarget/index.ts @@ -160,6 +160,11 @@ export async function setIdfTarget( if (!selectedTarget) { return; } + const customExtraVars = readParameter( + "idf.customExtraVars", + workspaceFolder + ) as { [key: string]: string }; + if (selectedTarget.isConnected && selectedTarget.boardInfo) { // Directly set OpenOCD configs for connected board const configFiles = selectedTarget.boardInfo.config_files || []; @@ -171,10 +176,6 @@ export async function setIdfTarget( ); // Store USB location if available if (selectedTarget.boardInfo.location) { - const customExtraVars = readParameter( - "idf.customExtraVars", - workspaceFolder - ) as { [key: string]: string }; const location = selectedTarget.boardInfo.location.replace( "usb://", "" @@ -186,8 +187,25 @@ export async function setIdfTarget( configurationTarget, workspaceFolder.uri ); + } else { + // Clear OPENOCD_USB_ADAPTER_LOCATION if no location is available + delete customExtraVars["OPENOCD_USB_ADAPTER_LOCATION"]; + await writeParameter( + "idf.customExtraVars", + customExtraVars, + configurationTarget, + workspaceFolder.uri + ); } } else { + // Clear OPENOCD_USB_ADAPTER_LOCATION when switching to non-connected target + delete customExtraVars["OPENOCD_USB_ADAPTER_LOCATION"]; + await writeParameter( + "idf.customExtraVars", + customExtraVars, + configurationTarget, + workspaceFolder.uri + ); await selectOpenOcdConfigFiles( workspaceFolder.uri, selectedTarget.idfTarget.target @@ -195,10 +213,6 @@ export async function setIdfTarget( } await setTargetInIDF(workspaceFolder, selectedTarget.idfTarget); - const customExtraVars = readParameter( - "idf.customExtraVars", - workspaceFolder - ) as { [key: string]: string }; customExtraVars["IDF_TARGET"] = selectedTarget.idfTarget.target; await writeParameter( "idf.customExtraVars", From 43e9899834b9f88e0ee9db1e33362cf6cb72a240 Mon Sep 17 00:00:00 2001 From: Radu Date: Thu, 20 Nov 2025 15:38:00 +0200 Subject: [PATCH 2/5] fix: clear OPENOCD_USB_ADAPTER_LOCATION when target has no USB location Clear the OPENOCD_USB_ADAPTER_LOCATION setting when switching to a target that doesn't have a USB location, preventing OpenOCD from using stale device addresses from previous targets. Create a plain object copy of customExtraVars to avoid proxy issues when deleting properties, and remove unnecessary configuration re-read. --- src/espIdf/setTarget/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/espIdf/setTarget/index.ts b/src/espIdf/setTarget/index.ts index 48d48dcce..b9bce664d 100644 --- a/src/espIdf/setTarget/index.ts +++ b/src/espIdf/setTarget/index.ts @@ -160,10 +160,12 @@ export async function setIdfTarget( if (!selectedTarget) { return; } - const customExtraVars = readParameter( + // Create a plain object copy to avoid proxy issues when modifying/deleting properties + const customExtraVarsRead = readParameter( "idf.customExtraVars", workspaceFolder ) as { [key: string]: string }; + const customExtraVars = { ...customExtraVarsRead }; if (selectedTarget.isConnected && selectedTarget.boardInfo) { // Directly set OpenOCD configs for connected board From 07841ab9d35a1376bf0426eb9c96ee83d774b27c Mon Sep 17 00:00:00 2001 From: Radu Date: Fri, 21 Nov 2025 11:00:16 +0200 Subject: [PATCH 3/5] fix: allow device detection to scan all devices Remove OPENOCD_USB_ADAPTER_LOCATION from environment during device detection so the script can scan all available devices, not just the one at the configured location. --- src/espIdf/setTarget/DevkitsCommand.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/espIdf/setTarget/DevkitsCommand.ts b/src/espIdf/setTarget/DevkitsCommand.ts index fd906de5e..05df63643 100644 --- a/src/espIdf/setTarget/DevkitsCommand.ts +++ b/src/espIdf/setTarget/DevkitsCommand.ts @@ -88,6 +88,10 @@ export class DevkitsCommand { const pythonBinPath = await getVirtualEnvPythonPath(this.workspaceRoot); const modifiedEnv = await appendIdfAndToolsToPath(this.workspaceRoot); + // Remove OPENOCD_USB_ADAPTER_LOCATION from environment during device detection + // to allow scanning all available devices, not just the one at the configured location + delete modifiedEnv.OPENOCD_USB_ADAPTER_LOCATION; + OutputChannel.init(); OutputChannel.appendLine( "Running ESP Detect Config...", From 12660a9d6c156653e0f345a256d9446557940fd6 Mon Sep 17 00:00:00 2001 From: Radu Date: Fri, 21 Nov 2025 13:29:43 +0200 Subject: [PATCH 4/5] fix: optimising openocd --version calls --- src/espIdf/setTarget/DevkitsCommand.ts | 8 ++------ src/espIdf/setTarget/index.ts | 7 +++++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/espIdf/setTarget/DevkitsCommand.ts b/src/espIdf/setTarget/DevkitsCommand.ts index 05df63643..7683610d8 100644 --- a/src/espIdf/setTarget/DevkitsCommand.ts +++ b/src/espIdf/setTarget/DevkitsCommand.ts @@ -34,7 +34,7 @@ export class DevkitsCommand { this.workspaceRoot = workspaceRoot; } - public async runDevkitsScript(): Promise { + public async runDevkitsScript(openOCDVersion: string): Promise { try { const workspaceFolder = vscode.workspace.getWorkspaceFolder( this.workspaceRoot @@ -47,8 +47,6 @@ export class DevkitsCommand { "idf.toolsPath", this.workspaceRoot ) as string; - const openOCDManager = OpenOCDManager.init(); - const openOCDVersion = await openOCDManager.version(); if (!toolsPath || !openOCDVersion) { throw new Error("Could not get toolsPath or OpenOCD version"); @@ -144,14 +142,12 @@ export class DevkitsCommand { } } - public async getScriptPath(): Promise { + public async getScriptPath(openOCDVersion: string): Promise { try { const toolsPath = idfConf.readParameter( "idf.toolsPath", this.workspaceRoot ) as string; - const openOCDManager = OpenOCDManager.init(); - const openOCDVersion = await openOCDManager.version(); if (!toolsPath || !openOCDVersion) { return null; diff --git a/src/espIdf/setTarget/index.ts b/src/espIdf/setTarget/index.ts index b9bce664d..b620600ac 100644 --- a/src/espIdf/setTarget/index.ts +++ b/src/espIdf/setTarget/index.ts @@ -34,6 +34,7 @@ import { import { Logger } from "../../logger/logger"; import { OutputChannel } from "../../logger/outputChannel"; import { selectOpenOcdConfigFiles } from "../openOcd/boardConfiguration"; +import { OpenOCDManager } from "../openOcd/openOcdManager"; import { getTargetsFromEspIdf, IdfTarget } from "./getTargets"; import { setTargetInIDF } from "./setTargetInIdf"; import { updateCurrentProfileIdfTarget } from "../../project-conf"; @@ -95,11 +96,13 @@ export async function setIdfTarget( if (!isDebugging) { try { + const openOCDManager = OpenOCDManager.init(); + const openOCDVersion = await openOCDManager.version(); const devkitsCmd = new DevkitsCommand(workspaceFolder.uri); - const scriptPath = await devkitsCmd.getScriptPath(); + const scriptPath = await devkitsCmd.getScriptPath(openOCDVersion); if (scriptPath) { - const devkitsOutput = await devkitsCmd.runDevkitsScript(); + const devkitsOutput = await devkitsCmd.runDevkitsScript(openOCDVersion); if (devkitsOutput) { const parsed = JSON.parse(devkitsOutput); if (parsed && Array.isArray(parsed.boards)) { From 39597d015c7d6539e4607763ca30dba663decc93 Mon Sep 17 00:00:00 2001 From: Radu Date: Mon, 24 Nov 2025 10:13:37 +0200 Subject: [PATCH 5/5] feat: Add bool parameter for displaying OpenOCD version - Added extra bool parameter for running openOCDManager.version() with default value false, for not silencing the output from it. --- src/espIdf/hints/index.ts | 2 +- src/espIdf/hints/openocdhint.ts | 2 +- src/espIdf/openOcd/openOcdManager.ts | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/espIdf/hints/index.ts b/src/espIdf/hints/index.ts index cd1dd5a83..d54809631 100644 --- a/src/espIdf/hints/index.ts +++ b/src/espIdf/hints/index.ts @@ -203,7 +203,7 @@ export class ErrorHintProvider let openOcdHintsPath: string | null = null; try { const openOCDManager = OpenOCDManager.init(); - const openOCDVersion = await openOCDManager.version(); + const openOCDVersion = await openOCDManager.version(true); if (toolsPath && openOCDVersion) { openOcdHintsPath = await getOpenOcdHintsYmlPath( toolsPath, diff --git a/src/espIdf/hints/openocdhint.ts b/src/espIdf/hints/openocdhint.ts index 5f25901fd..c3fccc6f6 100644 --- a/src/espIdf/hints/openocdhint.ts +++ b/src/espIdf/hints/openocdhint.ts @@ -70,7 +70,7 @@ export class OpenOCDErrorMonitor { try { // Check OpenOCD version first const openOCDManager = OpenOCDManager.init(); - const version = await openOCDManager.version(); + const version = await openOCDManager.version(true); if (!version) { Logger.info( diff --git a/src/espIdf/openOcd/openOcdManager.ts b/src/espIdf/openOcd/openOcdManager.ts index d01e0c0b6..46fd3fd49 100644 --- a/src/espIdf/openOcd/openOcdManager.ts +++ b/src/espIdf/openOcd/openOcdManager.ts @@ -59,7 +59,7 @@ export class OpenOCDManager extends EventEmitter { this.configureServerWithDefaultParam(); } - public async version(): Promise { + public async version(silent: boolean = false): Promise { const modifiedEnv = await appendIdfAndToolsToPath(this.workspace); const openOcdPath = await isBinInPath("openocd", modifiedEnv, [ "openocd-esp32", @@ -70,6 +70,7 @@ export class OpenOCDManager extends EventEmitter { const resp = await sspawn(openOcdPath, ["--version"], { cwd: this.workspace.fsPath, env: modifiedEnv, + silent, }); const versionString = resp.toString(); const match = versionString.match(/v\d+\.\d+\.\d+\-\S*/gi);