@@ -61,7 +61,7 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
6161 try
6262 {
6363 this . dotnet = DotNet . Make ( options , logger , tempWorkingDirectory ) ;
64- runtimeLazy = new Lazy < Runtime > ( ( ) => new Runtime ( dotnet ) ) ;
64+ runtimeLazy = new Lazy < Runtime > ( ( ) => new Runtime ( dotnet , logger ) ) ;
6565 }
6666 catch
6767 {
@@ -303,7 +303,7 @@ private void AddNetFrameworkDlls(ISet<string> dllPaths, ISet<string> frameworkLo
303303 var packagesInPrioOrder = FrameworkPackageNames . NetFrameworks ;
304304
305305 var frameworkPaths = packagesInPrioOrder
306- . Select ( ( s , index ) => ( Index : index , Path : GetPackageDirectory ( s ) ) )
306+ . Select ( ( s , index ) => ( Index : index , Path : GetPackageDirectory ( s , packageDirectory ) ) )
307307 . Where ( pair => pair . Path is not null )
308308 . ToArray ( ) ;
309309
@@ -335,6 +335,16 @@ private void AddNetFrameworkDlls(ISet<string> dllPaths, ISet<string> frameworkLo
335335 else if ( fileContent . IsLegacyProjectStructureUsed )
336336 {
337337 runtimeLocation = Runtime . DesktopRuntime ;
338+
339+ if ( runtimeLocation is null )
340+ {
341+ logger . LogInfo ( "No .NET Desktop Runtime location found. Attempting to restore the .NET Framework reference assemblies manually." ) ;
342+
343+ if ( TryRestorePackageManually ( FrameworkPackageNames . LatestNetFrameworkReferenceAssemblies , null ) )
344+ {
345+ runtimeLocation = GetPackageDirectory ( FrameworkPackageNames . LatestNetFrameworkReferenceAssemblies , missingPackageDirectory ) ;
346+ }
347+ }
338348 }
339349
340350 runtimeLocation ??= Runtime . ExecutingRuntime ;
@@ -374,7 +384,7 @@ private void AddAspNetCoreFrameworkDlls(ISet<string> dllPaths, ISet<string> fram
374384 }
375385
376386 // First try to find ASP.NET Core assemblies in the NuGet packages
377- if ( GetPackageDirectory ( FrameworkPackageNames . AspNetCoreFramework ) is string aspNetCorePackage )
387+ if ( GetPackageDirectory ( FrameworkPackageNames . AspNetCoreFramework , packageDirectory ) is string aspNetCorePackage )
378388 {
379389 SelectNewestFrameworkPath ( aspNetCorePackage , "ASP.NET Core" , dllPaths , frameworkLocations ) ;
380390 return ;
@@ -390,15 +400,15 @@ private void AddAspNetCoreFrameworkDlls(ISet<string> dllPaths, ISet<string> fram
390400
391401 private void AddMicrosoftWindowsDesktopDlls ( ISet < string > dllPaths , ISet < string > frameworkLocations )
392402 {
393- if ( GetPackageDirectory ( FrameworkPackageNames . WindowsDesktopFramework ) is string windowsDesktopApp )
403+ if ( GetPackageDirectory ( FrameworkPackageNames . WindowsDesktopFramework , packageDirectory ) is string windowsDesktopApp )
394404 {
395405 SelectNewestFrameworkPath ( windowsDesktopApp , "Windows Desktop App" , dllPaths , frameworkLocations ) ;
396406 }
397407 }
398408
399- private string ? GetPackageDirectory ( string packagePrefix )
409+ private string ? GetPackageDirectory ( string packagePrefix , TemporaryDirectory root )
400410 {
401- return new DirectoryInfo ( packageDirectory . DirInfo . FullName )
411+ return new DirectoryInfo ( root . DirInfo . FullName )
402412 . EnumerateDirectories ( packagePrefix + "*" , new EnumerationOptions { MatchCasing = MatchCasing . CaseInsensitive , RecurseSubdirectories = false } )
403413 . FirstOrDefault ( ) ?
404414 . FullName ;
@@ -434,19 +444,19 @@ private void GenerateSourceFileFromImplicitUsings()
434444 }
435445
436446 // Hardcoded values from https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview#implicit-using-directives
437- usings . UnionWith ( new [ ] { "System" , "System.Collections.Generic" , "System.IO" , "System.Linq" , "System.Net.Http" , "System.Threading" ,
438- "System.Threading.Tasks" } ) ;
447+ usings . UnionWith ( [ "System" , "System.Collections.Generic" , "System.IO" , "System.Linq" , "System.Net.Http" , "System.Threading" ,
448+ "System.Threading.Tasks" ] ) ;
439449
440450 if ( fileContent . UseAspNetCoreDlls )
441451 {
442- usings . UnionWith ( new [ ] { "System.Net.Http.Json" , "Microsoft.AspNetCore.Builder" , "Microsoft.AspNetCore.Hosting" ,
452+ usings . UnionWith ( [ "System.Net.Http.Json" , "Microsoft.AspNetCore.Builder" , "Microsoft.AspNetCore.Hosting" ,
443453 "Microsoft.AspNetCore.Http" , "Microsoft.AspNetCore.Routing" , "Microsoft.Extensions.Configuration" ,
444- "Microsoft.Extensions.DependencyInjection" , "Microsoft.Extensions.Hosting" , "Microsoft.Extensions.Logging" } ) ;
454+ "Microsoft.Extensions.DependencyInjection" , "Microsoft.Extensions.Hosting" , "Microsoft.Extensions.Logging" ] ) ;
445455 }
446456
447457 if ( fileContent . UseWindowsForms )
448458 {
449- usings . UnionWith ( new [ ] { "System.Drawing" , "System.Windows.Forms" } ) ;
459+ usings . UnionWith ( [ "System.Drawing" , "System.Windows.Forms" ] ) ;
450460 }
451461
452462 usings . UnionWith ( fileContent . CustomImplicitUsings ) ;
@@ -828,47 +838,58 @@ private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<string> dllPa
828838
829839 Parallel . ForEach ( notYetDownloadedPackages , new ParallelOptions { MaxDegreeOfParallelism = options . Threads } , package =>
830840 {
831- logger . LogInfo ( $ "Restoring package { package } ...") ;
832- using var tempDir = new TemporaryDirectory ( ComputeTempDirectory ( package , "missingpackages_workingdir" ) ) ;
833- var success = dotnet . New ( tempDir . DirInfo . FullName ) ;
841+ var success = TryRestorePackageManually ( package , nugetConfig ) ;
834842 if ( ! success )
835843 {
836844 return ;
837845 }
838846
839- success = dotnet . AddPackage ( tempDir . DirInfo . FullName , package ) ;
840- if ( ! success )
847+ lock ( sync )
841848 {
842- return ;
849+ successCount ++ ;
843850 }
851+ } ) ;
844852
845- var res = dotnet . Restore ( new ( tempDir . DirInfo . FullName , missingPackageDirectory . DirInfo . FullName , ForceDotnetRefAssemblyFetching : false , PathToNugetConfig : nugetConfig ) ) ;
846- if ( ! res . Success )
847- {
848- if ( res . HasNugetPackageSourceError )
849- {
850- // Restore could not be completed because the listed source is unavailable. Try without the nuget.config:
851- res = dotnet . Restore ( new ( tempDir . DirInfo . FullName , missingPackageDirectory . DirInfo . FullName , ForceDotnetRefAssemblyFetching : false , PathToNugetConfig : null , ForceReevaluation : true ) ) ;
852- }
853+ CompilationInfos . Add ( ( "Successfully ran fallback nuget restore" , successCount . ToString ( ) ) ) ;
853854
854- // TODO: the restore might fail, we could retry with a prerelease (*-* instead of *) version of the package.
855+ dllPaths . Add ( missingPackageDirectory . DirInfo . FullName ) ;
856+ }
855857
856- if ( ! res . Success )
857- {
858- logger . LogInfo ( $ "Failed to restore nuget package { package } ") ;
859- return ;
860- }
861- }
858+ private bool TryRestorePackageManually ( string package , string ? nugetConfig )
859+ {
860+ logger . LogInfo ( $ "Restoring package { package } ...") ;
861+ using var tempDir = new TemporaryDirectory ( ComputeTempDirectory ( package , "missingpackages_workingdir" ) ) ;
862+ var success = dotnet . New ( tempDir . DirInfo . FullName ) ;
863+ if ( ! success )
864+ {
865+ return false ;
866+ }
862867
863- lock ( sync )
868+ success = dotnet . AddPackage ( tempDir . DirInfo . FullName , package ) ;
869+ if ( ! success )
870+ {
871+ return false ;
872+ }
873+
874+ var res = dotnet . Restore ( new ( tempDir . DirInfo . FullName , missingPackageDirectory . DirInfo . FullName , ForceDotnetRefAssemblyFetching : false , PathToNugetConfig : nugetConfig ) ) ;
875+ if ( ! res . Success )
876+ {
877+ if ( res . HasNugetPackageSourceError && nugetConfig is not null )
864878 {
865- successCount ++ ;
879+ // Restore could not be completed because the listed source is unavailable. Try without the nuget.config:
880+ res = dotnet . Restore ( new ( tempDir . DirInfo . FullName , missingPackageDirectory . DirInfo . FullName , ForceDotnetRefAssemblyFetching : false , PathToNugetConfig : null , ForceReevaluation : true ) ) ;
866881 }
867- } ) ;
868882
869- CompilationInfos . Add ( ( "Successfully ran fallback nuget restore" , successCount . ToString ( ) ) ) ;
883+ // TODO: the restore might fail, we could retry with a prerelease (*-* instead of *) version of the package.
870884
871- dllPaths . Add ( missingPackageDirectory . DirInfo . FullName ) ;
885+ if ( ! res . Success )
886+ {
887+ logger . LogInfo ( $ "Failed to restore nuget package { package } ") ;
888+ return false ;
889+ }
890+ }
891+
892+ return true ;
872893 }
873894
874895 public void Dispose ( TemporaryDirectory ? dir , string name )
0 commit comments