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

Disable nuget.exe pack for project.json by default, add a fallback env var to enable it #4243

Merged
merged 9 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 42 additions & 20 deletions src/NuGet.Clients/NuGet.CommandLine/Commands/ProjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@

namespace NuGet.CommandLine
{

[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
public class ProjectFactory : MSBuildUser, IProjectFactory, CoreV2.NuGet.IPropertyProvider
{
private const string NUGET_ENABLE_LEGACY_PROJECT_JSON_PACK = nameof(NUGET_ENABLE_LEGACY_PROJECT_JSON_PACK);

// Its type is Microsoft.Build.Evaluation.Project
private dynamic _project;

private Common.ILogger _logger;
private ILogger _logger;

private bool _usingJsonFile;

private IEnvironmentVariableReader _environmentVariableReader;

// Files we want to always exclude from the resulting package
private static readonly HashSet<string> _excludeFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
private static readonly HashSet<string> ExcludeFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
NuGetConstants.PackageReferenceFile,
"Web.Debug.config",
"Web.Release.config"
Expand Down Expand Up @@ -84,6 +87,8 @@ public ProjectFactory(string msbuildDirectory, string path, IDictionary<string,
{
LoadAssemblies(msbuildDirectory);

_environmentVariableReader = EnvironmentVariableWrapper.Instance;

// Create project, allowing for assembly load failures
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolve);

Expand All @@ -106,17 +111,20 @@ public ProjectFactory(string msbuildDirectory, dynamic project)
{
LoadAssemblies(msbuildDirectory);
Initialize(project);
_environmentVariableReader = EnvironmentVariableWrapper.Instance;
}

private ProjectFactory(
string msbuildDirectory,
Assembly msbuildAssembly,
Assembly frameworkAssembly,
dynamic project)
dynamic project,
IEnvironmentVariableReader environmentVariableReader)
{
_msbuildDirectory = msbuildDirectory;
_msbuildAssembly = msbuildAssembly;
_frameworkAssembly = frameworkAssembly;
_environmentVariableReader = environmentVariableReader;
LoadTypes();
Initialize(project);
}
Expand All @@ -128,7 +136,7 @@ private void Initialize(dynamic project)
AddSolutionDir();
// Get the target framework of the project
string targetFrameworkMoniker = _project.GetPropertyValue("TargetFrameworkMoniker");
if (!String.IsNullOrEmpty(targetFrameworkMoniker))
if (!string.IsNullOrEmpty(targetFrameworkMoniker))
{
TargetFramework = NuGetFramework.Parse(targetFrameworkMoniker);
}
Expand Down Expand Up @@ -213,7 +221,7 @@ public ILogger Logger
}

[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We want to continue regardless of any error we encounter extracting metadata.")]
public Packaging.PackageBuilder CreateBuilder(string basePath, NuGetVersion version, string suffix, bool buildIfNeeded, Packaging.PackageBuilder builder = null)
public PackageBuilder CreateBuilder(string basePath, NuGetVersion version, string suffix, bool buildIfNeeded, Packaging.PackageBuilder builder = null)
{
if (buildIfNeeded)
{
Expand Down Expand Up @@ -275,10 +283,12 @@ public Packaging.PackageBuilder CreateBuilder(string basePath, NuGetVersion vers
}
}

Packaging.Manifest manifest = null;
Manifest manifest = null;

// If there is a project.json file, load that and skip any nuspec that may exist
#pragma warning disable CS0612 // Type or member is obsolete
if (!PackCommandRunner.ProcessProjectJsonFile(builder, _project.DirectoryPath as string, builder.Id, version, suffix, GetPropertyValue))
#pragma warning restore CS0612 // Type or member is obsolete
{
// If the package contains a nuspec file then use it for metadata
manifest = ProcessNuspec(builder, basePath);
Expand All @@ -290,6 +300,18 @@ public Packaging.PackageBuilder CreateBuilder(string basePath, NuGetVersion vers
string.Format(NuGetResources.ProjectJsonPack_Deprecated, builder.Id),
NuGetLogCode.NU5126));
_usingJsonFile = true;

_ = bool.TryParse(_environmentVariableReader.GetEnvironmentVariable(NUGET_ENABLE_LEGACY_PROJECT_JSON_PACK), out bool enableLegacyProjectJsonPack);

if (!enableLegacyProjectJsonPack)
{
Logger.Log(
PackagingLogMessage.CreateError(
string.Format(NuGetResources.Error_ProjectJson_Deprecated_And_Removed, builder.Id, NUGET_ENABLE_LEGACY_PROJECT_JSON_PACK),
NuGetLogCode.NU5042));
return null;
}

}

// Remove the extra author
Expand Down Expand Up @@ -324,7 +346,7 @@ public Packaging.PackageBuilder CreateBuilder(string basePath, NuGetVersion vers
ProcessDependencies(builder);

// Set defaults if some required fields are missing
if (String.IsNullOrEmpty(builder.Description))
if (string.IsNullOrEmpty(builder.Description))
{
builder.Description = "Description";
Logger.Log(PackagingLogMessage.CreateWarning(string.Format(
Expand Down Expand Up @@ -365,23 +387,23 @@ public string InitializeProperties(Packaging.IPackageMetadata metadata)

_properties.Add("Version", metadata.Version.ToFullString());

if (!String.IsNullOrEmpty(metadata.Title))
if (!string.IsNullOrEmpty(metadata.Title))
{
_properties.Add("Title", metadata.Title);
}

if (!String.IsNullOrEmpty(metadata.Description))
if (!string.IsNullOrEmpty(metadata.Description))
{
_properties.Add("Description", metadata.Description);
}

if (!String.IsNullOrEmpty(metadata.Copyright))
if (!string.IsNullOrEmpty(metadata.Copyright))
{
_properties.Add("Copyright", metadata.Copyright);
}

string projectAuthor = metadata.Authors.FirstOrDefault();
if (!String.IsNullOrEmpty(projectAuthor))
if (!string.IsNullOrEmpty(projectAuthor))
{
_properties.Add("Author", projectAuthor);
}
Expand Down Expand Up @@ -588,7 +610,7 @@ private void RecursivelyApply(Action<ProjectFactory> action, dynamic alreadyAppl
null,
alreadyAppliedProjects);
var referencedProject = new ProjectFactory(
_msbuildDirectory, _msbuildAssembly, _frameworkAssembly, project);
_msbuildDirectory, _msbuildAssembly, _frameworkAssembly, project, _environmentVariableReader);
referencedProject.Logger = _logger;
referencedProject.IncludeSymbols = IncludeSymbols;
referencedProject.Build = Build;
Expand Down Expand Up @@ -696,19 +718,19 @@ private void AddProjectReferenceDependencies(Dictionary<string, Packaging.Core.P
}
}

[Obsolete]
private bool ProcessJsonFile(PackageBuilder builder, string basePath, string id)
{
return PackCommandRunner.ProcessProjectJsonFile(builder, basePath, id, null, null, GetPropertyValue);
}

// Creates a package dependency from the given project, which has a corresponding
// nuspec file.
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We want to continue regardless of any error we encounter extracting metadata.")]
private PackageDependency CreateDependencyFromProject(dynamic project, Dictionary<string, Packaging.Core.PackageDependency> dependencies)
{
try
{
var projectFactory = new ProjectFactory(_msbuildDirectory, _msbuildAssembly, _frameworkAssembly, project);
var projectFactory = new ProjectFactory(_msbuildDirectory, _msbuildAssembly, _frameworkAssembly, project, EnvironmentVariableWrapper.Instance);
projectFactory.Build = Build;
projectFactory.ProjectProperties = ProjectProperties;
projectFactory.SymbolPackageFormat = SymbolPackageFormat;
Expand Down Expand Up @@ -1175,7 +1197,7 @@ private void AddSolutionDir()
// Add a path separator for Visual Studio macro compatibility
solutionDir += Path.DirectorySeparatorChar;

if (!String.IsNullOrEmpty(solutionDir))
if (!string.IsNullOrEmpty(solutionDir))
{
if (ProjectProperties.ContainsKey("SolutionDir"))
{
Expand All @@ -1202,7 +1224,7 @@ private Packaging.Manifest ProcessNuspec(Packaging.PackageBuilder builder, strin
{
string nuspecFile = GetNuspec();

if (String.IsNullOrEmpty(nuspecFile))
if (string.IsNullOrEmpty(nuspecFile))
{
return null;
}
Expand All @@ -1221,7 +1243,7 @@ private Packaging.Manifest ProcessNuspec(Packaging.PackageBuilder builder, strin

if (manifest.HasFilesNode)
{
basePath = String.IsNullOrEmpty(basePath) ? Path.GetDirectoryName(nuspecFile) : basePath;
basePath = string.IsNullOrEmpty(basePath) ? Path.GetDirectoryName(nuspecFile) : basePath;
builder.PopulateFiles(basePath, manifest.Files);
}

Expand Down Expand Up @@ -1280,7 +1302,7 @@ private void AddFiles(Packaging.PackageBuilder builder, string itemType, string
foreach (var item in _project.GetItems(itemType))
{
string fullPath = item.GetMetadataValue("FullPath");
if (_excludeFiles.Contains(Path.GetFileName(fullPath)))
if (ExcludeFiles.Contains(Path.GetFileName(fullPath)))
{
continue;
}
Expand Down
13 changes: 12 additions & 1 deletion src/NuGet.Clients/NuGet.CommandLine/NuGetResources.Designer.cs

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

6 changes: 6 additions & 0 deletions src/NuGet.Clients/NuGet.CommandLine/NuGetResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5427,4 +5427,10 @@ Oluşturma sırasında NuGet'in paketleri indirmesini önlemek için, Visual Stu
<data name="ArgumentNullOrEmpty" xml:space="preserve">
<value>Argument cannot be null or empty.</value>
</data>
<data name="Error_ProjectJson_Deprecated_And_Removed" xml:space="preserve">
<value>`project.json` pack is disabled in the current NuGet version, and will be permanently removed in a future version.
Please consider migrating '{0}' to `PackageReference` and using the pack targets.
You can set the '{1}' environment variable to 'true' to temporarily reenable this functionality.</value>
<comment>Please do not localize `project.json` pack and `PackageReference`. 0 - path, 1 - env var name</comment>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,6 @@ private async Task<PackageSpec> GetPackageSpecAsync(ISettings settings)
{
Name = projectName,
Version = new NuGetVersion(_vsProjectAdapter.Version),
Authors = Array.Empty<string>(),
Owners = Array.Empty<string>(),
Tags = Array.Empty<string>(),
ContentFiles = Array.Empty<string>(),
FilePath = _projectFullPath,
RuntimeGraph = runtimeGraph,
RestoreMetadata = new ProjectRestoreMetadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ private void InitCommonPackageBuilderProperties(PackageBuilder builder)
ExcludeFiles(builder.Files);
}

[Obsolete]
public static bool ProcessProjectJsonFile(
PackageBuilder builder,
string basePath,
Expand All @@ -348,6 +349,7 @@ public static bool ProcessProjectJsonFile(
return false;
}

[Obsolete]
private static void LoadProjectJsonFile(
PackageBuilder builder,
string path,
Expand Down
5 changes: 5 additions & 0 deletions src/NuGet.Core/NuGet.Common/Errors/NuGetLogCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,11 @@ public enum NuGetLogCode
/// </summary>
NU5041 = 5041,

/// <summary>
/// Error_ProjectJsonPack_Deprecated_And_Disabled
/// </summary>
NU5042 = 5042,

/// <summary>
/// Invalid icon extension error
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
NuGet.Common.NuGetLogCode.NU1703 = 1703 -> NuGet.Common.NuGetLogCode
NuGet.Common.NuGetLogCode.NU5042 = 5042 -> NuGet.Common.NuGetLogCode
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
NuGet.Common.NuGetLogCode.NU1703 = 1703 -> NuGet.Common.NuGetLogCode
NuGet.Common.NuGetLogCode.NU5042 = 5042 -> NuGet.Common.NuGetLogCode
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
NuGet.Common.NuGetLogCode.NU1703 = 1703 -> NuGet.Common.NuGetLogCode
NuGet.Common.NuGetLogCode.NU5042 = 5042 -> NuGet.Common.NuGetLogCode
Loading