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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Microsoft.ComponentDetection.Detectors.DotNet;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
Expand Down Expand Up @@ -56,39 +57,42 @@ public DotNetComponentDetector(

public override IEnumerable<string> Categories => ["DotNet"];

[return: NotNullIfNotNull(nameof(path))]
private string? NormalizeDirectory(string? path) => string.IsNullOrEmpty(path) ? path : Path.TrimEndingDirectorySeparator(this.pathUtilityService.NormalizePath(path));

/// <summary>
/// Given a path relative to sourceDirectory, and the same path in another filesystem,
/// determine what path could be replaced with root.
/// Given a path under sourceDirectory, and the same path in another filesystem,
/// determine what path could be replaced with sourceDirectory.
/// </summary>
/// <param name="rootBasedPath">Some path under root, including the root path.</param>
/// <param name="rebasePath">Path to the same file as <paramref name="rootBasedPath"/> but in a different root.</param>
/// <param name="sourceDirectoryBasedPath">Some directory path under sourceDirectory, including sourceDirectory.</param>
/// <param name="rebasePath">Path to the same directory as <paramref name="sourceDirectoryBasedPath"/> but in a different root.</param>
/// <returns>Portion of <paramref name="rebasePath"/> that corresponds to root, or null if it can not be rebased.</returns>
private string? GetRootRebasePath(string rootBasedPath, string? rebasePath)
private string? GetRootRebasePath(string sourceDirectoryBasedPath, string? rebasePath)
{
if (string.IsNullOrEmpty(rebasePath) || string.IsNullOrEmpty(this.sourceDirectory) || string.IsNullOrEmpty(rootBasedPath))
if (string.IsNullOrEmpty(rebasePath) || string.IsNullOrEmpty(this.sourceDirectory) || string.IsNullOrEmpty(sourceDirectoryBasedPath))
{
return null;
}

// sourceDirectory is normalized, normalize others
rootBasedPath = this.pathUtilityService.NormalizePath(rootBasedPath);
rebasePath = this.pathUtilityService.NormalizePath(rebasePath);
sourceDirectoryBasedPath = this.NormalizeDirectory(sourceDirectoryBasedPath);
rebasePath = this.NormalizeDirectory(rebasePath);

// nothing to do if the paths are the same
if (rebasePath.Equals(rootBasedPath, StringComparison.Ordinal))
if (rebasePath.Equals(sourceDirectoryBasedPath, StringComparison.Ordinal))
{
return null;
}

// find the relative path under root.
var rootRelativePath = this.pathUtilityService.NormalizePath(Path.GetRelativePath(this.sourceDirectory!, rootBasedPath));
// find the relative path under sourceDirectory.
var sourceDirectoryRelativePath = this.NormalizeDirectory(Path.GetRelativePath(this.sourceDirectory!, sourceDirectoryBasedPath));

this.Logger.LogDebug("Attempting to rebase {RebasePath} to {SourceDirectoryBasedPath} using relative {SourceDirectoryRelativePath}", rebasePath, sourceDirectoryBasedPath, sourceDirectoryRelativePath);

// if the rebase path has the same relative portion, then we have a replacement.
if (rebasePath.EndsWith(rootRelativePath))
if (rebasePath.EndsWith(sourceDirectoryRelativePath))
{
return rebasePath[..^rootRelativePath.Length];
return rebasePath[..^sourceDirectoryRelativePath.Length];
}

// The path didn't have a common relative path, it might have been copied from a completely different location since it was built.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ private static string ProjectAssets(string projectName, string outputPath, strin
LockFile lockFile = new();
using var textWriter = new StringWriter();

// assets file always includes a trailing separator
if (!Path.EndsInDirectorySeparator(outputPath))
{
outputPath += Path.DirectorySeparatorChar;
}

lockFile.Targets = targetFrameworks.Select(tfm => new LockFileTarget() { TargetFramework = NuGetFramework.Parse(tfm) }).ToList();
lockFile.PackageSpec = new()
{
Expand Down
Loading