Skip to content

Commit 15dafc4

Browse files
authored
Enabling nullability in NuGet.Packaging - Package Creation (#7217)
1 parent 15db684 commit 15dafc4

51 files changed

Lines changed: 899 additions & 1157 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/NuGet.Core/NuGet.Commands/RestoreCommand/DependencyGraphResolver.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,10 @@ .. chosenResolvedItem.Suppressions
12921292
// The list of suppressions should be an aggregate of all parent item's suppressions so add the parent suppressions to the list, otherwise just use the current item's suppressions
12931293
if (suppressions != null)
12941294
{
1295-
suppressions.AddRange(currentDependencyGraphItem.Suppressions);
1295+
if (currentDependencyGraphItem.Suppressions != null)
1296+
{
1297+
suppressions.AddRange(currentDependencyGraphItem.Suppressions);
1298+
}
12961299
}
12971300
else
12981301
{

src/NuGet.Core/NuGet.Packaging/Core/PackageIdentity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public PackageIdentity(string id, NuGetVersion? version)
4040
/// Package Version
4141
/// </summary>
4242
/// <remarks>can be null</remarks>
43-
/// <remarks>This property is annotated as not nullable intentionally.
43+
/// <remarks>NU_NULL_INC: This property is annotated as not nullable intentionally.
4444
/// The null is used in very few scenarios and practically illegal in cases dealing with an actual package identity. </remarks>
4545
public NuGetVersion Version
4646
{

src/NuGet.Core/NuGet.Packaging/NupkgMetadata/NupkgMetadataFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class NupkgMetadataFile : IEquatable<NupkgMetadataFile>
1010
{
1111
public int Version { get; set; } = NupkgMetadataFileFormat.Version;
1212

13-
public string? ContentHash { get; set; }
13+
public required string? ContentHash { get; set; }
1414

1515
public string? Source { get; set; }
1616

src/NuGet.Core/NuGet.Packaging/NuspecReader.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,9 @@ public RepositoryMetadata GetRepositoryMetadata()
437437

438438
if (licenseNode != null)
439439
{
440-
var type = licenseNode.Attribute(NuspecUtility.Type)?.Value.SafeTrim();
441-
var license = licenseNode.Value.SafeTrim();
442-
var versionValue = licenseNode.Attribute(NuspecUtility.Version)?.Value.SafeTrim();
440+
var type = licenseNode.Attribute(NuspecUtility.Type)?.Value?.Trim();
441+
var license = licenseNode.Value?.Trim();
442+
var versionValue = licenseNode.Attribute(NuspecUtility.Version)?.Value?.Trim();
443443

444444
var isKnownType = Enum.TryParse(type, ignoreCase: true, result: out LicenseType licenseType);
445445

@@ -482,7 +482,7 @@ public RepositoryMetadata GetRepositoryMetadata()
482482
{
483483
try
484484
{
485-
var expression = NuGetLicenseExpression.Parse(license);
485+
var expression = NuGetLicenseExpression.Parse(license!);
486486

487487
var invalidLicenseIdentifiers = GetNonStandardLicenseIdentifiers(expression);
488488
if (invalidLicenseIdentifiers != null)
@@ -502,7 +502,7 @@ public RepositoryMetadata GetRepositoryMetadata()
502502
errors.Add(string.Format(CultureInfo.CurrentCulture, Strings.NuGetLicenseExpression_UnlicensedPackageWarning));
503503
}
504504

505-
return new LicenseMetadata(type: licenseType, license: license, expression: expression, warningsAndErrors: errors, version: version);
505+
return new LicenseMetadata(type: licenseType, license: license!, expression: expression, warningsAndErrors: errors, version: version);
506506
}
507507
catch (NuGetLicenseExpressionParsingException e)
508508
{
@@ -512,7 +512,7 @@ public RepositoryMetadata GetRepositoryMetadata()
512512
}
513513
errors.Add(e.Message);
514514
}
515-
return new LicenseMetadata(type: licenseType, license: license, expression: null, warningsAndErrors: errors, version: version);
515+
return new LicenseMetadata(type: licenseType, license: license!, expression: null, warningsAndErrors: errors, version: version);
516516
}
517517
else
518518
{
@@ -528,11 +528,11 @@ public RepositoryMetadata GetRepositoryMetadata()
528528
version,
529529
LicenseMetadata.CurrentVersion));
530530

531-
return new LicenseMetadata(type: licenseType, license: license, expression: null, warningsAndErrors: errors, version: version);
531+
return new LicenseMetadata(type: licenseType, license: license!, expression: null, warningsAndErrors: errors, version: version);
532532
}
533533
}
534534
}
535-
return new LicenseMetadata(type: licenseType, license: license, expression: null, warningsAndErrors: errors, version: version);
535+
return new LicenseMetadata(type: licenseType, license: license!, expression: null, warningsAndErrors: errors, version: version);
536536
}
537537
}
538538
return null;
@@ -642,11 +642,8 @@ private static List<string> GetFlags(string? flags)
642642
// PERF: Avoid Linq on hot paths
643643
var splitFlags = flags!.Split(CommaArray, StringSplitOptions.RemoveEmptyEntries);
644644

645-
#if NETSTANDARD2_0
646-
var set = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
647-
#elif NET472_OR_GREATER || NET5_0_OR_GREATER
648645
var set = new HashSet<string>(splitFlags.Length, StringComparer.OrdinalIgnoreCase);
649-
#endif
646+
650647
foreach (string flag in splitFlags)
651648
{
652649
set.Add(flag.Trim());

src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/EmptyFrameworkFolderFile.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.IO;
67

78
namespace NuGet.Packaging
@@ -12,6 +13,7 @@ namespace NuGet.Packaging
1213
/// </summary>
1314
internal sealed class EmptyFrameworkFolderFile : PhysicalPackageFile
1415
{
16+
[SetsRequiredMembers]
1517
public EmptyFrameworkFolderFile(string directoryPathInPackage) :
1618
base(() => Stream.Null)
1719
{

src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/FrameworkAssemblyReference.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using NuGet.Frameworks;

src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/IPackageFile.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Diagnostics.CodeAnalysis;
86
using System.IO;
@@ -30,7 +28,7 @@ string Path
3028
///
3129
/// If it is 'tools\init.ps1', the EffectivePath will be 'init.ps1'.
3230
/// </example>
33-
string EffectivePath
31+
string? EffectivePath
3432
{
3533
get;
3634
}
@@ -39,15 +37,15 @@ string EffectivePath
3937
/// FrameworkName object representing this package file's target framework. Deprecated. Must be null on net5.0 and greater.
4038
/// </summary>
4139
[Obsolete("Use NuGetFramework instead. This property will be null for any frameworks net5.0 or above.")]
42-
FrameworkName TargetFramework
40+
FrameworkName? TargetFramework
4341
{
4442
get;
4543
}
4644

4745
/// <summary>
4846
/// NuGetFramework object representing this package file's target framework. Use this instead of TargetFramework.
4947
/// </summary>
50-
NuGetFramework NuGetFramework
48+
NuGetFramework? NuGetFramework
5149
{
5250
get;
5351
}

src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/IPackageMetadata.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using NuGet.Packaging.Core;
@@ -12,26 +10,26 @@ namespace NuGet.Packaging
1210
{
1311
public interface IPackageMetadata
1412
{
15-
string Id { get; }
16-
NuGetVersion Version { get; }
13+
string? Id { get; }
14+
NuGetVersion? Version { get; }
1715

18-
string Title { get; }
16+
string? Title { get; }
1917
IEnumerable<string> Authors { get; }
2018
IEnumerable<string> Owners { get; }
21-
Uri IconUrl { get; }
22-
Uri LicenseUrl { get; }
23-
Uri ProjectUrl { get; }
19+
Uri? IconUrl { get; }
20+
Uri? LicenseUrl { get; }
21+
Uri? ProjectUrl { get; }
2422
bool RequireLicenseAcceptance { get; }
2523
bool DevelopmentDependency { get; }
26-
string Description { get; }
27-
string Summary { get; }
28-
string ReleaseNotes { get; }
29-
string Language { get; }
30-
string Tags { get; }
24+
string? Description { get; }
25+
string? Summary { get; }
26+
string? ReleaseNotes { get; }
27+
string? Language { get; }
28+
string? Tags { get; }
3129
bool Serviceable { get; }
32-
string Copyright { get; }
33-
string Icon { get; }
34-
string Readme { get; }
30+
string? Copyright { get; }
31+
string? Icon { get; }
32+
string? Readme { get; }
3533

3634
/// <summary>
3735
/// Specifies assemblies from GAC that the package depends on.
@@ -48,7 +46,7 @@ public interface IPackageMetadata
4846
/// </summary>
4947
IEnumerable<PackageDependencyGroup> DependencyGroups { get; }
5048

51-
Version MinClientVersion { get; }
49+
Version? MinClientVersion { get; }
5250

5351
/// <summary>
5452
/// Returns sets of Content Files specified in the manifest.
@@ -57,9 +55,9 @@ public interface IPackageMetadata
5755

5856
IEnumerable<PackageType> PackageTypes { get; }
5957

60-
RepositoryMetadata Repository { get; }
58+
RepositoryMetadata? Repository { get; }
6159

62-
LicenseMetadata LicenseMetadata { get; }
60+
LicenseMetadata? LicenseMetadata { get; }
6361

6462
IEnumerable<FrameworkReferenceGroup> FrameworkReferenceGroups { get; }
6563
}

src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/LicenseMetadata.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public override int GetHashCode()
9191
return combiner.CombinedHash;
9292
}
9393

94-
public Uri? LicenseUrl
94+
public Uri LicenseUrl
9595
{
9696
get
9797
{
@@ -104,7 +104,7 @@ public Uri? LicenseUrl
104104
return new Uri(GenerateLicenseServiceLink(License));
105105

106106
default:
107-
return null;
107+
throw new NotSupportedException($"Unsupported license type: {Type}");
108108
}
109109
}
110110
}

src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/Manifest.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using System.Globalization;
@@ -30,7 +28,7 @@ public Manifest(ManifestMetadata metadata)
3028
{
3129
}
3230

33-
public Manifest(ManifestMetadata metadata, ICollection<ManifestFile> files)
31+
public Manifest(ManifestMetadata metadata, ICollection<ManifestFile>? files)
3432
{
3533
if (metadata == null)
3634
{
@@ -105,7 +103,7 @@ public void Save(Stream stream, int minimumManifestVersion, bool generateBackwar
105103
Files.Any() ?
106104
new XElement(schemaNamespace + "files",
107105
Files.Select(file => new XElement(schemaNamespace + "file",
108-
new XAttribute("src", file.Source),
106+
new XAttribute("src", file.Source!),
109107
file.Target != null ? new XAttribute("target", file.Target) : null,
110108
file.Exclude != null ? new XAttribute("exclude", file.Exclude) : null))) : null)).Save(stream);
111109
}
@@ -115,12 +113,12 @@ public static Manifest ReadFrom(Stream stream, bool validateSchema)
115113
return ReadFrom(stream, null, validateSchema);
116114
}
117115

118-
public static Manifest ReadFrom(Stream stream, Func<string, string> propertyProvider, bool validateSchema)
116+
public static Manifest ReadFrom(Stream stream, Func<string, string>? propertyProvider, bool validateSchema)
119117
{
120118
return ReadFrom(stream, propertyProvider, validateSchema, overrideVersion: null);
121119
}
122120

123-
public static Manifest ReadFrom(Stream stream, Func<string, string> propertyProvider, bool validateSchema, NuGetVersion overrideVersion)
121+
public static Manifest ReadFrom(Stream stream, Func<string, string>? propertyProvider, bool validateSchema, NuGetVersion? overrideVersion)
124122
{
125123
XDocument document;
126124
if (propertyProvider == null)
@@ -166,7 +164,7 @@ public static Manifest ReadFrom(Stream stream, Func<string, string> propertyProv
166164
private static string GetSchemaNamespace(XDocument document)
167165
{
168166
string schemaNamespace = ManifestSchemaUtility.SchemaVersionV1;
169-
var rootNameSpace = document.Root.Name.Namespace;
167+
var rootNameSpace = document.Root!.Name.Namespace;
170168
if (rootNameSpace != null && !String.IsNullOrEmpty(rootNameSpace.NamespaceName))
171169
{
172170
schemaNamespace = rootNameSpace.NamespaceName;
@@ -227,7 +225,7 @@ private static void CheckSchemaVersion(XDocument document)
227225
}
228226

229227
// Get the package id from the metadata node
230-
string packageId = GetPackageId(metadata);
228+
string? packageId = GetPackageId(metadata);
231229

232230
// If the schema of the document doesn't match any of our known schemas
233231
if (!ManifestSchemaUtility.IsKnownSchema(document.Root.Name.Namespace.NamespaceName))
@@ -243,7 +241,7 @@ private static void CheckSchemaVersion(XDocument document)
243241
}
244242

245243
#if !IS_CORECLR
246-
private static string GetPackageId(XElement metadataElement)
244+
private static string? GetPackageId(XElement metadataElement)
247245
{
248246
XName idName = XName.Get("id", metadataElement.Document.Root.Name.NamespaceName);
249247
XElement element = metadataElement.Element(idName);

0 commit comments

Comments
 (0)