Skip to content

Change incorrect usage of ArgumentNullException #48243

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

Merged
merged 10 commits into from
May 19, 2023
Merged

Change incorrect usage of ArgumentNullException #48243

merged 10 commits into from
May 19, 2023

Conversation

captainsafia
Copy link
Member

@captainsafia captainsafia commented May 15, 2023

Following up on #47731.

@ghost ghost added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label May 15, 2023
@captainsafia captainsafia added area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels May 15, 2023
@ghost
Copy link

ghost commented May 15, 2023

Hey @dotnet/aspnet-build, looks like this PR is something you want to take a look at.

@@ -28,7 +28,8 @@ public class UserClaimsPrincipalFactory<TUser> : IUserClaimsPrincipalFactory<TUs
ArgumentNullThrowHelper.ThrowIfNull(userManager);
if (optionsAccessor == null || optionsAccessor.Value == null)
{
throw new ArgumentNullException(nameof(optionsAccessor));
ArgumentNullThrowHelper.ThrowIfNull(optionsAccessor);
throw new ArgumentException($"{nameof(optionsAccessor)} cannot wrap a null value.", nameof(optionsAccessor));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why two throw calls?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first one is conditional if optionsAccessor is null, the second one is for optionsAccessor.Value.

internal static partial class ArgumentThrowHelper
{
#if !NET7_0_OR_GREATER || NETSTANDARD ||NETFRAMEWORK
private const string EmptyString = "";
Copy link
Member

@BrennanConroy BrennanConroy May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, string.Empty is available in the versions targeted here so it should be fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me a while to figure out why this was needed. string.Empty isn't a const so you can't use it in equality checks. We also cannot use string.IsNullOrEmpty because it isn't annotated for nullability in netfx and netstandard.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't use it in equality checks

o.O
You mean can't use it with or? Why not just use || then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merp -- thought that was what I tried in b09309a but looks like I was wrong.

@@ -599,10 +599,7 @@ internal async Task SendFileAsync(string fileName, long offset, long? count, Can
// It's too expensive to validate the file attributes before opening the file. Open the file and then check the lengths.
// This all happens inside of ResponseStreamAsyncResult.
// TODO: Verbose log parameters
if (string.IsNullOrWhiteSpace(fileName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are losing the whitespace check, is that ok? A couple other spots have this too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Levi advised against ever using a WhiteSpace check because it does way more than you'd think.

{
throw new ArgumentNullException(nameof(applicationName));
}
ArgumentThrowHelper.ThrowIfNullOrEmpty(applicationName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it OK that these are theoretical breaking changes in the empty string case, changing the exception type?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I assume so, I believe dotnet/runtime does such things.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true, although I'm comfortable documenting this as a breaking change and explaining.

@@ -26,8 +30,10 @@ public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpres
#endif
}

#if !NET7_0_OR_GREATER
#if !NET7_0_OR_GREATER || NETSTANDARD || NETFRAMEWORK
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't #if !NET7_0_OR_GREATER include NETSTANDARD and NETFRAMEWORK? Assuming SDK style projects.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my understanding of the docs and the errors I was seeing in the build that resulted in this fix NET7_0_OR_GREATER is only set for projects that target a .NET Core version. They aren't set at all for netstandard or netfx projects.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. If NET7_0_OR_GREATER is not defined then you're in netstandard or netframework, which is what we want. And that's what #if !NET7_0_OR_GREATER does.

@captainsafia captainsafia merged commit 887c7b3 into main May 19, 2023
@captainsafia captainsafia deleted the 23671 branch May 19, 2023 01:40
@ghost ghost added this to the 8.0-preview5 milestone May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants