Skip to content
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
12 changes: 12 additions & 0 deletions src/Shared/ReferenceItemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ public static string ToFileLine(this ReferenceItem item)

public static void SaveToFile(this List<ReferenceItem> items, string outputFile)
{
if (File.Exists(outputFile))
{
var currentContents = File.ReadAllLines(outputFile);
var newContents = items.Select(item => item.ToFileLine()).ToArray();
// Assuming order is the same for incremental builds, no sorting needed
if (currentContents.SequenceEqual(newContents))
{
// No changes, skip writing to avoid updating the timestamp, which will trigger unnecessary rebuilds
return;
}
}

File.WriteAllLines(outputFile, items.Select(item => item.ToFileLine()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ internal async Task Build(string additionalArgs = "")
{
string buildArgs =
$"build dirs.proj " +
$"-m:1 -t:Rebuild -restore -nologo -nodeReuse:false -noAutoResponse " +
$"-m:1 -restore -nologo -nodeReuse:false -noAutoResponse " +
$"/p:Configuration=Debug " +
$"/p:ReferenceProtectorTaskAssembly={Path.Combine(Directory.GetCurrentDirectory(), "ReferenceProtector.Tasks.dll")} " +
$"/v:m" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,28 @@ public async Task CollectAllReferences_PackageReferences_AreIncluded()

Assert.Equal(expectedReferences, references);
}

/// <summary>
/// Verifies that the CollectAllReferences task does not update the reference file timestamp if no changes were made.
/// </summary>
[Fact]
public async Task CollectAllReferences_IncrememtalBuild_ReferenceTimestampIsNotUpdated()
{
CreateProject("A");
CreateProject("B");
await AddProjectReference("A", "B");
await Build();

var generatedFiles1 = GetGeneratedReferencesFiles();
var timeStamps1 = generatedFiles1.Select(File.GetLastWriteTimeUtc).ToList();

// Rebuild without any changes
await Build();

var generatedFiles2 = GetGeneratedReferencesFiles();
var timeStamps2 = generatedFiles2.Select(File.GetLastWriteTimeUtc).ToList();

Assert.Equal(generatedFiles1, generatedFiles2);
Assert.Equal(timeStamps1, timeStamps2);
}
}
4 changes: 3 additions & 1 deletion src/Tasks/ReferenceProtector.Tasks/CollectAllReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public override bool Execute()

if (OutputFile is not null)
{
references.SaveToFile(OutputFile);
// Is sorted to ensure consistent ordering for easier testing and to avoid unnecessary diffs
// May not needed, if msbuild guarantees a consistent order
references.OrderBy(x => (x.Source, x.Target, x.LinkType)).ToList().SaveToFile(OutputFile);
return true;
}

Expand Down
Loading