Skip to content

Commit 413edc5

Browse files
authored
Compile the MSTest generator against the Roslyn version it is packed for (#1899)
The generator was packed only into analyzers/dotnet/roslyn4.4, but its Microsoft.CodeAnalysis.CSharp reference had been bumped to 4.14.0 without the folder moving with it. Every compiler from 4.4 up to 4.13 therefore selected that folder and then skipped the assembly with CS9057, which is a warning. So [UsesVerify] generated nothing and each Verify call failed at run time with 'TestContext is null. Ensure test class has a [UsesVerify] attribute'. That covers VS 17.8 to 17.13, the .NET 8 LTS SDK, and .NET 9 SDKs before 9.0.300. The generator now compiles against 4.4 again, restoring the support floor the folder always claimed. It only uses APIs from 4.3 and earlier (ForAttributeWithMetadataName, WithTrackingName), so nothing is given up. Both the reference and the pack path now read one VerifyRoslynVersion property, so they cannot drift apart again. This replaces the CheckPackageVersion guard on Microsoft.CodeAnalysis.CSharp, which asserted the central version that this project no longer uses.
1 parent 27db6b6 commit 413edc5

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/Directory.Build.props

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
<ImportVerifyMsbuildFiles Condition="'$(MSBuildProjectName)' != '' And $(MSBuildProjectName.EndsWith('Tests'))">true</ImportVerifyMsbuildFiles>
2121
<!-- Work around https://github.com/dotnet/sdk/issues/51265#issuecomment-3407578810 -->
2222
<RestoreEnablePackagePruning>false</RestoreEnablePackagePruning>
23+
<!-- The Roslyn version the MSTest source generator compiles against, and the analyzers
24+
folder Verify.MSTest packs it into. The two have to agree: a generator compiled
25+
against a higher version than its folder claims is loaded by every compiler in
26+
between and then skipped with CS9057, which silently disables [UsesVerify] and
27+
surfaces only as "TestContext is null" at run time. Kept here so neither can move
28+
without the other. Lower means more compilers are supported, so raise it only for
29+
an API the generator actually needs.
30+
https://learn.microsoft.com/en-us/visualstudio/extensibility/roslyn-version-support -->
31+
<VerifyRoslynVersion>4.4</VerifyRoslynVersion>
2332
</PropertyGroup>
2433
<!-- This directory contains two solutions (Verify.slnx and VerifyDangling.slnx), so Verify cannot
2534
auto-discover the solution when a project is built outside of a solution context. Default to the

src/Verify.MSTest.SourceGenerator/Verify.MSTest.SourceGenerator.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all" />
12-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" />
12+
<!-- Overrides the central version, which the rest of the solution uses to consume
13+
Roslyn. This assembly is loaded by the consumer's compiler instead, so it is
14+
compiled against the oldest Roslyn it supports: the one named by the analyzers
15+
folder it is packed into. See VerifyRoslynVersion in Directory.Build.props. -->
16+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" VersionOverride="$(VerifyRoslynVersion).0" />
1317
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />
1418
</ItemGroup>
1519

1620
<Target Name="CheckPackageVersion" BeforeTargets="Build">
17-
<!-- https://learn.microsoft.com/en-us/visualstudio/extensibility/roslyn-version-support?view=vs-2022 -->
1821
<Error Condition="'%(PackageVersion.Identity)' == 'Microsoft.CodeAnalysis.Analyzers' AND '%(PackageVersion.Version)' != '3.11.0'" Text="Invalid package version for Microsoft.CodeAnalysis.Analyzers. Expected: 3.11.0." />
19-
<Error Condition="'%(PackageVersion.Identity)' == 'Microsoft.CodeAnalysis.CSharp' AND '%(PackageVersion.Version)' != '4.14.0'" Text="Invalid package version for Microsoft.CodeAnalysis.CSharp. Expected: 4.14.0." />
2022
</Target>
2123
</Project>

src/Verify.MSTest/Verify.MSTest.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<PropertyGroup>
33
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">net462;net472;net48</TargetFrameworks>
44
<TargetFrameworks>$(TargetFrameworks);net8.0;net9.0;net10.0;net11.0</TargetFrameworks>
5-
<RoslynVersion>4.4</RoslynVersion>
65
</PropertyGroup>
76
<ItemGroup>
87
<PackageReference Include="Microsoft.Sbom.Targets" PrivateAssets="all" Condition="'$(CI)' == 'true'" />
@@ -16,7 +15,7 @@
1615
<None Include="buildTransitive\Verify.MSTest.props" Pack="true" PackagePath="build\" />
1716
<None Include="..\Verify.MSTest.SourceGenerator\bin\Release\netstandard2.0\Verify.MSTest.SourceGenerator.dll"
1817
Pack="true"
19-
PackagePath="analyzers\dotnet\roslyn$(RoslynVersion)\cs\" />
18+
PackagePath="analyzers\dotnet\roslyn$(VerifyRoslynVersion)\cs\" />
2019
</ItemGroup>
2120
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
2221
<Reference Include="System.IO.Compression" />

0 commit comments

Comments
 (0)