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
Adds trimming and AOT related attribute definitions for down-level TFMs #4580
Conversation
@@ -1,7 +1,7 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<PropertyGroup> | |||
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What exactly does "descending order" mean here? Is this still in "descending order"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out. This seems to originate here: #2015 (comment)
And that in turn says that VS Code only uses the first TFM in the list. So this would change that selection to .NET 6.
Personally I would argue that's a good thing. And it's also consistent with OpenTelemetry.csproj
which also starts with net6.0
.
src/OpenTelemetry.Api/Internal/Shims/DynamicallyAccessedMembersAttribute.cs
Outdated
Show resolved
Hide resolved
src/OpenTelemetry.Api/Internal/Shims/DynamicallyAccessedMemberTypes.cs
Outdated
Show resolved
Hide resolved
build/Common.props
Outdated
@@ -65,4 +65,12 @@ | |||
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\StatusHelper.cs" Link="Includes\StatusHelper.cs" /> | |||
</ItemGroup> | |||
|
|||
<ItemGroup Condition="'$(IncludeCodeAnalysisAttributes)'=='true' Or '$(IncludeInstrumentationHelpers)'=='true' Or '$(IncludeDiagnosticSourceInstrumentationHelpers)'=='true'"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Who sets IncludeCodeAnalysisAttributes
? I'm not seeing it used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently no one. So maybe I should remove it - it was meant to be used if a project wants to include the attributes. But I guess this is typically done one by one here. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of like the model of just including what you need. But others may have different opinions.
If no project is using the setting, I would drop it until someone uses it.
build/Common.props
Outdated
@@ -65,4 +65,12 @@ | |||
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\StatusHelper.cs" Link="Includes\StatusHelper.cs" /> | |||
</ItemGroup> | |||
|
|||
<ItemGroup Condition="'$(IncludeCodeAnalysisAttributes)'=='true' Or '$(IncludeInstrumentationHelpers)'=='true' Or '$(IncludeDiagnosticSourceInstrumentationHelpers)'=='true'"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it weird to include Or '$(IncludeInstrumentationHelpers)'=='true' Or '$(IncludeDiagnosticSourceInstrumentationHelpers)'=='true'
here? Why are they needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently this is not needed, but once I annotate PropertyFetcher this will be necessary because both of these properties will include PropertyFetcher.
Alternatively I could include these files in the same group as PropertyFetcher.
102f4fa
to
629224d
Compare
This approach looks good to me. But it looks like the build is broken: https://github.com/open-telemetry/opentelemetry-dotnet/actions/runs/5250186245/jobs/9483858384
|
@vitek-karas @eerhardt @utpilla @CodeBlanch: Throwing out potential alternatives for discussion. From the below prototype that fixed AOT warnings to the point where the app can successfully publish with AOT flag equals to true: main...vitek-karas:opentelemetry-dotnet:FixWarnings,
There are 28 places we'll need to add the preprocessors for the AOT attributes in order to bring the OTel packages to be AOT compatible. Performance-wise there should be no difference in using preprocessors or the approach in this PR. It's just different styling. Pros I can think of with the preprocessor approach is that it respects the API/SDK separation of OpenTelemetry - to prevent cross-cutting concerns and it also helps to keep the publicAPI of the API project clean. Another potential alternative is to use a mixture of both approaches - the preprocessor approach and directly define different include files in .csproj file. For the projects with a direct dependency on the SDK project, we leverage the .csproj file for conditional compilation between different .NET frameworks. And for the projects that references API projects directly, we use preprocessors. |
Can you prototype what this change would look like? Are you suggesting to just use If that is what you are proposing, you will still need to target |
As of right now, only the |
@vitek-karas and @eerhardt - |
I modified this to reduce the impact of the change. I kept the attribute definition files in Instead I retargeted |
It doesn't use it yet, but it will - and this is a good "test" that the compilation works.
Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
a99aa5d
to
7cc3277
Compare
@@ -15,4 +15,8 @@ | |||
<ItemGroup> | |||
<PackageReference Include="System.Diagnostics.DiagnosticSource" /> | |||
</ItemGroup> | |||
|
|||
<ItemGroup> | |||
<Compile Remove="Internal\Shims\*.*" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make more sense to put these in https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/Shared instead? I think it is a bit confusing to put them in this folder, but not actually used by this project.
v1.4.0 doesn't contain a net6.0 target, so we can't run ApiCompat against it. | ||
Instead, run net6.0's ApiCompat check against netstandard2.0. | ||
Remove this once 1.5.0 is released. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.5.0 is already released.
v1.4.0 doesn't contain a net6.0 target, so we can't run ApiCompat against it. | |
Instead, run net6.0's ApiCompat check against netstandard2.0. | |
Remove this once 1.5.0 is released. | |
v1.5.0 doesn't contain a net6.0 target, so we can't run ApiCompat against it. | |
Instead, run net6.0's ApiCompat check against netstandard2.0. | |
Remove this once 1.6.0 is released. |
Can you elaborate more on this? I know that AOT and trimming run different tools. But from a .NET library's perspective, shouldn't AOT warnings be a superset of trimming warnings? (assuming the underlying tools have no bugs). |
It's basically about the fact that AOT compiler in .NET 7 doesn't have fully compliant analysis engine, so it misreports some warnings (both reports warnings where it should not, and doesn't report them where it should). Since upgrading the AOT test to .NET 8 is not going to happen anytime soon (I doubt OTel would want to require SDK previews in CI/CD), adding a variation of the test which just runs the trimmer is a good workaround. |
@@ -2,7 +2,7 @@ | |||
|
|||
<PropertyGroup> | |||
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. --> | |||
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks> | |||
<TargetFrameworks>net6.0;netstandard2.0;net462</TargetFrameworks> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to still target net6.0
for this project? That was necessary for OpenTelemetry.Api because of InternalsVisibleTo. Is the same necessary here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - it has IVT to OpenTelemetry
, so basically the same problem as OpenTelemetry.Api
.
opentelemetry-dotnet/src/OpenTelemetry.Api.ProviderBuilderExtensions/AssemblyInfo.cs
Line 19 in 5f825cb
[assembly: InternalsVisibleTo("OpenTelemetry" + AssemblyInfo.PublicKey)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also in-line with the result of a discussion around retargeting to .NET 6. The conclusion was that retargeting projects which actually make use of the new attributes is a good solution.
This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or Pushing will instruct the bot to automatically remove the label. This bot runs once per day. |
Closed as inactive. Feel free to reopen if this PR is still being worked on. |
Related to #3429
Changes
Adds the definition of
RequiresUnreferencedCodeAttribute
,RequiresDynamicCodeAttribute
andDynamicallyAccessedMembersAttribute
when one is not provided by the framework (so for anything but .NET6+).There's a complexity associated with this change. Normally the attributes are added as
internal
to any build target which doesn't have them in the framework. For example, if an assembly is built fornetstandard2.0
aninternal
definition of the attributes would be added to the compilation. All the tools consuming the attributes only look at theNamespace.TypeName
of the attribute and they ignore which assembly the attribute came from.OpenTelemetry builds some assemblies for
net6.0
while other assemblies are justnetstandard2.0
. For example,OpenTelemetry
targetsnet6.0
, but its dependencyOpenTelemetry.Api
only targetsnetstandard2.0
. That would normally not be a problem since the added attribute definitions inOpenTelemetry.Api
areinternal
. Unfortunately,OpenTelemetry
hasInternalsVisibleTo
intoOpenTelemetry.Api
. When building thenet6.0
target ofOpenTelemetry
the compiler sees the attributes defined in the framework as well as the ones inOpenTelemetry.Api
.This situation has already been discussed in #4460 (comment).
The solution implemented in this change is to add
net6.0
as a target forOpenTelemetry.Api
. That this does is that when buildingOpenTelemetry
fornet6.0
it builds againstOpenTelemetry.Api
fornet6.0
as well and neither assembly will have the attributes defined internally, they will all come from the framework.This internal attribute definitions are in
OpenTelemetry.Api
project and are compiled into other assemblies as necessary. Part of this change moves theUnconditionalSuppressedMessageAttribute
(which is used for trimming/AOT suppressions) toOpenTelemetry.Api
as well to keep all of those attributes together.It also adds these attributes to the compilation whenever the
PropertyFetcher.cs
is added. This is currently not needed in this PR, but it will be needed in the future, and it was cleanest to do it here. It also allows for testing that the ifdefs are correct across the projects.The attribute definitions are copied from
dotnet/runtime
repo and I modified them to fit the code style in this repo - so I changed the file headers and some other small formatting changes. They are also defined asinternal
instead ofpublic
.Merge requirement checklist
CHANGELOG.md
files updated for non-trivial changes