Skip to content

Commit 3a7df99

Browse files
committed
C#: The dependency manager should find assets.json files when doing a project or solution restore.
1 parent 387a241 commit 3a7df99

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,11 @@ private void AnalyseProject(FileInfo project)
623623

624624
}
625625

626-
private bool RestoreProject(string project, bool forceDotnetRefAssemblyFetching, string? pathToNugetConfig = null) =>
627-
dotnet.RestoreProjectToDirectory(project, packageDirectory.DirInfo.FullName, forceDotnetRefAssemblyFetching, pathToNugetConfig);
626+
private bool RestoreProject(string project, bool forceDotnetRefAssemblyFetching, out IEnumerable<string> assets, string? pathToNugetConfig = null) =>
627+
dotnet.RestoreProjectToDirectory(project, packageDirectory.DirInfo.FullName, forceDotnetRefAssemblyFetching, out assets, pathToNugetConfig);
628628

629-
private bool RestoreSolution(string solution, out IEnumerable<string> projects) =>
630-
dotnet.RestoreSolutionToDirectory(solution, packageDirectory.DirInfo.FullName, forceDotnetRefAssemblyFetching: true, out projects);
629+
private bool RestoreSolution(string solution, out IEnumerable<string> projects, out IEnumerable<string> assets) =>
630+
dotnet.RestoreSolutionToDirectory(solution, packageDirectory.DirInfo.FullName, forceDotnetRefAssemblyFetching: true, out projects, out assets);
631631

632632
/// <summary>
633633
/// Executes `dotnet restore` on all solution files in solutions.
@@ -640,7 +640,7 @@ private bool RestoreSolution(string solution, out IEnumerable<string> projects)
640640
private IEnumerable<string> RestoreSolutions(IEnumerable<string> solutions) =>
641641
solutions.SelectMany(solution =>
642642
{
643-
RestoreSolution(solution, out var restoredProjects);
643+
RestoreSolution(solution, out var restoredProjects, out var assets);
644644
return restoredProjects;
645645
});
646646

@@ -653,7 +653,7 @@ private void RestoreProjects(IEnumerable<string> projects)
653653
{
654654
Parallel.ForEach(projects, new ParallelOptions { MaxDegreeOfParallelism = options.Threads }, project =>
655655
{
656-
RestoreProject(project, forceDotnetRefAssemblyFetching: true);
656+
RestoreProject(project, forceDotnetRefAssemblyFetching: true, out var assets);
657657
});
658658
}
659659

@@ -698,7 +698,7 @@ private void DownloadMissingPackages(List<FileInfo> allFiles)
698698
return;
699699
}
700700

701-
success = RestoreProject(tempDir.DirInfo.FullName, forceDotnetRefAssemblyFetching: false, pathToNugetConfig: nugetConfig);
701+
success = RestoreProject(tempDir.DirInfo.FullName, forceDotnetRefAssemblyFetching: false, out var _, pathToNugetConfig: nugetConfig);
702702
// TODO: the restore might fail, we could retry with a prerelease (*-* instead of *) version of the package.
703703
if (!success)
704704
{

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,38 @@ private string GetRestoreArgs(string projectOrSolutionFile, string packageDirect
6060
return args;
6161
}
6262

63-
public bool RestoreProjectToDirectory(string projectFile, string packageDirectory, bool forceDotnetRefAssemblyFetching, string? pathToNugetConfig = null)
63+
private static IEnumerable<string> GetFirstGroupOnMatch(Regex regex, IEnumerable<string> lines) =>
64+
lines
65+
.Select(line => regex.Match(line))
66+
.Where(match => match.Success)
67+
.Select(match => match.Groups[1].Value);
68+
69+
private static IEnumerable<string> GetAssetsFilePaths(IEnumerable<string> lines) =>
70+
GetFirstGroupOnMatch(AssetsFileRegex(), lines);
71+
72+
private static IEnumerable<string> GetRestoredProjects(IEnumerable<string> lines) =>
73+
GetFirstGroupOnMatch(RestoredProjectRegex(), lines);
74+
75+
public bool RestoreProjectToDirectory(string projectFile, string packageDirectory, bool forceDotnetRefAssemblyFetching, out IEnumerable<string> assets, string? pathToNugetConfig = null)
6476
{
6577
var args = GetRestoreArgs(projectFile, packageDirectory, forceDotnetRefAssemblyFetching);
6678
if (pathToNugetConfig != null)
6779
{
6880
args += $" --configfile \"{pathToNugetConfig}\"";
6981
}
7082

71-
return dotnetCliInvoker.RunCommand(args);
83+
var success = dotnetCliInvoker.RunCommand(args, out var output);
84+
assets = success ? GetAssetsFilePaths(output) : Array.Empty<string>();
85+
return success;
7286
}
7387

74-
public bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, bool forceDotnetRefAssemblyFetching, out IEnumerable<string> projects)
88+
public bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, bool forceDotnetRefAssemblyFetching, out IEnumerable<string> projects, out IEnumerable<string> assets)
7589
{
7690
var args = GetRestoreArgs(solutionFile, packageDirectory, forceDotnetRefAssemblyFetching);
77-
if (dotnetCliInvoker.RunCommand(args, out var output))
78-
{
79-
var regex = RestoreProjectRegex();
80-
projects = output
81-
.Select(line => regex.Match(line))
82-
.Where(match => match.Success)
83-
.Select(match => match.Groups[1].Value);
84-
return true;
85-
}
86-
87-
projects = Array.Empty<string>();
88-
return false;
91+
var success = dotnetCliInvoker.RunCommand(args, out var output);
92+
projects = success ? GetRestoredProjects(output) : Array.Empty<string>();
93+
assets = success ? GetAssetsFilePaths(output) : Array.Empty<string>();
94+
return success;
8995
}
9096

9197
public bool New(string folder)
@@ -120,6 +126,9 @@ public bool Exec(string execArgs)
120126
}
121127

122128
[GeneratedRegex("Restored\\s+(.+\\.csproj)", RegexOptions.Compiled)]
123-
private static partial Regex RestoreProjectRegex();
129+
private static partial Regex RestoredProjectRegex();
130+
131+
[GeneratedRegex("[Assets\\sfile\\shas\\snot\\schanged.\\sSkipping\\sassets\\sfile\\swriting.|Writing\\sassets\\sfile\\sto\\sdisk.]\\sPath:\\s(.*)", RegexOptions.Compiled)]
132+
private static partial Regex AssetsFileRegex();
124133
}
125134
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
44
{
55
internal interface IDotNet
66
{
7-
bool RestoreProjectToDirectory(string project, string directory, bool forceDotnetRefAssemblyFetching, string? pathToNugetConfig = null);
8-
bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, bool forceDotnetRefAssemblyFetching, out IEnumerable<string> projects);
7+
bool RestoreProjectToDirectory(string project, string directory, bool forceDotnetRefAssemblyFetching, out IEnumerable<string> assets, string? pathToNugetConfig = null);
8+
bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, bool forceDotnetRefAssemblyFetching, out IEnumerable<string> projects, out IEnumerable<string> assets);
99
bool New(string folder);
1010
bool AddPackage(string folder, string package);
1111
IList<string> GetListedRuntimes();

0 commit comments

Comments
 (0)