Skip to content
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

Improve analyzer consistency check message #73329

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -749,5 +749,6 @@ public class TestSourceAndIncrementalGenerator : IIncrementalGenerator, ISourceG
public void AddDependencyLocation(string fullPath) { }
public bool IsHostAssembly(Assembly assembly) => false;
public Assembly LoadFromPath(string fullPath) => throw new Exception();
public string? GetOriginalDependencyLocation(AssemblyName assembly) => throw new Exception();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ internal interface IAnalyzerAssemblyLoaderInternal : IAnalyzerAssemblyLoader
/// process. Either part of the compiler itself or the process hosting the compiler.
/// </summary>
bool IsHostAssembly(Assembly assembly);

/// <summary>
/// For a given <see cref="AssemblyName"/> return the location it was originally added
/// from. This will return null for any value that was not directly added through the
/// loader.
/// </summary>
string? GetOriginalDependencyLocation(AssemblyName assembly);
}

/// <summary>
Expand Down Expand Up @@ -235,6 +242,9 @@ public Assembly LoadFromPath(string originalAnalyzerPath)
}
}

public string? GetOriginalDependencyLocation(AssemblyName assemblyName) =>
GetBestPath(assemblyName).BestOriginalPath;

/// <summary>
/// Return the best (original, real) path information for loading an assembly with the specified <see cref="AssemblyName"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ private static bool CheckCore(
var loadedAssemblyMvid = loadedAssembly.ManifestModule.ModuleVersionId;
if (resolvedPathMvid != loadedAssemblyMvid)
{
var message = $"analyzer assembly '{resolvedPath}' has MVID '{resolvedPathMvid}' but loaded assembly '{loadedAssembly.Location}' has MVID '{loadedAssemblyMvid}'";
var loadedAssemblyLocation = loader.GetOriginalDependencyLocation(loadedAssembly.GetName()) ?? loadedAssembly.Location;
var message = $"analyzer assembly '{resolvedPath}' has MVID '{resolvedPathMvid}' but loaded assembly '{loadedAssemblyLocation}' has MVID '{loadedAssemblyMvid}'";
errorMessages ??= new List<string>();
errorMessages.Add(message);
logger.LogError(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,16 @@ public void DifferingMvidsSameDirectory()
directory.Path,
ImmutableArray.Create(new CommandLineAnalyzerReference(mvidAlpha2.Path)),
assemblyLoader,
Logger);
Logger,
out List<string>? errorMessages);
Assert.False(result);
Assert.NotNull(errorMessages);

// Both the original and failed paths need to appear in the message, not the shadow copy
// paths
var errorMessage = errorMessages!.Single();
Assert.Contains(mvidAlpha1.Path, errorMessage);
Assert.Contains(mvidAlpha2.Path, errorMessage);
}

/// <summary>
Expand Down Expand Up @@ -242,5 +250,6 @@ public void LoadingSimpleLibrary()
public void AddDependencyLocation(string fullPath) { }
public bool IsHostAssembly(Assembly assembly) => false;
public Assembly LoadFromPath(string fullPath) => throw new Exception();
public string? GetOriginalDependencyLocation(AssemblyName assembly) => throw new Exception();
}
}