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

Eliminate obsolete API warnings/errors in product source-build #5496

Merged
merged 1 commit into from
Nov 9, 2023

Conversation

NikolaMilosavljevic
Copy link
Contributor

@NikolaMilosavljevic NikolaMilosavljevic commented Nov 6, 2023

Bug

Fixes: NuGet/Home#12988

Regression? No

Description

When building nuget.client repo in full product source-build we see the following error:

/vmr/src/nuget-client/src/NuGet.Core/NuGet.Frameworks/FrameworkException.cs(17,88): error SYSLIB0051: 'Exception.Exception(SerializationInfo, StreamingContext)' is obsolete: 'This API supports obsolete formatter-based serialization. It should not be called or extended by application code.' (https://aka.ms/dotnet-warnings/SYSLIB0051) [/vmr/src/nuget-client/src/NuGet.Core/NuGet.Frameworks/NuGet.Frameworks.csproj::TargetFramework=net8.0]

To work around this, source-build process have been modifying Directory.Build.props during build to add a nowarn for SYSLIB0051. We are moving away from modifying sources during source-build and this needs to be fixed in the repo.

This PR adds the conditional Obsolete attribute to all affected APIs. Attribute is applicable on .NET 8.0+.

@NikolaMilosavljevic NikolaMilosavljevic requested a review from a team as a code owner November 6, 2023 19:18
@ghost ghost added the Community PRs created by someone not in the NuGet team label Nov 6, 2023
@NikolaMilosavljevic
Copy link
Contributor Author

@MichaelSimons
Copy link
Contributor

Taking a step back and looking at the source of the problem. Is there any chance of fixing the underlying issue? I see the type is public so maybe not without a breaking change. If not, would it be better to #pragma warning disable/restore the warning in code around the specific instance? The reason I mention this is to avoid the source-build specialization here.

@zivkan
Copy link
Member

zivkan commented Nov 6, 2023

The ironic thing is that we added these constructor overloads because roslyn CA code analyzers told us to! 🤦

Something else to note is that these constructors & ISerializable implementation is needed for cross-AppDomain support on .NET Framework, which NuGet still supports (largely due to Visual Studio). Having said that, NuGet doesn't use AppDomains, so we don't explicitly need cross-AppDomain support, but it goes to show the challenge of supporting .NET (Core) & .NET Framework at the same time. Depending on which TFM/LangVersion, you get contradictory analyzers 😭

@NikolaMilosavljevic
Copy link
Contributor Author

Taking a step back and looking at the source of the problem. Is there any chance of fixing the underlying issue? I see the type is public so maybe not without a breaking change. If not, would it be better to #pragma warning disable/restore the warning in code around the specific instance? The reason I mention this is to avoid the source-build specialization here.

Based on the guidance in the breaking change we should be able to add the following:

#if NET8_0_OR_GREATER
    [Obsolete(DiagnosticId = "SYSLIB0051")] // add this attribute to the serialization ctor
#endif

@NikolaMilosavljevic
Copy link
Contributor Author

NikolaMilosavljevic commented Nov 6, 2023

Taking a step back and looking at the source of the problem. Is there any chance of fixing the underlying issue? I see the type is public so maybe not without a breaking change. If not, would it be better to #pragma warning disable/restore the warning in code around the specific instance? The reason I mention this is to avoid the source-build specialization here.

Based on the guidance in the breaking change we should be able to add the following:

#if NET8_0_OR_GREATER
    [Obsolete(DiagnosticId = "SYSLIB0051")] // add this attribute to the serialization ctor
#endif

Fixed with 49ed7f4

Full source-build validation build is in progress: https://dev.azure.com/dnceng/internal/_build/results?buildId=2308863&view=results

cc @zivkan @MichaelSimons @mthalman

@@ -14,6 +14,9 @@ public FrameworkException(string message)
{
}

#if NET8_0_OR_GREATER
[Obsolete(DiagnosticId = "SYSLIB0051")] // add this attribute to the serialization ctor
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider replaceing the // add this attribute to the serialization ctor comment with a reference to the breaking change that remove the StreamingContext overload.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely - this was a simple copy from the guidance doc - will update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in fcb9c45

I think this should work - it points to the breaking change issue, describing the change and providing guidance for updating source code.

@NikolaMilosavljevic
Copy link
Contributor Author

There are more instances, about a dozen. I wonder if we should add a new NoWarn here instead of making all the source changes:

<!-- additional warnings new in .NET 6 that we need to disable when building with source-build -->
<NoWarn Condition="'$(DotNetBuildFromSource)' == 'true'">$(NoWarn);CS1998;CA1416;CS0618;CS1574</NoWarn>

@NikolaMilosavljevic
Copy link
Contributor Author

I've updated the PR with conditional obsolete attributes, per guidance in breaking change document: dotnet/docs#34893

@kartheekp-ms
Copy link
Contributor

This PR adds the nowarn to repo's Directory.Build.props file - conditioned for full product source-build. It does not affect any other build target.

Thank you for the contribution. However, I don't see any changes to the repo's Directory.Build.props file in the proposed changes. Could you please update the PR description to accurately reflect the proposed changes?

@NikolaMilosavljevic
Copy link
Contributor Author

This PR adds the nowarn to repo's Directory.Build.props file - conditioned for full product source-build. It does not affect any other build target.

Thank you for the contribution. However, I don't see any changes to the repo's Directory.Build.props file in the proposed changes. Could you please update the PR description to accurately reflect the proposed changes?

Thank you - will update the description. Implementation has changed based on PR discussion.

@NikolaMilosavljevic NikolaMilosavljevic changed the title Add nowarn for product source-build Eliminate obsolete API warnings/errors in product source-build Nov 8, 2023
@kartheekp-ms
Copy link
Contributor

kartheekp-ms commented Nov 9, 2023

The ironic thing is that we added these constructor overloads because roslyn CA code analyzers told us to! 🤦

Something else to note is that these constructors & ISerializable implementation is needed for cross-AppDomain support on .NET Framework, which NuGet still supports (largely due to Visual Studio). Having said that, NuGet doesn't use AppDomains, so we don't explicitly need cross-AppDomain support, but it goes to show the challenge of supporting .NET (Core) & .NET Framework at the same time. Depending on which TFM/LangVersion, you get contradictory analyzers 😭

I created a bug report dotnet/roslyn-analyzers#7026. I am also thinking to disable these analyzers from our code analysis ruleset file, remove [Serializable] attribute and constructor from the code as recommended in the dotnet/docs#34893 issue especially Microsoft strongly recommends against serialization libraries supporting the legacy serialization infrastructure ([Serializable] and ISerializable).

@kartheekp-ms kartheekp-ms merged commit 243a0c5 into NuGet:dev Nov 9, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PRs created by someone not in the NuGet team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Obsolete API error when building in source-build
6 participants