Skip to content

Commit c5b09d5

Browse files
authored
Disable CLI project.json restore (#6799)
1 parent 4c2f5c5 commit c5b09d5

8 files changed

Lines changed: 71 additions & 123 deletions

File tree

src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,6 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary<
10691069
(ProjectStyle ProjectStyle, string PackagesConfigFilePath) projectStyleResult = BuildTasksUtility.GetProjectRestoreStyle(
10701070
restoreProjectStyle: projectStyleOrNull,
10711071
hasPackageReferenceItems: hasPackageReferenceItems,
1072-
projectJsonPath: project.GetProperty("_CurrentProjectJsonPath"),
10731072
projectDirectory: project.Directory,
10741073
projectName: project.GetProperty("MSBuildProjectName"),
10751074
log: log);

src/NuGet.Core/NuGet.Build.Tasks/BuildTasksUtility.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,12 @@ public static async Task<List<RestoreSummary>> RestoreAsync(
324324
/// </summary>
325325
/// <param name="restoreProjectStyle">An optional user supplied restore style.</param>
326326
/// <param name="hasPackageReferenceItems">A <see cref="bool"/> indicating whether or not the project has any PackageReference items.</param>
327-
/// <param name="projectJsonPath">An optional path to the project's project.json file.</param>
328327
/// <param name="projectDirectory">The full path to the project directory.</param>
329328
/// <param name="projectName">The name of the project file.</param>
330329
/// <param name="log">An <see cref="NuGet.Common.ILogger"/> object used to log messages.</param>
331330
/// <returns>A <see cref="Tuple{ProjectStyle, Boolean}"/> containing the project style and a value indicating if the project is using a style that is compatible with PackageReference.
332331
/// If the value of <paramref name="restoreProjectStyle"/> is not empty and could not be parsed, <code>null</code> is returned.</returns>
333-
public static (ProjectStyle ProjectStyle, string PackagesConfigFilePath) GetProjectRestoreStyle(ProjectStyle? restoreProjectStyle, bool hasPackageReferenceItems, string projectJsonPath, string projectDirectory, string projectName, Common.ILogger log)
332+
public static (ProjectStyle ProjectStyle, string PackagesConfigFilePath) GetProjectRestoreStyle(ProjectStyle? restoreProjectStyle, bool hasPackageReferenceItems, string projectDirectory, string projectName, Common.ILogger log)
334333
{
335334
ProjectStyle projectStyle;
336335
string packagesConfigFilePath = null;
@@ -345,11 +344,6 @@ public static (ProjectStyle ProjectStyle, string PackagesConfigFilePath) GetProj
345344
// If any PackageReferences exist treat it as PackageReference. This has priority over project.json.
346345
projectStyle = ProjectStyle.PackageReference;
347346
}
348-
else if (!string.IsNullOrWhiteSpace(projectJsonPath))
349-
{
350-
// If this is not a PackageReference project check if project.json or projectName.project.json exists.
351-
projectStyle = ProjectStyle.ProjectJson;
352-
}
353347
else if (ProjectHasPackagesConfigFile(projectDirectory, projectName, out packagesConfigFilePath))
354348
{
355349
// If this is not a PackageReference or ProjectJson project check if packages.config or packages.ProjectName.config exists
@@ -370,15 +364,14 @@ public static (ProjectStyle ProjectStyle, string PackagesConfigFilePath) GetProj
370364
/// </summary>
371365
/// <param name="restoreProjectStyle">An optional user supplied restore style.</param>
372366
/// <param name="hasPackageReferenceItems">A <see cref="bool"/> indicating whether or not the project has any PackageReference items.</param>
373-
/// <param name="projectJsonPath">An optional path to the project's project.json file.</param>
374367
/// <param name="projectDirectory">The full path to the project directory.</param>
375368
/// <param name="projectName">The name of the project file.</param>
376369
/// <param name="log">An <see cref="NuGet.Common.ILogger"/> object used to log messages.</param>
377370
/// <returns>A <see cref="Tuple{ProjectStyle, Boolean}"/> containing the project style and a value indicating if the project is using a style that is compatible with PackageReference.
378371
/// If the value of <paramref name="restoreProjectStyle"/> is not empty and could not be parsed, <code>null</code> is returned.</returns>
379-
public static (ProjectStyle ProjectStyle, string PackagesConfigFilePath) GetProjectRestoreStyle(string restoreProjectStyle, bool hasPackageReferenceItems, string projectJsonPath, string projectDirectory, string projectName, Common.ILogger log)
372+
public static (ProjectStyle ProjectStyle, string PackagesConfigFilePath) GetProjectRestoreStyle(string restoreProjectStyle, bool hasPackageReferenceItems, string projectDirectory, string projectName, Common.ILogger log)
380373
{
381-
return GetProjectRestoreStyle(GetProjectRestoreStyleFromProjectProperty(restoreProjectStyle), hasPackageReferenceItems, projectJsonPath, projectDirectory, projectName, log);
374+
return GetProjectRestoreStyle(GetProjectRestoreStyleFromProjectProperty(restoreProjectStyle), hasPackageReferenceItems, projectDirectory, projectName, log);
382375
}
383376

384377
/// <summary>

src/NuGet.Core/NuGet.Build.Tasks/GetRestoreProjectJsonPathTask.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/NuGet.Core/NuGet.Build.Tasks/GetRestoreProjectStyleTask.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ public sealed class GetRestoreProjectStyleTask : Microsoft.Build.Utilities.Task
3131
[Required]
3232
public string MSBuildProjectName { get; set; }
3333

34-
/// <summary>
35-
/// The path to a project.json file.
36-
/// </summary>
37-
public string ProjectJsonPath { get; set; }
38-
3934
/// <summary>
4035
/// Gets or sets the <see cref="ProjectModel.ProjectStyle"/> of the project.
4136
/// </summary>
@@ -51,7 +46,7 @@ public override bool Execute()
5146
{
5247
var log = new MSBuildLogger(Log);
5348

54-
var result = BuildTasksUtility.GetProjectRestoreStyle(RestoreProjectStyle, HasPackageReferenceItems, ProjectJsonPath, MSBuildProjectDirectory, MSBuildProjectName, log);
49+
var result = BuildTasksUtility.GetProjectRestoreStyle(RestoreProjectStyle, HasPackageReferenceItems, MSBuildProjectDirectory, MSBuildProjectName, log);
5550

5651
IsPackageReferenceCompatibleProjectStyle = result.ProjectStyle == ProjectStyle.PackageReference;
5752
ProjectStyle = result.ProjectStyle;

src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ Copyright (c) .NET Foundation. All rights reserved.
158158
<!-- Tasks -->
159159
<UsingTask TaskName="NuGet.Build.Tasks.RestoreTask" AssemblyFile="$(RestoreTaskAssemblyFile)" />
160160
<UsingTask TaskName="NuGet.Build.Tasks.WriteRestoreGraphTask" AssemblyFile="$(RestoreTaskAssemblyFile)" />
161-
<UsingTask TaskName="NuGet.Build.Tasks.GetRestoreProjectJsonPathTask" AssemblyFile="$(RestoreTaskAssemblyFile)" />
162161
<UsingTask TaskName="NuGet.Build.Tasks.GetRestoreProjectReferencesTask" AssemblyFile="$(RestoreTaskAssemblyFile)" />
163162
<UsingTask TaskName="NuGet.Build.Tasks.GetRestorePackageReferencesTask" AssemblyFile="$(RestoreTaskAssemblyFile)" />
164163
<UsingTask TaskName="NuGet.Build.Tasks.GetCentralPackageVersionsTask" AssemblyFile="$(RestoreTaskAssemblyFile)" />
@@ -655,31 +654,14 @@ Copyright (c) .NET Foundation. All rights reserved.
655654

656655
</Target>
657656

658-
<!--
659-
============================================================
660-
_GetProjectJsonPath
661-
Discover the project.json path if one exists for the project.
662-
============================================================
663-
-->
664-
<Target Name="_GetProjectJsonPath"
665-
Returns="$(_CurrentProjectJsonPath)">
666-
<!-- Get project.json path -->
667-
<!-- Skip this if the project style is already set. -->
668-
<GetRestoreProjectJsonPathTask
669-
ProjectPath="$(MSBuildProjectFullPath)"
670-
Condition=" '$(RestoreProjectStyle)' == 'ProjectJson' OR '$(RestoreProjectStyle)' == '' ">
671-
<Output TaskParameter="ProjectJsonPath" PropertyName="_CurrentProjectJsonPath" />
672-
</GetRestoreProjectJsonPathTask>
673-
</Target>
674-
675657
<!--
676658
============================================================
677659
_GetRestoreProjectStyle
678660
Determine the project restore type.
679661
============================================================
680662
-->
681663
<Target Name="_GetRestoreProjectStyle"
682-
DependsOnTargets="_GetProjectJsonPath;CollectPackageReferences"
664+
DependsOnTargets="CollectPackageReferences"
683665
Returns="$(RestoreProjectStyle);$(PackageReferenceCompatibleProjectStyle)">
684666

685667
<!--
@@ -698,7 +680,6 @@ Copyright (c) .NET Foundation. All rights reserved.
698680
HasPackageReferenceItems="$(_HasPackageReferenceItems)"
699681
MSBuildProjectDirectory="$(MSBuildProjectDirectory)"
700682
MSBuildProjectName="$(MSBuildProjectName)"
701-
ProjectJsonPath="$(_CurrentProjectJsonPath)"
702683
RestoreProjectStyle="$(RestoreProjectStyle)">
703684
<Output TaskParameter="ProjectStyle" PropertyName="RestoreProjectStyle" />
704685
<Output TaskParameter="IsPackageReferenceCompatibleProjectStyle" PropertyName="PackageReferenceCompatibleProjectStyle" />
@@ -744,7 +725,6 @@ Copyright (c) .NET Foundation. All rights reserved.
744725
<_TargetFrameworkToBeUsed Condition=" '$(_TargetFrameworkOverride)' == '' ">$(TargetFrameworks)</_TargetFrameworkToBeUsed>
745726
</PropertyGroup>
746727

747-
<!-- For project.json projects target frameworks will be read from project.json. -->
748728
<GetProjectTargetFrameworksTask
749729
Condition=" '$(RestoreProjectStyle)' != 'ProjectJson'"
750730
ProjectPath="$(MSBuildProjectFullPath)"
@@ -981,23 +961,6 @@ Copyright (c) .NET Foundation. All rights reserved.
981961
</_RestoreGraphEntry>
982962
</ItemGroup>
983963

984-
<!-- Use project.json -->
985-
<ItemGroup Condition=" '$(RestoreProjectStyle)' == 'ProjectJson' ">
986-
<_RestoreGraphEntry Include="$([System.Guid]::NewGuid())">
987-
<Type>ProjectSpec</Type>
988-
<ProjectUniqueName>$(MSBuildProjectFullPath)</ProjectUniqueName>
989-
<ProjectPath>$(MSBuildProjectFullPath)</ProjectPath>
990-
<ProjectName>$(_RestoreProjectName)</ProjectName>
991-
<Sources>$(_OutputSources)</Sources>
992-
<OutputPath>$(RestoreOutputAbsolutePath)</OutputPath>
993-
<FallbackFolders>$(_OutputFallbackFolders)</FallbackFolders>
994-
<PackagesPath>$(_OutputPackagesPath)</PackagesPath>
995-
<ProjectJsonPath>$(_CurrentProjectJsonPath)</ProjectJsonPath>
996-
<ProjectStyle>$(RestoreProjectStyle)</ProjectStyle>
997-
<ConfigFilePaths>$(_OutputConfigFilePaths)</ConfigFilePaths>
998-
</_RestoreGraphEntry>
999-
</ItemGroup>
1000-
1001964
<!-- Use packages.config -->
1002965
<ItemGroup Condition=" '$(RestoreProjectStyle)' == 'PackagesConfig' ">
1003966
<_RestoreGraphEntry Include="$([System.Guid]::NewGuid())">

src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/PackageSpecFactory.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ private static (ProjectRestoreMetadata? RestoreMetadata, List<TargetFrameworkInf
174174
GetProjectRestoreStyle(
175175
restoreProjectStyle: projectStyleOrNull,
176176
hasPackageReferenceItems: hasPackageReferenceItems,
177-
projectJsonPath: project.OuterBuild.GetProperty("_CurrentProjectJsonPath"),
178177
projectDirectory: project.Directory,
179178
projectName: project.OuterBuild.GetProperty("MSBuildProjectName"));
180179

@@ -317,13 +316,12 @@ internal static bool IsLegacyProject(ITargetFramework project)
317316
/// </summary>
318317
/// <param name="restoreProjectStyle">An optional user supplied restore style.</param>
319318
/// <param name="hasPackageReferenceItems">A <see cref="bool"/> indicating whether or not the project has any PackageReference items.</param>
320-
/// <param name="projectJsonPath">An optional path to the project's project.json file.</param>
321319
/// <param name="projectDirectory">The full path to the project directory.</param>
322320
/// <param name="projectName">The name of the project file.</param>
323321
/// <returns>A <see cref="Tuple{ProjectStyle, Boolean}"/> containing the project style and a value indicating if the project is using a style that is compatible with PackageReference.
324322
/// If the value of <paramref name="restoreProjectStyle"/> is not empty and could not be parsed, <code>null</code> is returned.</returns>
325323
private static (ProjectStyle ProjectStyle, string? PackagesConfigFilePath)
326-
GetProjectRestoreStyle(ProjectStyle? restoreProjectStyle, bool hasPackageReferenceItems, string projectJsonPath, string projectDirectory, string projectName)
324+
GetProjectRestoreStyle(ProjectStyle? restoreProjectStyle, bool hasPackageReferenceItems, string projectDirectory, string projectName)
327325
{
328326
ProjectStyle projectStyle;
329327
string? packagesConfigFilePath = null;
@@ -338,11 +336,6 @@ private static (ProjectStyle ProjectStyle, string? PackagesConfigFilePath)
338336
// If any PackageReferences exist treat it as PackageReference. This has priority over project.json.
339337
projectStyle = ProjectStyle.PackageReference;
340338
}
341-
else if (!string.IsNullOrWhiteSpace(projectJsonPath))
342-
{
343-
// If this is not a PackageReference project check if project.json or projectName.project.json exists.
344-
projectStyle = ProjectStyle.ProjectJson;
345-
}
346339
else if (ProjectHasPackagesConfigFile(projectDirectory, projectName, out packagesConfigFilePath))
347340
{
348341
// If this is not a PackageReference or ProjectJson project check if packages.config or packages.ProjectName.config exists

test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,5 +2149,70 @@ public void MsbuildRestore_StaticGraphRestore_CanReadSolutionFiles(bool useSlnx)
21492149
result.Success.Should().BeTrue(because: result.AllOutput);
21502150
project.AssetsFile.Should().NotBeNull();
21512151
}
2152+
2153+
[PlatformTheory(Platform.Windows)]
2154+
[InlineData(true, true)]
2155+
[InlineData(true, false)]
2156+
[InlineData(false, true)]
2157+
[InlineData(false, false)]
2158+
public async Task MsbuildRestore_WithProjectJsonProject_Skips(bool useStaticGraphRestore, bool usePackageSpecFactory)
2159+
{
2160+
// Arrange
2161+
using (var pathContext = new SimpleTestPathContext())
2162+
{
2163+
// Set up solution, project, and packages
2164+
var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot);
2165+
2166+
var framework = NuGetFramework.Parse("net472");
2167+
2168+
var project = new SimpleTestProjectContext("a", ProjectStyle.ProjectJson, pathContext.SolutionRoot);
2169+
project.Frameworks.Add(new SimpleTestProjectFrameworkContext(framework));
2170+
2171+
solution.Projects.Add(project);
2172+
solution.Create(pathContext.SolutionRoot);
2173+
2174+
File.WriteAllText(Path.Combine(Path.GetDirectoryName(project.ProjectPath), "project.json"), @"
2175+
{
2176+
""dependencies"": {
2177+
""x"": ""1.0.0""
2178+
},
2179+
""frameworks"": {
2180+
""net472"": {}
2181+
},
2182+
""runtimes"": {
2183+
""win-anycpu"": {},
2184+
""win"": {}
2185+
}
2186+
}");
2187+
2188+
var packageX = new SimpleTestPackageContext()
2189+
{
2190+
Id = "x",
2191+
Version = "1.0.0"
2192+
};
2193+
2194+
await SimpleTestPackageUtility.CreateFolderFeedV3Async(
2195+
pathContext.PackageSource,
2196+
packageX);
2197+
2198+
var projectOutputPaths = new[]
2199+
{
2200+
project.AssetsFileOutputPath
2201+
};
2202+
2203+
var environmentVariables = new Dictionary<string, string>();
2204+
environmentVariables.AddRange(_msbuildFixture.DefaultProcessEnvironmentVariables);
2205+
environmentVariables["NUGET_USE_NEW_PACKAGESPEC_FACTORY"] = usePackageSpecFactory.ToString();
2206+
2207+
var result = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore {project.ProjectPath}" + (useStaticGraphRestore ? " /p:RestoreUseStaticGraphEvaluation=\"true\"" : string.Empty), ignoreExitCode: true, testOutputHelper: _testOutputHelper, environmentVariables);
2208+
result.Success.Should().BeTrue(because: result.AllOutput);
2209+
2210+
foreach (var asset in projectOutputPaths)
2211+
{
2212+
var fileInfo = new FileInfo(asset);
2213+
fileInfo.Exists.Should().BeFalse(because: result.AllOutput);
2214+
}
2215+
}
2216+
}
21522217
}
21532218
}

test/NuGet.Core.Tests/NuGet.Build.Tasks.Test/GetRestoreProjectStyleTaskTests.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public void Execute_WhenNothingMatches_ReturnsUnknown(string restoreStyle)
2626
{
2727
BuildEngine = buildEngine,
2828
RestoreProjectStyle = restoreStyle,
29-
ProjectJsonPath = string.Empty,
3029
HasPackageReferenceItems = false,
3130
MSBuildProjectName = "ProjectA",
3231
MSBuildProjectDirectory = "SomeDirectory"
@@ -80,23 +79,6 @@ public void Execute_WhenProjectHasPackagesConfigFile_ReturnsPackagesConfig(strin
8079
}
8180
}
8281

83-
[Fact]
84-
public void Execute_WhenProjectJsonPathSpecified_ReturnsProjectJson()
85-
{
86-
var buildEngine = new TestBuildEngine();
87-
88-
var task = new GetRestoreProjectStyleTask
89-
{
90-
BuildEngine = buildEngine,
91-
ProjectJsonPath = "SomePath"
92-
};
93-
94-
task.Execute().Should().BeTrue();
95-
96-
task.ProjectStyle.Should().Be(ProjectStyle.ProjectJson);
97-
task.IsPackageReferenceCompatibleProjectStyle.Should().BeFalse();
98-
}
99-
10082
[Theory]
10183
[InlineData(true)]
10284
[InlineData(false)]
@@ -141,7 +123,6 @@ public void Execute_WhenUserSuppliedValueOverridesDefault_ReturnsUserSuppliedPro
141123
{
142124
BuildEngine = buildEngine,
143125
RestoreProjectStyle = expected.ToString(),
144-
ProjectJsonPath = "Some value",
145126
HasPackageReferenceItems = true,
146127
MSBuildProjectName = "ProjectA",
147128
MSBuildProjectDirectory = "SomeDirectory"

0 commit comments

Comments
 (0)