aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Add analyzer and code fix to recommend against IHeaderDictionary.Add

Open david-acker opened this issue 2 years ago • 11 comments

Description

  • Adds an analyzer which identifies invocations of IHeaderDictionary.Add.
  • Adds two code fixes, one for replacing Add with Append and another for replacing Add with the indexer.

Questions

  • Is there a preferred architecture or way of organizing new analyzers and code fixes? The analyzers and codes fixes in AspNetCore.Analyzers, Mvc.Analyzers, Mvc.Api.Analyzers, and Framework/AspNetCoreAnalyzers seemed to all have slightly different styles and ways for handling the analyzers, code fixes, tests, etc. I pulled from these when creating this draft, but didn't follow the style of any one of them too rigidly. [Resolved: Moved to Framework/AspNetCoreAnalyzers and used the analyzer and code fix pattern used there.]
  • What's the process for picking a rule ID for the new diagnostic? I used ASP0015 as a placeholder for now as this appeared to be the next one available, based on this list of diagnostics: Code analysis in ASP.NET Core apps. [Updated to use ASP0019]

Fixes #41362

david-acker avatar Oct 11 '22 01:10 david-acker

Thanks for your PR, @david-acker. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

ghost avatar Oct 11 '22 01:10 ghost

@david-acker Can you please put it in src/Framework/AspNetCoreAnalyzers and follow the pattern used there?

As for the ID, for now we're just incrementing the number (see https://github.com/dotnet/aspnetcore/blob/main/src/Framework/AspNetCoreAnalyzers/src/Analyzers/DiagnosticDescriptors.cs).

adityamandaleeka avatar Oct 12 '22 22:10 adityamandaleeka

@captainsafia @Youssef1313 Sounds good! I'll address the remaining comments within the next few days.

I'll also file an issue in the docs repo and submit a PR to update the diagnostics doc page that's linked above.

david-acker avatar Oct 22 '22 10:10 david-acker

@david-acker Looks like there's some test failures.

adityamandaleeka avatar Oct 26 '22 22:10 adityamandaleeka

@adityamandaleeka Hmm, that's odd. I'm not getting those test failures locally. I'll dig into this a bit.

david-acker avatar Oct 26 '22 23:10 david-acker

@adityamandaleeka Hmm, that's odd. I'm not getting those test failures locally. I'll dig into this a bit.

Seems to be a pesky line-ending issue. I wonder if there is a way to get the verifier to ignore these when comparing texts...

captainsafia avatar Oct 27 '22 04:10 captainsafia

@adityamandaleeka Hmm, that's odd. I'm not getting those test failures locally. I'll dig into this a bit.

Seems to be a pesky line-ending issue. I wonder if there is a way to get the verifier to ignore these when comparing texts...

I'm not sure if adding an editorconfig to the tests with end_of_line = crlf can fix the failure (or if it's the correct action).

cc @sharwell @CyrusNajmabadi for suggestions.

Youssef1313 avatar Oct 27 '22 08:10 Youssef1313

@captainsafia Could the failing test be skipped on Linux and get the PR merged?

Youssef1313 avatar Nov 14 '22 14:11 Youssef1313

@captainsafia Could the failing test be skipped on Linux and get the PR merged?

I'd like to see if trying

I'm not sure if adding an editorconfig to the tests with end_of_line = crlf can fix the failure (or if it's the correct action).

might help here.

We've had issues before where analyzer tests were not running and resulted in some unsavory unhandled exceptions. Let me see if I can tweak this PR to flow it along.

captainsafia avatar Nov 14 '22 16:11 captainsafia

Could the failing test be skipped on Linux and get the PR merged?

@david-acker Could you try that out? Apply the OSSkipCondition to the affected tests: https://github.com/dotnet/aspnetcore/blob/de3019b7dab5b5995942db1fbec6aa466c4af34c/src/Middleware/Diagnostics.EntityFrameworkCore/test/FunctionalTests/DatabaseErrorPageMiddlewareTest.cs#L97

BrennanConroy avatar Nov 23 '22 23:11 BrennanConroy

I updated the failing test to be skipped on Linux and macOS. I also reverted the last commit which added the .editorconfig file.

david-acker avatar Nov 24 '22 01:11 david-acker

@david-acker Thanks for submitting this PR! Documentation has already been handled in https://github.com/dotnet/aspnetcore/issues/45025#issuecomment-1326100598. Feel free to submit any updates to the docs if you'd like.

captainsafia avatar Nov 30 '22 03:11 captainsafia