@@ -61,7 +61,7 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
61
61
try
62
62
{
63
63
this . dotnet = DotNet . Make ( options , logger , tempWorkingDirectory ) ;
64
- runtimeLazy = new Lazy < Runtime > ( ( ) => new Runtime ( dotnet ) ) ;
64
+ runtimeLazy = new Lazy < Runtime > ( ( ) => new Runtime ( dotnet , logger ) ) ;
65
65
}
66
66
catch
67
67
{
@@ -303,7 +303,7 @@ private void AddNetFrameworkDlls(ISet<string> dllPaths, ISet<string> frameworkLo
303
303
var packagesInPrioOrder = FrameworkPackageNames . NetFrameworks ;
304
304
305
305
var frameworkPaths = packagesInPrioOrder
306
- . Select ( ( s , index ) => ( Index : index , Path : GetPackageDirectory ( s ) ) )
306
+ . Select ( ( s , index ) => ( Index : index , Path : GetPackageDirectory ( s , packageDirectory ) ) )
307
307
. Where ( pair => pair . Path is not null )
308
308
. ToArray ( ) ;
309
309
@@ -335,6 +335,16 @@ private void AddNetFrameworkDlls(ISet<string> dllPaths, ISet<string> frameworkLo
335
335
else if ( fileContent . IsLegacyProjectStructureUsed )
336
336
{
337
337
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
+ }
338
348
}
339
349
340
350
runtimeLocation ??= Runtime . ExecutingRuntime ;
@@ -374,7 +384,7 @@ private void AddAspNetCoreFrameworkDlls(ISet<string> dllPaths, ISet<string> fram
374
384
}
375
385
376
386
// 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 )
378
388
{
379
389
SelectNewestFrameworkPath ( aspNetCorePackage , "ASP.NET Core" , dllPaths , frameworkLocations ) ;
380
390
return ;
@@ -390,15 +400,15 @@ private void AddAspNetCoreFrameworkDlls(ISet<string> dllPaths, ISet<string> fram
390
400
391
401
private void AddMicrosoftWindowsDesktopDlls ( ISet < string > dllPaths , ISet < string > frameworkLocations )
392
402
{
393
- if ( GetPackageDirectory ( FrameworkPackageNames . WindowsDesktopFramework ) is string windowsDesktopApp )
403
+ if ( GetPackageDirectory ( FrameworkPackageNames . WindowsDesktopFramework , packageDirectory ) is string windowsDesktopApp )
394
404
{
395
405
SelectNewestFrameworkPath ( windowsDesktopApp , "Windows Desktop App" , dllPaths , frameworkLocations ) ;
396
406
}
397
407
}
398
408
399
- private string ? GetPackageDirectory ( string packagePrefix )
409
+ private string ? GetPackageDirectory ( string packagePrefix , TemporaryDirectory root )
400
410
{
401
- return new DirectoryInfo ( packageDirectory . DirInfo . FullName )
411
+ return new DirectoryInfo ( root . DirInfo . FullName )
402
412
. EnumerateDirectories ( packagePrefix + "*" , new EnumerationOptions { MatchCasing = MatchCasing . CaseInsensitive , RecurseSubdirectories = false } )
403
413
. FirstOrDefault ( ) ?
404
414
. FullName ;
@@ -434,19 +444,19 @@ private void GenerateSourceFileFromImplicitUsings()
434
444
}
435
445
436
446
// 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" ] ) ;
439
449
440
450
if ( fileContent . UseAspNetCoreDlls )
441
451
{
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" ,
443
453
"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" ] ) ;
445
455
}
446
456
447
457
if ( fileContent . UseWindowsForms )
448
458
{
449
- usings . UnionWith ( new [ ] { "System.Drawing" , "System.Windows.Forms" } ) ;
459
+ usings . UnionWith ( [ "System.Drawing" , "System.Windows.Forms" ] ) ;
450
460
}
451
461
452
462
usings . UnionWith ( fileContent . CustomImplicitUsings ) ;
@@ -828,47 +838,58 @@ private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<string> dllPa
828
838
829
839
Parallel . ForEach ( notYetDownloadedPackages , new ParallelOptions { MaxDegreeOfParallelism = options . Threads } , package =>
830
840
{
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 ) ;
834
842
if ( ! success )
835
843
{
836
844
return ;
837
845
}
838
846
839
- success = dotnet . AddPackage ( tempDir . DirInfo . FullName , package ) ;
840
- if ( ! success )
847
+ lock ( sync )
841
848
{
842
- return ;
849
+ successCount ++ ;
843
850
}
851
+ } ) ;
844
852
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 ( ) ) ) ;
853
854
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
+ }
855
857
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
+ }
862
867
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 )
864
878
{
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 ) ) ;
866
881
}
867
- } ) ;
868
882
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.
870
884
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 ;
872
893
}
873
894
874
895
public void Dispose ( TemporaryDirectory ? dir , string name )
0 commit comments