Skip to content

Analyzers should build against Microsoft.CodeAnalysis matching SDK #123559

@sbomer

Description

@sbomer

In #123503 we encountered a problem caused by us shipping an analyzer that referenced a removed preview Roslyn API (see #123509 for details). The analyzer was built against an older version of Microsoft.CodeAnalysis that still had the API.

Some details on the current setup:

The older version in this case comes from MicrosoftCodeAnalysisVersion_LatestVS:

The exact version is always a moving target. This version should never go ahead of the version of Roslyn that is included in the most recent
public Visual Studio preview version. If it were to go ahead, then any components depending on this version would not work in Visual Studio
and would cause a major regression for any local development that depends on those components contributing to the build.
This version must also not go ahead of the most recently release .NET SDK version, as that would break the source-build build.
Source-build builds the product with the most recent previously source-built release. Thankfully, these two requirements line up nicely
such that any version that satisfies the VS version requirement will also satisfy the .NET SDK version requirement because of how we ship.
-->
<MicrosoftCodeAnalysisVersion_LatestVS>4.14.0</MicrosoftCodeAnalysisVersion_LatestVS>

Analyzers typically reference either the dependency-flow version (in source-only build) or this LatestVS version otherwise:

<PackageReference Condition="'$(DotNetBuildSourceOnly)' != 'true'" Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisVersion_LatestVS)" PrivateAssets="all" />
<PackageReference Condition="'$(DotNetBuildSourceOnly)' == 'true'" Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisVersion)" PrivateAssets="all" />

To prevent this, we should ensure that the MS product build references a recent version compatible with what we ship in the SDK. Additional constraints we try to keep, as noted above:

  • The version should be <= the Roslyn from the latest VS to enable local dev using VS
  • The version should be <= the latest SDK preview release to enable local dev using that SDK

It sounds like it's safe to use the dependency-flow version of Microsoft.CodeAnalysis in our release/servicing branches. What's a good way to stabilize the dependency versions? Should we use a policy similar to what we do for the compiler version (always use toolset version when building in VMR)?:
https://github.com/dotnet/dotnet/blob/867522e22452ccfb35203fc1dcf6b8a1a2486ba2/repo-projects/Directory.Build.targets#L26-L27

Requirements for the fix:

  • Shipping (release/servicing) analyzers should reference a version of Microsoft.CodeAnalysis that is source-compatible and binary-compatible with the version included in that SDK's Roslyn.
  • This version should be kept up-to-date automatically, or at least validated automatically.

@jkoritzinsky @ViktorHofer @jjonescz

Metadata

Metadata

Assignees

Labels

area-Infrastructureneeds-area-labelAn area label is needed to ensure this gets routed to the appropriate area ownersuntriagedNew issue has not been triaged by the area owner

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions