You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Support build-before-run for non-active executable targets via targetName arg (#4757)
* feat: support build-before-run for non-active executable targets (#4656)
When a targetName is provided via ${input:...} args, resolve the named
target directly without calling setLaunchTargetByName(), preventing the
active launch target from changing as a side effect.
Add build deduplication cache (10s TTL) to prepareLaunchTargetExecutable
to avoid duplicate builds when multiple input variables resolve the same
target within a single launch.json evaluation.
Co-authored-by: hanniavalera <[email protected]>
* docs and tests: document ${input:...} pattern and add named target tests
- docs/debug-launch.md: add "Debugging a specific target" section with
full ${input:...} examples for multi-executable projects
- docs/cmake-settings.md: document targetName argument for command
substitution commands
- CHANGELOG.md: add feature entry for #4656
- test: add tests for named target resolution, active target
preservation, buildBeforeRun honor/skip, and invalid target handling
Co-authored-by: hanniavalera <[email protected]>
* fix: check file existence before using build dedup cache
The _prepareCache would return cached results even when the built
binary was deleted, preventing buildBeforeRun from triggering a
rebuild. Add fs.exists() check so the cache is invalidated when the
artifact no longer exists on disk.
Co-authored-by: hanniavalera <[email protected]>
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: hanniavalera <[email protected]>
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ Features:
10
10
- Add "Set Build and Launch/Debug Target" command that sets both the build target and launch target simultaneously. [#4732](https://github.com/microsoft/vscode-cmake-tools/pull/4732)
11
11
- Add `cmake.setBuildTargetSameAsLaunchTarget` setting to automatically set the build target when the launch/debug target is changed. [#4519](https://github.com/microsoft/vscode-cmake-tools/pull/4519)[@nikita-karatun](https://github.com/nikita-karatun)
12
12
- Add `cmake.additionalBuildProblemMatchers` setting to define custom problem matchers for build output. Supports tools like clang-tidy, PCLint Plus, cppcheck, or custom scripts integrated via `add_custom_command`/`add_custom_target`. [#4077](https://github.com/microsoft/vscode-cmake-tools/issues/4077)
13
+
- Support `targetName` argument for launch-target command substitutions (`cmake.launchTargetPath`, etc.) via `${input:...}` variables, enabling build-before-run for non-active executable targets without changing the active launch target. [#4656](https://github.com/microsoft/vscode-cmake-tools/issues/4656)
13
14
14
15
Improvements:
15
16
- Make "CMake: Add ... Preset" commands available in the command palette when `cmake.useCMakePresets` is set to `auto`, even before a CMakePresets.json file exists. [#4401](https://github.com/microsoft/vscode-cmake-tools/issues/4401)
Copy file name to clipboardExpand all lines: docs/cmake-settings.md
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -219,6 +219,27 @@ Each matcher entry has the following properties:
219
219
]
220
220
```
221
221
222
+
#### Resolving a specific target with `${input:...}`
223
+
224
+
All launch-target commands (`cmake.launchTargetPath`, `cmake.getLaunchTargetPath`, and their directory/filename/name variants) accept an optional `targetName` argument. When `targetName` is provided, the command resolves that specific executable target **without changing the active launch target**. This is useful for projects with multiple executables, allowing stable per-target `launch.json` configurations.
225
+
226
+
Use VS Code [input variables](https://code.visualstudio.com/docs/editor/variables-reference#_input-variables) to pass arguments:
227
+
228
+
```jsonc
229
+
{
230
+
"inputs": [
231
+
{
232
+
"id":"serverPath",
233
+
"type":"command",
234
+
"command":"cmake.launchTargetPath",
235
+
"args": { "targetName":"my_server" }
236
+
}
237
+
]
238
+
}
239
+
```
240
+
241
+
Then reference it in a launch configuration as `"program": "${input:serverPath}"`. See [Debugging a specific target](debug-launch.md#debugging-a-specific-target-multi-executable-projects) for full examples.
242
+
222
243
## Next steps
223
244
224
245
- Learn about [user vs. workspace settings](https://code.visualstudio.com/docs/getstarted/settings)
Copy file name to clipboardExpand all lines: docs/debug-launch.md
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -186,6 +186,57 @@ Here are minimal examples of a `launch.json` file that uses `cmake.launchTargetP
186
186
187
187
The value of the `program` attribute is expanded by CMake Tools to be the absolute path of the program to run.
188
188
189
+
### Debugging a specific target (multi-executable projects)
190
+
191
+
If your project defines multiple executables (for example, a `client` and a `server`), you can create stable per-target debug configurations using VS Code's [input variables](https://code.visualstudio.com/docs/editor/variables-reference#_input-variables). Pass the `targetName` argument to any launch-target command so that it resolves a specific executable **without changing the active launch target**. If `cmake.buildBeforeRun` is enabled, the named target is built automatically.
192
+
193
+
```jsonc
194
+
{
195
+
"version":"0.2.0",
196
+
"inputs": [
197
+
{
198
+
"id":"serverPath",
199
+
"type":"command",
200
+
"command":"cmake.launchTargetPath",
201
+
"args": { "targetName":"my_server" }
202
+
},
203
+
{
204
+
"id":"serverDir",
205
+
"type":"command",
206
+
"command":"cmake.getLaunchTargetDirectory",
207
+
"args": { "targetName":"my_server" }
208
+
},
209
+
{
210
+
"id":"clientPath",
211
+
"type":"command",
212
+
"command":"cmake.launchTargetPath",
213
+
"args": { "targetName":"my_client" }
214
+
}
215
+
],
216
+
"configurations": [
217
+
{
218
+
"name":"Debug Server",
219
+
"type":"cppdbg",
220
+
"request":"launch",
221
+
"program":"${input:serverPath}",
222
+
"cwd":"${input:serverDir}"
223
+
},
224
+
{
225
+
"name":"Debug Client",
226
+
"type":"cppdbg",
227
+
"request":"launch",
228
+
"program":"${input:clientPath}",
229
+
"cwd":"${workspaceFolder}"
230
+
}
231
+
]
232
+
}
233
+
```
234
+
235
+
When multiple `${input:...}` variables reference the same target (for example, `serverPath` and `serverDir` above), the build is triggered only once — results are cached for 10 seconds to avoid redundant builds.
236
+
237
+
> **Tip:**
238
+
> For large projects, consider setting `cmake.buildBeforeRun` to `false` and using a `preLaunchTask` instead to keep launch times predictable.
239
+
189
240
### Cache variable substitution
190
241
191
242
You can substitute the value of any variable in the CMake cache by adding a `command`-type input for the `cmake.cacheVariable` command to the `inputs` section of `launch.json` with `args.name` as the name of the cache variable. That input can then be used with input variable substitution of values in the `configuration` section of `launch.json`. The optional `args.default` can provide a default value if the named variable isn't found in the CMake cache.
0 commit comments