@@ -1452,6 +1452,85 @@ public void AnalyzePruningResults_WithMultiTargetingProjectAndVariousVersions_Wa
14521452 testEvent [ "Pruning.Pruned.Direct.Count" ] . Should ( ) . Be ( 1 ) ;
14531453 }
14541454
1455+ [ Fact ]
1456+ public void AnalyzePruningResults_WithCPMAndNullVersionRange_DoesNotThrowNullReferenceException ( )
1457+ {
1458+ // Test for issue: NullReferenceException in package pruning when using CPM and a version is not specified
1459+ var rootProject = @"
1460+ {
1461+ ""frameworks"": {
1462+ ""net10.0"": {
1463+ ""dependencies"": {
1464+ ""A"": {
1465+ ""target"": ""Package"",
1466+ },
1467+ },
1468+ ""packagesToPrune"": {
1469+ ""A"" : ""(,1.0.0]""
1470+ }
1471+ }
1472+ }
1473+ }" ;
1474+
1475+ var projectSpec = ProjectTestHelpers . GetPackageSpecWithProjectNameAndSpec ( "Project1" , Path . GetTempPath ( ) , rootProject ) ;
1476+ var testLogger = new TestLogger ( ) ;
1477+ var testEvent = new TelemetryEvent ( "dummyEvent" ) ;
1478+
1479+ // This should not throw a NullReferenceException
1480+ RestoreCommand . AnalyzePruningResults ( projectSpec , testEvent , testLogger ) ;
1481+
1482+ // Should not warn because the version range is null (not within pruning range)
1483+ testLogger . WarningMessages . Should ( ) . BeEmpty ( ) ;
1484+ testEvent [ "Pruning.Pruned.Direct.Count" ] . Should ( ) . Be ( 0 ) ;
1485+ }
1486+
1487+ [ Fact ]
1488+ public async Task RestoreCommand_WithCPMAndMissingVersion_DoesNotThrowNullReferenceException ( )
1489+ {
1490+ // Test for issue: NullReferenceException in package pruning when using CPM and a version is not specified
1491+ using var pathContext = new SimpleTestPathContext ( ) ;
1492+
1493+ // Setup packages
1494+ var packageA = new SimpleTestPackageContext ( "A" , "1.0.0" )
1495+ {
1496+ Dependencies = [ new SimpleTestPackageContext ( "B" , "1.0.0" ) ]
1497+ } ;
1498+
1499+ await SimpleTestPackageUtility . CreateFolderFeedV3Async (
1500+ pathContext . PackageSource ,
1501+ PackageSaveMode . Defaultv3 ,
1502+ packageA ) ;
1503+
1504+ var rootProject = @"
1505+ {
1506+ ""frameworks"": {
1507+ ""net10.0"": {
1508+ ""dependencies"": {
1509+ ""A"": {
1510+ ""target"": ""Package"",
1511+ },
1512+ },
1513+ ""packagesToPrune"": {
1514+ ""A"" : ""(,1.0.0]""
1515+ }
1516+ }
1517+ }
1518+ }" ;
1519+
1520+ // Setup project
1521+ var projectSpec = ProjectTestHelpers . GetPackageSpecWithProjectNameAndSpec ( "Project1" , pathContext . SolutionRoot , rootProject ) ;
1522+ var testLogger = new TestLogger ( ) ;
1523+
1524+ // Act - This should not throw a NullReferenceException during restore
1525+ var result = await RunRestoreAsync ( pathContext , testLogger , projectSpec ) ;
1526+
1527+ // Assert - Restore should fail with NU1010 (missing version in CPM), but not crash with NRE
1528+ result . Success . Should ( ) . BeFalse ( ) ;
1529+ testLogger . ErrorMessages . Should ( ) . Contain ( msg => msg . Contains ( "NU1010" ) ) ;
1530+ // Ensure no NullReferenceException occurred
1531+ testLogger . ErrorMessages . Should ( ) . NotContain ( msg => msg . Contains ( "Object reference not set" ) ) ;
1532+ }
1533+
14551534 // A 1.0.0 -> B 1.0.0
14561535 // C 1.0.0
14571536 private static async Task SetupCommonPackagesAsync ( SimpleTestPathContext pathContext )
0 commit comments