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
48 changes: 48 additions & 0 deletions src/Common/Internal/AbsolutePathExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;

namespace Microsoft.Build.Framework;

/// <summary>
/// Polyfill for <c>AbsolutePath.GetCanonicalForm()</c>, which is internal in the MSBuild packages
/// this repository currently consumes.
/// </summary>
/// <remarks>
/// <para>
/// <c>TaskEnvironment.GetAbsolutePath</c> absolutizes but deliberately does not canonicalize, so
/// <c>"foo/../bar"</c> stays <c>"&lt;project&gt;\foo/../bar"</c>. Code that previously called
/// <see cref="Path.GetFullPath(string)"/> got both, which matters wherever a path is compared,
/// deduplicated, or written to an output property, item metadata, or a generated file.
/// </para>
/// <para>
/// Only call this on paths that are already fully qualified, which is what
/// <c>TaskEnvironment.GetAbsolutePath</c> returns. <see cref="Path.GetFullPath(string)"/> would
/// otherwise consult per-drive process state for Windows rooted-but-not-fully-qualified inputs
/// such as <c>"C:foo"</c>, which is the process state this migration exists to remove.
/// </para>
/// <para>
/// TODO: delete this file and its &lt;Compile&gt; links once this repository consumes an MSBuild
/// that exposes the API - see https://github.com/dotnet/arcade/issues/17506. The swap is source
/// compatible because the real instance method takes precedence over this extension method.
/// </para>
/// </remarks>
internal static class AbsolutePathExtensions
{
/// <summary>
/// Returns the canonical form of an already fully qualified path, resolving <c>.</c> and
/// <c>..</c> segments and normalizing directory separators.
/// </summary>
/// <remarks>
/// Unlike the MSBuild implementation this does not preserve <c>OriginalValue</c>, because the
/// constructor overload that carries it is internal.
/// </remarks>
internal static AbsolutePath GetCanonicalForm(this AbsolutePath path) =>
// MSBuildTask0005 flags Path.GetFullPath transitively at every calling task. It is safe
// here because the input is already fully qualified, so no process state is consulted.
// The suppression goes away with the file - see https://github.com/dotnet/arcade/issues/17506.
#pragma warning disable MSBuildTask0005
new(Path.GetFullPath(path.Value));
#pragma warning restore MSBuildTask0005
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void FromFile_ProducesSameCatalogAsInMemoryContent()
{
File.WriteAllBytes(path, s_registerJsContent);

byte[] fromFile = new CatalogBuilder().AddFile(path, "register.js").Build();
byte[] fromFile = new CatalogBuilder().AddFile(new FileInfo(path), "register.js").Build();
byte[] fromMemory = new CatalogBuilder().Add(new CatalogEntry("register.js", s_registerJsContent)).Build();

fromFile.Should().Equal(fromMemory);
Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.DotNet.Build.Tasks.FileCatalog/CatalogBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public CatalogBuilder Add(CatalogEntry entry)
}

/// <summary>Adds a file by path.</summary>
public CatalogBuilder AddFile(string filePath, string? name = null)
public CatalogBuilder AddFile(FileInfo filePath, string? name = null)
=> Add(CatalogEntry.FromFile(filePath, name));

/// <summary>Builds the DER-encoded .cat file bytes.</summary>
Expand All @@ -94,14 +94,14 @@ public byte[] Build()
}

/// <summary>Builds and writes the catalog to <paramref name="path"/>.</summary>
public void WriteTo(string path)
public void WriteTo(FileInfo path)
{
if (string.IsNullOrEmpty(path))
if (path is null)
{
throw new ArgumentException("Path must not be null or empty.", nameof(path));
throw new ArgumentNullException(nameof(path));
}

File.WriteAllBytes(path, Build());
File.WriteAllBytes(path.FullName, Build());
}

private List<Member> BuildSortedMembers()
Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.DotNet.Build.Tasks.FileCatalog/CatalogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ public sealed class CatalogEntry
/// of <paramref name="filePath"/>. The name is not embedded in the catalog (makecat
/// V2 catalogs do not carry per-member file names); it is retained only for diagnostics.
/// </param>
public static CatalogEntry FromFile(string filePath, string? name = null)
public static CatalogEntry FromFile(FileInfo filePath, string? name = null)
{
if (string.IsNullOrEmpty(filePath))
if (filePath is null)
{
throw new ArgumentException("File path must not be null or empty.", nameof(filePath));
throw new ArgumentNullException(nameof(filePath));
}

// Stream the file through both hash algorithms in a single pass so the full
// content is never buffered in memory (catalog generation only needs the hashes).
using IncrementalHash sha1 = IncrementalHash.CreateHash(HashAlgorithmName.SHA1);
using IncrementalHash sha256 = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
using (FileStream stream = File.OpenRead(filePath))
using (FileStream stream = File.OpenRead(filePath.FullName))
{
byte[] buffer = ArrayPool<byte>.Shared.Rent(81920);
try
Expand All @@ -53,7 +53,7 @@ public static CatalogEntry FromFile(string filePath, string? name = null)
}
}

return new CatalogEntry(name ?? Path.GetFileName(filePath), sha1.GetHashAndReset(), sha256.GetHashAndReset());
return new CatalogEntry(name ?? filePath.Name, sha1.GetHashAndReset(), sha256.GetHashAndReset());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ namespace Microsoft.DotNet.Build.Tasks.FileCatalog;
/// (no <c>makecat.exe</c> / Windows SDK required). The catalog is unsigned and ready to be
/// Authenticode-signed by the Arcade signing infrastructure (via <c>FileExtensionSignInfo</c>).
/// </summary>
public class GenerateFileCatalog : Task
[MSBuildMultiThreadableTask]
public class GenerateFileCatalog : Task, IMultiThreadableTask
{
/// <summary>Injected by MSBuild so paths resolve against the project directory in multithreaded builds.</summary>
public TaskEnvironment TaskEnvironment { get; set; } = TaskEnvironment.Fallback;

/// <summary>The files to include in the catalog.</summary>
[Required]
public ITaskItem[] Files { get; set; } = Array.Empty<ITaskItem>();
Expand All @@ -31,15 +35,17 @@ public override bool Execute()
return false;
}

AbsolutePath outputPath = TaskEnvironment.GetAbsolutePath(OutputPath);

if (Files is null || Files.Length == 0)
{
Log.LogWarning("No files were provided - skipping catalog generation for '{0}'.", OutputPath);

// Remove any stale catalog from a previous run so incremental builds don't
// package a catalog describing files that are no longer present.
if (File.Exists(OutputPath))
if (File.Exists(outputPath))
{
File.Delete(OutputPath);
File.Delete(outputPath);
}

return true;
Expand All @@ -56,22 +62,23 @@ public override bool Execute()
path = file.ItemSpec;
}

if (!File.Exists(path))
AbsolutePath filePath = TaskEnvironment.GetAbsolutePath(path);
if (!File.Exists(filePath))
{
Log.LogError("File not found: '{0}'.", path);
return false;
}

builder.AddFile(path);
builder.AddFile(new FileInfo(filePath));
}

string? directory = Path.GetDirectoryName(OutputPath);
string? directory = Path.GetDirectoryName(outputPath);
if (!string.IsNullOrEmpty(directory))
{
Directory.CreateDirectory(directory);
}

builder.WriteTo(OutputPath);
builder.WriteTo(new FileInfo(outputPath));
Log.LogMessage(MessageImportance.High, "Generated catalog with {0} file(s): {1}", Files.Length, OutputPath);
return !Log.HasLoggedErrors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ namespace Microsoft.DotNet.Build.Tasks.Templating;
/// </code>
/// </example>
/// </summary>
public class GenerateFileFromTemplate : Microsoft.Build.Utilities.Task
[MSBuildMultiThreadableTask]
public class GenerateFileFromTemplate : Task, IMultiThreadableTask
{
/// <summary>Injected by MSBuild so paths resolve against the project directory in multithreaded builds.</summary>
public TaskEnvironment TaskEnvironment { get; set; } = TaskEnvironment.Fallback;

/// <summary>
/// The template file using the variable syntax <c>${VarName}</c>.
/// If your template file needs to output this format, you can escape the dollar sign with a backtick e.g. <c>`${NotReplaced}</c>.
Expand Down Expand Up @@ -66,41 +70,46 @@ public class GenerateFileFromTemplate : Microsoft.Build.Utilities.Task

public override bool Execute()
{
ResolvedOutputPath = Path.GetFullPath(OutputPath.Replace('\\', '/'));

if (!File.Exists(TemplateFile))
// GetAbsolutePath deliberately does not canonicalize, but this output property was
// produced by Path.GetFullPath, which also resolved "." and ".." and turned the forward
// slashes substituted just above back into the platform separator.
AbsolutePath resolvedOutputPath = TaskEnvironment.GetAbsolutePath(OutputPath.Replace('\\', '/')).GetCanonicalForm();
ResolvedOutputPath = resolvedOutputPath;

AbsolutePath templateFile = TaskEnvironment.GetAbsolutePath(TemplateFile);
if (!File.Exists(templateFile))
{
Log.LogError($"File {TemplateFile} does not exist");
return false;
}

IDictionary<string, string> values = MSBuildListSplitter.GetNamedProperties(Properties, Log);
string template = File.ReadAllText(TemplateFile);
string template = File.ReadAllText(templateFile);

string result = Replace(template, values);

// File.WriteAllText writes UTF-8 without a byte-order mark, so compare against the same encoding
// to determine whether the on-disk bytes would actually change.
byte[] resultBytes = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false).GetBytes(result);

if (SkipUnchanged && FileContentsMatch(ResolvedOutputPath, resultBytes))
if (SkipUnchanged && FileContentsMatch(resolvedOutputPath, resultBytes))
{
Log.LogMessage(MessageImportance.Low, $"Skipping unchanged file {ResolvedOutputPath}");
return !Log.HasLoggedErrors;
}

string directory = Path.GetDirectoryName(ResolvedOutputPath);
string directory = Path.GetDirectoryName(resolvedOutputPath);
if (!string.IsNullOrEmpty(directory))
{
Directory.CreateDirectory(directory);
}

File.WriteAllBytes(ResolvedOutputPath, resultBytes);
File.WriteAllBytes(resolvedOutputPath, resultBytes);

return !Log.HasLoggedErrors;
}

private static bool FileContentsMatch(string path, byte[] expectedBytes)
private static bool FileContentsMatch(AbsolutePath path, byte[] expectedBytes)
{
var fileInfo = new FileInfo(path);
if (!fileInfo.Exists || fileInfo.Length != expectedBytes.Length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
<PackageReference Include="Microsoft.Build.Utilities.Core" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\Common\Internal\AbsolutePathExtensions.cs" Link="Internal\AbsolutePathExtensions.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<PackageReference Include="Microsoft.Build.Framework" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Common\Internal\AbsolutePathExtensions.cs" Link="Internal\AbsolutePathExtensions.cs" />
</ItemGroup>

</Project>
10 changes: 7 additions & 3 deletions src/Microsoft.DotNet.CMake.Sdk/src/CreateCMakeFileApiQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ namespace Microsoft.DotNet.CMake.Sdk;
/// <summary>
/// Creates a CMake File API query file to request codemodel information.
/// </summary>
public class CreateCMakeFileApiQuery : Task
[MSBuildMultiThreadableTask]
public class CreateCMakeFileApiQuery : Task, IMultiThreadableTask
{
/// <summary>Injected by MSBuild so paths resolve against the project directory in multithreaded builds.</summary>
public TaskEnvironment TaskEnvironment { get; set; } = TaskEnvironment.Fallback;

/// <summary>
/// The CMake build output directory where the query should be created.
/// </summary>
Expand All @@ -25,12 +29,12 @@ public override bool Execute()
{
// Create a client stateless query file with client name "Microsoft.DotNet.CMake.Sdk"
string queryDir = Path.Combine(CMakeOutputDir, ".cmake", "api", "v1", "query", "client-Microsoft.DotNet.CMake.Sdk");
Directory.CreateDirectory(queryDir);
Directory.CreateDirectory(TaskEnvironment.GetAbsolutePath(queryDir));

string queryFile = Path.Combine(queryDir, "codemodel-v2");

// Create an empty file to request codemodel-v2 information
File.WriteAllText(queryFile, string.Empty);
File.WriteAllText(TaskEnvironment.GetAbsolutePath(queryFile), string.Empty);

Log.LogMessage(MessageImportance.Low, "Created CMake File API query at: {0}", queryFile);

Expand Down
Loading
Loading