Skip to content

Commit a0a0a22

Browse files
authored
Set ExcludeRestorePackageImports when reading PackageReference for list package (#6981)
1 parent 8e2579a commit a0a0a22

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,10 @@ private static IEnumerable<ProjectItem> GetPackageReferences(Project project, Li
907907
private static IEnumerable<InstalledPackageReference> GetPackageReferencesFromTargets(string projectPath, string framework)
908908
{
909909
var globalProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
910-
{ { "TargetFramework", framework } };
910+
{
911+
{ "TargetFramework", framework },
912+
{ "ExcludeRestorePackageImports", bool.TrueString }
913+
};
911914
var newProject = new ProjectInstance(projectPath, globalProperties, null);
912915
newProject.Build(new[] { CollectPackageReferences, CollectCentralPackageVersions }, new List<Microsoft.Build.Framework.ILogger> { }, out var targetOutputs);
913916

test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetListPackageTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,46 @@ public void SolutionFilter_DoesNotOutputExcludedProject(bool useSlnx)
10941094
projects[0]["path"].ToString().Should().Be(PathUtility.GetPathWithForwardSlashes(projectA.ProjectPath));
10951095
}
10961096

1097+
[PlatformFact(Platform.Windows)]
1098+
public async Task DotnetListPackage_PackageWithTargetsFileErrors_ReturnsCorrectResults()
1099+
{
1100+
// Arrange
1101+
using var pathContext = _fixture.CreateSimpleTestPathContext();
1102+
_fixture.CreateDotnetNewProject(pathContext.SolutionRoot, ProjectName, args: "classlib", _testOutputHelper);
1103+
string projectPath = Path.Combine(pathContext.SolutionRoot, ProjectName, $"{ProjectName}.csproj");
1104+
1105+
// Create a package with a .targets file that logs an error
1106+
var packageX = new SimpleTestPackageContext("PackageX", "1.0.0");
1107+
packageX.AddFile("lib/net5.0/_._");
1108+
1109+
var targetsContent = @"
1110+
<Project InitialTargets=""ErrorToFailBuild"">
1111+
<Target Name=""ErrorToFailBuild"">
1112+
<Error Text=""This is a failure within a package targets, to ensure list package is able to handle it."" />
1113+
</Target>
1114+
</Project>";
1115+
packageX.AddFile("build/PackageX.targets", targetsContent);
1116+
1117+
// Generate Package
1118+
await SimpleTestPackageUtility.CreateFolderFeedV3Async(
1119+
pathContext.PackageSource,
1120+
PackageSaveMode.Defaultv3,
1121+
packageX);
1122+
1123+
// Pre-Req
1124+
_fixture.RunDotnetExpectSuccess(Directory.GetParent(projectPath).FullName,
1125+
$"add {projectPath} package PackageX --version 1.0.0",
1126+
testOutputHelper: _testOutputHelper);
1127+
1128+
// Act - dotnet list package should succeed despite the MSBuild error in the .targets file
1129+
CommandRunnerResult listResult = _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectPath).FullName,
1130+
$"list {projectPath} package",
1131+
testOutputHelper: _testOutputHelper);
1132+
1133+
// Assert - Verify the package is listed correctly
1134+
Assert.True(ContainsIgnoringSpaces(listResult.AllOutput, "PackageX1.0.01.0.0"));
1135+
}
1136+
10971137
private static string CollapseSpaces(string input)
10981138
{
10991139
return Regex.Replace(input, " +", " ");

0 commit comments

Comments
 (0)