-
Notifications
You must be signed in to change notification settings - Fork 4
fix: resolve CS8032 assembly version mismatch (#850) #888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rjmurillo
merged 21 commits into
main
from
fix/850-system-collections-immutable-version-mismatch
Feb 16, 2026
Merged
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7265686
fix: resolve CS8032 assembly version mismatch for System.Collections.…
rjmurillo 368dc64
fix: add SRM check to MSBuild gate, fix PerfDiff NU1109
rjmurillo ffc4d3a
ci: add analyzer host compatibility check to CI pipeline
rjmurillo 2c2ba70
fix(ci): correct DLL paths in analyzer host compatibility check
cursoragent 94f0b9b
fix(build): enhance analyzer host compatibility validation
cursoragent 3519f32
ci: add analyzer load integration test for supported SDK versions
rjmurillo e8414c5
ci: add net10.0 to analyzer load integration test matrix
rjmurillo 9ff4f72
fix(ci): check build exit code in analyzer-load-test
cursoragent 6fefa40
ci: add .NET Framework 4.7.2, 4.8, 4.8.1 to analyzer load test matrix
rjmurillo 063d773
ci: use x64 runners for .NET Framework analyzer load tests
rjmurillo 32d9a52
fix(ci): restore correct artifact paths for analyzer host validation
rjmurillo 7a68343
ci: use msbuild.exe on Windows for .NET Framework analyzer load tests
rjmurillo 7a9fd93
docs: add gh act workflow verification guide to CONTRIBUTING.md
rjmurillo 67c7132
ci: test analyzer loading on both VS 2022 and VS 2026 hosts
rjmurillo c388191
ci: split build and test jobs, remove Windows ARM build
rjmurillo 40802cc
ci: extract perf into separate job, unblock test and load-test
rjmurillo d4a3717
fix(ci): convert MSYS path to Windows format for local NuGet feed
cursoragent 6bdd5db
ci: switch analyzer-load-test to pwsh, fix msbuild not found
rjmurillo 86e37e7
ci: fix misleading OK output in host compatibility check
rjmurillo 18a6d96
fix(ci): add null check and build exit code validation in analyzer-lo…
cursoragent 49ad078
fix: address PR review feedback for analyzer load test robustness
rjmurillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <Project> | ||
| <!-- | ||
| Analyzer Host Compatibility Validation | ||
|
|
||
| Analyzers run inside the user's compiler host (csc). The host provides | ||
| BCL assemblies at specific versions. If the analyzer was compiled against | ||
| a NEWER version than the host provides, assembly binding fails with CS8032. | ||
|
|
||
| This target enforces version ceilings on host-provided packages for all | ||
| shipped analyzer projects. It is a build-breaking gate. | ||
|
|
||
| See: https://github.com/rjmurillo/moq.analyzers/issues/850 | ||
| --> | ||
| <PropertyGroup> | ||
| <!-- Maximum assembly versions the minimum supported host (.NET 8 SDK) provides --> | ||
| <_MaxSystemCollectionsImmutableMajor>8</_MaxSystemCollectionsImmutableMajor> | ||
| <_MaxSystemReflectionMetadataMajor>8</_MaxSystemReflectionMetadataMajor> | ||
| </PropertyGroup> | ||
|
|
||
| <Target Name="ValidateAnalyzerHostCompatibility" | ||
| AfterTargets="ResolvePackageAssets" | ||
| Condition="'$(GeneratePackageOnBuild)' == 'true' OR '$(MSBuildProjectName)' == 'Moq.CodeFixes'"> | ||
|
rjmurillo marked this conversation as resolved.
|
||
|
|
||
| <ItemGroup> | ||
| <_SciRef Include="@(ResolvedCompileFileDefinitions)" | ||
| Condition="'%(NuGetPackageId)' == 'System.Collections.Immutable'" /> | ||
| <_SrmRef Include="@(ResolvedCompileFileDefinitions)" | ||
| Condition="'%(NuGetPackageId)' == 'System.Reflection.Metadata'" /> | ||
| </ItemGroup> | ||
|
|
||
| <Error Condition="'@(_SciRef)' != '' AND | ||
| $([System.Version]::Parse('%(_SciRef.NuGetPackageVersion)').Major) > $(_MaxSystemCollectionsImmutableMajor)" | ||
| Text="BUILD BLOCKED: System.Collections.Immutable resolved to %(_SciRef.NuGetPackageVersion) for shipped analyzer project '$(MSBuildProjectName)'. Maximum allowed major version is $(_MaxSystemCollectionsImmutableMajor).x. The analyzer runs in the user's compiler host which may only have version $(_MaxSystemCollectionsImmutableMajor).0.0.0. See: https://github.com/rjmurillo/moq.analyzers/issues/850" /> | ||
|
|
||
| <Error Condition="'@(_SrmRef)' != '' AND | ||
| $([System.Version]::Parse('%(_SrmRef.NuGetPackageVersion)').Major) > $(_MaxSystemReflectionMetadataMajor)" | ||
| Text="BUILD BLOCKED: System.Reflection.Metadata resolved to %(_SrmRef.NuGetPackageVersion) for shipped analyzer project '$(MSBuildProjectName)'. Maximum allowed major version is $(_MaxSystemReflectionMetadataMajor).x. The analyzer runs in the user's compiler host which may only have version $(_MaxSystemReflectionMetadataMajor).0.0.0. See: https://github.com/rjmurillo/moq.analyzers/issues/850" /> | ||
|
|
||
| </Target> | ||
| </Project> | ||
|
rjmurillo marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/Moq.Analyzers.Test/AnalyzerAssemblyCompatibilityTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| using System.Reflection; | ||
| using System.Runtime.Loader; | ||
|
|
||
| namespace Moq.Analyzers.Test; | ||
|
|
||
| public class AnalyzerAssemblyCompatibilityTests | ||
| { | ||
| // Primary shipped assemblies built by this project | ||
| private static readonly string PrimaryAnalyzerAssembly = "Moq.Analyzers"; | ||
| private static readonly string PrimaryCodeFixAssembly = "Moq.CodeFixes"; | ||
|
|
||
| // Bundled third-party assemblies included in the package | ||
| private static readonly string BundledAnalyzerUtilities = "Microsoft.CodeAnalysis.AnalyzerUtilities"; | ||
|
|
||
| // Maximum assembly versions that the minimum supported SDK host (.NET 8) provides. | ||
| // The analyzer must not reference anything higher, or it will fail to load with CS8032. | ||
| // See: https://github.com/rjmurillo/moq.analyzers/issues/850 | ||
| private static readonly Version MaxImmutableVersion = new(8, 0, 0, 0); | ||
| private static readonly Version MaxMetadataVersion = new(8, 0, 0, 0); | ||
|
|
||
| public static TheoryData<string> ShippedAssemblies => | ||
| new() | ||
| { | ||
| { PrimaryAnalyzerAssembly }, | ||
| { PrimaryCodeFixAssembly }, | ||
| { BundledAnalyzerUtilities }, | ||
| }; | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(ShippedAssemblies))] | ||
| public void ShippedDlls_MustNotExceedMinimumHostAssemblyVersions(string assemblyName) | ||
| { | ||
| FileInfo testAssembly = new(Assembly.GetExecutingAssembly().Location); | ||
| FileInfo dllFile = new(Path.Combine(testAssembly.DirectoryName!, $"{assemblyName}.dll")); | ||
|
|
||
| Assert.True(dllFile.Exists, $"Expected shipped DLL not found: {dllFile.FullName}"); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| AssemblyLoadContext context = new("compat-check", isCollectible: true); | ||
| try | ||
| { | ||
| Assembly assembly = context.LoadFromAssemblyPath(dllFile.FullName); | ||
|
|
||
| // For bundled third-party DLLs, verify we are testing the same artifact | ||
| // that the primary analyzer assembly references, not a different version | ||
| // that might exist in the test output from the test project's own dependencies. | ||
| if (string.Equals(assemblyName, BundledAnalyzerUtilities, StringComparison.Ordinal)) | ||
| { | ||
| VerifyBundledAssemblyMatchesPrimaryReference(context, testAssembly.DirectoryName!, assembly); | ||
| } | ||
|
|
||
| AssemblyName[] references = assembly.GetReferencedAssemblies(); | ||
|
|
||
| AssertVersionNotExceeded(assemblyName, references, "System.Collections.Immutable", MaxImmutableVersion); | ||
| AssertVersionNotExceeded(assemblyName, references, "System.Reflection.Metadata", MaxMetadataVersion); | ||
| } | ||
| finally | ||
| { | ||
| context.Unload(); | ||
| } | ||
| } | ||
|
|
||
| private static void VerifyBundledAssemblyMatchesPrimaryReference( | ||
| AssemblyLoadContext context, | ||
| string outputDirectory, | ||
| Assembly bundledAssembly) | ||
| { | ||
| // Load the primary analyzer assembly to verify the bundled assembly matches | ||
| // what the analyzer actually references. This ensures we're testing the | ||
| // artifact that will be packaged, not a different version from the test project. | ||
| string primaryPath = Path.Combine(outputDirectory, $"{PrimaryAnalyzerAssembly}.dll"); | ||
| Assembly primaryAssembly = context.LoadFromAssemblyPath(primaryPath); | ||
|
|
||
| AssemblyName? expectedReference = primaryAssembly | ||
| .GetReferencedAssemblies() | ||
| .FirstOrDefault(r => string.Equals(r.Name, bundledAssembly.GetName().Name, StringComparison.Ordinal)); | ||
|
|
||
| Assert.NotNull(expectedReference); | ||
| Assert.Equal( | ||
| expectedReference.Version, | ||
| bundledAssembly.GetName().Version); | ||
|
rjmurillo marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private static void AssertVersionNotExceeded( | ||
| string assemblyName, | ||
| AssemblyName[] references, | ||
| string referenceName, | ||
| Version maxVersion) | ||
| { | ||
| AssemblyName? reference = references.FirstOrDefault( | ||
| r => string.Equals(r.Name, referenceName, StringComparison.Ordinal)); | ||
|
|
||
| if (reference?.Version is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| Assert.True( | ||
| reference.Version <= maxVersion, | ||
| $"{assemblyName} references {referenceName} version {reference.Version}, but the minimum supported SDK host only provides {maxVersion}. See: https://github.com/rjmurillo/moq.analyzers/issues/850"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.