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

CA-1418 Validate platform string #4838

Merged

Conversation

buyaa-n
Copy link
Contributor

@buyaa-n buyaa-n commented Feb 12, 2021

Validates platform string

Provided in constructor arguments [SupportedOSPlatform("platform")], [UnsupportedOSPlatform("platform")] attributes and OperatingSystem.IsOSPlatform("platform"), OperatingSystem.IsOSPlatformVersionAtLeast("platform", 1) methods

Fixes dotnet/runtime#45851

  • Validates if platform name is known. The know platform names list is the combined set from two places:
    1. OperatingSystem.Is(PlatformName)VersionAtLeast. That's the target framework's known set of platforms.
    2. The project's MSBuild item group SupportedPlatform. This is the project's specific knowledge of known platforms. This allows class library authors to add custom platform name
  • Optionally validates version part if provided

Example:

class Some
{    
    public static void GuardedCall()
    {
        if (OperatingSystem.IsOSPlatform("x")) // The platform 'x' is not a known platform name
            SomeType.Api1();

        if (OperatingSystem.IsOSPlatformVersionAtLeast("x", 1)) // The platform 'x' is not a known platform name
            SomeType.Api1();
    }
}

class SomeType
{
    [SupportedOSPlatform("x")] // The platform 'x' is not a known platform name
    public static void Api1() { }

    [UnsupportedOSPlatform("y")] // The platform 'y' is not a known platform name
    public static void Api2() { }

    // The '7' is not a valid version. Please provide an input having two to four numbers separated with a dot.
    [UnsupportedOSPlatform("windows7")] 
    public event EventHandler Windows7Event;
}

@buyaa-n buyaa-n requested a review from a team as a code owner February 12, 2021 00:43
Copy link
Member

@jeffhandley jeffhandley left a comment

Choose a reason for hiding this comment

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

Looks good to me. I just have a question about the versions that might have been covered during the API review of this already: Should we respect the number of version segments each OSPlatform supports? Several only support 3 segments; some don't support versions at all.

@buyaa-n
Copy link
Contributor Author

buyaa-n commented Feb 12, 2021

Looks good to me. I just have a question about the versions that might have been covered during the API review of this already: Should we respect the number of version segments each OSPlatform supports? Several only support 3 segments; some don't support versions at all.

I don't think that is covered during API review, i am not sure if that information for all OS are available somewhere, if we could get that info we can add that check

@jeffhandley
Copy link
Member

If the platform was found in the OperatingSystem.Is<Platform> check, we could check to see if there's a ...VersionAtLeast function as well. If not, versions aren't supported; if so, then the min/max numbers of parameters would define the supported segments.

{
public class UseValidPlatformStringTest
{
private const string s_msBuildPlatforms = "build_property._SupportedPlatformList=CustomPlatform";
Copy link
Member

Choose a reason for hiding this comment

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

AFAIK, some editorconfig options doesn't do anything without something like [*.cs].

@mavasani Can you confirm whether this is correct for this specific option?

If so, then there is a test gap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

AFAIK, some editorconfig options doesn't do anything without something like [*.cs].

hm, not sure, i was using it like this previously and it still works here too

@carlossanlop carlossanlop added this to the .NET 6.x milestone Feb 16, 2021
@codecov
Copy link

codecov bot commented Feb 17, 2021

Codecov Report

Merging #4838 (6428d82) into release/6.0.1xx-preview2 (3c2b67e) will decrease coverage by 0.00%.
The diff coverage is 97.58%.

@@                     Coverage Diff                      @@
##           release/6.0.1xx-preview2    #4838      +/-   ##
============================================================
- Coverage                     95.61%   95.61%   -0.01%     
============================================================
  Files                          1167     1169       +2     
  Lines                        266899   267438     +539     
  Branches                      16134    16174      +40     
============================================================
+ Hits                         255206   255716     +510     
- Misses                         9613     9619       +6     
- Partials                       2080     2103      +23     

@buyaa-n
Copy link
Contributor Author

buyaa-n commented Feb 17, 2021

If the platform was found in the OperatingSystem.Is<Platform> check, we could check to see if there's a ...VersionAtLeast function as well. If not, versions aren't supported; if so, then the min/max numbers of parameters would define the supported segments.

@jeffhandley added validation for the number of version segments each OSPlatform supports, as version is optional we need only a max number of parameters. You might want to review messaging, sample messages are below cc @terrajobst :

// versions are not supported case:
[SupportedOSPlatform("Linux4.1")|}] // The platform 'Linux' is not versioned, please remove version part
public void SupportedLinux_41() { }

// a platform support Max 4 version segments
[SupportedOSPlatform("Android4.8.1.2.3")|}] // The '4.8.1.2.3' is not a valid version. Please provide an input having 2 to 4 numbers separated with a dot.
public void SupportedOSPlatformAndroid5Parts() { }

// a platform support Max 3 version segments
[UnsupportedOSPlatform("Ios14.1.2.3")] // The '14.1.2.3' is not a valid version. Please provide an input having 2 to 3 numbers separated with a dot.
public void UnsupportedOSPlatformIos4PartsInvalid() { }

@jeffhandley
Copy link
Member

I suggest the following messages. Each also shows using lower-case for the platform names in the messages to recommend that practice.

// versions are not supported case:
[SupportedOSPlatform("Linux4.1")|}] // Version '4.1' is not valid for the platform 'linux'. Do not use versions for this platform.
public void SupportedLinux_41() { }

// a platform support Max 4 version segments
[SupportedOSPlatform("Android4.8.1.2.3")|}] // Version '4.8.1.2.3' is not valid for the platform 'android'. Use a version with 2-4 parts for this platform.
public void SupportedOSPlatformAndroid5Parts() { }

// a platform support Max 3 version segments
[UnsupportedOSPlatform("Ios14.1.2.3")] // Version '14.1.2.3' is not valid for the platform 'ios'. Use a version with 2-3 parts for this platform.
public void UnsupportedOSPlatformIos4PartsInvalid() { }

// a platform supporting Max 2 version segments
[UnsupportedOSPlatform("foo1.2.3"] // Version '1.2.3' is not valid for the platform 'foo'. Use a version with 2 parts for this platform.

Copy link
Member

@jeffhandley jeffhandley left a comment

Choose a reason for hiding this comment

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

I have a couple suggestions and I also commented with suggested messages, but otherwise looks good to me. Once my feedback is addressed, I approve without needing to re-review.

@buyaa-n
Copy link
Contributor Author

buyaa-n commented Feb 18, 2021

I suggest the following messages. Each also shows using lower-case for the platform names in the messages to recommend that practice.

Thanks, I like the messages except the lower casing part, isn't it better to report back the platform names as it is provided by the user for expressing that there is nothing wrong with the platform name itself?

@jeffhandley
Copy link
Member

Thanks, I like the messages except the lower casing part, isn't it better to report back the platform names as it is provided by the user for expressing that there is nothing wrong with the platform name itself?

Fair enough; thanks for considering it.

@buyaa-n buyaa-n changed the base branch from master to release/6.0.1xx-preview2 February 24, 2021 21:37

if (constructorArguments.Length == 1)
{
if (constructorArguments[0].Value is string value)
Copy link
Contributor

Choose a reason for hiding this comment

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

This will throw if the argument kind is array: http://sourceroslyn.io/#Microsoft.CodeAnalysis/Symbols/TypedConstant.cs,79

I would suggest adding a .Kind != TypedConstantKind.Array before accessing .Value

Copy link
Contributor Author

@buyaa-n buyaa-n Feb 25, 2021

Choose a reason for hiding this comment

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

This is called only if the attribute were SupportedOSPlatform or UnsupportedOSPlatform attribute, which only accepts a string argument, if anyone calls them with an array type there will be a compiler error, so i think that is unnecessary, do you think that check is still needed?

@buyaa-n buyaa-n merged commit 6b1d2cf into dotnet:release/6.0.1xx-preview2 Feb 26, 2021
@buyaa-n buyaa-n deleted the validate_platform_string branch February 26, 2021 16:21
buyaa-n pushed a commit that referenced this pull request Mar 15, 2021
@jmarolf jmarolf removed this from the .NET 6.x milestone Apr 15, 2021
@jmarolf jmarolf mentioned this pull request Aug 16, 2021
@jmarolf jmarolf mentioned this pull request Aug 30, 2021
This was referenced Aug 31, 2021
This was referenced Sep 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refering to unknown platform names should result in warnings
6 participants