33using System . IO ;
44using System . Linq ;
55using System . Text . RegularExpressions ;
6+ using Semmle . Util ;
67
78namespace Semmle . Extraction . CSharp . DependencyFetching
89{
@@ -13,19 +14,21 @@ internal partial class DotNet : IDotNet
1314 {
1415 private readonly IDotNetCliInvoker dotnetCliInvoker ;
1516 private readonly ProgressMonitor progressMonitor ;
17+ private readonly TemporaryDirectory ? tempWorkingDirectory ;
1618
17- private DotNet ( IDotNetCliInvoker dotnetCliInvoker , ProgressMonitor progressMonitor )
19+ private DotNet ( IDotNetCliInvoker dotnetCliInvoker , ProgressMonitor progressMonitor , TemporaryDirectory ? tempWorkingDirectory = null )
1820 {
1921 this . progressMonitor = progressMonitor ;
22+ this . tempWorkingDirectory = tempWorkingDirectory ;
2023 this . dotnetCliInvoker = dotnetCliInvoker ;
2124 Info ( ) ;
2225 }
2326
24- private DotNet ( IDependencyOptions options , ProgressMonitor progressMonitor ) : this ( new DotNetCliInvoker ( progressMonitor , Path . Combine ( options . DotNetPath ?? string . Empty , "dotnet" ) ) , progressMonitor ) { }
27+ private DotNet ( IDependencyOptions options , ProgressMonitor progressMonitor , TemporaryDirectory tempWorkingDirectory ) : this ( new DotNetCliInvoker ( progressMonitor , Path . Combine ( options . DotNetPath ?? string . Empty , "dotnet" ) ) , progressMonitor , tempWorkingDirectory ) { }
2528
2629 internal static IDotNet Make ( IDotNetCliInvoker dotnetCliInvoker , ProgressMonitor progressMonitor ) => new DotNet ( dotnetCliInvoker , progressMonitor ) ;
2730
28- public static IDotNet Make ( IDependencyOptions options , ProgressMonitor progressMonitor ) => new DotNet ( options , progressMonitor ) ;
31+ public static IDotNet Make ( IDependencyOptions options , ProgressMonitor progressMonitor , TemporaryDirectory tempWorkingDirectory ) => new DotNet ( options , progressMonitor , tempWorkingDirectory ) ;
2932
3033 private void Info ( )
3134 {
@@ -37,12 +40,29 @@ private void Info()
3740 }
3841 }
3942
40- private static string GetRestoreArgs ( string projectOrSolutionFile , string packageDirectory ) =>
41- $ "restore --no-dependencies \" { projectOrSolutionFile } \" --packages \" { packageDirectory } \" /p:DisableImplicitNuGetFallbackFolder=true";
43+ private string GetRestoreArgs ( string projectOrSolutionFile , string packageDirectory , bool forceDotnetRefAssemblyFetching )
44+ {
45+ var args = $ "restore --no-dependencies \" { projectOrSolutionFile } \" --packages \" { packageDirectory } \" /p:DisableImplicitNuGetFallbackFolder=true";
46+
47+ if ( forceDotnetRefAssemblyFetching )
48+ {
49+ // Ugly hack: we set the TargetFrameworkRootPath and NetCoreTargetingPackRoot properties to an empty folder:
50+ var path = ".empty" ;
51+ if ( tempWorkingDirectory != null )
52+ {
53+ path = Path . Combine ( tempWorkingDirectory . ToString ( ) , "emptyFakeDotnetRoot" ) ;
54+ Directory . CreateDirectory ( path ) ;
55+ }
56+
57+ args += $ " /p:TargetFrameworkRootPath=\" { path } \" /p:NetCoreTargetingPackRoot=\" { path } \" ";
58+ }
59+
60+ return args ;
61+ }
4262
43- public bool RestoreProjectToDirectory ( string projectFile , string packageDirectory , string ? pathToNugetConfig = null )
63+ public bool RestoreProjectToDirectory ( string projectFile , string packageDirectory , bool forceDotnetRefAssemblyFetching , string ? pathToNugetConfig = null )
4464 {
45- var args = GetRestoreArgs ( projectFile , packageDirectory ) ;
65+ var args = GetRestoreArgs ( projectFile , packageDirectory , forceDotnetRefAssemblyFetching ) ;
4666 if ( pathToNugetConfig != null )
4767 {
4868 args += $ " --configfile \" { pathToNugetConfig } \" ";
@@ -51,9 +71,9 @@ public bool RestoreProjectToDirectory(string projectFile, string packageDirector
5171 return dotnetCliInvoker . RunCommand ( args ) ;
5272 }
5373
54- public bool RestoreSolutionToDirectory ( string solutionFile , string packageDirectory , out IEnumerable < string > projects )
74+ public bool RestoreSolutionToDirectory ( string solutionFile , string packageDirectory , bool forceDotnetRefAssemblyFetching , out IEnumerable < string > projects )
5575 {
56- var args = GetRestoreArgs ( solutionFile , packageDirectory ) ;
76+ var args = GetRestoreArgs ( solutionFile , packageDirectory , forceDotnetRefAssemblyFetching ) ;
5777 args += " --verbosity normal" ;
5878 if ( dotnetCliInvoker . RunCommand ( args , out var output ) )
5979 {
0 commit comments