@@ -882,14 +882,14 @@ private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<string> dllPa
882882 var alreadyDownloadedPackages = GetRestoredPackageDirectoryNames ( packageDirectory . DirInfo ) ;
883883 var alreadyDownloadedLegacyPackages = GetRestoredLegacyPackageNames ( ) ;
884884
885- var notYetDownloadedPackages = new HashSet < string > ( fileContent . AllPackages ) ;
885+ var notYetDownloadedPackages = new HashSet < PackageReference > ( fileContent . AllPackages ) ;
886886 foreach ( var alreadyDownloadedPackage in alreadyDownloadedPackages )
887887 {
888- notYetDownloadedPackages . Remove ( alreadyDownloadedPackage ) ;
888+ notYetDownloadedPackages . Remove ( new ( alreadyDownloadedPackage , PackageReferenceSource . SdkCsProj ) ) ;
889889 }
890890 foreach ( var alreadyDownloadedLegacyPackage in alreadyDownloadedLegacyPackages )
891891 {
892- notYetDownloadedPackages . Remove ( alreadyDownloadedLegacyPackage ) ;
892+ notYetDownloadedPackages . Remove ( new ( alreadyDownloadedLegacyPackage , PackageReferenceSource . PackagesConfig ) ) ;
893893 }
894894
895895 if ( notYetDownloadedPackages . Count == 0 )
@@ -930,7 +930,7 @@ private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<string> dllPa
930930
931931 Parallel . ForEach ( notYetDownloadedPackages , new ParallelOptions { MaxDegreeOfParallelism = options . Threads } , package =>
932932 {
933- var success = TryRestorePackageManually ( package , nugetConfig ) ;
933+ var success = TryRestorePackageManually ( package . Name , nugetConfig , package . PackageReferenceSource ) ;
934934 if ( ! success )
935935 {
936936 return ;
@@ -947,7 +947,10 @@ private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<string> dllPa
947947 dllPaths . Add ( missingPackageDirectory . DirInfo . FullName ) ;
948948 }
949949
950- private bool TryRestorePackageManually ( string package , string ? nugetConfig )
950+ [ GeneratedRegex ( @"<TargetFramework>.*</TargetFramework>" , RegexOptions . IgnoreCase | RegexOptions . Compiled | RegexOptions . Singleline ) ]
951+ private static partial Regex TargetFramework ( ) ;
952+
953+ private bool TryRestorePackageManually ( string package , string ? nugetConfig , PackageReferenceSource packageReferenceSource = PackageReferenceSource . SdkCsProj )
951954 {
952955 logger . LogInfo ( $ "Restoring package { package } ...") ;
953956 using var tempDir = new TemporaryDirectory ( ComputeTempDirectory ( package , "missingpackages_workingdir" ) ) ;
@@ -957,6 +960,11 @@ private bool TryRestorePackageManually(string package, string? nugetConfig)
957960 return false ;
958961 }
959962
963+ if ( packageReferenceSource == PackageReferenceSource . PackagesConfig )
964+ {
965+ TryChangeTargetFrameworkMoniker ( tempDir . DirInfo ) ;
966+ }
967+
960968 success = dotnet . AddPackage ( tempDir . DirInfo . FullName , package ) ;
961969 if ( ! success )
962970 {
@@ -972,7 +980,9 @@ private bool TryRestorePackageManually(string package, string? nugetConfig)
972980 res = dotnet . Restore ( new ( tempDir . DirInfo . FullName , missingPackageDirectory . DirInfo . FullName , ForceDotnetRefAssemblyFetching : false , PathToNugetConfig : null , ForceReevaluation : true ) ) ;
973981 }
974982
975- // TODO: the restore might fail, we could retry with a prerelease (*-* instead of *) version of the package.
983+ // TODO: the restore might fail, we could retry with
984+ // - a prerelease (*-* instead of *) version of the package,
985+ // - a different target framework moniker.
976986
977987 if ( ! res . Success )
978988 {
@@ -984,6 +994,38 @@ private bool TryRestorePackageManually(string package, string? nugetConfig)
984994 return true ;
985995 }
986996
997+ private void TryChangeTargetFrameworkMoniker ( DirectoryInfo tempDir )
998+ {
999+ try
1000+ {
1001+ logger . LogInfo ( $ "Changing the target framework moniker in { tempDir . FullName } ...") ;
1002+
1003+ var csprojs = tempDir . GetFiles ( "*.csproj" , new EnumerationOptions { RecurseSubdirectories = false , MatchCasing = MatchCasing . CaseInsensitive } ) ;
1004+ if ( csprojs . Length != 1 )
1005+ {
1006+ logger . LogError ( $ "Could not find the .csproj file in { tempDir . FullName } , count = { csprojs . Length } ") ;
1007+ return ;
1008+ }
1009+
1010+ var csproj = csprojs [ 0 ] ;
1011+ var content = File . ReadAllText ( csproj . FullName ) ;
1012+ var matches = TargetFramework ( ) . Matches ( content ) ;
1013+ if ( matches . Count == 0 )
1014+ {
1015+ logger . LogError ( $ "Could not find target framework in { csproj . FullName } ") ;
1016+ }
1017+ else
1018+ {
1019+ content = TargetFramework ( ) . Replace ( content , $ "<TargetFramework>{ FrameworkPackageNames . LatestNetFrameworkMoniker } </TargetFramework>", 1 ) ;
1020+ File . WriteAllText ( csproj . FullName , content ) ;
1021+ }
1022+ }
1023+ catch ( Exception exc )
1024+ {
1025+ logger . LogError ( $ "Failed to update target framework in { tempDir . FullName } : { exc } ") ;
1026+ }
1027+ }
1028+
9871029 public void Dispose ( TemporaryDirectory ? dir , string name )
9881030 {
9891031 try
0 commit comments