Skip to content
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

Analyzer for methods with PlatformNotSupportedException body but missing platform attributes #42967

Closed
marek-safar opened this issue Oct 1, 2020 · 14 comments
Assignees
Labels
area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer Cost:M Work that requires one engineer up to 2 weeks

Comments

@marek-safar
Copy link
Contributor

marek-safar commented Oct 1, 2020

Describe the problem you are trying to solve

In .NET5 new attributes were introduced to support CA1416 analyzer rule to detect when unsupported APIs are called. To make the analyzer more effective existing libraries need to annotate their code with UnsupportedOSPlatformAttribute or SupportedOSPlatformAttribute for the analyzer to be able to work.

Describe suggestions on how to achieve the rule

The implementation could be as simple as check for throw PlatformNotSupportedException body with missing UnsupportedOSPlatformAttribute or SupportedOSPlatformAttribute anywhere in the method hierarchy.

@jeffhandley @terrajobst

EDIT: This should also include the work applying the rule to the runtime repo and addressing the warnings that appear.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-Infrastructure-libraries untriaged New issue has not been triaged by the area owner labels Oct 1, 2020
@ghost
Copy link

ghost commented Oct 1, 2020

Tagging subscribers to this area: @safern, @ViktorHofer
See info in area-owners.md if you want to be subscribed.

@marek-safar marek-safar added code-analyzer Marks an issue that suggests a Roslyn analyzer area-System.Runtime and removed area-Infrastructure-libraries labels Oct 1, 2020
@jeffhandley jeffhandley added this to the Future milestone Oct 3, 2020
@jeffhandley jeffhandley added api-suggestion Early API idea and discussion, it is NOT ready for implementation and removed untriaged New issue has not been triaged by the area owner labels Oct 3, 2020
@marek-safar marek-safar added the help wanted [up-for-grabs] Good issue for external contributors label Oct 11, 2020
@jeffhandley
Copy link
Member

Do I understand the following correctly?

  • This would be a new analyzer, separate from CA1416
  • Any time a method body throws PlatformNotSupportedException, and there are no [UnsupportedOSPlatform] or [SupportedOSPlatform] attributes, a diagnostic would be raised?
  • The analyzer would not attempt to discern what platform(s) are (un)supported
  • The diagnostic message would merely indicate that attributes should be applied, presumably with a link to the Platform Compatibility Analyzer documentation

@marek-safar
Copy link
Contributor Author

@jeffhandley correct

@buyaa-n
Copy link
Contributor

buyaa-n commented Dec 8, 2020

Plus to the @jeffhandley's list:

  • Containing type/assemblies attributes also need to be checked
  • Need to handle Throw helpers, probably if the method is not doing anything except throwing then should warn on the call site of those methods

Suggested Category: Interoperability
Suggested Severity: We might want to keep it hidden, it might be noisy

@buyaa-n buyaa-n added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Dec 8, 2020
@carlossanlop carlossanlop removed the help wanted [up-for-grabs] Good issue for external contributors label Jan 8, 2021
@buyaa-n buyaa-n modified the milestones: Future, 6.0.0 Jan 14, 2021
@jeffhandley jeffhandley changed the title Missing analyzer for methods with PlatformNotSupportedException body Analyzer for methods missing platform attributes with PlatformNotSupportedException body Jan 15, 2021
@jeffhandley jeffhandley changed the title Analyzer for methods missing platform attributes with PlatformNotSupportedException body Analyzer for methods with PlatformNotSupportedException body but missing platform attributes Jan 15, 2021
@buyaa-n buyaa-n added the Cost:S Work that requires one engineer up to 1 week label Jan 15, 2021
@buyaa-n
Copy link
Contributor

buyaa-n commented Jan 15, 2021

Even the analyzer implementation could be small applying it to runtime repo and fixing the warnings might need a lot more work, increasing size to medium

@buyaa-n buyaa-n added Cost:M Work that requires one engineer up to 2 weeks and removed Cost:S Work that requires one engineer up to 1 week labels Jan 15, 2021
@bartonjs
Copy link
Member

bartonjs commented Jan 15, 2021

Video

Because we don't currently have the ability to mark things as OS-specific based on parameter input, it can only be reliable for methods that are entirely throw new PlatformNotSupportedException(...). As such, we don't think there's sufficient value in it at this time.

When it is possible to do something more complicated the analyzer should not warn if the method is declared [Obsolete], in addition to the OS-based attributes.

@jeffhandley
Copy link
Member

@marek-safar @terrajobst Until we get through #44922 and find ways to handle all of the conditional PNSE scenarios, an analyzer warning for PNSE bodies would be too noisy.

My understanding is that the APIs we've marked with [SupportedOSPlatform] and [UnsupportedOSPlatform] were largely discovered through static analysis that found method bodies throwing PNSE; is it correct to assume we have likely found the vast majority of the simple cases of a body that very simply throwing the exception?

If we do revisit this analyzer proposal, we would likely want the diagnostic to surface on the line of code throwing the PlatformNotSupportedException as opposed to the method definition too, as that would allow for a cleaner #pragma warning disable suppression around the throw.

@marek-safar
Copy link
Contributor Author

is it correct to assume we have likely found the vast majority of the simple cases of a body that very simply throwing the exception?

That's not correct. We did a manual pass for one platform (and we missed many places) in .net5 and will be doing another one in .net6 for different platforms and another one in the future. Having a reliable tool/way to do that is beneficial over grep for throw and manual edits.

I'm also not sure I understand what would be noisy on a one-line throw PNSE body detection.

@jeffhandley
Copy link
Member

The one-line throw PNSE body detection could be done if we also have the analyzer respect [Obsolete] attributes (for APIs not implemented in .NET Core, where PNSE is orthogonal to OSPlatform). It would not be noisy. I thought we had already most of the one-line throw PNSE bodies that were related to OSPlatform; now I understand that we haven't. I found over 4500 PNSEs thrown across runtime (any many appear to be one-liners on first glance).

Would the value of the analyzer for one-liners be mostly for the existing cases, or to guard against us writing more of the one-liners? I don't know common it's going to be with the mobile platforms for us to introduce new one-liners.

@bartonjs
Copy link
Member

FWIW: If it makes sense to do as an internal analyzer there's nothing in the API review "no" that impedes that. Once it's done we can then decide that it makes sense as a public analyzer and move the code.

@terrajobst
Copy link
Member

Right, but the attribute is viral and the leaf nodes are public APIs...

@marek-safar
Copy link
Contributor Author

I don't know common it's going to be with the mobile platforms for us to introduce new one-liners.

Most PNSE in runtime are one liners and I think it's going to stay this way. I cannot tell how many are going to be added (wild guess between 10 and 100) but it won't be zero in every upcoming release.

@terrajobst
Copy link
Member

Related: #47228

@jeffhandley
Copy link
Member

Also related #44916.

@carlossanlop carlossanlop removed this from the 6.0.0 milestone Feb 23, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Mar 25, 2021
@carlossanlop carlossanlop removed the api-ready-for-review API is ready for review, it is NOT ready for implementation label Mar 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer Cost:M Work that requires one engineer up to 2 weeks
Projects
None yet
Development

No branches or pull requests

7 participants