Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MSBuildProjectExtensionsPath for RestoreOutputPath #2056

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ private static string[] SplitArgs(string unsplitArguments)
{
if (string.IsNullOrWhiteSpace(unsplitArguments))
{
return new string[0];
return Array.Empty<string>();
}

var ptrToSplitArgs = CommandLineToArgvW(unsplitArguments, out int numberOfArgs);
if (ptrToSplitArgs == IntPtr.Zero)
{
return new string[0];
return Array.Empty<string>();
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public async Task<string[]> GetProjectTypeGuidsAsync()
return new string[] { _projectTypeGuid };
}

return new string[0];
return Array.Empty<string>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static string[] GetProjectTypeGuids(IVsHierarchy hierarchy, string defaul
return new[] { defaultType };
}

return new string[0];
return Array.Empty<string>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public string ActivePackageSource

public string[] GetPackageSources()
{
return new string[0];
return Array.Empty<string>();
}

public string DefaultProject
Expand All @@ -60,7 +60,7 @@ public void SetDefaultProjectIndex(int index)

public string[] GetAvailableProjects()
{
return new string[0];
return Array.Empty<string>();
}

public void SetDefaultRunspace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Copyright (c) .NET Foundation. All rights reserved.
</PropertyGroup>
<PropertyGroup>
<PackageOutputPath Condition=" '$(PackageOutputPath)' == '' ">$(OutputPath)</PackageOutputPath>
<RestoreOutputPath Condition=" '$(RestoreOutputPath)' == '' " >$(BaseIntermediateOutputPath)</RestoreOutputPath>
<RestoreOutputPath Condition=" '$(RestoreOutputPath)' == '' " >$(MSBuildProjectExtensionsPath)</RestoreOutputPath>
</PropertyGroup>

<ConvertToAbsolutePath Paths="$(NuspecOutputPath)">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using NuGet.Commands;

namespace NuGet.Build.Tasks
{
public class ErrorForMismatchRestoreOutputPathTask : Task
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed in chat, can you please remove this error message, because it will break the advanced scenarios (namely dotnetclitool and global tools).

{
[Required]
public string RestoreOutputAbsolutePath { get; set; }

[Required]
public string MSBuildProjectExtensionsPath { get; set; }

public override bool Execute()
{
if (RestoreOutputAbsolutePath != MSBuildProjectExtensionsPath)
{
// The RestoreOutputPath, which resolved to '{0}', did not match the MSBuildProjectExtensionsPath, which was '{1}'. These properties must match in order for assets from NuGet restore to be applied correctly when building.
var log = new MSBuildLogger(Log);
var message = MSBuildRestoreUtility.GetErrorForRestoreOutputPathMismatch(RestoreOutputAbsolutePath, MSBuildProjectExtensionsPath);
log.Log(message);
}

return true;
}
}
}
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Build.Tasks/GetRestoreSettingsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public override bool Execute()
// Sources
var currentSources = RestoreSettingsUtils.GetValue(
() => RestoreSourcesOverride?.Select(MSBuildRestoreUtility.FixSourcePath).Select(e => GetGlobalAbsolutePath(e)).ToArray(),
() => MSBuildRestoreUtility.ContainsClearKeyword(RestoreSources) ? new string[0] : null,
() => MSBuildRestoreUtility.ContainsClearKeyword(RestoreSources) ? Array.Empty<string>() : null,
() => RestoreSources?.Select(MSBuildRestoreUtility.FixSourcePath).Select(e => UriUtility.GetAbsolutePathFromFile(ProjectUniqueName, e)).ToArray(),
() => (new PackageSourceProvider(settings)).LoadPackageSources().Where(e => e.IsEnabled).Select(e => e.Source).ToArray());

Expand All @@ -148,7 +148,7 @@ public override bool Execute()
// Fallback folders
var currentFallbackFolders = RestoreSettingsUtils.GetValue(
() => RestoreFallbackFoldersOverride?.Select(e => GetGlobalAbsolutePath(e)).ToArray(),
() => MSBuildRestoreUtility.ContainsClearKeyword(RestoreFallbackFolders) ? new string[0] : null,
() => MSBuildRestoreUtility.ContainsClearKeyword(RestoreFallbackFolders) ? Array.Empty<string>() : null,
() => RestoreFallbackFolders?.Select(e => UriUtility.GetAbsolutePathFromFile(ProjectUniqueName, e)).ToArray(),
() => SettingsUtility.GetFallbackPackageFolders(settings).ToArray());

Expand Down
9 changes: 8 additions & 1 deletion src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<UsingTask TaskName="NuGet.Build.Tasks.GetRestoreSettingsTask" AssemblyFile="$(RestoreTaskAssemblyFile)" />
<UsingTask TaskName="NuGet.Build.Tasks.WarnForInvalidProjectsTask" AssemblyFile="$(RestoreTaskAssemblyFile)" />
<UsingTask TaskName="NuGet.Build.Tasks.GetReferenceNearestTargetFrameworkTask" AssemblyFile="$(RestoreTaskAssemblyFile)" />
<UsingTask TaskName="NuGet.Build.Tasks.ErrorForMismatchRestoreOutputPathTask" AssemblyFile="$(RestoreTaskAssemblyFile)" />

<!--
============================================================
Expand Down Expand Up @@ -570,13 +571,19 @@ Copyright (c) .NET Foundation. All rights reserved.

<!-- Determine the restore output path -->
<PropertyGroup Condition=" '$(PackageReferenceCompatibleProjectStyle)' == 'true' OR '$(RestoreProjectStyle)' == 'ProjectJson' ">
<RestoreOutputPath Condition=" '$(RestoreOutputPath)' == '' " >$(BaseIntermediateOutputPath)</RestoreOutputPath>
<RestoreOutputPath Condition=" '$(RestoreOutputPath)' == '' " >$(MSBuildProjectExtensionsPath)</RestoreOutputPath>
</PropertyGroup>

<ConvertToAbsolutePath Paths="$(RestoreOutputPath)" Condition=" '$(PackageReferenceCompatibleProjectStyle)' == 'true' OR '$(RestoreProjectStyle)' == 'ProjectJson'">
<Output TaskParameter="AbsolutePaths" PropertyName="RestoreOutputAbsolutePath" />
</ConvertToAbsolutePath>

<ErrorForMismatchRestoreOutputPathTask
RestoreOutputAbsolutePath="$(RestoreOutputAbsolutePath)"
MSBuildProjectExtensionsPath="$(MSBuildProjectExtensionsPath)"
Condition=" '$(PackageReferenceCompatibleProjectStyle)' == 'true' OR '$(RestoreProjectStyle)' == 'ProjectJson'"
/>

<!--
Determine project name for the assets file.
Highest priority: PackageId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
Expand Down Expand Up @@ -28,8 +29,8 @@ public override bool Execute()
var log = new MSBuildLogger(Log);

// item -> string
var all = AllProjects?.Select(e => e.ItemSpec).ToArray() ?? new string[0];
var valid = ValidProjects?.Select(e => e.ItemSpec).ToArray() ?? new string[0];
var all = AllProjects?.Select(e => e.ItemSpec).ToArray() ?? Array.Empty<string>();
var valid = ValidProjects?.Select(e => e.ItemSpec).ToArray() ?? Array.Empty<string>();

// log inputs
BuildTasksUtility.LogInputParam(log, nameof(AllProjects), all);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,15 @@ public static bool LogErrorForClearIfInvalid(IEnumerable<string> values, string
return false;
}

// Log error NU1003
public static RestoreLogMessage GetErrorForRestoreOutputPathMismatch(string restoreOutputAbsolutePath, string msbuildProjectExtensionsPath)
{
var text = string.Format(CultureInfo.CurrentCulture, Strings.Error_RestoreOutputPathMismatch, restoreOutputAbsolutePath, msbuildProjectExtensionsPath);
var message = RestoreLogMessage.CreateError(NuGetLogCode.NU1503, text);

return message;
}

/// <summary>
/// Log warning NU1503
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/NuGet.Core/NuGet.Commands/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/NuGet.Core/NuGet.Commands/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -691,4 +691,7 @@ For more information, visit http://docs.nuget.org/docs/reference/command-line-re
<data name="Error_InvalidDependencyVersionConstraints" xml:space="preserve">
<value>Package version constraints for '{0}' return a version range that is empty.</value>
</data>
<data name="Error_RestoreOutputPathMismatch" xml:space="preserve">
<value>The RestoreOutputPath, which resolved to '{0}', did not match the MSBuildProjectExtensionsPath, which was '{1}'. These properties must match in order for assets from NuGet restore to be applied correctly when building.</value>
</data>
</root>
7 changes: 6 additions & 1 deletion src/NuGet.Core/NuGet.Common/Errors/NuGetLogCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace NuGet.Common
{
/// <summary>
/// This enum is used to quantify NuGet error and wanring codes.
/// This enum is used to quantify NuGet error and warning codes.
/// Format - NUxyzw where NU is the profix indicating NuGet and xyzw is a 4 digit code
///
/// Numbers - xyzw
Expand Down Expand Up @@ -59,6 +59,11 @@ public enum NuGetLogCode
/// </summary>
NU1003 = 1003,

/// <summary>
/// RestoreOutputPath does not match MSBuildProjectExtensionsPath
/// </summary>
NU1004 = 1004,

/// <summary>
/// Unable to resolve package, generic message for unknown type constraints.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Common/MsBuildStringUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static string[] Split(string s, params char[] chars)
.ToArray();
}

return new string[0];
return Array.Empty<string>();
}

/// <summary>
Expand All @@ -59,7 +59,7 @@ public static string[] TrimAndExcludeNullOrEmpty(string[] strings)
{
if (strings == null)
{
return new string[0];
return Array.Empty<string>();
}

return strings
Expand Down
2 changes: 1 addition & 1 deletion src/NuGet.Core/NuGet.Packaging/TopologicalSortUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static void CalculateRelationships(ItemDependencyInfo[] packages, Dictio
{
foreach (var package in packages)
{
var dependencies = package.DependencyIds ?? new string[0];
var dependencies = package.DependencyIds ?? Array.Empty<string>();

foreach (var id in dependencies)
{
Expand Down
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ private static PackOptions GetPackOptions(PackageSpec packageSpec, JObject rawPa
}
var owners = rawPackOptions["owners"];
var tags = rawPackOptions["tags"];
packageSpec.Owners = owners == null ? new string[0] { } : owners.ValueAsArray<string>();
packageSpec.Tags = tags == null ? new string[0] { } : tags.ValueAsArray<string>();
packageSpec.Owners = owners == null ? Array.Empty<string>() : owners.ValueAsArray<string>();
packageSpec.Tags = tags == null ? Array.Empty<string>() : tags.ValueAsArray<string>();
packageSpec.ProjectUrl = rawPackOptions.GetValue<string>("projectUrl");
packageSpec.IconUrl = rawPackOptions.GetValue<string>("iconUrl");
packageSpec.Summary = rawPackOptions.GetValue<string>("summary");
Expand Down
6 changes: 3 additions & 3 deletions src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public NuGetVersion Version

public string ReleaseNotes { get; set; }

public string[] Authors { get; set; } = new string[0];
public string[] Authors { get; set; } = Array.Empty<string>();

public string[] Owners { get; set; } = new string[0];
public string[] Owners { get; set; } = Array.Empty<string>();

public string ProjectUrl { get; set; }

Expand All @@ -77,7 +77,7 @@ public NuGetVersion Version

public BuildOptions BuildOptions { get; set; }

public string[] Tags { get; set; } = new string[0];
public string[] Tags { get; set; } = Array.Empty<string>();

public IList<string> ContentFiles { get; set; } = new List<string>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public V2FeedPackageInfo(PackageIdentity identity, string title, string summary,
{
_summary = summary;
_description = description;
_authors = authors == null ? new string[0] : authors.ToArray();
_owners = owners == null ? new string[0] : owners.ToArray();
_authors = authors == null ? Array.Empty<string>() : authors.ToArray();
_owners = owners == null ? Array.Empty<string>() : owners.ToArray();
_iconUrl = iconUrl;
_licenseUrl = licenseUrl;
_projectUrl = projectUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace NuGet.Versioning
public partial class SemanticVersion
{
// Reusable set of empty release labels
internal static readonly string[] EmptyReleaseLabels = new string[0];
internal static readonly string[] EmptyReleaseLabels = Array.Empty<string>();

/// <summary>
/// Parses a SemVer string using strict SemVer rules.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ public void PackTask_MapsAllProperties()
{
AssemblyName = "AssemblyName",
FrameworkAssemblyReferences = new ITaskItem[0],
Authors = new string[0],
AllowedOutputExtensionsInPackageBuildOutputFolder = new string[0],
AllowedOutputExtensionsInSymbolsPackageBuildOutputFolder = new string[0],
Authors = Array.Empty<string>(),
AllowedOutputExtensionsInPackageBuildOutputFolder = Array.Empty<string>(),
AllowedOutputExtensionsInSymbolsPackageBuildOutputFolder = Array.Empty<string>(),
BuildOutputFolder = "BuildOutputFolder",
ContentTargetFolders = new string[] { "ContentTargetFolders" } ,
ContinuePackingAfterGeneratingNuspec = true,
Expand All @@ -335,13 +335,13 @@ public void PackTask_MapsAllProperties()
MinClientVersion = "MinClientVersion",
NoPackageAnalysis = true,
NuspecOutputPath = "NuspecOutputPath",
NuspecProperties = new string[0],
NuspecProperties = Array.Empty<string>(),
PackItem = null, // This is asserted by other tests. It does not serialize well.
PackageFiles = new ITaskItem[0],
PackageFilesToExclude = new ITaskItem[0],
PackageId = "PackageId",
PackageOutputPath = "PackageOutputPath",
PackageTypes = new string[0],
PackageTypes = Array.Empty<string>(),
PackageVersion = "PackageVersion",
ProjectReferencesWithVersions = new ITaskItem[0],
ProjectUrl = "ProjectUrl",
Expand All @@ -353,8 +353,8 @@ public void PackTask_MapsAllProperties()
RequireLicenseAcceptance = true,
Serviceable = true,
SourceFiles = new ITaskItem[0],
Tags = new string[0],
TargetFrameworks = new string[0],
Tags = Array.Empty<string>(),
TargetFrameworks = Array.Empty<string>(),
BuildOutputInPackage = new ITaskItem[0],
TargetPathsToSymbols = new ITaskItem[0]
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void GetRestoreSettingsTask_GetValueGetLastValue()
RestoreSettingsUtils.GetValue(
() => null,
() => null,
() => new string[0]).ShouldBeEquivalentTo(new string[0]);
() => Array.Empty<string>()).ShouldBeEquivalentTo(Array.Empty<string>());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public TestContext(string userPackageFolder = null)
.Returns(userPackageFolder);
NuGetPathContext
.Setup(x => x.FallbackPackageFolders)
.Returns(new string[0]);
.Returns(Array.Empty<string>());

NuspecReader
.Setup(p => p.GetIdentity())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ public void CreateNuGetPackageOwners_WhenPackageOwnersNull_Throws()
public void CreateNuGetPackageOwners_WhenPackageOwnersEmpty_Throws()
{
var exception = Assert.Throws<ArgumentException>(
() => AttributeUtility.CreateNuGetPackageOwners(new string[0]));
() => AttributeUtility.CreateNuGetPackageOwners(Array.Empty<string>()));

Assert.Equal("packageOwners", exception.ParamName);
Assert.StartsWith("The argument cannot be null or empty.", exception.Message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Constructor_WhenPackageOwnersNull_Throws()
public void Constructor_WhenPackageOwnersEmpty_Throws()
{
var exception = Assert.Throws<ArgumentException>(
() => new NuGetPackageOwners(new string[0]));
() => new NuGetPackageOwners(Array.Empty<string>()));

Assert.Equal("packageOwners", exception.ParamName);
Assert.StartsWith("The argument cannot be null or empty.", exception.Message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void CreateSignedAttributes_RepositorySignPackageRequest_WhenPackageOwner
public void CreateSignedAttributes_RepositorySignPackageRequest_WhenPackageOwnersEmpty_ReturnsAttributes()
{
var v3ServiceIndexUrl = new Uri("https://test.test", UriKind.Absolute);
var packageOwners = new string[0];
var packageOwners = Array.Empty<string>();

using (var certificate = _fixture.GetDefaultCertificate())
using (var request = CreateRequestRepository(certificate, v3ServiceIndexUrl, packageOwners))
Expand Down
Loading