Skip to content

Commit

Permalink
Improve analyzer consistency check message (#73329)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar authored May 6, 2024
1 parent 062ad3d commit e674c91
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
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();
}
}

0 comments on commit e674c91

Please sign in to comment.