@@ -882,14 +882,14 @@ private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<string> dllPa
882
882
var alreadyDownloadedPackages = GetRestoredPackageDirectoryNames ( packageDirectory . DirInfo ) ;
883
883
var alreadyDownloadedLegacyPackages = GetRestoredLegacyPackageNames ( ) ;
884
884
885
- var notYetDownloadedPackages = new HashSet < string > ( fileContent . AllPackages ) ;
885
+ var notYetDownloadedPackages = new HashSet < PackageReference > ( fileContent . AllPackages ) ;
886
886
foreach ( var alreadyDownloadedPackage in alreadyDownloadedPackages )
887
887
{
888
- notYetDownloadedPackages . Remove ( alreadyDownloadedPackage ) ;
888
+ notYetDownloadedPackages . Remove ( new ( alreadyDownloadedPackage , PackageReferenceSource . SdkCsProj ) ) ;
889
889
}
890
890
foreach ( var alreadyDownloadedLegacyPackage in alreadyDownloadedLegacyPackages )
891
891
{
892
- notYetDownloadedPackages . Remove ( alreadyDownloadedLegacyPackage ) ;
892
+ notYetDownloadedPackages . Remove ( new ( alreadyDownloadedLegacyPackage , PackageReferenceSource . PackagesConfig ) ) ;
893
893
}
894
894
895
895
if ( notYetDownloadedPackages . Count == 0 )
@@ -930,7 +930,7 @@ private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<string> dllPa
930
930
931
931
Parallel . ForEach ( notYetDownloadedPackages , new ParallelOptions { MaxDegreeOfParallelism = options . Threads } , package =>
932
932
{
933
- var success = TryRestorePackageManually ( package , nugetConfig ) ;
933
+ var success = TryRestorePackageManually ( package . Name , nugetConfig , package . PackageReferenceSource ) ;
934
934
if ( ! success )
935
935
{
936
936
return ;
@@ -947,7 +947,10 @@ private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<string> dllPa
947
947
dllPaths . Add ( missingPackageDirectory . DirInfo . FullName ) ;
948
948
}
949
949
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 )
951
954
{
952
955
logger . LogInfo ( $ "Restoring package { package } ...") ;
953
956
using var tempDir = new TemporaryDirectory ( ComputeTempDirectory ( package , "missingpackages_workingdir" ) ) ;
@@ -957,6 +960,11 @@ private bool TryRestorePackageManually(string package, string? nugetConfig)
957
960
return false ;
958
961
}
959
962
963
+ if ( packageReferenceSource == PackageReferenceSource . PackagesConfig )
964
+ {
965
+ TryChangeTargetFrameworkMoniker ( tempDir . DirInfo ) ;
966
+ }
967
+
960
968
success = dotnet . AddPackage ( tempDir . DirInfo . FullName , package ) ;
961
969
if ( ! success )
962
970
{
@@ -972,7 +980,9 @@ private bool TryRestorePackageManually(string package, string? nugetConfig)
972
980
res = dotnet . Restore ( new ( tempDir . DirInfo . FullName , missingPackageDirectory . DirInfo . FullName , ForceDotnetRefAssemblyFetching : false , PathToNugetConfig : null , ForceReevaluation : true ) ) ;
973
981
}
974
982
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.
976
986
977
987
if ( ! res . Success )
978
988
{
@@ -984,6 +994,38 @@ private bool TryRestorePackageManually(string package, string? nugetConfig)
984
994
return true ;
985
995
}
986
996
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
+
987
1029
public void Dispose ( TemporaryDirectory ? dir , string name )
988
1030
{
989
1031
try
0 commit comments