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

Add ILLink Analyzers #15239

Merged
merged 7 commits into from
Jan 26, 2021
Merged

Add ILLink Analyzers #15239

merged 7 commits into from
Jan 26, 2021

Conversation

mateoatr
Copy link
Contributor

@mateoatr mateoatr commented Jan 8, 2021

Adds ILLinkAnalyzers. These help us report diagnostics about the usage of APIs that are potentially dangerous in the context of single-file applications as well as warnings related to the utilization of RequiresUnreferencedCode attribute.

The single-file analyzer was originally located in dotnet/roslyn-analyzers; it was recently moved to mono/linker. With this change we can fully migrate its ownership to the linker repo (and remove it from roslyn-analyzers).

@mateoatr mateoatr requested review from agocke, sbomer and tlakollo January 8, 2021 18:28
Copy link
Member

@sbomer sbomer left a comment

Choose a reason for hiding this comment

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

I'm not too familiar with the analyzers MSBuild logic, but it LGTM.

Copy link
Contributor

@tlakollo tlakollo 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'm curious if we plan to do something to enable it for visual basic as for other analyzers, but I guess that can be enabled in the future.

@mateoatr
Copy link
Contributor Author

Adding @jmarolf and @mavasani as they may know more about the MSBuild logic for analyzers in the SDK.

@mavasani
Copy link
Contributor

Just a confirmation - the target (master) branch of this PR targets .NET 6.0 SDK, correct?

@mateoatr
Copy link
Contributor Author

Just a confirmation - the target (master) branch of this PR targets .NET 6.0 SDK, correct?

Yes.

@@ -73,6 +73,7 @@
<PropertyGroup>
<!-- Dependencies from https://github.com/mono/linker -->
<MicrosoftNETILLinkTasksPackageVersion>6.0.0-alpha.1.21057.1</MicrosoftNETILLinkTasksPackageVersion>
<MicrosoftNETILLinkAnalyzerPackageVersion>$(MicrosoftNETILLinkTasksPackageVersion)</MicrosoftNETILLinkAnalyzerPackageVersion>
Copy link
Member

Choose a reason for hiding this comment

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

Is this sufficient to register the package in darc? Don't we also need to add an entry to Version.details.xml?

Copy link
Member

Choose a reason for hiding this comment

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

I think it is - since the MicrosoftNETILLinkTasksPackageVersion is already flowing. I find it a bit odd to tie the versions together, but this seems to be the pattern used elsewhere in sdk.

@@ -500,6 +499,45 @@ public void It_rewrites_the_apphost_for_non_single_file_publish()
appHostSize.Should().BeLessThan(singleFileSize);
}

[RequiresMSBuildVersionTheory("16.8.0")]
[InlineData("net6.0")]
public void ILLink_analyzer_warnings_are_produced(string targetFramework)
Copy link
Member

Choose a reason for hiding this comment

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

Note: if this succeeds it's probably because we still have a reference to the old package, as opposed to the new analyzers working. I think dotnet/linker#1745 is needed to fix the new analyzers.

Copy link
Member

@agocke agocke left a comment

Choose a reason for hiding this comment

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

LGTM

@mateoatr mateoatr merged commit 8e3c273 into dotnet:master Jan 26, 2021
@agocke
Copy link
Member

agocke commented Jan 26, 2021

@marcpopMSFT Do you know if this made the snap for 6.0 P1? I'd like to get it in if possible as it prevents a regression from 5.0.

@marcpopMSFT
Copy link
Member

@agocke I see it in the list of commits in the preview 1 branch so we should be good: https://github.com/dotnet/sdk/commits/release/6.0.1xx-preview1

jonpryor pushed a commit to dotnet/android that referenced this pull request Jun 9, 2021
Fixes: #5943

Context: https://github.com/dotnet/sdk/blob/c1b8e80be1f7cca767ed9f3c90a45d6830b166a0/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.Analyzers.targets#L92-L96
Context: https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file
Context: dotnet/sdk#15239

[.NET Single File Apps][0] are a .NET deployment construct in which
"all application-dependent files [are built] into a single binary."
It's conceptually similar to Xamarin.Android `.apk` files, and there
is some overlap in the "semantic [API incompatibilities][1]" between
"normal" desktop .NET apps and Xamarin.Android/single-file apps,
such as the (lack of) utility in `Assembly.Location`, which is the
empty string in Xamarin.Android apps when
`$(EmbedAssembliesIntoApk)`=True, which is required for "app store"
distribution.

For .NET 6+ app builds, enable analyzers which check for usage of
these "semantically changed" APIs, by setting
`$(EnableSingleFileAnalyzer)`=True.

Update `Mono.Android.csproj` and `Mono.Android.Export.csproj` to also
enable the single-file analyzers.

After enabling this analyzer, no warnings appeared. But I do see the
appropriate analyzers setup in projects now:

	Analyzer
	    ~\android-toolchain\dotnet\sdk\6.0.100-preview.6.21280.2\Sdks\Microsoft.NET.Sdk\targets\..\analyzers\ILLink.CodeFixProvider.dll
	        IsImplicitlyDefined = true
	    ~\android-toolchain\dotnet\sdk\6.0.100-preview.6.21280.2\Sdks\Microsoft.NET.Sdk\targets\..\analyzers\ILLink.RoslynAnalyzer.dll
	        IsImplicitlyDefined = true

[0]: https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file
[1]: https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file#api-incompatibility
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.

7 participants