Skip to content

Infer MSVC dev environment from preset structure without explicit compiler#4779

Open
Copilot wants to merge 3 commits intomainfrom
copilot/improve-msvc-setup-feedback
Open

Infer MSVC dev environment from preset structure without explicit compiler#4779
Copilot wants to merge 3 commits intomainfrom
copilot/improve-msvc-setup-feedback

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 2, 2026

This change addresses item #4225

This changes visible behavior and documentation

The following changes are proposed:

  • Detect VS Developer Environment intent from preset structure (VS generator, strategy: "external" on toolset/architecture) without requiring explicit CMAKE_CXX_COMPILER: "cl" in cacheVariables
  • Emit log.info hint on Windows when auto mode skips VS Dev Env setup, so users understand why builds may fail
  • Document the improved auto-detection in docs/cmake-presets.md

The purpose of this change

In "auto" mode, tryApplyVsDevEnv only activated when CMAKE_CXX_COMPILER or CMAKE_C_COMPILER was explicitly set to a recognized compiler in cacheVariables. Visual Studio and CLion infer MSVC intent from the toolset/architecture fields alone. This caused friction for users sharing presets across IDEs.

Now, three additional signals trigger VS Dev Env setup in auto mode:

  1. A Visual Studio generator ("generator": "Visual Studio 17 2022")
  2. "toolset": { "strategy": "external" }
  3. "architecture": { "strategy": "external" }

This means presets like the following now work without the cl compiler hint:

{
  "name": "ninja-msvc",
  "generator": "Ninja",
  "toolset": { "value": "v143", "strategy": "external" },
  "architecture": { "value": "x64", "strategy": "external" }
}

Other Notes/Information

  • New exported hasVsDevEnvSignals() pure function handles signal detection, tested independently
  • "always" and "never" modes are unchanged
  • Presets with explicit CMAKE_CXX_COMPILER follow the same path as before (backward compatible)
  • 8 unit tests added covering positive detection, negative cases (Ninja-only, set strategy, string toolset), and combined signals
Original prompt

This section details on the original issue you should resolve

<issue_title>Improve automatic setup of MSVC dev environment with presets</issue_title>
<issue_description>### Brief Issue Summary

Feedback from Customers:

There should be better feedback about automatic setup of MSVC dev environment with presets. It took me a long time to figure out that I needed to set CMAKE_CXX_COMPILER to cl.exe explicitly in my CMakePresets.json for it to work. Since this is not needed for typical command line / CI workflows where you set up the environment manually (and rely on CMake to correctly guess that you want to compile with MSVC), some help from IDE is desirable here.

From what I can tell, Visual Studio and even JetBrains CLion do not need "CMAKE_CXX_COMPILER": "cl" as an explicit hint in the preset to provide the MSVC environment setup, just the toolset/architecture keys are enough.

Would it be possible to drop this requirement in cmake-tools, reducing friction with presets authored by/for other tools/IDEs?

CMake Tools Diagnostics

Debug Log

Additional Information

No response</issue_description>

<agent_instructions>## Fix Issue #4225 — Improve automatic MSVC dev environment setup with presets

Context

Issue: #4225
HEAD commit at time of writing: c7ce06129bcfc8248383d93b6d2cb52f00b2394a

Users with presets that use Ninja + MSVC (a common cross-IDE pattern) must currently add "CMAKE_CXX_COMPILER": "cl" explicitly to their cacheVariables for cmake-tools to automatically set up the VS Developer Environment. Visual Studio and CLion do not require this — they infer MSVC intent from the toolset and architecture fields alone. This is a friction point for users sharing presets across IDEs.


Primary entry point

Start your investigation at:

src/presets/preset.ts — function tryApplyVsDevEnv (around line 965)

This is the only place where the VS Dev Env decision is made. In "auto" mode, the function only activates if CMAKE_CXX_COMPILER or CMAKE_C_COMPILER is explicitly set to a supported compiler name in preset.cacheVariables. If neither is present, it silently skips setup with no user feedback.

Also read:

  • getToolset() and getArchitecture() (same file) — these already parse the toolset and architecture preset fields
  • util.isSupportedCompiler() in src/util.ts — the compiler name allowlist
  • test/unit-tests/presets/presets.test.ts — existing test patterns for this function
  • docs/cmake-presets.md — user-facing documentation for this feature

Problem to solve

When a configure preset has no explicit CMAKE_CXX_COMPILER/CMAKE_C_COMPILER but does have one of these MSVC-intent signals:

  1. A Visual Studio generator (e.g. "generator": "Visual Studio 17 2022")
  2. "toolset": { "value": "...", "strategy": "external" } — the "external" strategy explicitly means "the calling IDE should set up the environment"
  3. "architecture": { "value": "x64", "strategy": "external" }

…then cmake-tools should still attempt to activate the VS Dev Env in "auto" mode, just as it does when CMAKE_CXX_COMPILER: "cl" is present.

Additionally, when "auto" mode silently skips VS Dev Env on Windows, a user-visible log.info message should be emitted so users understand why their build may fail and how to fix it.


Suggested approach (investigate and improve upon as needed)

This is a starting point — do your own investigation and deviate if the codebase reveals a better or cleaner pattern.

The key change in tryApplyVsDevEnv:

// CURRENT (auto mode only activates if compiler name is explicit):
const compilerName = util.isSupportedCompiler(cxxCompiler) || util.isSupportedCompiler(cCompiler);
if (compilerName && whereExecutable) { ... }

// PROPOSED ADDITION: also check for structural MSVC signals in the preset itself
const hasVsGenerator = /Visual Studio \d+/.test(preset.generator ?? '');
const toolsetStrategy = typeof preset.toolset === 'object' ? (preset.toolset as any).strategy : undefined;
const archStrategy   = typeof preset.architecture === 'object' ? (preset.architecture as any).strategy : undefined;
const infersMsvc = !compilerName && (hasVsGenerator || toolsetStrategy === 'external' || archStrategy === 'external');

// If MSVC is inferred, treat 'cl' as the effective compiler name for env setup
const effectiveCompilerName = compilerName ?? (infersMsvc ? 'cl' : undefined);

If effectiveCompilerName is set but compilerName was not (i.e. inferred), proceed with getVsDevEnv as normal.

When neither compilerName nor infersMsvc is true and we are on Windows, emit:

log.info(localize(
    'vs.dev.env.skipped.hint',
    'Configure preset "{0}": VS Developer Environment was not set up automatically. ' +
    'To enable it, set CMAKE_CXX_COMPILER to "cl" in cacheVariables, or ...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes microsoft/vscode-cmake-tools#4225

<!-- START COPILOT CODING AGENT TIPS -->
---

 Let Copilot coding agent [set things up for you](https://github.com/microsoft/vscode-cmake-tools/issues/new?title=+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI added a commit that referenced this pull request Mar 2, 2026
Co-authored-by: hanniavalera <90047725+hanniavalera@users.noreply.github.com>
Detect VS Developer Environment signals from preset structure (VS
generator, external toolset/architecture strategy) without requiring
explicit CMAKE_CXX_COMPILER in cacheVariables. Add log.info hint when
auto mode skips VS Dev Env on Windows.

Fixes #4225

Co-authored-by: hanniavalera <90047725+hanniavalera@users.noreply.github.com>
@hanniavalera hanniavalera marked this pull request as ready for review March 2, 2026 17:35
Co-authored-by: hanniavalera <90047725+hanniavalera@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve automatic setup of MSVC dev environment with presets Infer MSVC dev environment from preset structure without explicit compiler Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants