dotnet / dotnet/sdk

Microsoft.DotNet.ApiCompat.Tool CP0012 mis-fires when replacing `virtual` with `override` across target frameworks

Open
#50,671 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### Describe the bug

A `TextWriter`-derived class declares a method as `virtual` on netstandard2.0 and `override` on netstandard2.1.
The APICompat tool

### To Reproduce

Compile this class in a project that multitargets netstandard2.0 and netstandard2.1:

```cs
class MyWriter : TextWriter
{
#if NETSTANDARD2_1_OR_GREATER
public override void Write(ReadOnlySpan buffer)
#else
public virtual void Write(ReadOnlySpan buffer)
#endif
{ }
}
```

(methods irrelevant to the repro omitted above for brevity.)

Expected: no issues

Actual:

> P0012: Cannot remove virtual keyword from member 'Nerdbank.Streams.BufferTextWriter.Write(System.ReadOnlySpan)'.

Whether a method is declared `virtual` or `override`, the caller's IL is the same (I believe), and therefore switching from one to the other is not a breaking change.

### Further technical details

microsoft.dotnet.apicompat.tool 9.0.304

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.