Skip to content

build: make the IDE code-style analyzers reach the build, and gate IDE0017 - #132

Merged
HandyS11 merged 1 commit into
developfrom
build/enforce-ide0017-at-build-time
Sep 8, 2026
Merged

build: make the IDE code-style analyzers reach the build, and gate IDE0017#132
HandyS11 merged 1 commit into
developfrom
build/enforce-ide0017-at-build-time

Conversation

@HandyS11

@HandyS11 HandyS11 commented Sep 8, 2026

Copy link
Copy Markdown
Owner

Why

SonarQube keeps reporting roslyn:IDE0017 ("Simplify object initialization") on code a green dotnet build has just declared clean. This closes that gap for the whole IDE* family.

The part that isn't obvious

EnforceCodeStyleInBuild and TreatWarningsAsErrors were already on, so setting the severity looked like it should be enough. It isn't — I set dotnet_diagnostic.IDE0017.severity = error and a deliberate probe violation still built clean, while a deliberate Sonar S3400 in the same build failed it as expected. Analyzers were running; no IDE* diagnostic was ever reported.

Roslyn's command-line driver skips analyzers whose diagnostics are all info/hidden by default, and that decision happens before your .editorconfig severity is consulted. So the IDE* family was being dropped from the build no matter what severity it was given.

The differentiator is /errorlog. Confirmed by elimination:

Attempt Result
dotnet_diagnostic.IDE0017.severity = error in .editorconfig still clean
same, via a .globalconfig (verified it reached csc) still clean
-p:EnableCodeStyleSeverity=true still clean
-p:ErrorLog=… fails with error IDE0017

That also explains the original symptom: SonarScanner sets ErrorLog itself to import roslyn:* issues, so Sonar has been seeing a diagnostic set the build never computed.

Changes

  • Directory.Build.targets (new) — sets ErrorLog to obj/…/<Project>.sarif. In .targets rather than .props because IntermediateOutputPath isn't defined that early; from .props it expands to empty and litters a .sarif into every project directory (I hit exactly that). Guarded by Condition="'$(ErrorLog)' == ''" so SonarScanner's own value wins when it runs.
  • .editorconfigdotnet_diagnostic.IDE0017.severity = warning, promoted to an error by TreatWarningsAsErrors.
  • CameraControllerSeamTests.cs — the two real violations this surfaced, rewritten to the object-initializer form already used elsewhere in the file.

Verification

  • dotnet build RustPlusApi.sln --no-incremental → exit 0. The only IDE0017 hits solution-wide were those two; no flood of newly-unmasked analyzers.
  • Probe violation reintroduced → plain dotnet build fails with error IDE0017, exit 1.
  • dotnet test RustPlusApi.sln1239 passed, 18 projects, both TFM hosts.
  • jb cleanupcode on the edited file is idempotent, so the pre-push hook won't reject it.
  • .sarif files land in obj/, already covered by .gitignore:31.

The pre-existing MSB3073 warning (git config core.hooksPath exiting 255) is a local sandbox git-lock artifact, present before these changes and unrelated.

Follow-ups (not in this PR)

  • This unblocks the entire IDE* family, so any other roslyn:IDE* smell Sonar reports can now be gated by adding a severity line next to IDE0017 — no further plumbing.
  • CLAUDE.md's "Commands" section describes the strict build as "TreatWarningsAsErrors + latest-all analyzers"; that's now slightly incomplete. The mechanism is documented in the Directory.Build.targets comment for the moment.

🤖 Generated with Claude Code

…E0017

SonarQube kept reporting roslyn:IDE0017 ("Simplify object initialization") on
code the build had just declared clean. Raising the severity in .editorconfig
does not fix that on its own: Roslyn's command-line driver skips analyzers whose
diagnostics are all info/hidden by default, and it makes that call before the
.editorconfig severity is consulted, so the whole IDE* family was dropped from
the build regardless of the severity given to it. Pinning IDE0017 to `error`
still produced a green build.

Asking for a SARIF error log lifts that filter. It is the same switch
SonarScanner sets in order to import roslyn:* issues, which is exactly why Sonar
saw a diagnostic set the build never computed. With it on, an IDE rule raised to
`warning` becomes a build error through TreatWarningsAsErrors.

The property lives in Directory.Build.targets rather than Directory.Build.props
because IntermediateOutputPath is only defined once
Microsoft.Common.CurrentVersion.targets has run; from .props it expands to empty
and drops the .sarif in each project directory instead of obj/. The condition
leaves SonarScanner's own ErrorLog alone when it runs.

Enabling this surfaced two real IDE0017 violations in CameraControllerSeamTests,
rewritten to the object-initializer form already used elsewhere in that file.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings September 8, 2026 01:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The changes are small, consistent with the repo’s strict build configuration, and the updated test code and build-time SARIF logging approach are coherent and self-contained.

Pull request overview

This PR closes the gap between Sonar-reported roslyn:IDE* code-style diagnostics (notably IDE0017) and what a clean dotnet build currently enforces, by ensuring the compiler actually computes and reports IDE analyzer diagnostics during command-line builds.

Changes:

  • Add a repo-wide Directory.Build.targets that sets ErrorLog to a per-project SARIF file under obj/ (only when $(ErrorLog) is not already set), which causes Roslyn to include IDE analyzer diagnostics in the build.
  • Configure IDE0017 as a warning in .editorconfig, relying on TreatWarningsAsErrors=true to gate it as a build error.
  • Update CameraControllerSeamTests to use object initializers, resolving IDE0017 hits surfaced by the new build behavior.
File summaries
File Description
tests/RustPlusApi.Camera.UnitTests/CameraControllerSeamTests.cs Refactors two test constructions to object initializers to satisfy IDE0017.
Directory.Build.targets Enables SARIF error logging in builds (when not already set) so IDE analyzers are not silently skipped.
.editorconfig Sets IDE0017 severity to warning so it becomes a build-breaking warning under TreatWarningsAsErrors.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@HandyS11
HandyS11 merged commit db548b4 into develop Sep 8, 2026
7 checks passed
@HandyS11
HandyS11 deleted the build/enforce-ide0017-at-build-time branch September 8, 2026 02:21
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