Skip to content

Commit 2819cf0

Browse files
authored
Revert "Make GenerateDepsFile and GenerateRuntimeConfigurationFiles tasks internally-incremental" (#49938)
2 parents 836489f + 60311d4 commit 2819cf0

File tree

5 files changed

+5
-256
lines changed

5 files changed

+5
-256
lines changed

src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAGenerateDepsFile.cs

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAGenerateRuntimeConfigurationFiles.cs

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using FluentAssertions;
5-
using Microsoft.Build.Utilities;
65
using Microsoft.NET.TestFramework;
76
using Xunit;
87

@@ -219,70 +218,6 @@ public void GivenTargetMonikerItGeneratesShortName()
219218
}}");
220219
}
221220

222-
[Fact]
223-
public void ItDoesNotOverwriteFileWithSameContent()
224-
{
225-
// Execute task first time
226-
var task = CreateBasicTestTask();
227-
task.PublicExecuteCore();
228-
var firstWriteTime = File.GetLastWriteTimeUtc(_runtimeConfigPath);
229-
230-
// Wait a bit to ensure timestamp would change if file is rewritten
231-
Thread.Sleep(100);
232-
233-
// Execute task again with same configuration
234-
var task2 = CreateBasicTestTask();
235-
task2.PublicExecuteCore();
236-
var secondWriteTime = File.GetLastWriteTimeUtc(_runtimeConfigPath);
237-
238-
// File should not have been rewritten when content is the same
239-
secondWriteTime.Should().Be(firstWriteTime, "file should not be rewritten when content is unchanged");
240-
}
241-
242-
[Fact]
243-
public void GivenDifferentRuntimeHostOptionsItWritesNewConfig()
244-
{
245-
// Execute task first time
246-
var task = CreateBasicTestTask();
247-
task.PublicExecuteCore();
248-
var firstWriteTime = File.GetLastWriteTimeUtc(_runtimeConfigPath);
249-
250-
// Wait a bit to ensure timestamp would change if file is rewritten
251-
Thread.Sleep(100);
252-
253-
// Execute task again with different host options
254-
var task2 = CreateBasicTestTask();
255-
task2.HostConfigurationOptions = [
256-
new TaskItem("System.Runtime.TieredCompilation", new Dictionary<string, string>{{"Value", "false"}}),
257-
new TaskItem("System.GC.Concurrent", new Dictionary<string, string>{{"Value", "false"}}),
258-
];
259-
task2.PublicExecuteCore();
260-
var secondWriteTime = File.GetLastWriteTimeUtc(_runtimeConfigPath);
261-
// File should have been rewritten when content is different
262-
secondWriteTime.Should().BeAfter(firstWriteTime, "file should be rewritten when content is different");
263-
}
264-
265-
private TestableGenerateRuntimeConfigurationFiles CreateBasicTestTask()
266-
{
267-
return new TestableGenerateRuntimeConfigurationFiles
268-
{
269-
BuildEngine = new MockNeverCacheBuildEngine4(),
270-
TargetFrameworkMoniker = $".NETCoreApp,Version=v{ToolsetInfo.CurrentTargetFrameworkVersion}",
271-
RuntimeConfigPath = _runtimeConfigPath,
272-
RuntimeFrameworks = new[]
273-
{
274-
new MockTaskItem(
275-
"Microsoft.NETCore.App",
276-
new Dictionary<string, string>
277-
{
278-
{"FrameworkName", "Microsoft.NETCore.App"}, {"Version", $"{ToolsetInfo.CurrentTargetFrameworkVersion}.0"}
279-
}
280-
)
281-
},
282-
RollForward = "LatestMinor"
283-
};
284-
}
285-
286221
private class TestableGenerateRuntimeConfigurationFiles : GenerateRuntimeConfigurationFiles
287222
{
288223
public void PublicExecuteCore()

src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -254,54 +254,10 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item)
254254
DependencyContext dependencyContext = builder.Build(UserRuntimeAssemblies);
255255

256256
var writer = new DependencyContextWriter();
257-
258-
bool shouldWriteFile = true;
259-
var tempDepsFilePath = depsFilePath + ".tmp";
260-
261-
// Generate new content
262-
using (var fileStream = File.Create(tempDepsFilePath))
257+
using (var fileStream = File.Create(depsFilePath))
263258
{
264259
writer.Write(dependencyContext, fileStream);
265260
}
266-
267-
// If file exists, check if content is different using streaming hash comparison
268-
if (File.Exists(depsFilePath))
269-
{
270-
Log.LogMessage("File {0} already exists, checking hash.", depsFilePath);
271-
using var existingFileContentStream = File.OpenRead(depsFilePath);
272-
var existingContentHash = HashingUtils.ComputeXXHash64(existingFileContentStream);
273-
var existingContentHashRendered = BitConverter.ToString(existingContentHash).Replace("-", "");
274-
Log.LogMessage("Existing file hash: {0}", existingContentHashRendered);
275-
using var newContentStream = File.OpenRead(tempDepsFilePath);
276-
var newContentHash = HashingUtils.ComputeXXHash64(newContentStream);
277-
var newContentHashRendered = BitConverter.ToString(newContentHash).Replace("-", "");
278-
Log.LogMessage("New content hash: {0}", newContentHashRendered);
279-
// If hashes are equal, content is the same - don't write
280-
if (existingContentHash.SequenceEqual(newContentHash))
281-
{
282-
Log.LogMessage("File {0} is unchanged, skipping write.", depsFilePath);
283-
shouldWriteFile = false;
284-
}
285-
}
286-
287-
if (shouldWriteFile)
288-
{
289-
Log.LogMessage("Writing file {0}.", depsFilePath);
290-
#if NET
291-
File.Move(tempDepsFilePath, depsFilePath, overwrite: true);
292-
#else
293-
// For .NET Framework, we can't use File.Move because it doesn't overwrite the existing file
294-
// so we delete the existing file first.
295-
File.Delete(depsFilePath);
296-
File.Move(tempDepsFilePath, depsFilePath);
297-
#endif
298-
}
299-
else
300-
{
301-
// If we didn't write the file, delete the temporary file
302-
Log.LogMessage("Deleting temporary file {0}.", tempDepsFilePath);
303-
File.Delete(tempDepsFilePath);
304-
}
305261
_filesWritten.Add(new TaskItem(depsFilePath));
306262

307263
if (ValidRuntimeIdentifierPlatformsForAssets != null)

src/Tasks/Microsoft.NET.Build.Tasks/GenerateRuntimeConfigurationFiles.cs

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private void WriteRuntimeConfig(
176176
AddAdditionalProbingPaths(config.RuntimeOptions, packageFolders);
177177
}
178178

179-
WriteToJsonFile(Log, RuntimeConfigPath, config);
179+
WriteToJsonFile(RuntimeConfigPath, config);
180180
_filesWritten.Add(new TaskItem(RuntimeConfigPath));
181181
}
182182

@@ -340,7 +340,7 @@ private void WriteDevRuntimeConfig(IList<LockFileItem> packageFolders)
340340

341341
AddAdditionalProbingPaths(devConfig.RuntimeOptions, packageFolders);
342342

343-
WriteToJsonFile(Log, RuntimeConfigDevPath, devConfig);
343+
WriteToJsonFile(RuntimeConfigDevPath, devConfig);
344344
_filesWritten.Add(new TaskItem(RuntimeConfigDevPath));
345345
}
346346

@@ -381,7 +381,7 @@ private static string EnsureNoTrailingDirectorySeparator(string path)
381381
return path;
382382
}
383383

384-
private static void WriteToJsonFile(Logger log, string filePath, object value)
384+
private static void WriteToJsonFile(string fileName, object value)
385385
{
386386
JsonSerializer serializer = new()
387387
{
@@ -390,55 +390,10 @@ private static void WriteToJsonFile(Logger log, string filePath, object value)
390390
DefaultValueHandling = DefaultValueHandling.Ignore
391391
};
392392

393-
bool shouldWriteFile = true;
394-
var tempFilePath = filePath + ".tmp";
395-
396-
// Generate new content
397-
using (JsonTextWriter writer = new(new StreamWriter(File.Create(tempFilePath))))
393+
using (JsonTextWriter writer = new(new StreamWriter(File.Create(fileName))))
398394
{
399395
serializer.Serialize(writer, value);
400396
}
401-
402-
// If file exists, check if content is different using streaming hash comparison
403-
if (File.Exists(filePath))
404-
{
405-
log.LogMessage("File {0} already exists, checking hash.", filePath);
406-
// stream positions are reset as part of these utility calls
407-
using var existingContentStream = File.OpenRead(filePath);
408-
var existingContentHash = HashingUtils.ComputeXXHash64(existingContentStream);
409-
var existingContentHashRendered = BitConverter.ToString(existingContentHash).Replace("-", "");
410-
log.LogMessage("Existing file hash: {0}", existingContentHashRendered);
411-
using var newContentStream = File.OpenRead(tempFilePath);
412-
var newContentHash = HashingUtils.ComputeXXHash64(newContentStream);
413-
var newContentHashRendered = BitConverter.ToString(newContentHash).Replace("-", "");
414-
log.LogMessage("New content hash: {0}", existingContentHashRendered);
415-
// If hashes are equal, content is the same - don't write
416-
if (existingContentHash.SequenceEqual(newContentHash))
417-
{
418-
log.LogMessage("File {0} is unchanged, skipping write.", filePath);
419-
shouldWriteFile = false;
420-
}
421-
422-
}
423-
424-
if (shouldWriteFile)
425-
{
426-
log.LogMessage("Writing file {0}.", filePath);
427-
#if NET
428-
File.Move(tempFilePath, filePath, overwrite: true);
429-
#else
430-
// For .NET Framework, we can't use File.Move because it doesn't overwrite the existing file
431-
// so we delete the existing file first.
432-
File.Delete(filePath);
433-
File.Move(tempFilePath, filePath);
434-
#endif
435-
}
436-
else
437-
{
438-
// If we didn't write the file, delete the temporary file
439-
log.LogMessage("Deleting temporary file {0}.", tempFilePath);
440-
File.Delete(tempFilePath);
441-
}
442397
}
443398
}
444399
}

src/Tasks/Microsoft.NET.Build.Tasks/HashingUtils.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)