From 5b217b624fc5eebeb11f078ac1c9fed45b592aff Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sun, 15 Aug 2021 19:20:04 +0300 Subject: [PATCH 01/21] Annotate src --- .../src/CollectionExtensions.cs | 6 +- .../src/CompilationLibrary.cs | 8 +- .../src/CompilationOptions.cs | 16 +- .../src/Dependency.cs | 6 +- .../src/DependencyContext.cs | 12 +- .../src/DependencyContextExtensions.cs | 8 +- .../src/DependencyContextJsonReader.cs | 148 +++++++++--------- .../src/DependencyContextLoader.cs | 38 +++-- .../src/DependencyContextPaths.cs | 22 +-- .../src/DependencyContextWriter.cs | 16 +- .../src/DirectoryWrapper.cs | 3 +- .../src/EnvironmentWrapper.cs | 4 +- .../src/FileWrapper.cs | 3 +- .../src/HashCodeCombiner.cs | 6 +- .../src/IDirectory.cs | 4 +- .../src/IEnvironment.cs | 4 +- .../src/IFile.cs | 3 +- .../src/Library.cs | 24 +-- ...icrosoft.Extensions.DependencyModel.csproj | 20 ++- .../AppBaseCompilationAssemblyResolver.cs | 4 +- .../DotNetReferenceAssembliesPathResolver.cs | 8 +- .../PackageCompilationAssemblyResolver.cs | 17 +- .../ReferenceAssemblyPathResolver.cs | 24 +-- .../src/Resolution/ResolverUtils.cs | 2 +- .../src/RuntimeAssembly.cs | 2 +- .../src/RuntimeAssetGroup.cs | 8 +- .../src/RuntimeFile.cs | 6 +- .../src/RuntimeLibrary.cs | 12 +- .../src/Utf8JsonReaderExtensions.cs | 11 +- 29 files changed, 239 insertions(+), 206 deletions(-) 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..e38082ebe93bb 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationOptions.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationOptions.cs @@ -11,9 +11,9 @@ public class CompilationOptions { 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,13 +21,13 @@ 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; } @@ -48,15 +48,15 @@ public class CompilationOptions generateXmlDocumentation: null); public CompilationOptions(IEnumerable defines, - string languageVersion, - string platform, + 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 bf51c3a414a42..eb4fd981714f2 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,10 +31,9 @@ public bool Equals(Dependency other) return string.Equals(Name, other.Name) && string.Equals(Version, other.Version); } - public override bool Equals(object obj) + public override bool Equals([NotNullWhen(true)] object? obj) { - if (obj is null) return false; - return obj is Dependency && Equals((Dependency) obj); + return obj is Dependency dependency && Equals(dependency); } public override int 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..11932bd2e5fdf 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextExtensions.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextExtensions.cs @@ -182,7 +182,7 @@ private static IEnumerable ResolveAssets( string runtimeIdentifier, IEnumerable assets) { - RuntimeFallbacks fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); + RuntimeFallbacks? fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); IEnumerable rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty()); return SelectAssets(rids, assets); } @@ -192,7 +192,7 @@ private static IEnumerable ResolveRuntimeFiles( string runtimeIdentifier, IEnumerable assets) { - RuntimeFallbacks fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); + RuntimeFallbacks? fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); IEnumerable rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty()); return SelectRuntimeFiles(rids, assets); } @@ -201,7 +201,7 @@ private static IEnumerable SelectAssets(IEnumerable rids, IEnume { 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; @@ -216,7 +216,7 @@ private static IEnumerable SelectRuntimeFiles(IEnumerable r { 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..12d99eade92a9 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; @@ -209,16 +210,16 @@ private DependencyContext Read(Utf8JsonReader reader) } return new DependencyContext( - new TargetInfo(framework, runtime, runtimeSignature, isPortable), + new TargetInfo(framework, runtime, runtimeSignature ?? string.Empty, isPortable), compilationOptions, CreateLibraries(compileTarget?.Libraries, false, libraryStubs).Cast().ToArray(), CreateLibraries(runtimeTarget.Libraries, true, libraryStubs).Cast().ToArray(), runtimeFallbacks ?? Enumerable.Empty()); } - private static Target SelectRuntimeTarget(List targets, string runtimeTargetName) + private static Target? SelectRuntimeTarget(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; @@ -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,20 +523,20 @@ 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) { case DependencyContextStrings.RidPropertyName: - runtimeTarget.Rid = Pool(propertyValue); + runtimeTarget.Rid = Pool(propertyValue)!; break; case DependencyContextStrings.AssetTypePropertyName: - runtimeTarget.Type = Pool(propertyValue); + runtimeTarget.Type = Pool(propertyValue)!; break; case DependencyContextStrings.AssemblyVersionPropertyName: runtimeTarget.AssemblyVersion = propertyValue; @@ -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,7 +669,7 @@ private static List ReadRuntimes(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string runtime = reader.GetString(); + 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; @@ -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,15 +821,15 @@ 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; } @@ -838,24 +842,24 @@ private struct RuntimeTargetEntryStub 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..b78bc26a553bd 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,15 @@ private string GetDepsJsonPath(Assembly assembly) null; } - private static string GetNormalizedCodeBasePath(Assembly assembly) + private static string? GetNormalizedCodeBasePath(Assembly assembly) { - if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out Uri codeBase) +#if NETCOREAPP + string assemblyLocation = assembly.Location; +#else + string assemblyLocation = assembly.CodeBase; +#endif + + if (Uri.TryCreate(assemblyLocation, UriKind.Absolute, out Uri? codeBase) && 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..0e344b59cddfc 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs @@ -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,9 +171,9 @@ private void WritePortableTarget(string key, IReadOnlyList runti Debug.Assert(compilationLibrary.RuntimeStoreManifestName == null); } - Library library = (Library)compilationLibrary ?? (Library)runtimeLibrary; + Library? library = (Library?)compilationLibrary ?? runtimeLibrary; - WritePortableTargetLibrary(library.Name + DependencyContextStrings.VersionSeparator + library.Version, + WritePortableTargetLibrary(library?.Name + DependencyContextStrings.VersionSeparator + library?.Version, runtimeLibrary, compilationLibrary, jsonWriter); } jsonWriter.WriteEndObject(); @@ -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..5b570852b3136 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Microsoft.Extensions.DependencyModel.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Microsoft.Extensions.DependencyModel.csproj @@ -1,6 +1,7 @@ - + - netstandard2.0;net461 + $(NetCoreAppCurrent);netstandard2.0;net461 + enable true Abstractions for reading `.deps` files. @@ -11,8 +12,13 @@ Microsoft.Extensions.DependencyModel.DependencyContext + + + + + @@ -25,11 +31,17 @@ 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..bc1c24410e73a 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs @@ -71,10 +71,10 @@ 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)!; string sharedRefs = Path.Combine(sharedDirectory, RefsDirectoryName); if (_fileSystem.Directory.Exists(sharedRefs)) { 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/PackageCompilationAssemblyResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs index c4e3290cdedd2..00091003bc18c 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 @@ -36,23 +37,23 @@ internal PackageCompilationAssemblyResolver(IFileSystem fileSystem, string[] nug 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"); @@ -84,8 +85,7 @@ 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); return true; @@ -95,14 +95,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..5690c3bb7d899 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() @@ -30,7 +31,7 @@ internal ReferenceAssemblyPathResolver(IFileSystem fileSystem, IEnvironment envi { } - internal ReferenceAssemblyPathResolver(IFileSystem fileSystem, string defaultReferenceAssembliesPath, string[] fallbackSearchPaths) + internal ReferenceAssemblyPathResolver(IFileSystem fileSystem, string? defaultReferenceAssembliesPath, string[] fallbackSearchPaths) { _fileSystem = fileSystem; _defaultReferenceAssembliesPath = defaultReferenceAssembliesPath; @@ -45,8 +46,7 @@ public bool TryResolveAssemblyPaths(CompilationLibrary library, List ass } 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)); } @@ -55,7 +55,7 @@ public bool TryResolveAssemblyPaths(CompilationLibrary library, List ass return true; } - private bool TryResolveReferenceAssembly(string path, out string fullPath) + private bool TryResolveReferenceAssembly(string path, [MaybeNullWhen(false)] out string fullPath) { fullPath = null; @@ -91,19 +91,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 +124,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..30c7042a97144 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssetGroup.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssetGroup.cs @@ -8,8 +8,8 @@ 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) { } @@ -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/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..834c6638221a5 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeLibrary.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeLibrary.cs @@ -41,8 +41,8 @@ public RuntimeLibrary(string type, 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/Utf8JsonReaderExtensions.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Utf8JsonReaderExtensions.cs index 1c6a012288f7a..c5475c64504b9 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,19 +15,19 @@ 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, [MaybeNullWhen(false)] out string name, out string? value) { name = null; value = null; if (reader.Read() && reader.IsTokenTypeProperty()) { - name = reader.GetString(); + name = reader.GetString()!; if (reader.Read()) { if (reader.TokenType == JsonTokenType.String) { - value = reader.GetString(); + value = reader.GetString()!; } else { @@ -74,7 +75,7 @@ public static string[] ReadStringArray(this ref Utf8JsonReader reader) while (reader.Read() && reader.TokenType == JsonTokenType.String) { - items.Add(reader.GetString()); + items.Add(reader.GetString()!); } if (reader.TokenType != JsonTokenType.EndArray) @@ -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(); From fddbdaf44ffb7286eb6b879f34b9f85a929088fd Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sun, 15 Aug 2021 19:47:46 +0300 Subject: [PATCH 02/21] Annotate ref --- .../Microsoft.Extensions.DependencyModel.cs | 56 ++++++++++--------- ...icrosoft.Extensions.DependencyModel.csproj | 10 +++- 2 files changed, 38 insertions(+), 28 deletions(-) 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 25cb39d936a98..15d6a72f52d5d 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs @@ -4,6 +4,8 @@ // Changes to this file must follow the https://aka.ms/api-review process. // ------------------------------------------------------------------------------ +using System.Diagnostics.CodeAnalysis; + namespace Microsoft.DotNet.PlatformAbstractions { [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] @@ -23,26 +25,26 @@ 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 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; } } } @@ -54,7 +56,7 @@ public partial struct Dependency 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.NotNullWhen(true)] object? obj) { throw null; } public override int GetHashCode() { throw null; } } public partial class DependencyContext @@ -63,12 +65,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 +100,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 +113,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; } } @@ -155,16 +157,16 @@ public RuntimeFallbacks(string runtime, params string[] fallbacks) { } } 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; } } @@ -195,7 +197,7 @@ 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 { @@ -219,10 +221,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..63e93bddbd173 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,17 @@ - netstandard2.0;net461 + $(NetCoreAppCurrent);netstandard2.0;net461 + enable + + + + + + + From d5f37ddefb860a1705aaf76c49745bc7afdf4788 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sun, 15 Aug 2021 19:58:20 +0300 Subject: [PATCH 03/21] Update ref csproj --- .../ref/Microsoft.Extensions.DependencyModel.csproj | 2 ++ 1 file changed, 2 insertions(+) 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 63e93bddbd173..cd00f5e195cba 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.csproj @@ -5,6 +5,8 @@ + + From 7fc679c049eccdc7843ea65f60ec75f7740c4c00 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Mon, 16 Aug 2021 09:15:08 +0300 Subject: [PATCH 04/21] Dont use Assembly.CodeBase --- .../src/DependencyContextLoader.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs index b78bc26a553bd..8b030aab6ba95 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs @@ -141,7 +141,7 @@ private static bool IsEntryAssembly(Assembly assembly) string depsJsonFile = Path.ChangeExtension(assemblyLocation, DepsJsonExtension); bool depsJsonFileExists = _fileSystem.File.Exists(depsJsonFile); - +#if !NET5_0_OR_GREATER if (!depsJsonFileExists) { // in some cases (like .NET Framework shadow copy) the Assembly Location @@ -154,21 +154,17 @@ private static bool IsEntryAssembly(Assembly assembly) depsJsonFileExists = _fileSystem.File.Exists(depsJsonFile); } } +#endif return depsJsonFileExists ? depsJsonFile : null; } +#if !NET5_0_OR_GREATER private static string? GetNormalizedCodeBasePath(Assembly assembly) { -#if NETCOREAPP - string assemblyLocation = assembly.Location; -#else - string assemblyLocation = assembly.CodeBase; -#endif - - if (Uri.TryCreate(assemblyLocation, UriKind.Absolute, out Uri? codeBase) + if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out Uri? codeBase) && codeBase.IsFile) { return codeBase.LocalPath; @@ -178,5 +174,6 @@ private static bool IsEntryAssembly(Assembly assembly) return null; } } +#endif } } From da8ec2744a0dda1b11948ec2e31a37900a9509c3 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Tue, 17 Aug 2021 10:56:57 +0300 Subject: [PATCH 05/21] Add some Debug.Asserts --- .../src/DependencyContextJsonReader.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs index 12d99eade92a9..26157d605bea5 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs @@ -448,7 +448,8 @@ private IEnumerable ReadTargetLibraryDependencies(ref Utf8JsonReader while (reader.TryReadStringProperty(out string? name, out string? version)) { - dependencies.Add(new Dependency(Pool(name), Pool(version)!)); + Debug.Assert(version != null); + dependencies.Add(new Dependency(Pool(name), Pool(version))); } reader.CheckEndObject(); @@ -533,10 +534,12 @@ private List ReadTargetLibraryRuntimeTargets(ref Utf8Jso switch (propertyName) { case DependencyContextStrings.RidPropertyName: - runtimeTarget.Rid = Pool(propertyValue)!; + Debug.Assert(propertyValue != null); + runtimeTarget.Rid = Pool(propertyValue); break; case DependencyContextStrings.AssetTypePropertyName: - runtimeTarget.Type = Pool(propertyValue)!; + Debug.Assert(propertyValue != null); + runtimeTarget.Type = Pool(propertyValue); break; case DependencyContextStrings.AssemblyVersionPropertyName: runtimeTarget.AssemblyVersion = propertyValue; @@ -650,10 +653,12 @@ private LibraryStub ReadOneLibrary(ref Utf8JsonReader reader) reader.CheckEndObject(); + Debug.Assert(type != null); + return new LibraryStub() { Hash = hash, - Type = Pool(type)!, + Type = Pool(type), Serviceable = serviceable, Path = path, HashPath = hashPath, From 0ccc382fb739b5514ce0171a8003c07e1085424e Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Tue, 17 Aug 2021 11:02:00 +0300 Subject: [PATCH 06/21] One more assert --- .../src/Resolution/AppBaseCompilationAssemblyResolver.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs index bc1c24410e73a..87f31400fbbbd 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 @@ -74,7 +75,9 @@ public bool TryResolveAssemblyPaths(CompilationLibrary library, List ass 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)) { From ac62b42e6f2c283f89e6498b20a5f6c660b73ff4 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 18 Aug 2021 09:48:37 +0300 Subject: [PATCH 07/21] runtimeSignature can be null --- .../src/DependencyContextJsonReader.cs | 2 +- .../src/TargetInfo.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs index 26157d605bea5..7856261cce3e6 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs @@ -210,7 +210,7 @@ private DependencyContext Read(Utf8JsonReader reader) } return new DependencyContext( - new TargetInfo(framework, runtime, runtimeSignature ?? string.Empty, isPortable), + new TargetInfo(framework, runtime, runtimeSignature, isPortable), compilationOptions, CreateLibraries(compileTarget?.Libraries, false, libraryStubs).Cast().ToArray(), CreateLibraries(runtimeTarget.Libraries, true, libraryStubs).Cast().ToArray(), 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; } From 6ad9a5186015571c64028b1287b780d918e96778 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 18 Aug 2021 10:15:07 +0300 Subject: [PATCH 08/21] reader.GetString() can be null --- .../src/CompilationOptions.cs | 6 +-- .../src/DependencyContextExtensions.cs | 12 +++--- .../src/DependencyContextJsonReader.cs | 38 +++++++++++++------ .../src/DependencyContextWriter.cs | 4 +- .../src/RuntimeFallbacks.cs | 6 +-- .../src/Utf8JsonReaderExtensions.cs | 12 +++--- 6 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationOptions.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationOptions.cs index e38082ebe93bb..676e8a3a88d4f 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationOptions.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/CompilationOptions.cs @@ -9,7 +9,7 @@ namespace Microsoft.Extensions.DependencyModel { public class CompilationOptions { - public IReadOnlyList Defines { get; } + public IReadOnlyList Defines { get; } public string? LanguageVersion { get; } @@ -34,7 +34,7 @@ public class CompilationOptions public bool? GenerateXmlDocumentation { get; } public static CompilationOptions Default { get; } = new CompilationOptions( - defines: Enumerable.Empty(), + defines: Enumerable.Empty(), languageVersion: null, platform: null, allowUnsafe: null, @@ -47,7 +47,7 @@ public class CompilationOptions emitEntryPoint: null, generateXmlDocumentation: null); - public CompilationOptions(IEnumerable defines, + public CompilationOptions(IEnumerable defines, string? languageVersion, string? platform, bool? allowUnsafe, diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextExtensions.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextExtensions.cs index 11932bd2e5fdf..ee9f0be63e5b8 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextExtensions.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextExtensions.cs @@ -183,7 +183,7 @@ private static IEnumerable ResolveAssets( IEnumerable assets) { RuntimeFallbacks? fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); - IEnumerable rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty()); + IEnumerable rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty()); return SelectAssets(rids, assets); } @@ -193,13 +193,13 @@ private static IEnumerable ResolveRuntimeFiles( IEnumerable assets) { RuntimeFallbacks? fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); - IEnumerable rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty()); + 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); if (group != null) @@ -212,9 +212,9 @@ 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); if (group != null) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs index 7856261cce3e6..0238ec9e4ce06 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs @@ -272,7 +272,7 @@ private static void ReadRuntimeTarget(ref Utf8JsonReader reader, out string? run private static CompilationOptions ReadCompilationOptions(ref Utf8JsonReader reader) { - IEnumerable? defines = null; + IEnumerable? defines = null; string? languageVersion = null; string? platform = null; bool? allowUnsafe = null; @@ -336,7 +336,7 @@ private static CompilationOptions ReadCompilationOptions(ref Utf8JsonReader read reader.CheckEndObject(); return new CompilationOptions( - defines ?? Enumerable.Empty(), + defines ?? Enumerable.Empty(), languageVersion, platform, allowUnsafe, @@ -358,7 +358,10 @@ private List ReadTargets(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - targets.Add(ReadTarget(ref reader, reader.GetString()!)); + string? targetName = reader.GetString(); + Debug.Assert(targetName != null); + + targets.Add(ReadTarget(ref reader, targetName)); } reader.CheckEndObject(); @@ -374,7 +377,10 @@ private Target ReadTarget(ref Utf8JsonReader reader, string targetName) while (reader.Read() && reader.IsTokenTypeProperty()) { - libraries.Add(ReadTargetLibrary(ref reader, reader.GetString()!)); + string? targetLibraryName = reader.GetString(); + Debug.Assert(targetLibraryName != null); + + libraries.Add(ReadTargetLibrary(ref reader, targetLibraryName)); } reader.CheckEndObject(); @@ -448,6 +454,7 @@ private IEnumerable ReadTargetLibraryDependencies(ref Utf8JsonReader while (reader.TryReadStringProperty(out string? name, out string? version)) { + Debug.Assert(name != null); Debug.Assert(version != null); dependencies.Add(new Dependency(Pool(name), Pool(version))); } @@ -465,7 +472,8 @@ private static List ReadPropertyNames(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string libraryName = reader.GetString()!; + string? libraryName = reader.GetString(); + Debug.Assert(libraryName != null); reader.Skip(); runtimes.Add(libraryName); @@ -487,7 +495,8 @@ private static List ReadRuntimeFiles(ref Utf8JsonReader reader) string? assemblyVersion = null; string? fileVersion = null; - string path = reader.GetString()!; + string? path = reader.GetString(); + Debug.Assert(path != null); reader.ReadStartObject(); @@ -522,9 +531,12 @@ private List ReadTargetLibraryRuntimeTargets(ref Utf8Jso while (reader.Read() && reader.IsTokenTypeProperty()) { + string? path = reader.GetString(); + Debug.Assert(path != null); + var runtimeTarget = new RuntimeTargetEntryStub { - Path = reader.GetString()! + Path = path }; reader.ReadStartObject(); @@ -568,7 +580,7 @@ private List ReadTargetLibraryResources(ref Utf8JsonReader rea while (reader.Read() && reader.IsTokenTypeProperty()) { - string path = reader.GetString()!; + string? path = reader.GetString(); string? locale = null; reader.ReadStartObject(); @@ -585,6 +597,7 @@ private List ReadTargetLibraryResources(ref Utf8JsonReader rea if (locale != null) { + Debug.Assert(path != null); resources.Add(new ResourceAssembly(path, Pool(locale))); } } @@ -602,7 +615,8 @@ private Dictionary ReadLibraries(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string libraryName = reader.GetString()!; + string? libraryName = reader.GetString(); + Debug.Assert(libraryName != null); libraries.Add(Pool(libraryName), ReadOneLibrary(ref reader)); } @@ -674,8 +688,10 @@ 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(); + + Debug.Assert(runtime != null); runtimeFallbacks.Add(new RuntimeFallbacks(runtime, fallbacks)); } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs index 0e344b59cddfc..ececefc910df0 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); } 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/Utf8JsonReaderExtensions.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Utf8JsonReaderExtensions.cs index c5475c64504b9..807efa2eb71a6 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Utf8JsonReaderExtensions.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Utf8JsonReaderExtensions.cs @@ -15,19 +15,19 @@ internal static class Utf8JsonReaderExtensions public static bool IsTokenTypeProperty(this ref Utf8JsonReader reader) => reader.TokenType == JsonTokenType.PropertyName; - public static bool TryReadStringProperty(this ref Utf8JsonReader reader, [MaybeNullWhen(false)] out string name, out string? value) + public static bool TryReadStringProperty(this ref Utf8JsonReader reader, out string? name, out string? value) { name = null; value = null; if (reader.Read() && reader.IsTokenTypeProperty()) { - name = reader.GetString()!; + name = reader.GetString(); if (reader.Read()) { if (reader.TokenType == JsonTokenType.String) { - value = reader.GetString()!; + value = reader.GetString(); } else { @@ -63,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) @@ -71,11 +71,11 @@ 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) { - items.Add(reader.GetString()!); + items.Add(reader.GetString()); } if (reader.TokenType != JsonTokenType.EndArray) From 9ed265bd6fc2116826c1156536a30737280fce21 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 18 Aug 2021 10:17:11 +0300 Subject: [PATCH 09/21] Throw NRE when library is null --- .../src/DependencyContextWriter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs index ececefc910df0..499dc84be832d 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs @@ -171,9 +171,9 @@ private void WritePortableTarget(string key, IReadOnlyList runti Debug.Assert(compilationLibrary.RuntimeStoreManifestName == null); } - Library? library = (Library?)compilationLibrary ?? runtimeLibrary; + Library library = (Library?)compilationLibrary ?? runtimeLibrary ?? throw new NullReferenceException(nameof(library)); - WritePortableTargetLibrary(library?.Name + DependencyContextStrings.VersionSeparator + library?.Version, + WritePortableTargetLibrary(library.Name + DependencyContextStrings.VersionSeparator + library.Version, runtimeLibrary, compilationLibrary, jsonWriter); } jsonWriter.WriteEndObject(); From dde76da4c8e72ddc8e7fda139005c3cfc061cb13 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 18 Aug 2021 10:21:51 +0300 Subject: [PATCH 10/21] Update ref --- .../Microsoft.Extensions.DependencyModel.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) 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 15d6a72f52d5d..1b7079c055b20 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs @@ -4,8 +4,6 @@ // Changes to this file must follow the https://aka.ms/api-review process. // ------------------------------------------------------------------------------ -using System.Diagnostics.CodeAnalysis; - namespace Microsoft.DotNet.PlatformAbstractions { [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] @@ -37,7 +35,7 @@ public CompilationOptions(System.Collections.Generic.IEnumerable defines public bool? AllowUnsafe { 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; } } @@ -56,7 +54,7 @@ public partial struct Dependency 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([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] 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 @@ -150,9 +148,9 @@ public RuntimeAssetGroup(string runtime, params string[] assetPaths) { } } 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 @@ -173,11 +171,11 @@ public partial class RuntimeLibrary : Microsoft.Extensions.DependencyModel.Libra } 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 From b076ad30e90c0e4dae19979f09d99d20df4c928b Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 25 Aug 2021 22:58:35 +0300 Subject: [PATCH 11/21] CodeBase is obsolete --- .../src/DependencyContextLoader.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs index 8b030aab6ba95..b5806bbcf48f8 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs @@ -141,7 +141,7 @@ private static bool IsEntryAssembly(Assembly assembly) string depsJsonFile = Path.ChangeExtension(assemblyLocation, DepsJsonExtension); bool depsJsonFileExists = _fileSystem.File.Exists(depsJsonFile); -#if !NET5_0_OR_GREATER + if (!depsJsonFileExists) { // in some cases (like .NET Framework shadow copy) the Assembly Location @@ -154,17 +154,17 @@ private static bool IsEntryAssembly(Assembly assembly) depsJsonFileExists = _fileSystem.File.Exists(depsJsonFile); } } -#endif return depsJsonFileExists ? depsJsonFile : null; } -#if !NET5_0_OR_GREATER private static string? GetNormalizedCodeBasePath(Assembly assembly) { +#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; @@ -174,6 +174,5 @@ private static bool IsEntryAssembly(Assembly assembly) return null; } } -#endif } } From cc9dfca6420e8289a39193831e9d07b6a6a714a7 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 25 Aug 2021 23:04:29 +0300 Subject: [PATCH 12/21] Update ref for HashCodeCombiner --- .../ref/Microsoft.Extensions.DependencyModel.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 1db3da5c5ed1e..77f71b7f0603b 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; } } } From dd75fb015e78a3cb26d601acd5aeb64f8cb07823 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 25 Aug 2021 23:04:51 +0300 Subject: [PATCH 13/21] Annotate RuntimeAssetGroup --- .../ref/Microsoft.Extensions.DependencyModel.cs | 8 ++++---- .../src/RuntimeAssetGroup.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) 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 77f71b7f0603b..f443ef4f8493a 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs @@ -139,11 +139,11 @@ 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 diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssetGroup.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssetGroup.cs index 30c7042a97144..f303e642f6eec 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssetGroup.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssetGroup.cs @@ -11,15 +11,15 @@ public class RuntimeAssetGroup 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 From ffe7d773c65ac2798b4a1f979f3eb266bac11da8 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 25 Aug 2021 23:17:24 +0300 Subject: [PATCH 14/21] Null check resolvers --- .../AppBaseCompilationAssemblyResolver.cs | 16 +++++++++++++--- .../CompositeCompilationAssemblyResolver.cs | 13 ++++++++++++- .../PackageCompilationAssemblyResolver.cs | 14 ++++++++++++-- .../Resolution/ReferenceAssemblyPathResolver.cs | 14 ++++++++++++-- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs index 87f31400fbbbd..74b6425bca225 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs @@ -32,13 +32,23 @@ 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) { + if (library is null) + { + throw new ArgumentNullException(nameof(library)); + } + + if (assemblies is null) + { + throw new ArgumentNullException(nameof(assemblies)); + } + bool isProject = string.Equals(library.Type, "project", StringComparison.OrdinalIgnoreCase) || string.Equals(library.Type, "msbuildproject", StringComparison.OrdinalIgnoreCase); diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/CompositeCompilationAssemblyResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/CompositeCompilationAssemblyResolver.cs index aeeef0cdfc368..bcbff1330cab0 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,11 +12,21 @@ public class CompositeCompilationAssemblyResolver: ICompilationAssemblyResolver public CompositeCompilationAssemblyResolver(ICompilationAssemblyResolver[] resolvers) { - _resolvers = resolvers; + _resolvers = resolvers ?? throw new ArgumentNullException(nameof(resolvers)); } public bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies) { + if (library is null) + { + throw new ArgumentNullException(nameof(library)); + } + + if (assemblies is null) + { + throw new ArgumentNullException(nameof(assemblies)); + } + foreach (ICompilationAssemblyResolver resolver in _resolvers) { if (resolver.TryResolveAssemblyPaths(library, assemblies)) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs index 00091003bc18c..5ec46ef51c4e9 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs @@ -31,8 +31,8 @@ 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) @@ -73,6 +73,16 @@ internal static string[] GetDefaultProbeDirectories(IEnvironment environment) public bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies) { + if (library is null) + { + throw new ArgumentNullException(nameof(library)); + } + + if (assemblies is null) + { + throw new ArgumentNullException(nameof(assemblies)); + } + if (_nugetPackageDirectories == null || _nugetPackageDirectories.Length == 0 || !string.Equals(library.Type, "package", StringComparison.OrdinalIgnoreCase)) { diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs index 5690c3bb7d899..925a3c965b3e4 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs @@ -33,13 +33,23 @@ internal ReferenceAssemblyPathResolver(IFileSystem fileSystem, IEnvironment envi 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) { + if (library is null) + { + throw new ArgumentNullException(nameof(library)); + } + + if (assemblies is null) + { + throw new ArgumentNullException(nameof(assemblies)); + } + if (!string.Equals(library.Type, "referenceassembly", StringComparison.OrdinalIgnoreCase)) { return false; From 6d205a8c9ffa7e21960481331e416f7157bd71f9 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 25 Aug 2021 23:19:17 +0300 Subject: [PATCH 15/21] Update ReferenceAssemblyPathResolver's constructor --- .../ref/Microsoft.Extensions.DependencyModel.cs | 2 +- .../src/Resolution/ReferenceAssemblyPathResolver.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 f443ef4f8493a..01f8c22551f0a 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs @@ -210,7 +210,7 @@ public PackageCompilationAssemblyResolver(string nugetPackageDirectory) { } public partial class ReferenceAssemblyPathResolver : Microsoft.Extensions.DependencyModel.Resolution.ICompilationAssemblyResolver { public ReferenceAssemblyPathResolver() { } - public ReferenceAssemblyPathResolver(string defaultReferenceAssembliesPath, string[] fallbackSearchPaths) { } + public ReferenceAssemblyPathResolver(string? defaultReferenceAssembliesPath, string[] fallbackSearchPaths) { } public bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List assemblies) { throw null; } } } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs index 925a3c965b3e4..7bc5c2717eb38 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs @@ -19,7 +19,7 @@ public ReferenceAssemblyPathResolver() { } - public ReferenceAssemblyPathResolver(string defaultReferenceAssembliesPath, string[] fallbackSearchPaths) + public ReferenceAssemblyPathResolver(string? defaultReferenceAssembliesPath, string[] fallbackSearchPaths) : this(FileSystemWrapper.Default, defaultReferenceAssembliesPath, fallbackSearchPaths) { } From e7ac46c56de5026e45e059e264c956c00ed2ccb7 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 25 Aug 2021 23:21:04 +0300 Subject: [PATCH 16/21] Don't throw NRE explicitly --- .../src/DependencyContextWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs index 499dc84be832d..551c2d8922c11 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs @@ -171,7 +171,7 @@ private void WritePortableTarget(string key, IReadOnlyList runti Debug.Assert(compilationLibrary.RuntimeStoreManifestName == null); } - Library library = (Library?)compilationLibrary ?? runtimeLibrary ?? throw new NullReferenceException(nameof(library)); + Library library = (Library?)compilationLibrary ?? (Library)runtimeLibrary!; WritePortableTargetLibrary(library.Name + DependencyContextStrings.VersionSeparator + library.Version, runtimeLibrary, compilationLibrary, jsonWriter); From 57dee2460c49efd9a5c27d4a623f704d8b7c9733 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 25 Aug 2021 23:45:47 +0300 Subject: [PATCH 17/21] Remove Debug.Asserts TODO: Create an issue to remove ! --- .../src/DependencyContextJsonReader.cs | 42 +++++-------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs index 0238ec9e4ce06..c7f1363fde3ea 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs @@ -358,10 +358,7 @@ private List ReadTargets(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string? targetName = reader.GetString(); - Debug.Assert(targetName != null); - - targets.Add(ReadTarget(ref reader, targetName)); + targets.Add(ReadTarget(ref reader, reader.GetString()!)); } reader.CheckEndObject(); @@ -377,10 +374,7 @@ private Target ReadTarget(ref Utf8JsonReader reader, string targetName) while (reader.Read() && reader.IsTokenTypeProperty()) { - string? targetLibraryName = reader.GetString(); - Debug.Assert(targetLibraryName != null); - - libraries.Add(ReadTargetLibrary(ref reader, targetLibraryName)); + libraries.Add(ReadTargetLibrary(ref reader, reader.GetString()!)); } reader.CheckEndObject(); @@ -454,9 +448,7 @@ private IEnumerable ReadTargetLibraryDependencies(ref Utf8JsonReader while (reader.TryReadStringProperty(out string? name, out string? version)) { - Debug.Assert(name != null); - Debug.Assert(version != null); - dependencies.Add(new Dependency(Pool(name), Pool(version))); + dependencies.Add(new Dependency(Pool(name)!, Pool(version)!)); } reader.CheckEndObject(); @@ -472,8 +464,7 @@ private static List ReadPropertyNames(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string? libraryName = reader.GetString(); - Debug.Assert(libraryName != null); + string? libraryName = reader.GetString()!; reader.Skip(); runtimes.Add(libraryName); @@ -495,8 +486,7 @@ private static List ReadRuntimeFiles(ref Utf8JsonReader reader) string? assemblyVersion = null; string? fileVersion = null; - string? path = reader.GetString(); - Debug.Assert(path != null); + string? path = reader.GetString()!; reader.ReadStartObject(); @@ -531,12 +521,9 @@ private List ReadTargetLibraryRuntimeTargets(ref Utf8Jso while (reader.Read() && reader.IsTokenTypeProperty()) { - string? path = reader.GetString(); - Debug.Assert(path != null); - var runtimeTarget = new RuntimeTargetEntryStub { - Path = path + Path = reader.GetString()! }; reader.ReadStartObject(); @@ -546,12 +533,10 @@ private List ReadTargetLibraryRuntimeTargets(ref Utf8Jso switch (propertyName) { case DependencyContextStrings.RidPropertyName: - Debug.Assert(propertyValue != null); - runtimeTarget.Rid = Pool(propertyValue); + runtimeTarget.Rid = Pool(propertyValue)!; break; case DependencyContextStrings.AssetTypePropertyName: - Debug.Assert(propertyValue != null); - runtimeTarget.Type = Pool(propertyValue); + runtimeTarget.Type = Pool(propertyValue)!; break; case DependencyContextStrings.AssemblyVersionPropertyName: runtimeTarget.AssemblyVersion = propertyValue; @@ -580,7 +565,7 @@ private List ReadTargetLibraryResources(ref Utf8JsonReader rea while (reader.Read() && reader.IsTokenTypeProperty()) { - string? path = reader.GetString(); + string? path = reader.GetString()!; string? locale = null; reader.ReadStartObject(); @@ -597,7 +582,6 @@ private List ReadTargetLibraryResources(ref Utf8JsonReader rea if (locale != null) { - Debug.Assert(path != null); resources.Add(new ResourceAssembly(path, Pool(locale))); } } @@ -615,9 +599,7 @@ private Dictionary ReadLibraries(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string? libraryName = reader.GetString(); - Debug.Assert(libraryName != null); - + string? libraryName = reader.GetString()!; libraries.Add(Pool(libraryName), ReadOneLibrary(ref reader)); } @@ -667,12 +649,10 @@ private LibraryStub ReadOneLibrary(ref Utf8JsonReader reader) reader.CheckEndObject(); - Debug.Assert(type != null); - return new LibraryStub() { Hash = hash, - Type = Pool(type), + Type = Pool(type)!, Serviceable = serviceable, Path = path, HashPath = hashPath, From ee49e1f4abd55e4400071128d52f4b9ffad74736 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Thu, 26 Aug 2021 10:26:08 +0300 Subject: [PATCH 18/21] DisableImplicitAssemblyReferences --- .../src/Microsoft.Extensions.DependencyModel.csproj | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) 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 5b570852b3136..73949750fc3f1 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Microsoft.Extensions.DependencyModel.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Microsoft.Extensions.DependencyModel.csproj @@ -3,6 +3,8 @@ $(NetCoreAppCurrent);netstandard2.0;net461 enable true + + false Abstractions for reading `.deps` files. Commonly Used Types: @@ -35,13 +37,5 @@ Microsoft.Extensions.DependencyModel.DependencyContext - - - - - - - - From 75f22be499bd49542f93a16a342349251e18661d Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Thu, 26 Aug 2021 18:17:30 +0300 Subject: [PATCH 19/21] Use ProjectReference in ref --- .../ref/Microsoft.Extensions.DependencyModel.csproj | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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 cd00f5e195cba..107d05b171bfd 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.csproj @@ -10,10 +10,7 @@ - - - - - + + From ae167eb3487efd6b7acb801eafa398b0916804a6 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Wed, 1 Sep 2021 16:16:39 -0500 Subject: [PATCH 20/21] PR feedback and fix build --- .../src/DependencyContextJsonReader.cs | 27 +++++++++---------- .../src/DependencyContextLoader.cs | 1 + 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs index c7f1363fde3ea..127e905ff3d3a 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs @@ -193,7 +193,7 @@ private DependencyContext Read(Utf8JsonReader reader) Target? compileTarget = null; - Target? ridlessTarget = targets?.FirstOrDefault(t => !IsRuntimeTarget(t.Name)); + Target? ridlessTarget = targets.FirstOrDefault(t => !IsRuntimeTarget(t.Name)); if (ridlessTarget != null) { compileTarget = ridlessTarget; @@ -217,7 +217,7 @@ 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; @@ -464,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,7 +486,7 @@ private static List ReadRuntimeFiles(ref Utf8JsonReader reader) string? assemblyVersion = null; string? fileVersion = null; - string? path = reader.GetString()!; + string path = reader.GetString()!; reader.ReadStartObject(); @@ -533,10 +533,10 @@ private List ReadTargetLibraryRuntimeTargets(ref Utf8Jso switch (propertyName) { case DependencyContextStrings.RidPropertyName: - runtimeTarget.Rid = Pool(propertyValue)!; + runtimeTarget.Rid = Pool(propertyValue); break; case DependencyContextStrings.AssetTypePropertyName: - runtimeTarget.Type = Pool(propertyValue)!; + runtimeTarget.Type = Pool(propertyValue); break; case DependencyContextStrings.AssemblyVersionPropertyName: runtimeTarget.AssemblyVersion = propertyValue; @@ -565,7 +565,7 @@ private List ReadTargetLibraryResources(ref Utf8JsonReader rea while (reader.Read() && reader.IsTokenTypeProperty()) { - string? path = reader.GetString()!; + string path = reader.GetString()!; string? locale = null; reader.ReadStartObject(); @@ -599,7 +599,8 @@ 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)); } @@ -668,11 +669,9 @@ private static List ReadRuntimes(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string? runtime = reader.GetString(); + string runtime = reader.GetString()!; string?[] fallbacks = reader.ReadStringArray(); - Debug.Assert(runtime != null); - runtimeFallbacks.Add(new RuntimeFallbacks(runtime, fallbacks)); } @@ -719,7 +718,7 @@ private IEnumerable CreateLibraries(IEnumerable? librari 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) @@ -837,11 +836,11 @@ private struct TargetLibrary private struct RuntimeTargetEntryStub { - public string Type; + public string? Type; public string Path; - public string Rid; + public string? Rid; public string? AssemblyVersion; diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs index b5806bbcf48f8..2249ff69d8705 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs @@ -160,6 +160,7 @@ private static bool IsEntryAssembly(Assembly assembly) null; } + [RequiresAssemblyFiles] private static string? GetNormalizedCodeBasePath(Assembly assembly) { #pragma warning disable SYSLIB0012 // CodeBase is obsolete From 9a9a90fe1324cdbbfb261695bd1795f6a5a24c44 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Wed, 1 Sep 2021 17:52:38 -0500 Subject: [PATCH 21/21] Fix unit tests - * Don't throw for null assemblies in TryResolveAssemblyPaths * Fix up ref source * Fix RuntimeLibrary constructor overloads --- .../ref/Microsoft.Extensions.DependencyModel.cs | 14 +++++++------- .../AppBaseCompilationAssemblyResolver.cs | 7 +------ .../CompositeCompilationAssemblyResolver.cs | 12 +----------- .../src/Resolution/ICompilationAssemblyResolver.cs | 2 +- .../PackageCompilationAssemblyResolver.cs | 9 ++------- .../Resolution/ReferenceAssemblyPathResolver.cs | 9 ++------- .../src/RuntimeLibrary.cs | 4 ++-- 7 files changed, 16 insertions(+), 41 deletions(-) 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 01f8c22551f0a..f7ae0c80486ee 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs @@ -31,7 +31,7 @@ public partial class CompilationLibrary : Microsoft.Extensions.DependencyModel.L } 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 static Microsoft.Extensions.DependencyModel.CompilationOptions Default { get { throw null; } } @@ -46,7 +46,7 @@ public CompilationOptions(System.Collections.Generic.IEnumerable defines 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; @@ -184,12 +184,12 @@ 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 { @@ -199,19 +199,19 @@ public DotNetReferenceAssembliesPathResolver() { } } 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 bool TryResolveAssemblyPaths(Microsoft.Extensions.DependencyModel.CompilationLibrary library, System.Collections.Generic.List? assemblies) { throw null; } } } namespace System.Collections.Generic diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs index 74b6425bca225..e6d99ea4aec7a 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/AppBaseCompilationAssemblyResolver.cs @@ -37,18 +37,13 @@ internal AppBaseCompilationAssemblyResolver(IFileSystem fileSystem, string baseP _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)); } - if (assemblies is null) - { - throw new ArgumentNullException(nameof(assemblies)); - } - bool isProject = string.Equals(library.Type, "project", StringComparison.OrdinalIgnoreCase) || string.Equals(library.Type, "msbuildproject", StringComparison.OrdinalIgnoreCase); diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/CompositeCompilationAssemblyResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/CompositeCompilationAssemblyResolver.cs index bcbff1330cab0..fb758d084e977 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/CompositeCompilationAssemblyResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/CompositeCompilationAssemblyResolver.cs @@ -15,18 +15,8 @@ public CompositeCompilationAssemblyResolver(ICompilationAssemblyResolver[] resol _resolvers = resolvers ?? throw new ArgumentNullException(nameof(resolvers)); } - public bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies) + public bool TryResolveAssemblyPaths(CompilationLibrary library, List? assemblies) { - if (library is null) - { - throw new ArgumentNullException(nameof(library)); - } - - if (assemblies is null) - { - throw new ArgumentNullException(nameof(assemblies)); - } - foreach (ICompilationAssemblyResolver resolver in _resolvers) { if (resolver.TryResolveAssemblyPaths(library, assemblies)) 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 5ec46ef51c4e9..efb8cf12b91d6 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs @@ -71,18 +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 (assemblies is null) - { - throw new ArgumentNullException(nameof(assemblies)); - } - if (_nugetPackageDirectories == null || _nugetPackageDirectories.Length == 0 || !string.Equals(library.Type, "package", StringComparison.OrdinalIgnoreCase)) { @@ -97,7 +92,7 @@ public bool TryResolveAssemblyPaths(CompilationLibrary library, List ass { if (TryResolveFromPackagePath(_fileSystem, library, packagePath, out IEnumerable? fullPathsFromPackage)) { - assemblies.AddRange(fullPathsFromPackage); + assemblies?.AddRange(fullPathsFromPackage); return true; } } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs index 7bc5c2717eb38..39ccb9bf228fa 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/ReferenceAssemblyPathResolver.cs @@ -38,18 +38,13 @@ internal ReferenceAssemblyPathResolver(IFileSystem fileSystem, string? defaultRe _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 (assemblies is null) - { - throw new ArgumentNullException(nameof(assemblies)); - } - if (!string.Equals(library.Type, "referenceassembly", StringComparison.OrdinalIgnoreCase)) { return false; @@ -60,7 +55,7 @@ public bool TryResolveAssemblyPaths(CompilationLibrary library, List ass { throw new InvalidOperationException(SR.Format(SR.ReferenceAssemblyNotFound, assembly, library.Name)); } - assemblies.Add(fullName); + assemblies?.Add(fullName); } return true; } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeLibrary.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeLibrary.cs index 834c6638221a5..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,7 +35,7 @@ public RuntimeLibrary(string type, public RuntimeLibrary(string type, string name, string version, - string hash, + string? hash, IReadOnlyList runtimeAssemblyGroups, IReadOnlyList nativeLibraryGroups, IEnumerable resourceAssemblies,