Skip to content

Conversation

@jasonmalinowski
Copy link
Member

@jasonmalinowski jasonmalinowski commented Aug 7, 2025

If you were to have the same analyzer added as both a host analyzer and a project analyzer, we might end up removing it from the "project analyzer" set when we create the project CompilationWithAnalyzers. This confuses later code since it expects the analyzers to be present.

This was regressed in c860af4, where a mechanical refactoring took us from having a single CompilationWithAnalyzers to two, one for project analyzers and one for host analyzers. The loop in the method was copied into two, but the set wasn't cleared between the two loops. I believe the intent was to clear out the set (rather than the set persist to deal with duplicates between the host and project sets), given the comment implies we do want duplicates to overwrite in the analyzerMap that we're also producing.

I also split the two analyzer maps, one for project analyzers and one for host analyzers. If you don't do this, it means that a conflict where the same analyzer existing in both sets will overwrite each other, later code will then return a project analyzer which might be a different instance than the host analyzer equivalent.

Fixes #79706
Fixes https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2325694

@jasonmalinowski jasonmalinowski force-pushed the fix-analyzers-missing-in-project-analyzers branch from 5c027e3 to 5dbd4e2 Compare August 8, 2025 00:18
@dotnet-policy-service dotnet-policy-service bot added Needs UX Triage Needs API Review Needs to be reviewed by the API review council labels Aug 8, 2025
@jasonmalinowski jasonmalinowski changed the base branch from release/dev17.14 to main August 8, 2025 01:22
@dotnet dotnet deleted a comment from dotnet-policy-service bot Aug 8, 2025
@jasonmalinowski jasonmalinowski removed Needs UX Triage Needs API Review Needs to be reviewed by the API review council labels Aug 8, 2025
@jasonmalinowski jasonmalinowski self-assigned this Aug 8, 2025
@jasonmalinowski jasonmalinowski force-pushed the fix-analyzers-missing-in-project-analyzers branch 2 times, most recently from f231f34 to 50cb458 Compare August 8, 2025 22:27
@jasonmalinowski jasonmalinowski marked this pull request as ready for review August 8, 2025 22:28
@jasonmalinowski jasonmalinowski requested review from a team as code owners August 8, 2025 22:28
If you were to have the same analyzer added as both a host analyzer
and a project analyzer, we might end up removing it from the "project
analyzer" set when we create the project CompilationWithAnalyzers. This
confuses later code since it expects the analyzers to be present.

This was regressed in c860af4,
where a mechanical refactoring took us from having a single
CompilationWithAnalyzers to two, one for project analyzers and one for
host analyzers. The loop in the method was copied into two, but the
set wasn't cleared between the two loops. I believe the intent was to
clear out the set (rather than the set persist to deal with duplicates
between the host and project sets), given the comment implies we do
want duplicates to overwrite in the analyzerMap that we're also
producing.

I also split the two analyzer maps, one for project analyzers and one for
host analyzers. If you don't do this, it means that a conflict where the
same analyzer existing in both sets will overwrite each other, later
code will then return a project analyzer which might be a different
isntance than the host analyzer equivalent.
@jasonmalinowski jasonmalinowski force-pushed the fix-analyzers-missing-in-project-analyzers branch from 50cb458 to 3fcabbd Compare August 8, 2025 22:28
Comment on lines +749 to +751
// In this case, the analyzers are ran twice. This appears to be a bug in SkippedHostAnalyzersInfo.Create, because it calls
// HostDiagnosticAnalyzers.CreateProjectDiagnosticAnalyzersPerReference which already filters out references, it doesn't return any
// references to skip.
Copy link
Member Author

@jasonmalinowski jasonmalinowski Aug 8, 2025

Choose a reason for hiding this comment

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

It is entirely possible this is the "real" bug in this scenario. Fundamentally, the issue happening on user machines here is we have host-provided analyzers and project-provided analyzers that are overlapping in various ways, which eventually leads to crashes in the DiagnosticComputer. You can imagine fixing this two ways:

  1. We fix the logic for deduplication so that DiagnosticComputer never gets duplicates in any way.
  2. We fix DiagnosticComputer to deal with the duplicates.

I'm doing 2 in this PR. That's the approach I was following until writing this test, and then I had to debug what's going on here, so at this point I'm biased towards that! But for 17.14, I suspect that's the right thing to do, since:

  1. DiagnosticComputer has comments and code trying to deal with the duplicates (which makes me think it is expected to treat that as valid input.)
  2. The changes there to fix the duplicate handling appear to be straightforward changes to the code and addressing simple oversights, which is higher confidence than trying to ensure it never gets invalid inputs in the first place.

{
projectBuilder.Add(analyzer);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

aside. i kinda wish these two loops were just two calls to a helper function.

@jasonmalinowski jasonmalinowski merged commit bdb4dbe into dotnet:main Aug 9, 2025
28 checks passed
@jasonmalinowski jasonmalinowski deleted the fix-analyzers-missing-in-project-analyzers branch August 9, 2025 00:54
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Aug 9, 2025
@jasonmalinowski
Copy link
Member Author

/backport to release/dev17.14

@github-actions
Copy link
Contributor

github-actions bot commented Aug 9, 2025

Started backporting to release/dev17.14: https://github.com/dotnet/roslyn/actions/runs/16843503072

AbhitejJohn added a commit that referenced this pull request Aug 12, 2025
…ng diagnostics OOP (#79861)

Backport of #79821 to release/dev17.14

/cc @jasonmalinowski

## Customer Impact

## Regression

- [ ] Yes
- [ ] No

[If yes, specify when the regression was introduced. Provide the PR or
commit if known.]

## Testing

[How was the fix verified? How was the issue missed previously? What
tests were added?]

## Risk

[High/Medium/Low. Justify the indication by mentioning how risks were
measured and addressed.]
@RikkiGibson RikkiGibson modified the milestones: Next, 18.0 P1 Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

3 participants