diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs b/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs index 332e5066583f2..f7ae0c80486ee 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs @@ -13,9 +13,9 @@ public partial struct HashCodeCombiner private int _dummyPrimitive; public int CombinedHash { get { throw null; } } public void Add(int i) { } - public void Add(object o) { } - public void Add(string s) { } - public void Add(TValue value, System.Collections.Generic.IEqualityComparer comparer) { } + public void Add(object? o) { } + public void Add(string? s) { } + public void Add(TValue? value, System.Collections.Generic.IEqualityComparer comparer) { } public static Microsoft.DotNet.PlatformAbstractions.HashCodeCombiner Start() { throw null; } } } @@ -23,30 +23,30 @@ namespace Microsoft.Extensions.DependencyModel { public partial class CompilationLibrary : Microsoft.Extensions.DependencyModel.Library { - public CompilationLibrary(string type, string name, string version, string hash, System.Collections.Generic.IEnumerable assemblies, System.Collections.Generic.IEnumerable dependencies, bool serviceable) : base (default(string), default(string), default(string), default(string), default(System.Collections.Generic.IEnumerable), default(bool)) { } - public CompilationLibrary(string type, string name, string version, string hash, System.Collections.Generic.IEnumerable assemblies, System.Collections.Generic.IEnumerable dependencies, bool serviceable, string path, string hashPath) : base (default(string), default(string), default(string), default(string), default(System.Collections.Generic.IEnumerable), default(bool)) { } + public CompilationLibrary(string type, string name, string version, string? hash, System.Collections.Generic.IEnumerable assemblies, System.Collections.Generic.IEnumerable dependencies, bool serviceable) : base (default(string), default(string), default(string), default(string), default(System.Collections.Generic.IEnumerable), default(bool)) { } + public CompilationLibrary(string type, string name, string version, string? hash, System.Collections.Generic.IEnumerable assemblies, System.Collections.Generic.IEnumerable dependencies, bool serviceable, string? path, string? hashPath) : base (default(string), default(string), default(string), default(string), default(System.Collections.Generic.IEnumerable), default(bool)) { } public System.Collections.Generic.IReadOnlyList Assemblies { get { throw null; } } public System.Collections.Generic.IEnumerable ResolveReferencePaths() { throw null; } public System.Collections.Generic.IEnumerable ResolveReferencePaths(params Microsoft.Extensions.DependencyModel.Resolution.ICompilationAssemblyResolver[] customResolvers) { throw null; } } public partial class CompilationOptions { - public CompilationOptions(System.Collections.Generic.IEnumerable defines, string languageVersion, string platform, bool? allowUnsafe, bool? warningsAsErrors, bool? optimize, string keyFile, bool? delaySign, bool? publicSign, string debugType, bool? emitEntryPoint, bool? generateXmlDocumentation) { } + public CompilationOptions(System.Collections.Generic.IEnumerable defines, string? languageVersion, string? platform, bool? allowUnsafe, bool? warningsAsErrors, bool? optimize, string? keyFile, bool? delaySign, bool? publicSign, string? debugType, bool? emitEntryPoint, bool? generateXmlDocumentation) { } public bool? AllowUnsafe { get { throw null; } } - public string DebugType { get { throw null; } } + public string? DebugType { get { throw null; } } public static Microsoft.Extensions.DependencyModel.CompilationOptions Default { get { throw null; } } - public System.Collections.Generic.IReadOnlyList Defines { get { throw null; } } + public System.Collections.Generic.IReadOnlyList Defines { get { throw null; } } public bool? DelaySign { get { throw null; } } public bool? EmitEntryPoint { get { throw null; } } public bool? GenerateXmlDocumentation { get { throw null; } } - public string KeyFile { get { throw null; } } - public string LanguageVersion { get { throw null; } } + public string? KeyFile { get { throw null; } } + public string? LanguageVersion { get { throw null; } } public bool? Optimize { get { throw null; } } - public string Platform { get { throw null; } } + public string? Platform { get { throw null; } } public bool? PublicSign { get { throw null; } } public bool? WarningsAsErrors { get { throw null; } } } - public readonly partial struct Dependency : System.IEquatable + public readonly partial struct Dependency : System.IEquatable { private readonly object _dummy; private readonly int _dummyPrimitive; @@ -54,7 +54,7 @@ public CompilationOptions(System.Collections.Generic.IEnumerable defines public readonly string Name { get { throw null; } } public readonly string Version { get { throw null; } } public bool Equals(Microsoft.Extensions.DependencyModel.Dependency other) { throw null; } - public override bool Equals(object obj) { throw null; } + public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; } public override int GetHashCode() { throw null; } } public partial class DependencyContext @@ -63,12 +63,12 @@ public DependencyContext(Microsoft.Extensions.DependencyModel.TargetInfo target, public Microsoft.Extensions.DependencyModel.CompilationOptions CompilationOptions { get { throw null; } } public System.Collections.Generic.IReadOnlyList CompileLibraries { get { throw null; } } [System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")] - public static Microsoft.Extensions.DependencyModel.DependencyContext Default { get { throw null; } } + public static Microsoft.Extensions.DependencyModel.DependencyContext? Default { get { throw null; } } public System.Collections.Generic.IReadOnlyList RuntimeGraph { get { throw null; } } public System.Collections.Generic.IReadOnlyList RuntimeLibraries { get { throw null; } } public Microsoft.Extensions.DependencyModel.TargetInfo Target { get { throw null; } } [System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")] - public static Microsoft.Extensions.DependencyModel.DependencyContext Load(System.Reflection.Assembly assembly) { throw null; } + public static Microsoft.Extensions.DependencyModel.DependencyContext? Load(System.Reflection.Assembly assembly) { throw null; } public Microsoft.Extensions.DependencyModel.DependencyContext Merge(Microsoft.Extensions.DependencyModel.DependencyContext other) { throw null; } } public static partial class DependencyContextExtensions @@ -98,7 +98,7 @@ public partial class DependencyContextLoader public DependencyContextLoader() { } public static Microsoft.Extensions.DependencyModel.DependencyContextLoader Default { get { throw null; } } [System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")] - public Microsoft.Extensions.DependencyModel.DependencyContext Load(System.Reflection.Assembly assembly) { throw null; } + public Microsoft.Extensions.DependencyModel.DependencyContext? Load(System.Reflection.Assembly assembly) { throw null; } } public partial class DependencyContextWriter { @@ -111,15 +111,15 @@ public partial interface IDependencyContextReader : System.IDisposable } public partial class Library { - public Library(string type, string name, string version, string hash, System.Collections.Generic.IEnumerable dependencies, bool serviceable) { } - public Library(string type, string name, string version, string hash, System.Collections.Generic.IEnumerable dependencies, bool serviceable, string path, string hashPath) { } - public Library(string type, string name, string version, string hash, System.Collections.Generic.IEnumerable dependencies, bool serviceable, string path, string hashPath, string runtimeStoreManifestName = null) { } + public Library(string type, string name, string version, string? hash, System.Collections.Generic.IEnumerable dependencies, bool serviceable) { } + public Library(string type, string name, string version, string? hash, System.Collections.Generic.IEnumerable dependencies, bool serviceable, string? path, string? hashPath) { } + public Library(string type, string name, string version, string? hash, System.Collections.Generic.IEnumerable dependencies, bool serviceable, string? path, string? hashPath, string? runtimeStoreManifestName = null) { } public System.Collections.Generic.IReadOnlyList Dependencies { get { throw null; } } - public string Hash { get { throw null; } } - public string HashPath { get { throw null; } } + public string? Hash { get { throw null; } } + public string? HashPath { get { throw null; } } public string Name { get { throw null; } } - public string Path { get { throw null; } } - public string RuntimeStoreManifestName { get { throw null; } } + public string? Path { get { throw null; } } + public string? RuntimeStoreManifestName { get { throw null; } } public bool Serviceable { get { throw null; } } public string Type { get { throw null; } } public string Version { get { throw null; } } @@ -139,43 +139,43 @@ public RuntimeAssembly(string assemblyName, string path) { } } public partial class RuntimeAssetGroup { - public RuntimeAssetGroup(string runtime, System.Collections.Generic.IEnumerable runtimeFiles) { } - public RuntimeAssetGroup(string runtime, System.Collections.Generic.IEnumerable assetPaths) { } - public RuntimeAssetGroup(string runtime, params string[] assetPaths) { } + public RuntimeAssetGroup(string? runtime, System.Collections.Generic.IEnumerable runtimeFiles) { } + public RuntimeAssetGroup(string? runtime, System.Collections.Generic.IEnumerable assetPaths) { } + public RuntimeAssetGroup(string? runtime, params string[] assetPaths) { } public System.Collections.Generic.IReadOnlyList AssetPaths { get { throw null; } } - public string Runtime { get { throw null; } } + public string? Runtime { get { throw null; } } public System.Collections.Generic.IReadOnlyList RuntimeFiles { get { throw null; } } } public partial class RuntimeFallbacks { - public RuntimeFallbacks(string runtime, System.Collections.Generic.IEnumerable fallbacks) { } - public RuntimeFallbacks(string runtime, params string[] fallbacks) { } - public System.Collections.Generic.IReadOnlyList Fallbacks { get { throw null; } set { } } + public RuntimeFallbacks(string runtime, System.Collections.Generic.IEnumerable fallbacks) { } + public RuntimeFallbacks(string runtime, params string?[] fallbacks) { } + public System.Collections.Generic.IReadOnlyList Fallbacks { get { throw null; } set { } } public string Runtime { get { throw null; } set { } } } public partial class RuntimeFile { - public RuntimeFile(string path, string assemblyVersion, string fileVersion) { } - public string AssemblyVersion { get { throw null; } } - public string FileVersion { get { throw null; } } + public RuntimeFile(string path, string? assemblyVersion, string? fileVersion) { } + public string? AssemblyVersion { get { throw null; } } + public string? FileVersion { get { throw null; } } public string Path { get { throw null; } } } public partial class RuntimeLibrary : Microsoft.Extensions.DependencyModel.Library { - public RuntimeLibrary(string type, string name, string version, string hash, System.Collections.Generic.IReadOnlyList runtimeAssemblyGroups, System.Collections.Generic.IReadOnlyList nativeLibraryGroups, System.Collections.Generic.IEnumerable resourceAssemblies, System.Collections.Generic.IEnumerable dependencies, bool serviceable) : base (default(string), default(string), default(string), default(string), default(System.Collections.Generic.IEnumerable), default(bool)) { } - public RuntimeLibrary(string type, string name, string version, string hash, System.Collections.Generic.IReadOnlyList runtimeAssemblyGroups, System.Collections.Generic.IReadOnlyList nativeLibraryGroups, System.Collections.Generic.IEnumerable resourceAssemblies, System.Collections.Generic.IEnumerable dependencies, bool serviceable, string path, string hashPath) : base (default(string), default(string), default(string), default(string), default(System.Collections.Generic.IEnumerable), default(bool)) { } - public RuntimeLibrary(string type, string name, string version, string hash, System.Collections.Generic.IReadOnlyList runtimeAssemblyGroups, System.Collections.Generic.IReadOnlyList nativeLibraryGroups, System.Collections.Generic.IEnumerable resourceAssemblies, System.Collections.Generic.IEnumerable dependencies, bool serviceable, string path, string hashPath, string runtimeStoreManifestName) : base (default(string), default(string), default(string), default(string), default(System.Collections.Generic.IEnumerable), default(bool)) { } + public RuntimeLibrary(string type, string name, string version, string? hash, System.Collections.Generic.IReadOnlyList runtimeAssemblyGroups, System.Collections.Generic.IReadOnlyList nativeLibraryGroups, System.Collections.Generic.IEnumerable resourceAssemblies, System.Collections.Generic.IEnumerable dependencies, bool serviceable) : base (default(string), default(string), default(string), default(string), default(System.Collections.Generic.IEnumerable), default(bool)) { } + public RuntimeLibrary(string type, string name, string version, string? hash, System.Collections.Generic.IReadOnlyList runtimeAssemblyGroups, System.Collections.Generic.IReadOnlyList nativeLibraryGroups, System.Collections.Generic.IEnumerable resourceAssemblies, System.Collections.Generic.IEnumerable dependencies, bool serviceable, string? path, string? hashPath) : base (default(string), default(string), default(string), default(string), default(System.Collections.Generic.IEnumerable), default(bool)) { } + public RuntimeLibrary(string type, string name, string version, string? hash, System.Collections.Generic.IReadOnlyList runtimeAssemblyGroups, System.Collections.Generic.IReadOnlyList nativeLibraryGroups, System.Collections.Generic.IEnumerable resourceAssemblies, System.Collections.Generic.IEnumerable dependencies, bool serviceable, string? path, string? hashPath, string? runtimeStoreManifestName) : base (default(string), default(string), default(string), default(string), default(System.Collections.Generic.IEnumerable), default(bool)) { } public System.Collections.Generic.IReadOnlyList NativeLibraryGroups { get { throw null; } } public System.Collections.Generic.IReadOnlyList ResourceAssemblies { get { throw null; } } public System.Collections.Generic.IReadOnlyList RuntimeAssemblyGroups { get { throw null; } } } public partial class TargetInfo { - public TargetInfo(string framework, string runtime, string runtimeSignature, bool isPortable) { } + public TargetInfo(string framework, string? runtime, string? runtimeSignature, bool isPortable) { } public string Framework { get { throw null; } } public bool IsPortable { get { throw null; } } - public string Runtime { get { throw null; } } - public string RuntimeSignature { get { throw null; } } + public string? Runtime { get { throw null; } } + public string? RuntimeSignature { get { throw null; } } } } namespace Microsoft.Extensions.DependencyModel.Resolution @@ -184,34 +184,34 @@ public partial class AppBaseCompilationAssemblyResolver : Microsoft.Extensions.D { public AppBaseCompilationAssemblyResolver() { } public AppBaseCompilationAssemblyResolver(string basePath) { } - public bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List assemblies) { throw null; } + public bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List? assemblies) { throw null; } } public partial class CompositeCompilationAssemblyResolver : Microsoft.Extensions.DependencyModel.Resolution.ICompilationAssemblyResolver { public CompositeCompilationAssemblyResolver(Microsoft.Extensions.DependencyModel.Resolution.ICompilationAssemblyResolver[] resolvers) { } - public bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List assemblies) { throw null; } + public bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List? assemblies) { throw null; } } public partial class DotNetReferenceAssembliesPathResolver { public static readonly string DotNetReferenceAssembliesPathEnv; public DotNetReferenceAssembliesPathResolver() { } - public static string Resolve() { throw null; } + public static string? Resolve() { throw null; } } public partial interface ICompilationAssemblyResolver { - bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List assemblies); + bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List? assemblies); } public partial class PackageCompilationAssemblyResolver : Microsoft.Extensions.DependencyModel.Resolution.ICompilationAssemblyResolver { public PackageCompilationAssemblyResolver() { } public PackageCompilationAssemblyResolver(string nugetPackageDirectory) { } - public bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List assemblies) { throw null; } + public bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List? assemblies) { throw null; } } public partial class ReferenceAssemblyPathResolver : Microsoft.Extensions.DependencyModel.Resolution.ICompilationAssemblyResolver { public ReferenceAssemblyPathResolver() { } - public ReferenceAssemblyPathResolver(string defaultReferenceAssembliesPath, string[] fallbackSearchPaths) { } - public bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List assemblies) { throw null; } + public ReferenceAssemblyPathResolver(string? defaultReferenceAssembliesPath, string[] fallbackSearchPaths) { } + public bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List? assemblies) { throw null; } } } namespace System.Collections.Generic @@ -219,10 +219,10 @@ namespace System.Collections.Generic public static partial class CollectionExtensions { public static System.Collections.Generic.IEnumerable GetDefaultAssets(this System.Collections.Generic.IEnumerable self) { throw null; } - public static Microsoft.Extensions.DependencyModel.RuntimeAssetGroup GetDefaultGroup(this System.Collections.Generic.IEnumerable self) { throw null; } + public static Microsoft.Extensions.DependencyModel.RuntimeAssetGroup? GetDefaultGroup(this System.Collections.Generic.IEnumerable self) { throw null; } public static System.Collections.Generic.IEnumerable GetDefaultRuntimeFileAssets(this System.Collections.Generic.IEnumerable self) { throw null; } public static System.Collections.Generic.IEnumerable GetRuntimeAssets(this System.Collections.Generic.IEnumerable self, string runtime) { throw null; } public static System.Collections.Generic.IEnumerable GetRuntimeFileAssets(this System.Collections.Generic.IEnumerable self, string runtime) { throw null; } - public static Microsoft.Extensions.DependencyModel.RuntimeAssetGroup GetRuntimeGroup(this System.Collections.Generic.IEnumerable self, string runtime) { throw null; } + public static Microsoft.Extensions.DependencyModel.RuntimeAssetGroup? GetRuntimeGroup(this System.Collections.Generic.IEnumerable self, string runtime) { throw null; } } } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.csproj b/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.csproj index 2901720d3fe1f..107d05b171bfd 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.csproj @@ -1,9 +1,16 @@ - netstandard2.0;net461 + $(NetCoreAppCurrent);netstandard2.0;net461 + enable + + + + + + diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/CollectionExtensions.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/CollectionExtensions.cs index 9804f4a85ae8f..b00bf18b896d9 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/CollectionExtensions.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/CollectionExtensions.cs @@ -8,9 +8,9 @@ namespace System.Collections.Generic { public static class CollectionExtensions { - public static RuntimeAssetGroup GetDefaultGroup(this IEnumerable self) => GetGroup(self, string.Empty); + public static RuntimeAssetGroup? GetDefaultGroup(this IEnumerable self) => GetGroup(self, string.Empty); - public static RuntimeAssetGroup GetRuntimeGroup(this IEnumerable self, string runtime) + public static RuntimeAssetGroup? GetRuntimeGroup(this IEnumerable self, string runtime) { if (string.IsNullOrEmpty(runtime)) { @@ -19,7 +19,7 @@ public static RuntimeAssetGroup GetRuntimeGroup(this IEnumerable groups, string runtime) + private static RuntimeAssetGroup? GetGroup(IEnumerable groups, string runtime) { return groups.FirstOrDefault(g => g.Runtime == runtime); } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationLibrary.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationLibrary.cs index 604f334d35d29..fd49bff68e1a9 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationLibrary.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationLibrary.cs @@ -13,7 +13,7 @@ public class CompilationLibrary : Library public CompilationLibrary(string type, string name, string version, - string hash, + string? hash, IEnumerable assemblies, IEnumerable dependencies, bool serviceable) @@ -24,12 +24,12 @@ public CompilationLibrary(string type, public CompilationLibrary(string type, string name, string version, - string hash, + string? hash, IEnumerable assemblies, IEnumerable dependencies, bool serviceable, - string path, - string hashPath) + string? path, + string? hashPath) : base(type, name, version, hash, dependencies, serviceable, path, hashPath) { if (assemblies == null) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationOptions.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationOptions.cs index 8c2b2af5b5f74..676e8a3a88d4f 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationOptions.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationOptions.cs @@ -9,11 +9,11 @@ namespace Microsoft.Extensions.DependencyModel { public class CompilationOptions { - public IReadOnlyList Defines { get; } + public IReadOnlyList Defines { get; } - public string LanguageVersion { get; } + public string? LanguageVersion { get; } - public string Platform { get; } + public string? Platform { get; } public bool? AllowUnsafe { get; } @@ -21,20 +21,20 @@ public class CompilationOptions public bool? Optimize { get; } - public string KeyFile { get; } + public string? KeyFile { get; } public bool? DelaySign { get; } public bool? PublicSign { get; } - public string DebugType { get; } + public string? DebugType { get; } public bool? EmitEntryPoint { get; } public bool? GenerateXmlDocumentation { get; } public static CompilationOptions Default { get; } = new CompilationOptions( - defines: Enumerable.Empty(), + defines: Enumerable.Empty(), languageVersion: null, platform: null, allowUnsafe: null, @@ -47,16 +47,16 @@ public class CompilationOptions emitEntryPoint: null, generateXmlDocumentation: null); - public CompilationOptions(IEnumerable defines, - string languageVersion, - string platform, + public CompilationOptions(IEnumerable defines, + string? languageVersion, + string? platform, bool? allowUnsafe, bool? warningsAsErrors, bool? optimize, - string keyFile, + string? keyFile, bool? delaySign, bool? publicSign, - string debugType, + string? debugType, bool? emitEntryPoint, bool? generateXmlDocumentation) { diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Dependency.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Dependency.cs index 5ce3c29cdd3a7..85b4f0b7e4887 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Dependency.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Dependency.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics.CodeAnalysis; using System.Numerics.Hashing; namespace Microsoft.Extensions.DependencyModel @@ -30,7 +31,7 @@ public bool Equals(Dependency other) return string.Equals(Name, other.Name) && string.Equals(Version, other.Version); } - public override bool Equals(object obj) => obj is Dependency dependency && Equals(dependency); + public override bool Equals([NotNullWhen(true)] object? obj) => obj is Dependency dependency && Equals(dependency); public override int GetHashCode() => HashHelpers.Combine(Name.GetHashCode(), Version.GetHashCode()); diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContext.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContext.cs index 770123f2beb53..39a6d80a5f034 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContext.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContext.cs @@ -14,7 +14,7 @@ public class DependencyContext [UnconditionalSuppressMessage("SingleFile", "IL3002:Avoid calling members marked with 'RequiresAssemblyFilesAttribute' when publishing as a single-file", Justification = "The annotation should be on the static constructor but is Compiler Generated, annotating the caller Default method instead")] - private static readonly Lazy _defaultContext = new Lazy(LoadDefault); + private static readonly Lazy _defaultContext = new(LoadDefault); public DependencyContext(TargetInfo target, CompilationOptions compilationOptions, @@ -51,7 +51,7 @@ public DependencyContext(TargetInfo target, } [RequiresAssemblyFiles("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")] - public static DependencyContext Default => _defaultContext.Value; + public static DependencyContext? Default => _defaultContext.Value; public TargetInfo Target { get; } @@ -80,7 +80,7 @@ public DependencyContext Merge(DependencyContext other) } [RequiresAssemblyFiles("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")] - private static DependencyContext LoadDefault() + private static DependencyContext? LoadDefault() { var entryAssembly = Assembly.GetEntryAssembly(); if (entryAssembly == null) @@ -92,16 +92,16 @@ private static DependencyContext LoadDefault() } [RequiresAssemblyFiles("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")] - public static DependencyContext Load(Assembly assembly) + public static DependencyContext? Load(Assembly assembly) { return DependencyContextLoader.Default.Load(assembly); } private sealed class LibraryMergeEqualityComparer : IEqualityComparer where T : Library { - public bool Equals(T x, T y) + public bool Equals(T? x, T? y) { - return StringComparer.OrdinalIgnoreCase.Equals(x.Name, y.Name); + return StringComparer.OrdinalIgnoreCase.Equals(x?.Name, y?.Name); } public int GetHashCode(T obj) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextExtensions.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextExtensions.cs index 762a3ea173901..ee9f0be63e5b8 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextExtensions.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextExtensions.cs @@ -182,8 +182,8 @@ private static IEnumerable ResolveAssets( string runtimeIdentifier, IEnumerable assets) { - RuntimeFallbacks fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); - IEnumerable rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty()); + RuntimeFallbacks? fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); + IEnumerable rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty()); return SelectAssets(rids, assets); } @@ -192,16 +192,16 @@ private static IEnumerable ResolveRuntimeFiles( string runtimeIdentifier, IEnumerable assets) { - RuntimeFallbacks fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); - IEnumerable rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty()); + RuntimeFallbacks? fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); + IEnumerable rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty()); return SelectRuntimeFiles(rids, assets); } - private static IEnumerable SelectAssets(IEnumerable rids, IEnumerable groups) + private static IEnumerable SelectAssets(IEnumerable rids, IEnumerable groups) { - foreach (string rid in rids) + foreach (string? rid in rids) { - RuntimeAssetGroup group = groups.FirstOrDefault(g => g.Runtime == rid); + RuntimeAssetGroup? group = groups.FirstOrDefault(g => g.Runtime == rid); if (group != null) { return group.AssetPaths; @@ -212,11 +212,11 @@ private static IEnumerable SelectAssets(IEnumerable rids, IEnume return groups.GetDefaultAssets(); } - private static IEnumerable SelectRuntimeFiles(IEnumerable rids, IEnumerable groups) + private static IEnumerable SelectRuntimeFiles(IEnumerable rids, IEnumerable groups) { - foreach (string rid in rids) + foreach (string? rid in rids) { - RuntimeAssetGroup group = groups.FirstOrDefault(g => g.Runtime == rid); + RuntimeAssetGroup? group = groups.FirstOrDefault(g => g.Runtime == rid); if (group != null) { return group.RuntimeFiles; diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs index f356bd15cc844..127e905ff3d3a 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs @@ -5,6 +5,7 @@ using System.Buffers; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Text.Json; @@ -34,7 +35,7 @@ public DependencyContext Read(Stream stream) { // Holds document content, clear it before returning it. buffer.AsSpan().Clear(); - ArrayPool.Shared.Return(buffer.Array); + ArrayPool.Shared.Return(buffer.Array!); } } @@ -55,7 +56,7 @@ public void Dispose() private static ArraySegment ReadToEnd(Stream stream) { int written = 0; - byte[] rented = null; + byte[]? rented = null; ReadOnlySpan utf8Bom = Utf8Bom; @@ -134,13 +135,13 @@ private DependencyContext Read(Utf8JsonReader reader) string runtime = string.Empty; string framework = string.Empty; bool isPortable = true; - string runtimeTargetName = null; - string runtimeSignature = null; + string? runtimeTargetName = null; + string? runtimeSignature = null; - CompilationOptions compilationOptions = null; - List targets = null; - Dictionary libraryStubs = null; - List runtimeFallbacks = null; + CompilationOptions? compilationOptions = null; + List? targets = null; + Dictionary? libraryStubs = null; + List? runtimeFallbacks = null; while (reader.Read() && reader.IsTokenTypeProperty()) { @@ -172,7 +173,7 @@ private DependencyContext Read(Utf8JsonReader reader) compilationOptions = CompilationOptions.Default; } - Target runtimeTarget = SelectRuntimeTarget(targets, runtimeTargetName); + Target? runtimeTarget = SelectRuntimeTarget(targets, runtimeTargetName); runtimeTargetName = runtimeTarget?.Name; if (runtimeTargetName != null) @@ -190,9 +191,9 @@ private DependencyContext Read(Utf8JsonReader reader) } } - Target compileTarget = null; + Target? compileTarget = null; - Target ridlessTarget = targets.FirstOrDefault(t => !IsRuntimeTarget(t.Name)); + Target? ridlessTarget = targets.FirstOrDefault(t => !IsRuntimeTarget(t.Name)); if (ridlessTarget != null) { compileTarget = ridlessTarget; @@ -216,9 +217,9 @@ private DependencyContext Read(Utf8JsonReader reader) runtimeFallbacks ?? Enumerable.Empty()); } - private static Target SelectRuntimeTarget(List targets, string runtimeTargetName) + private static Target? SelectRuntimeTarget([NotNull] List? targets, string? runtimeTargetName) { - Target target; + Target? target; if (targets == null || targets.Count == 0) { @@ -246,14 +247,14 @@ private static bool IsRuntimeTarget(string name) return name.Contains(DependencyContextStrings.VersionSeparator); } - private static void ReadRuntimeTarget(ref Utf8JsonReader reader, out string runtimeTargetName, out string runtimeSignature) + private static void ReadRuntimeTarget(ref Utf8JsonReader reader, out string? runtimeTargetName, out string? runtimeSignature) { runtimeTargetName = null; runtimeSignature = null; reader.ReadStartObject(); - while (reader.TryReadStringProperty(out string propertyName, out string propertyValue)) + while (reader.TryReadStringProperty(out string? propertyName, out string? propertyValue)) { switch (propertyName) { @@ -271,16 +272,16 @@ private static void ReadRuntimeTarget(ref Utf8JsonReader reader, out string runt private static CompilationOptions ReadCompilationOptions(ref Utf8JsonReader reader) { - IEnumerable defines = null; - string languageVersion = null; - string platform = null; + IEnumerable? defines = null; + string? languageVersion = null; + string? platform = null; bool? allowUnsafe = null; bool? warningsAsErrors = null; bool? optimize = null; - string keyFile = null; + string? keyFile = null; bool? delaySign = null; bool? publicSign = null; - string debugType = null; + string? debugType = null; bool? emitEntryPoint = null; bool? generateXmlDocumentation = null; @@ -335,7 +336,7 @@ private static CompilationOptions ReadCompilationOptions(ref Utf8JsonReader read reader.CheckEndObject(); return new CompilationOptions( - defines ?? Enumerable.Empty(), + defines ?? Enumerable.Empty(), languageVersion, platform, allowUnsafe, @@ -357,7 +358,7 @@ private List ReadTargets(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - targets.Add(ReadTarget(ref reader, reader.GetString())); + targets.Add(ReadTarget(ref reader, reader.GetString()!)); } reader.CheckEndObject(); @@ -373,26 +374,22 @@ private Target ReadTarget(ref Utf8JsonReader reader, string targetName) while (reader.Read() && reader.IsTokenTypeProperty()) { - libraries.Add(ReadTargetLibrary(ref reader, reader.GetString())); + libraries.Add(ReadTargetLibrary(ref reader, reader.GetString()!)); } reader.CheckEndObject(); - return new Target() - { - Name = targetName, - Libraries = libraries - }; + return new Target(targetName, libraries); } private TargetLibrary ReadTargetLibrary(ref Utf8JsonReader reader, string targetLibraryName) { - IEnumerable dependencies = null; - List runtimes = null; - List natives = null; - List compilations = null; - List runtimeTargets = null; - List resources = null; + IEnumerable? dependencies = null; + List? runtimes = null; + List? natives = null; + List? compilations = null; + List? runtimeTargets = null; + List? resources = null; bool? compileOnly = null; reader.ReadStartObject(); @@ -449,9 +446,9 @@ private IEnumerable ReadTargetLibraryDependencies(ref Utf8JsonReader reader.ReadStartObject(); - while (reader.TryReadStringProperty(out string name, out string version)) + while (reader.TryReadStringProperty(out string? name, out string? version)) { - dependencies.Add(new Dependency(Pool(name), Pool(version))); + dependencies.Add(new Dependency(Pool(name)!, Pool(version)!)); } reader.CheckEndObject(); @@ -467,7 +464,7 @@ private static List ReadPropertyNames(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string libraryName = reader.GetString(); + string libraryName = reader.GetString()!; reader.Skip(); runtimes.Add(libraryName); @@ -486,14 +483,14 @@ private static List ReadRuntimeFiles(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string assemblyVersion = null; - string fileVersion = null; + string? assemblyVersion = null; + string? fileVersion = null; - string path = reader.GetString(); + string path = reader.GetString()!; reader.ReadStartObject(); - while (reader.TryReadStringProperty(out string propertyName, out string propertyValue)) + while (reader.TryReadStringProperty(out string? propertyName, out string? propertyValue)) { switch (propertyName) { @@ -526,12 +523,12 @@ private List ReadTargetLibraryRuntimeTargets(ref Utf8Jso { var runtimeTarget = new RuntimeTargetEntryStub { - Path = reader.GetString() + Path = reader.GetString()! }; reader.ReadStartObject(); - while (reader.TryReadStringProperty(out string propertyName, out string propertyValue)) + while (reader.TryReadStringProperty(out string? propertyName, out string? propertyValue)) { switch (propertyName) { @@ -568,12 +565,12 @@ private List ReadTargetLibraryResources(ref Utf8JsonReader rea while (reader.Read() && reader.IsTokenTypeProperty()) { - string path = reader.GetString(); - string locale = null; + string path = reader.GetString()!; + string? locale = null; reader.ReadStartObject(); - while (reader.TryReadStringProperty(out string propertyName, out string propertyValue)) + while (reader.TryReadStringProperty(out string? propertyName, out string? propertyValue)) { if (propertyName == DependencyContextStrings.LocalePropertyName) { @@ -602,7 +599,7 @@ private Dictionary ReadLibraries(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string libraryName = reader.GetString(); + string libraryName = reader.GetString()!; libraries.Add(Pool(libraryName), ReadOneLibrary(ref reader)); } @@ -614,12 +611,12 @@ private Dictionary ReadLibraries(ref Utf8JsonReader reader) private LibraryStub ReadOneLibrary(ref Utf8JsonReader reader) { - string hash = null; - string type = null; + string? hash = null; + string? type = null; bool serviceable = false; - string path = null; - string hashPath = null; - string runtimeStoreManifestName = null; + string? path = null; + string? hashPath = null; + string? runtimeStoreManifestName = null; reader.ReadStartObject(); @@ -656,7 +653,7 @@ private LibraryStub ReadOneLibrary(ref Utf8JsonReader reader) return new LibraryStub() { Hash = hash, - Type = Pool(type), + Type = Pool(type)!, Serviceable = serviceable, Path = path, HashPath = hashPath, @@ -672,8 +669,8 @@ private static List ReadRuntimes(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string runtime = reader.GetString(); - string[] fallbacks = reader.ReadStringArray(); + string runtime = reader.GetString()!; + string?[] fallbacks = reader.ReadStringArray(); runtimeFallbacks.Add(new RuntimeFallbacks(runtime, fallbacks)); } @@ -683,7 +680,7 @@ private static List ReadRuntimes(ref Utf8JsonReader reader) return runtimeFallbacks; } - private IEnumerable CreateLibraries(IEnumerable libraries, bool runtime, Dictionary libraryStubs) + private IEnumerable CreateLibraries(IEnumerable? libraries, bool runtime, Dictionary? libraryStubs) { if (libraries == null) { @@ -691,10 +688,10 @@ private IEnumerable CreateLibraries(IEnumerable librarie } return libraries .Select(property => CreateLibrary(property, runtime, libraryStubs)) - .Where(library => library != null); + .Where(library => library != null)!; } - private Library CreateLibrary(TargetLibrary targetLibrary, bool runtime, Dictionary libraryStubs) + private Library? CreateLibrary(TargetLibrary targetLibrary, bool runtime, Dictionary? libraryStubs) { string nameWithVersion = targetLibrary.Name; @@ -721,7 +718,7 @@ private Library CreateLibrary(TargetLibrary targetLibrary, bool runtime, Diction var nativeLibraryGroups = new List(); if (targetLibrary.RuntimeTargets != null) { - foreach (IGrouping ridGroup in targetLibrary.RuntimeTargets.GroupBy(e => e.Rid)) + foreach (IGrouping ridGroup in targetLibrary.RuntimeTargets.GroupBy(e => e.Rid)) { RuntimeFile[] groupRuntimeAssemblies = ridGroup .Where(e => e.Type == DependencyContextStrings.RuntimeAssetType) @@ -789,14 +786,15 @@ private Library CreateLibrary(TargetLibrary targetLibrary, bool runtime, Diction } } - private string Pool(string s) + [return: NotNullIfNotNull("s")] + private string? Pool(string? s) { if (s == null) { return null; } - if (!_stringPool.TryGetValue(s, out string result)) + if (!_stringPool.TryGetValue(s, out string? result)) { _stringPool[s] = s; result = s; @@ -809,6 +807,12 @@ private sealed class Target public string Name; public IEnumerable Libraries; + + public Target(string name, IEnumerable libraries) + { + Name = name; + Libraries = libraries; + } } private struct TargetLibrary @@ -817,45 +821,45 @@ private struct TargetLibrary public IEnumerable Dependencies; - public List Runtimes; + public List? Runtimes; - public List Natives; + public List? Natives; - public List Compilations; + public List? Compilations; - public List RuntimeTargets; + public List? RuntimeTargets; - public List Resources; + public List? Resources; public bool? CompileOnly; } private struct RuntimeTargetEntryStub { - public string Type; + public string? Type; public string Path; - public string Rid; + public string? Rid; - public string AssemblyVersion; + public string? AssemblyVersion; - public string FileVersion; + public string? FileVersion; } private struct LibraryStub { - public string Hash; + public string? Hash; public string Type; public bool Serviceable; - public string Path; + public string? Path; - public string HashPath; + public string? HashPath; - public string RuntimeStoreManifestName; + public string? RuntimeStoreManifestName; } } } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs index 6032c69281ae6..2249ff69d8705 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs @@ -14,7 +14,7 @@ public class DependencyContextLoader { private const string DepsJsonExtension = ".deps.json"; - private readonly string _entryPointDepsLocation; + private readonly string? _entryPointDepsLocation; private readonly IEnumerable _nonEntryPointDepsPaths; private readonly IFileSystem _fileSystem; private readonly Func _jsonReaderFactory; @@ -28,7 +28,7 @@ public DependencyContextLoader() : this( } internal DependencyContextLoader( - string entryPointDepsLocation, + string? entryPointDepsLocation, IEnumerable nonEntryPointDepsPaths, IFileSystem fileSystem, Func jsonReaderFactory) @@ -39,27 +39,27 @@ internal DependencyContextLoader( _jsonReaderFactory = jsonReaderFactory; } - public static DependencyContextLoader Default { get; } = new DependencyContextLoader(); + public static DependencyContextLoader Default { get; } = new(); private static bool IsEntryAssembly(Assembly assembly) { return assembly.Equals(Assembly.GetEntryAssembly()); } - private static Stream GetResourceStream(Assembly assembly, string name) + private static Stream? GetResourceStream(Assembly assembly, string name) { return assembly.GetManifestResourceStream(name); } [RequiresAssemblyFiles("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")] - public DependencyContext Load(Assembly assembly) + public DependencyContext? Load(Assembly assembly) { if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } - DependencyContext context = null; + DependencyContext? context = null; using (IDependencyContextReader reader = _jsonReaderFactory()) { if (IsEntryAssembly(assembly)) @@ -76,7 +76,7 @@ public DependencyContext Load(Assembly assembly) { foreach (string extraPath in _nonEntryPointDepsPaths) { - DependencyContext extraContext = LoadContext(reader, extraPath); + DependencyContext? extraContext = LoadContext(reader, extraPath); if (extraContext != null) { context = context.Merge(extraContext); @@ -87,12 +87,12 @@ public DependencyContext Load(Assembly assembly) return context; } - private DependencyContext LoadEntryAssemblyContext(IDependencyContextReader reader) + private DependencyContext? LoadEntryAssemblyContext(IDependencyContextReader reader) { return LoadContext(reader, _entryPointDepsLocation); } - private DependencyContext LoadContext(IDependencyContextReader reader, string location) + private DependencyContext? LoadContext(IDependencyContextReader reader, string? location) { if (!string.IsNullOrEmpty(location)) { @@ -106,9 +106,9 @@ private DependencyContext LoadContext(IDependencyContextReader reader, string lo } [RequiresAssemblyFiles("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")] - private DependencyContext LoadAssemblyContext(Assembly assembly, IDependencyContextReader reader) + private DependencyContext? LoadAssemblyContext(Assembly assembly, IDependencyContextReader reader) { - using (Stream stream = GetResourceStream(assembly, assembly.GetName().Name + DepsJsonExtension)) + using (Stream? stream = GetResourceStream(assembly, assembly.GetName().Name + DepsJsonExtension)) { if (stream != null) { @@ -116,7 +116,7 @@ private DependencyContext LoadAssemblyContext(Assembly assembly, IDependencyCont } } - string depsJsonFile = GetDepsJsonPath(assembly); + string? depsJsonFile = GetDepsJsonPath(assembly); if (!string.IsNullOrEmpty(depsJsonFile)) { using (Stream stream = _fileSystem.File.OpenRead(depsJsonFile)) @@ -129,7 +129,7 @@ private DependencyContext LoadAssemblyContext(Assembly assembly, IDependencyCont } [RequiresAssemblyFiles("The use of DependencyContextLoader is not supported when publishing as single-file")] - private string GetDepsJsonPath(Assembly assembly) + private string? GetDepsJsonPath(Assembly assembly) { // Assemblies loaded in memory (e.g. single file) return empty string from Location. // In these cases, don't try probing next to the assembly. @@ -146,7 +146,7 @@ private string GetDepsJsonPath(Assembly assembly) { // in some cases (like .NET Framework shadow copy) the Assembly Location // and CodeBase will be different, so also try the CodeBase - string assemblyCodeBase = GetNormalizedCodeBasePath(assembly); + string? assemblyCodeBase = GetNormalizedCodeBasePath(assembly); if (!string.IsNullOrEmpty(assemblyCodeBase) && assemblyLocation != assemblyCodeBase) { @@ -160,9 +160,12 @@ private string GetDepsJsonPath(Assembly assembly) null; } - private static string GetNormalizedCodeBasePath(Assembly assembly) + [RequiresAssemblyFiles] + private static string? GetNormalizedCodeBasePath(Assembly assembly) { - if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out Uri codeBase) +#pragma warning disable SYSLIB0012 // CodeBase is obsolete + if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out Uri? codeBase) +#pragma warning restore SYSLIB0012 // CodeBase is obsolete && codeBase.IsFile) { return codeBase.LocalPath; diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextPaths.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextPaths.cs index 6c1eaff38b9e2..3454571b0bb73 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextPaths.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextPaths.cs @@ -14,16 +14,16 @@ internal sealed class DependencyContextPaths public static DependencyContextPaths Current { get; } = GetCurrent(); - public string Application { get; } + public string? Application { get; } - public string SharedRuntime { get; } + public string? SharedRuntime { get; } public IEnumerable NonApplicationPaths { get; } public DependencyContextPaths( - string application, - string sharedRuntime, - IEnumerable nonApplicationPaths) + string? application, + string? sharedRuntime, + IEnumerable? nonApplicationPaths) { Application = application; SharedRuntime = sharedRuntime; @@ -32,18 +32,18 @@ public DependencyContextPaths( private static DependencyContextPaths GetCurrent() { - object deps = AppDomain.CurrentDomain.GetData(DepsFilesProperty); - object fxDeps = AppDomain.CurrentDomain.GetData(FxDepsFileProperty); + object? deps = AppDomain.CurrentDomain.GetData(DepsFilesProperty); + object? fxDeps = AppDomain.CurrentDomain.GetData(FxDepsFileProperty); return Create(deps as string, fxDeps as string); } - internal static DependencyContextPaths Create(string depsFiles, string sharedRuntime) + internal static DependencyContextPaths Create(string? depsFiles, string? sharedRuntime) { - string[] files = depsFiles?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); - string application = files != null && files.Length > 0 ? files[0] : null; + string[]? files = depsFiles?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); + string? application = files != null && files.Length > 0 ? files[0] : null; - string[] nonApplicationPaths = files? + string[]? nonApplicationPaths = files? .Skip(1) // the application path .ToArray(); diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs index cd662d0a912e2..551c2d8922c11 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs @@ -68,7 +68,7 @@ private void WriteRuntimeGraph(DependencyContext context, Utf8JsonWriter jsonWri foreach (RuntimeFallbacks runtimeFallback in context.RuntimeGraph) { jsonWriter.WriteStartArray(runtimeFallback.Runtime); - foreach (string fallback in runtimeFallback.Fallbacks) + foreach (string? fallback in runtimeFallback.Fallbacks) { jsonWriter.WriteStringValue(fallback); } @@ -83,7 +83,7 @@ private void WriteCompilationOptions(CompilationOptions compilationOptions, Utf8 if (compilationOptions.Defines?.Any() == true) { jsonWriter.WriteStartArray(DependencyContextStrings.DefinesPropertyName); - foreach (string define in compilationOptions.Defines) + foreach (string? define in compilationOptions.Defines) { jsonWriter.WriteStringValue(define); } @@ -103,7 +103,7 @@ private void WriteCompilationOptions(CompilationOptions compilationOptions, Utf8 jsonWriter.WriteEndObject(); } - private void AddStringPropertyIfNotNull(string name, string value, Utf8JsonWriter jsonWriter) + private void AddStringPropertyIfNotNull(string name, string? value, Utf8JsonWriter jsonWriter) { if (value != null) { @@ -156,9 +156,9 @@ private void WritePortableTarget(string key, IReadOnlyList runti foreach (string packageName in runtimeLookup.Keys.Concat(compileLookup.Keys).Distinct()) { - runtimeLookup.TryGetValue(packageName, out RuntimeLibrary runtimeLibrary); + runtimeLookup.TryGetValue(packageName, out RuntimeLibrary? runtimeLibrary); - compileLookup.TryGetValue(packageName, out CompilationLibrary compilationLibrary); + compileLookup.TryGetValue(packageName, out CompilationLibrary? compilationLibrary); if (compilationLibrary != null && runtimeLibrary != null) { @@ -171,7 +171,7 @@ private void WritePortableTarget(string key, IReadOnlyList runti Debug.Assert(compilationLibrary.RuntimeStoreManifestName == null); } - Library library = (Library)compilationLibrary ?? (Library)runtimeLibrary; + Library library = (Library?)compilationLibrary ?? (Library)runtimeLibrary!; WritePortableTargetLibrary(library.Name + DependencyContextStrings.VersionSeparator + library.Version, runtimeLibrary, compilationLibrary, jsonWriter); @@ -189,7 +189,7 @@ private void AddCompilationAssemblies(IEnumerable compilationAssemblies, WriteAssetList(DependencyContextStrings.CompileTimeAssembliesKey, compilationAssemblies, jsonWriter); } - private void AddAssets(string key, RuntimeAssetGroup group, Utf8JsonWriter jsonWriter) + private void AddAssets(string key, RuntimeAssetGroup? group, Utf8JsonWriter jsonWriter) { if (group == null || !group.RuntimeFiles.Any()) { @@ -259,7 +259,7 @@ private void WriteTargetLibrary(string key, Library library, Utf8JsonWriter json } } - private void WritePortableTargetLibrary(string key, RuntimeLibrary runtimeLibrary, CompilationLibrary compilationLibrary, Utf8JsonWriter jsonWriter) + private void WritePortableTargetLibrary(string key, RuntimeLibrary? runtimeLibrary, CompilationLibrary? compilationLibrary, Utf8JsonWriter jsonWriter) { jsonWriter.WriteStartObject(key); @@ -339,7 +339,7 @@ private bool AddRuntimeSpecificAssetGroups(string assetType, IEnumerable assets, string runtime, string assetType, Utf8JsonWriter jsonWriter) + private void AddRuntimeSpecificAssets(IEnumerable assets, string? runtime, string? assetType, Utf8JsonWriter jsonWriter) { foreach (RuntimeFile asset in assets) { diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DirectoryWrapper.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DirectoryWrapper.cs index 7fefd9483cdd4..52142afb09826 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DirectoryWrapper.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DirectoryWrapper.cs @@ -1,13 +1,14 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.IO; namespace Microsoft.Extensions.DependencyModel { internal sealed class DirectoryWrapper: IDirectory { - public bool Exists(string path) + public bool Exists([NotNullWhen(true)] string? path) { return Directory.Exists(path); } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/EnvironmentWrapper.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/EnvironmentWrapper.cs index a26698e0aaef3..47025ffb17ac3 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/EnvironmentWrapper.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/EnvironmentWrapper.cs @@ -10,9 +10,9 @@ internal sealed class EnvironmentWrapper : IEnvironment { public static IEnvironment Default = new EnvironmentWrapper(); - public string GetEnvironmentVariable(string name) => Environment.GetEnvironmentVariable(name); + public string? GetEnvironmentVariable(string name) => Environment.GetEnvironmentVariable(name); - public object GetAppContextData(string name) => AppDomain.CurrentDomain.GetData(name); + public object? GetAppContextData(string name) => AppDomain.CurrentDomain.GetData(name); public bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/FileWrapper.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/FileWrapper.cs index ef1bf5b2bc532..f9a0f4bb9da15 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/FileWrapper.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/FileWrapper.cs @@ -2,13 +2,14 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics.CodeAnalysis; using System.IO; namespace Microsoft.Extensions.DependencyModel { internal sealed class FileWrapper: IFile { - public bool Exists(string path) + public bool Exists([NotNullWhen(true)] string? path) { return File.Exists(path); } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/HashCodeCombiner.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/HashCodeCombiner.cs index ea0bf3f3a8ed7..2d7baba3517b6 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/HashCodeCombiner.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/HashCodeCombiner.cs @@ -33,21 +33,21 @@ public void Add(int i) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Add(string s) + public void Add(string? s) { int hashCode = (s != null) ? s.GetHashCode() : 0; Add(hashCode); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Add(object o) + public void Add(object? o) { int hashCode = (o != null) ? o.GetHashCode() : 0; Add(hashCode); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Add(TValue value, IEqualityComparer comparer) + public void Add(TValue? value, IEqualityComparer comparer) { int hashCode = value != null ? comparer.GetHashCode(value) : 0; Add(hashCode); diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/IDirectory.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/IDirectory.cs index 0bbb0db224a0d..81039408ebb9c 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/IDirectory.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/IDirectory.cs @@ -1,10 +1,12 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; + namespace Microsoft.Extensions.DependencyModel { internal interface IDirectory { - bool Exists(string path); + bool Exists([NotNullWhen(true)] string? path); } } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/IEnvironment.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/IEnvironment.cs index e35568fb8cf06..458933f73fa15 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/IEnvironment.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/IEnvironment.cs @@ -5,8 +5,8 @@ namespace Microsoft.Extensions.DependencyModel { internal interface IEnvironment { - string GetEnvironmentVariable(string name); - object GetAppContextData(string name); + string? GetEnvironmentVariable(string name); + object? GetAppContextData(string name); bool IsWindows(); } } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/IFile.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/IFile.cs index a909ba4274713..18596e675da75 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/IFile.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/IFile.cs @@ -1,13 +1,14 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.IO; namespace Microsoft.Extensions.DependencyModel { internal interface IFile { - bool Exists(string path); + bool Exists([NotNullWhen(true)] string? path); string ReadAllText(string path); diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Library.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Library.cs index 722e28abe99e2..ffdfa8032e8b0 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Library.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Library.cs @@ -12,7 +12,7 @@ public class Library public Library(string type, string name, string version, - string hash, + string? hash, IEnumerable dependencies, bool serviceable) : this(type, name, version, hash, dependencies, serviceable, path: null, hashPath: null) @@ -22,23 +22,23 @@ public Library(string type, public Library(string type, string name, string version, - string hash, + string? hash, IEnumerable dependencies, bool serviceable, - string path, - string hashPath) + string? path, + string? hashPath) : this(type, name, version, hash, dependencies, serviceable, path, hashPath, runtimeStoreManifestName: null) { } public Library(string type, string name, string version, - string hash, + string? hash, IEnumerable dependencies, bool serviceable, - string path, - string hashPath, - string runtimeStoreManifestName = null) + string? path, + string? hashPath, + string? runtimeStoreManifestName = null) { if (string.IsNullOrEmpty(type)) { @@ -73,16 +73,16 @@ public Library(string type, public string Version { get; } - public string Hash { get; } + public string? Hash { get; } public IReadOnlyList Dependencies { get; } public bool Serviceable { get; } - public string Path { get; } + public string? Path { get; } - public string HashPath { get; } + public string? HashPath { get; } - public string RuntimeStoreManifestName {get;} + public string? RuntimeStoreManifestName {get;} } } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Microsoft.Extensions.DependencyModel.csproj b/src/libraries/Microsoft.Extensions.DependencyModel/src/Microsoft.Extensions.DependencyModel.csproj index 81b60b7135205..73949750fc3f1 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Microsoft.Extensions.DependencyModel.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Microsoft.Extensions.DependencyModel.csproj @@ -1,7 +1,10 @@ - + - netstandard2.0;net461 + $(NetCoreAppCurrent);netstandard2.0;net461 + enable true + + false Abstractions for reading `.deps` files. Commonly Used Types: @@ -11,8 +14,13 @@ Microsoft.Extensions.DependencyModel.DependencyContext + + + + + @@ -25,11 +33,9 @@ Microsoft.Extensions.DependencyModel.DependencyContext - - - + diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs index 198584542a946..e6d99ea4aec7a 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; namespace Microsoft.Extensions.DependencyModel.Resolution @@ -31,13 +32,18 @@ internal AppBaseCompilationAssemblyResolver(IFileSystem fileSystem) internal AppBaseCompilationAssemblyResolver(IFileSystem fileSystem, string basePath, DependencyContextPaths dependencyContextPaths) { - _fileSystem = fileSystem; - _basePath = basePath; - _dependencyContextPaths = dependencyContextPaths; + _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); + _basePath = basePath ?? throw new ArgumentNullException(nameof(basePath)); + _dependencyContextPaths = dependencyContextPaths ?? throw new ArgumentNullException(nameof(dependencyContextPaths)); } - public bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies) + public bool TryResolveAssemblyPaths(CompilationLibrary library, List? assemblies) { + if (library is null) + { + throw new ArgumentNullException(nameof(library)); + } + bool isProject = string.Equals(library.Type, "project", StringComparison.OrdinalIgnoreCase) || string.Equals(library.Type, "msbuildproject", StringComparison.OrdinalIgnoreCase); @@ -71,10 +77,12 @@ public bool TryResolveAssemblyPaths(CompilationLibrary library, List ass } // Only packages can come from shared runtime - string sharedPath = _dependencyContextPaths.SharedRuntime; + string? sharedPath = _dependencyContextPaths.SharedRuntime; if (isPublished && isPackage && !string.IsNullOrEmpty(sharedPath)) { - string sharedDirectory = Path.GetDirectoryName(sharedPath); + string? sharedDirectory = Path.GetDirectoryName(sharedPath); + Debug.Assert(sharedDirectory != null); + string sharedRefs = Path.Combine(sharedDirectory, RefsDirectoryName); if (_fileSystem.Directory.Exists(sharedRefs)) { diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/CompositeCompilationAssemblyResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/CompositeCompilationAssemblyResolver.cs index aeeef0cdfc368..fb758d084e977 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/CompositeCompilationAssemblyResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/CompositeCompilationAssemblyResolver.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; using System.Collections.Generic; namespace Microsoft.Extensions.DependencyModel.Resolution @@ -11,10 +12,10 @@ public class CompositeCompilationAssemblyResolver: ICompilationAssemblyResolver public CompositeCompilationAssemblyResolver(ICompilationAssemblyResolver[] resolvers) { - _resolvers = resolvers; + _resolvers = resolvers ?? throw new ArgumentNullException(nameof(resolvers)); } - public bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies) + public bool TryResolveAssemblyPaths(CompilationLibrary library, List? assemblies) { foreach (ICompilationAssemblyResolver resolver in _resolvers) { diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/DotNetReferenceAssembliesPathResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/DotNetReferenceAssembliesPathResolver.cs index 1e5df07698013..9c78fca2b2518 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/DotNetReferenceAssembliesPathResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/DotNetReferenceAssembliesPathResolver.cs @@ -9,9 +9,9 @@ public class DotNetReferenceAssembliesPathResolver { public static readonly string DotNetReferenceAssembliesPathEnv = "DOTNET_REFERENCE_ASSEMBLIES_PATH"; - internal static string Resolve(IEnvironment environment, IFileSystem fileSystem) + internal static string? Resolve(IEnvironment environment, IFileSystem fileSystem) { - string path = environment.GetEnvironmentVariable(DotNetReferenceAssembliesPathEnv); + string? path = environment.GetEnvironmentVariable(DotNetReferenceAssembliesPathEnv); if (!string.IsNullOrEmpty(path)) { return path; @@ -20,12 +20,12 @@ internal static string Resolve(IEnvironment environment, IFileSystem fileSystem) return GetDefaultDotNetReferenceAssembliesPath(fileSystem); } - public static string Resolve() + public static string? Resolve() { return Resolve(EnvironmentWrapper.Default, FileSystemWrapper.Default); } - private static string GetDefaultDotNetReferenceAssembliesPath(IFileSystem fileSystem) + private static string? GetDefaultDotNetReferenceAssembliesPath(IFileSystem fileSystem) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ICompilationAssemblyResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ICompilationAssemblyResolver.cs index 13ec7f71befcc..aa3281a744b3d 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ICompilationAssemblyResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ICompilationAssemblyResolver.cs @@ -7,6 +7,6 @@ namespace Microsoft.Extensions.DependencyModel.Resolution { public interface ICompilationAssemblyResolver { - bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies); + bool TryResolveAssemblyPaths(CompilationLibrary library, List? assemblies); } } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs index c4e3290cdedd2..efb8cf12b91d6 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; namespace Microsoft.Extensions.DependencyModel.Resolution @@ -30,29 +31,29 @@ internal PackageCompilationAssemblyResolver(IEnvironment environment, internal PackageCompilationAssemblyResolver(IFileSystem fileSystem, string[] nugetPackageDirectories) { - _fileSystem = fileSystem; - _nugetPackageDirectories = nugetPackageDirectories; + _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); + _nugetPackageDirectories = nugetPackageDirectories ?? throw new ArgumentNullException(nameof(nugetPackageDirectories)); } internal static string[] GetDefaultProbeDirectories(IEnvironment environment) { - object probeDirectories = environment.GetAppContextData("PROBING_DIRECTORIES"); + object? probeDirectories = environment.GetAppContextData("PROBING_DIRECTORIES"); - string listOfDirectories = probeDirectories as string; + string? listOfDirectories = probeDirectories as string; if (!string.IsNullOrEmpty(listOfDirectories)) { return listOfDirectories.Split(new char[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries); } - string packageDirectory = environment.GetEnvironmentVariable("NUGET_PACKAGES"); + string? packageDirectory = environment.GetEnvironmentVariable("NUGET_PACKAGES"); if (!string.IsNullOrEmpty(packageDirectory)) { return new string[] { packageDirectory }; } - string basePath; + string? basePath; if (environment.IsWindows()) { basePath = environment.GetEnvironmentVariable("USERPROFILE"); @@ -70,8 +71,13 @@ internal static string[] GetDefaultProbeDirectories(IEnvironment environment) return new string[] { Path.Combine(basePath, ".nuget", "packages") }; } - public bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies) + public bool TryResolveAssemblyPaths(CompilationLibrary library, List? assemblies) { + if (library is null) + { + throw new ArgumentNullException(nameof(library)); + } + if (_nugetPackageDirectories == null || _nugetPackageDirectories.Length == 0 || !string.Equals(library.Type, "package", StringComparison.OrdinalIgnoreCase)) { @@ -84,10 +90,9 @@ public bool TryResolveAssemblyPaths(CompilationLibrary library, List ass if (ResolverUtils.TryResolvePackagePath(_fileSystem, library, directory, out packagePath)) { - IEnumerable fullPathsFromPackage; - if (TryResolveFromPackagePath(_fileSystem, library, packagePath, out fullPathsFromPackage)) + if (TryResolveFromPackagePath(_fileSystem, library, packagePath, out IEnumerable? fullPathsFromPackage)) { - assemblies.AddRange(fullPathsFromPackage); + assemblies?.AddRange(fullPathsFromPackage); return true; } } @@ -95,14 +100,13 @@ public bool TryResolveAssemblyPaths(CompilationLibrary library, List ass return false; } - private static bool TryResolveFromPackagePath(IFileSystem fileSystem, CompilationLibrary library, string basePath, out IEnumerable results) + private static bool TryResolveFromPackagePath(IFileSystem fileSystem, CompilationLibrary library, string basePath, [MaybeNullWhen(false)] out IEnumerable results) { var paths = new List(); foreach (string assembly in library.Assemblies) { - string fullName; - if (!ResolverUtils.TryResolveAssemblyFile(fileSystem, basePath, assembly, out fullName)) + if (!ResolverUtils.TryResolveAssemblyFile(fileSystem, basePath, assembly, out string fullName)) { // if one of the files can't be found, skip this package path completely. // there are package paths that don't include all of the "ref" assemblies diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs index 7478368ed68ee..39ccb9bf228fa 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; namespace Microsoft.Extensions.DependencyModel.Resolution @@ -10,7 +11,7 @@ namespace Microsoft.Extensions.DependencyModel.Resolution public class ReferenceAssemblyPathResolver: ICompilationAssemblyResolver { private readonly IFileSystem _fileSystem; - private readonly string _defaultReferenceAssembliesPath; + private readonly string? _defaultReferenceAssembliesPath; private readonly string[] _fallbackSearchPaths; public ReferenceAssemblyPathResolver() @@ -18,7 +19,7 @@ public ReferenceAssemblyPathResolver() { } - public ReferenceAssemblyPathResolver(string defaultReferenceAssembliesPath, string[] fallbackSearchPaths) + public ReferenceAssemblyPathResolver(string? defaultReferenceAssembliesPath, string[] fallbackSearchPaths) : this(FileSystemWrapper.Default, defaultReferenceAssembliesPath, fallbackSearchPaths) { } @@ -30,32 +31,36 @@ internal ReferenceAssemblyPathResolver(IFileSystem fileSystem, IEnvironment envi { } - internal ReferenceAssemblyPathResolver(IFileSystem fileSystem, string defaultReferenceAssembliesPath, string[] fallbackSearchPaths) + internal ReferenceAssemblyPathResolver(IFileSystem fileSystem, string? defaultReferenceAssembliesPath, string[] fallbackSearchPaths) { - _fileSystem = fileSystem; + _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); _defaultReferenceAssembliesPath = defaultReferenceAssembliesPath; - _fallbackSearchPaths = fallbackSearchPaths; + _fallbackSearchPaths = fallbackSearchPaths ?? throw new ArgumentNullException(nameof(fallbackSearchPaths)); } - public bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies) + public bool TryResolveAssemblyPaths(CompilationLibrary library, List? assemblies) { + if (library is null) + { + throw new ArgumentNullException(nameof(library)); + } + if (!string.Equals(library.Type, "referenceassembly", StringComparison.OrdinalIgnoreCase)) { return false; } foreach (string assembly in library.Assemblies) { - string fullName; - if (!TryResolveReferenceAssembly(assembly, out fullName)) + if (!TryResolveReferenceAssembly(assembly, out string? fullName)) { throw new InvalidOperationException(SR.Format(SR.ReferenceAssemblyNotFound, assembly, library.Name)); } - assemblies.Add(fullName); + assemblies?.Add(fullName); } return true; } - private bool TryResolveReferenceAssembly(string path, out string fullPath) + private bool TryResolveReferenceAssembly(string path, [MaybeNullWhen(false)] out string fullPath) { fullPath = null; @@ -91,19 +96,25 @@ internal static string[] GetFallbackSearchPaths(IFileSystem fileSystem, IEnviron return Array.Empty(); } - string net20Dir = Path.Combine(environment.GetEnvironmentVariable("WINDIR"), "Microsoft.NET", "Framework", "v2.0.50727"); + string? windir = environment.GetEnvironmentVariable("WINDIR"); + if (windir == null) + { + return Array.Empty(); + } + string net20Dir = Path.Combine(windir, "Microsoft.NET", "Framework", "v2.0.50727"); if (!fileSystem.Directory.Exists(net20Dir)) { return Array.Empty(); } + return new[] { net20Dir }; } - internal static string GetDefaultReferenceAssembliesPath(IFileSystem fileSystem, IEnvironment environment) + internal static string? GetDefaultReferenceAssembliesPath(IFileSystem fileSystem, IEnvironment environment) { // Allow setting the reference assemblies path via an environment variable - string referenceAssembliesPath = DotNetReferenceAssembliesPathResolver.Resolve(environment, fileSystem); + string? referenceAssembliesPath = DotNetReferenceAssembliesPathResolver.Resolve(environment, fileSystem); if (!string.IsNullOrEmpty(referenceAssembliesPath)) { return referenceAssembliesPath; @@ -118,7 +129,7 @@ internal static string GetDefaultReferenceAssembliesPath(IFileSystem fileSystem, // References assemblies are in %ProgramFiles(x86)% on // 64 bit machines - string programFiles = environment.GetEnvironmentVariable("ProgramFiles(x86)"); + string? programFiles = environment.GetEnvironmentVariable("ProgramFiles(x86)"); if (string.IsNullOrEmpty(programFiles)) { diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ResolverUtils.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ResolverUtils.cs index 1726e7755a703..3a32aaba35823 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ResolverUtils.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ResolverUtils.cs @@ -9,7 +9,7 @@ internal static class ResolverUtils { internal static bool TryResolvePackagePath(IFileSystem fileSystem, CompilationLibrary library, string basePath, out string packagePath) { - string path = library.Path; + string? path = library.Path; if (string.IsNullOrEmpty(path)) { path = Path.Combine(library.Name, library.Version); diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssembly.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssembly.cs index 80497ab89000b..ba8671a83b4f4 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssembly.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssembly.cs @@ -25,7 +25,7 @@ public RuntimeAssembly(string assemblyName, string path) Path = path; } - public AssemblyName Name => new AssemblyName(_assemblyName); + public AssemblyName Name => new(_assemblyName); public string Path { get; } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssetGroup.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssetGroup.cs index 06a931373a35b..f303e642f6eec 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssetGroup.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssetGroup.cs @@ -8,18 +8,18 @@ namespace Microsoft.Extensions.DependencyModel { public class RuntimeAssetGroup { - private IReadOnlyList _assetPaths; - private IReadOnlyList _runtimeFiles; + private IReadOnlyList? _assetPaths; + private IReadOnlyList? _runtimeFiles; - public RuntimeAssetGroup(string runtime, params string[] assetPaths) : this(runtime, (IEnumerable)assetPaths) { } + public RuntimeAssetGroup(string? runtime, params string[] assetPaths) : this(runtime, (IEnumerable)assetPaths) { } - public RuntimeAssetGroup(string runtime, IEnumerable assetPaths) + public RuntimeAssetGroup(string? runtime, IEnumerable assetPaths) { Runtime = runtime; _assetPaths = assetPaths.ToArray(); } - public RuntimeAssetGroup(string runtime, IEnumerable runtimeFiles) + public RuntimeAssetGroup(string? runtime, IEnumerable runtimeFiles) { Runtime = runtime; _runtimeFiles = runtimeFiles.ToArray(); @@ -28,7 +28,7 @@ public RuntimeAssetGroup(string runtime, IEnumerable runtimeFiles) /// /// The runtime ID associated with this group (may be empty if the group is runtime-agnostic) /// - public string Runtime { get; } + public string? Runtime { get; } /// /// Gets a list of asset paths provided in this runtime group @@ -42,7 +42,7 @@ public IReadOnlyList AssetPaths return _assetPaths; } - return _runtimeFiles.Select(file => file.Path).ToArray(); + return _runtimeFiles!.Select(file => file.Path).ToArray(); } } @@ -58,7 +58,7 @@ public IReadOnlyList RuntimeFiles return _runtimeFiles; } - return _assetPaths.Select(path => new RuntimeFile(path, null, null)).ToArray(); + return _assetPaths!.Select(path => new RuntimeFile(path, null, null)).ToArray(); } } } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeFallbacks.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeFallbacks.cs index 3e80e4e7720ae..bb153401a3a9a 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeFallbacks.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeFallbacks.cs @@ -10,10 +10,10 @@ namespace Microsoft.Extensions.DependencyModel public class RuntimeFallbacks { public string Runtime { get; set; } - public IReadOnlyList Fallbacks { get; set; } + public IReadOnlyList Fallbacks { get; set; } - public RuntimeFallbacks(string runtime, params string[] fallbacks) : this(runtime, (IEnumerable)fallbacks) { } - public RuntimeFallbacks(string runtime, IEnumerable fallbacks) + public RuntimeFallbacks(string runtime, params string?[] fallbacks) : this(runtime, (IEnumerable)fallbacks) { } + public RuntimeFallbacks(string runtime, IEnumerable fallbacks) { if (string.IsNullOrEmpty(runtime)) { diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeFile.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeFile.cs index 2247ac1b815c2..16a4524865ad4 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeFile.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeFile.cs @@ -7,7 +7,7 @@ namespace Microsoft.Extensions.DependencyModel { public class RuntimeFile { - public RuntimeFile(string path, string assemblyVersion, string fileVersion) + public RuntimeFile(string path, string? assemblyVersion, string? fileVersion) { if (string.IsNullOrEmpty(path)) { @@ -21,8 +21,8 @@ public RuntimeFile(string path, string assemblyVersion, string fileVersion) public string Path { get; } - public string AssemblyVersion { get; } + public string? AssemblyVersion { get; } - public string FileVersion { get; } + public string? FileVersion { get; } } } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeLibrary.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeLibrary.cs index 417d68f83490e..1b03ce8a181da 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeLibrary.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeLibrary.cs @@ -12,7 +12,7 @@ public class RuntimeLibrary : Library public RuntimeLibrary(string type, string name, string version, - string hash, + string? hash, IReadOnlyList runtimeAssemblyGroups, IReadOnlyList nativeLibraryGroups, IEnumerable resourceAssemblies, @@ -35,14 +35,14 @@ public RuntimeLibrary(string type, public RuntimeLibrary(string type, string name, string version, - string hash, + string? hash, IReadOnlyList runtimeAssemblyGroups, IReadOnlyList nativeLibraryGroups, IEnumerable resourceAssemblies, IEnumerable dependencies, bool serviceable, - string path, - string hashPath) + string? path, + string? hashPath) : this(type, name, version, @@ -86,15 +86,15 @@ public RuntimeLibrary(string type, public RuntimeLibrary(string type, string name, string version, - string hash, + string? hash, IReadOnlyList runtimeAssemblyGroups, IReadOnlyList nativeLibraryGroups, IEnumerable resourceAssemblies, IEnumerable dependencies, bool serviceable, - string path, - string hashPath, - string runtimeStoreManifestName) + string? path, + string? hashPath, + string? runtimeStoreManifestName) : base(type, name, version, diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/TargetInfo.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/TargetInfo.cs index 9b175c66cc98a..dc94716a3c58a 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/TargetInfo.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/TargetInfo.cs @@ -7,8 +7,8 @@ namespace Microsoft.Extensions.DependencyModel public class TargetInfo { public TargetInfo(string framework, - string runtime, - string runtimeSignature, + string? runtime, + string? runtimeSignature, bool isPortable) { if (string.IsNullOrEmpty(framework)) @@ -24,9 +24,9 @@ public TargetInfo(string framework, public string Framework { get; } - public string Runtime { get; } + public string? Runtime { get; } - public string RuntimeSignature { get; } + public string? RuntimeSignature { get; } public bool IsPortable { get; } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Utf8JsonReaderExtensions.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Utf8JsonReaderExtensions.cs index 1c6a012288f7a..807efa2eb71a6 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Utf8JsonReaderExtensions.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Utf8JsonReaderExtensions.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Text.Json; @@ -14,7 +15,7 @@ internal static class Utf8JsonReaderExtensions public static bool IsTokenTypeProperty(this ref Utf8JsonReader reader) => reader.TokenType == JsonTokenType.PropertyName; - public static bool TryReadStringProperty(this ref Utf8JsonReader reader, out string name, out string value) + public static bool TryReadStringProperty(this ref Utf8JsonReader reader, out string? name, out string? value) { name = null; value = null; @@ -62,7 +63,7 @@ public static void CheckEndObject(this ref Utf8JsonReader reader) } } - public static string[] ReadStringArray(this ref Utf8JsonReader reader) + public static string?[] ReadStringArray(this ref Utf8JsonReader reader) { reader.Read(); if (reader.TokenType != JsonTokenType.StartArray) @@ -70,7 +71,7 @@ public static string[] ReadStringArray(this ref Utf8JsonReader reader) throw CreateUnexpectedException(ref reader, "["); } - var items = new List(); + var items = new List(); while (reader.Read() && reader.TokenType == JsonTokenType.String) { @@ -85,7 +86,7 @@ public static string[] ReadStringArray(this ref Utf8JsonReader reader) return items.ToArray(); } - public static string ReadAsString(this ref Utf8JsonReader reader) + public static string? ReadAsString(this ref Utf8JsonReader reader) { Debug.Assert(reader.IsTokenTypeProperty()); reader.Read();