Skip to content

Commit

Permalink
Better detect equivalence in our strong name provider (#74151)
Browse files Browse the repository at this point in the history
* Better detect equivalence in our strong name provider

Addresses #74058

LiveUnitTest team reported a fairly recent regression where Roslyn started reporting Project changed workspace events during file addition / removal. This causes the LUT window to unnecessarilly rerun tests.

This regressed as part of a change I made back in March around passing in a temp path to the compiler's strong name provider, as otherwise an EmitStream call fails, breaking intellisense in projects using assembly signing. (Change here: #72587)

The regression is due to me changing several callers to creating a strong name provider based on the path, instead of them all using the default instance. With the previous behavior, equality checks on these newly created StrongNameFileSystem and DesktopStrongNameProvider instances would return false, even though the temp path in the file systems were the same.

It's a bit of a windy path to how this relates to the LUT window. The Roslyn ProjectSystemProject.ChangeProjectProperty call is the crux of where the issue was being exposed. UpdateProjectOptions_NoLock was calling it with a new DesktopStrongNameProvider created with the temp path as outlined above. ChangeProjectProperty checks the old value of the strong name provider against the newValue, and if they are the same, it doesn't add anything to the batched events it will later send out. However, in this case, the equality check failed, so we started sending out a project changed notification, breaking the LUT behavior.

The fix here is to simply implement Equals in StrongNameFileSystem, and have DesktopStrongNameProvider use that instead of just checking for reference equality.
  • Loading branch information
ToddGrun authored Jun 28, 2024
1 parent 31e6ba8 commit a1d7eb9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,56 @@ public void RespectNullTempPath()
Assert.Null(provider.FileSystem.GetSigningTempPath());
}

[Fact]
public void EqualityUsingPath()
{
var tempDir = Temp.CreateDirectory();
var provider1 = new DesktopStrongNameProvider(tempPath: tempDir.Path);
var provider2 = new DesktopStrongNameProvider(tempPath: tempDir.Path);

Assert.Equal(provider1, provider2);
}

[Fact]
public void EqualityUsingKeyFileSearchPaths()
{
var tempDir = Temp.CreateDirectory();
var provider1 = new DesktopStrongNameProvider(keyFileSearchPaths: [tempDir.Path]);
var provider2 = new DesktopStrongNameProvider(keyFileSearchPaths: [tempDir.Path]);

Assert.Equal(provider1, provider2);
}

[Fact]
public void InequalityUsingPath()
{
var tempDir = Temp.CreateDirectory();
var provider1 = new DesktopStrongNameProvider(tempPath: tempDir.Path);
var provider2 = new DesktopStrongNameProvider(tempPath: tempDir.Path + "2");

Assert.NotEqual(provider1, provider2);
}

[Fact]
public void InequalityUsingKeyFileSearchPaths()
{
var tempDir = Temp.CreateDirectory();
var provider1 = new DesktopStrongNameProvider(keyFileSearchPaths: [tempDir.Path]);
var provider2 = new DesktopStrongNameProvider(keyFileSearchPaths: [tempDir.Path + "2"]);

Assert.NotEqual(provider1, provider2);
}

[Fact]
public void InequalityUsingKeyFileSearchPathsDueToCaseSensitivity()
{
var tempDir = Temp.CreateDirectory();
var provider1 = new DesktopStrongNameProvider(keyFileSearchPaths: [tempDir.Path]);
var provider2 = new DesktopStrongNameProvider(keyFileSearchPaths: [tempDir.Path.ToUpper()]);

Assert.NotEqual(provider1, provider2);
}

[Fact]
public void EmitWithCustomTempPath()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public override bool Equals(object? obj)
}

var other = (DesktopStrongNameProvider)obj;
if (FileSystem != other.FileSystem)
if (!FileSystem.Equals(other.FileSystem))
{
return false;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Compilers/Core/Portable/StrongName/StrongNameFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,19 @@ internal virtual bool FileExists(string? fullPath)
}

internal string? GetSigningTempPath() => _signingTempPath;

public override int GetHashCode()
=> _signingTempPath != null ? StringComparer.Ordinal.GetHashCode(_signingTempPath) : 0;

public override bool Equals(object? obj)
=> Equals(obj as StrongNameFileSystem);

private bool Equals(StrongNameFileSystem? other)
{
if (this == other)
return true;

return this.GetType() == other?.GetType() && StringComparer.Ordinal.Equals(_signingTempPath, other?._signingTempPath);
}
}
}

0 comments on commit a1d7eb9

Please sign in to comment.