Skip to content

quality: clang-tidy on MSVC actions runs without the MSYS no-conversion env, so bash rewrites /I and /D flags into paths #8733

Description

@TrapsterDK

Problem

On Windows, MSYS bash rewrites arguments that start with / into paths when it starts a native exe. #7047 fixed this for the cl.exe syntax replay only, by adding _MSYS_NO_CONVERSION to its env. The ClangTidy action for the same compile action still uses plain action.env.

The tidy argv goes through bash: bash "$@" runs clang_tidy_run.sh, which runs clang-tidy.exe. _msvc_tidy_argv puts the cl flags after -- (/nologo, /DX=1, /I..., /std:c++17). They get rewritten the same way, so /DCOMPILER_MSVC becomes something like C:/msys64/DCOMPILER_MSVC.

Scenario: a cc_library built with --//cc/toolchains:compiler=msvc (cl.exe) on windows_x86_64 has a dep header or a /D define and is linted with clang_tidy. The fixed compilation database treats the rewritten args as input files, so the include dirs and defines are lost. The run reports 'dep.h' file not found / clang-diagnostic-error instead of the real findings. clang_tidy_run.sh always exits 0, so these bogus diagnostics are parsed as findings.

Where

  • quality/clang_syntax.bzl:36: _MSYS_NO_CONVERSION is defined.
  • quality/clang_syntax.bzl:362-364: syntax_env gets _MSYS_NO_CONVERSION for the msvc driver. Only ClangSyntax uses it (line 373).
  • quality/clang_syntax.bzl:380-391: the ClangTidy run_shell passes env = action.env (line 388).
  • quality/clang_syntax.bzl:133-155: _msvc_tidy_argv puts the /-style flags after --.
  • quality/clang_tidy_run.sh:20: runs clang-tidy.exe from bash with no conversion guard.
  • quality/testdata/real_result_cases.bzl:44-49: _msvc_case covers clang_syntax only.

Fix

  • Pass syntax_env as env to the ClangTidy run_shell at quality/clang_syntax.bzl:388. It already includes _MSYS_NO_CONVERSION for the msvc driver.
  • Add a real_result_clang_tidy_cc_msvc_* case next to _msvc_case. It should use cl.exe, include a dep header and set a /D define, and assert a real finding with no file not found.

Activity

  1. added
    needs-host
    P1
    model:sonnet
    on Nov 6, 2026
  2. TrapsterDK commented on Nov 6, 2026

    @TrapsterDK
    CollaboratorAuthor

    Plan

    Size: S. Risk: low (only the env of the msvc ClangTidy action changes; ClangSyntax already uses the same env).

    Confirmed at HEAD f404f79. quality/clang_syntax.bzl:362-364 builds syntax_env with _MSYS_NO_CONVERSION for identity.driver == "msvc". ClangSyntax uses it (:373). The ClangTidy run_shell at :381-391 still passes env = action.env (:388). _msvc_tidy_argv (:131-155) puts /nologo, /D..., /I... after --. The unit test at quality/clang_syntax_tests.bzl:118-121 shows that argv.

    Chosen: reuse one env helper for both msvc actions. Rejected: export the MSYS vars inside clang_tidy_run.sh. Too late, because the outer bash "$@" has already started. It would also put Windows logic into the shared runner.

    Steps

    1. quality/clang_syntax.bzl: add a public clang_action_env(env, driver). It returns a copy of env, plus _MSYS_NO_CONVERSION when the driver is "msvc". The docstring is one line.
    2. quality/clang_syntax.bzl:_clang_syntax_impl: set syntax_env = clang_action_env(action.env, identity.driver). Pass env = syntax_env to the ClangTidy run_shell at :388. Leave envs.append(dict(action.env)) (:394) and the CUDA tidy env (:339) as they are.
    3. quality/testdata/clang_syntax/tidy_dirty.c and its BUILD.bazel:82-88: add local_defines = ["TIDY_DEFINE=1"]. Guard the file with #ifndef TIDY_DEFINE / #error. cl renders this as /DTIDY_DEFINE=1. The file already includes dep.h, which needs /I.
    4. quality/testdata/real_result_cases.bzl:44-49: let _msvc_case take tool, capability, policy (default to clang_syntax). Build the name as real_result_<tool>_cc_msvc_<name> and update the two existing calls. Add:
      • clang_tidy "clean" on clean: contains clean.c; absent readability-else-after-return, file not found, clang-diagnostic-error.
      • clang_tidy "dirty" on tidy_dirty: contains readability-else-after-return; absent file not found, clang-diagnostic-error, TIDY_DEFINE.

    Steps 1-2 come first. Step 3 can run alongside them. Step 4 depends on step 3.

    Tests

    • quality/clang_syntax_tests.bzl (//quality:clang_syntax_unit): clang_action_env({"A":"1"}, "msvc") -> A plus the 3 MSYS keys. clang_action_env({"A":"1"}, "gcc") -> {"A":"1"}.
    • The real-result cases from step 4. They are tagged windows_x86_64 only. Make sure the existing _cc_case/_gcc_case tidy_dirty cases still pass with the new define.

    Verify

    • tools/dx test //quality:clang_syntax_unit
    • tools/dx test //quality/testdata:real_result_clang_tidy_cc_dirty //quality/testdata:real_result_clang_tidy_cc_gcc_clean
    • On a Windows x86_64 host with cl.exe: tools/dx test //quality/testdata:real_result_clang_tidy_cc_msvc_dirty //quality/testdata:real_result_clang_tidy_cc_msvc_clean //quality/testdata:real_result_clang_syntax_cc_msvc_dirty
    • tools/dx check --apply --affected, tools/dx test --affected, tools/dx check --affected

    Notes

    • Needs a Windows x86_64 host. Before the fix, the msvc dirty case should fail with file not found. Run it once without the fix to confirm.
    • No docs change is needed. This is a bug fix and documented behaviour does not change. No upstream issue, since MSYS conversion is documented behaviour.
    • Possible follow-up, not part of this PR: the CUDA tidy path (:339) on Windows. It has no / flags today because an MSVC -ccbin is rejected.
  3. TrapsterDK commented on Nov 6, 2026

    @TrapsterDK
    CollaboratorAuthor

    Fixed by #8853 (merged as b2532b5). Windows evidence runs 42307832913 (repro) and 42307836687 (fix) both failed before the tests ran, on an unrelated package load error: @@+haskell+dx_ghc_windows_x86_64 cannot load @@rules_cc+//cc:cc_toolchain.bzl. I dispatched a new msvc verification run from main: 42309468960.

  4. TrapsterDK commented on Nov 6, 2026

    @TrapsterDK
    CollaboratorAuthor

    Verification run 42309468960 (evidence.yml, ref main, windows_x86_64, --//cc/toolchains:compiler=msvc) failed again before any test ran, with the same package load error: error loading package '@@+haskell+dx_ghc_windows_x86_64//': cannot load '@@rules_cc+//cc:cc_toolchain.bzl': no such file (preceded by at index 0 of provides, got element of type NoneType, want Provider). This is an infra problem with the Haskell Windows toolchain on main and has nothing to do with #8733. The fix stays as merged in #8853 (b2532b5). The msvc clang_tidy/clang_syntax real_result targets still need a green Windows run once the Haskell load error is fixed.

    Assisted by Claude.

  5. TrapsterDK commented on Nov 7, 2026

    @TrapsterDK
    CollaboratorAuthor

    Windows msvc evidence run for the Haskell mingw load fix (PR #8976): 42323331884. Upstream rules_haskell issue tweag/rules_haskell#2483 and PR tweag/rules_haskell#2484.

    Assisted by Claude.

  6. TrapsterDK commented on Nov 7, 2026

    @TrapsterDK
    CollaboratorAuthor

    Run 42323331884 got past the Haskell load error (#8976). The msvc clang_tidy cases then failed with unexpected: file: Bazel shell-tokenizes sh_test args, so the absent string file not found was split into separate words. The fix is in the new PR on branch real-result-args-quote-8733. Windows msvc evidence: run 42325679242.

    Assisted by Claude.

  7. TrapsterDK commented on Nov 7, 2026

    @TrapsterDK
    CollaboratorAuthor

    PR #8996 (merged) fixed real_result_test arg quoting and refreshed the stale windows_support/qt_sdk lockfile digests.

    Evidence, windows_x86_64 with --//cc/toolchains:compiler=msvc:

    Remaining: the checker now matches the whole string, so clang-tidy under msvc really reports a missing include. Next step: read the msvc tidy test.log for the missing header and check include/sysroot handling in quality/clang_syntax.bzl (_msvc_tidy_argv, #8853).

    Assisted by Claude.

  8. TrapsterDK commented on Nov 7, 2026

    @TrapsterDK
    CollaboratorAuthor

    Evidence for #9012 (merged): run 42329489267 (evidence.yml, windows_x86_64, --//cc/toolchains:compiler=msvc). real_result_clang_tidy_cc_msvc_dirty and real_result_clang_tidy_cc_msvc_clean passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1model:sonnetneeds-host

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions