-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Group 1] Enable nullable annotations for Microsoft.Extensions.DependencyModel
#57445
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @eerhardt Issue DetailsReady for review, but not for merge Questions (look for
|
src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs
Outdated
Show resolved
Hide resolved
@@ -141,28 +141,30 @@ private string GetDepsJsonPath(Assembly assembly) | |||
|
|||
string depsJsonFile = Path.ChangeExtension(assemblyLocation, DepsJsonExtension); | |||
bool depsJsonFileExists = _fileSystem.File.Exists(depsJsonFile); | |||
|
|||
#if !NET5_0_OR_GREATER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we making this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assembly.CodeBase
is deprecated in .net5+: #31127
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think removing this code is a good idea for now. You should just suppress the Obsolete warning instead. Ex:
#pragma warning disable SYSLIB0012 // CodeBase is obsolete
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just suppressing the warning breaks CI:
Using member 'System.Reflection.Assembly.CodeBase' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. This member throws an exception for assemblies embedded in a single-file app.
src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs
Outdated
Show resolved
Hide resolved
@@ -357,7 +358,7 @@ private List<Target> ReadTargets(ref Utf8JsonReader reader) | |||
|
|||
while (reader.Read() && reader.IsTokenTypeProperty()) | |||
{ | |||
targets.Add(ReadTarget(ref reader, reader.GetString())); | |||
targets.Add(ReadTarget(ref reader, reader.GetString()!)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this !
is lying that this can't be null
. We should make the ReadTarget
method accept string? targetName
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar comment for ReadTargetLibrary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Experimented with it and looks like targetName
shouldn't be null, or code will NRE elsewhere:
- https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs#L195 (
IsRuntimeTarget
would crash ifName
is null) - https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs#L202 (
new TargetInfo(framework...)
a bit later would crash ifframework
is null) - https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs#L238 (
IsRuntimeTarget
would crash ifName
is null)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added Debug.Assert
here just in case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returned !
and created an issue #58139
src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyModel/src/Utf8JsonReaderExtensions.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyModel/src/Utf8JsonReaderExtensions.cs
Outdated
Show resolved
Hide resolved
# Conflicts: # src/libraries/Microsoft.Extensions.DependencyModel/src/Dependency.cs
...braries/Microsoft.Extensions.DependencyModel/src/Microsoft.Extensions.DependencyModel.csproj
Outdated
Show resolved
Hide resolved
...braries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.csproj
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyModel/src/RuntimeAssetGroup.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyModel/ref/Microsoft.Extensions.DependencyModel.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextWriter.cs
Outdated
Show resolved
Hide resolved
@@ -141,28 +141,30 @@ private string GetDepsJsonPath(Assembly assembly) | |||
|
|||
string depsJsonFile = Path.ChangeExtension(assemblyLocation, DepsJsonExtension); | |||
bool depsJsonFileExists = _fileSystem.File.Exists(depsJsonFile); | |||
|
|||
#if !NET5_0_OR_GREATER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think removing this code is a good idea for now. You should just suppress the Obsolete warning instead. Ex:
#pragma warning disable SYSLIB0012 // CodeBase is obsolete
src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs
Outdated
Show resolved
Hide resolved
TODO: Create an issue to remove !
* Don't throw for null assemblies in TryResolveAssemblyPaths * Fix up ref source * Fix RuntimeLibrary constructor overloads
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you again, @maxkoshevoi, for this work. This really helps!
I pushed a couple commits to this branch to get the build and unit tests passing. I also fixed a few nits and inconsistencies while I was working here.
I'll merge this once CI is green.
Related to #43605, #54012
Ready for review, but not for merge
Questions (look for
!
in the PR):GetNormalizedCodeBasePath.GetNormalizedCodeBasePath
is not used on net5+ due toAssembly.CodeBase
being obsolete. Is there some alternative to preserve previous behavior, or is it ok to leave it like this?Suppressed warning for now.
reader.GetString()
returns non-null value, but it can returnnull
.Should be resolved in: Remove
!
fromMicrosoft.Extensions.DependencyModel
#58139DependencyContextJsonReader.Pool(str)
returns non-null value, but it only does that for non-null when argument. Suppressed errors with!
for now.Should be resolved in: Remove
!
fromMicrosoft.Extensions.DependencyModel
#58139AppBaseCompilationAssemblyResolver.TryResolveAssemblyPaths
Path.GetDirectoryName(sharedPath)
might returnnull
, but this is not handled.Added
Debug.Assert
.