Skip to content

Commit f41023e

Browse files
authored
Add breaking change for preprocessor symbols (#20848)
1 parent 6633c3b commit f41023e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

docs/core/compatibility/msbuild.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ The following breaking changes are documented on this page:
99

1010
| Breaking change | Version introduced |
1111
| - | - |
12+
| [NETCOREAPP3_1 preprocessor symbol is not defined when targeting .NET 5](#netcoreapp3_1-preprocessor-symbol-is-not-defined-when-targeting-net-5) | 5.0 |
1213
| [PublishDepsFilePath behavior change](#publishdepsfilepath-behavior-change) | 5.0 |
1314
| [Directory.Packages.props files is imported by default](#directorypackagesprops-files-is-imported-by-default) | 5.0 |
1415
| [Resource manifest file name change](#resource-manifest-file-name-change) | 3.0 |
1516

1617
## .NET 5.0
1718

19+
[!INCLUDE [netcoreapp3_1-preprocessor-symbol-not-defined](../../../includes/core-changes/msbuild/5.0/netcoreapp3_1-preprocessor-symbol-not-defined.md)]
20+
21+
***
22+
1823
[!INCLUDE [publishdepsfilepath-behavior-change](../../../includes/core-changes/msbuild/5.0/publishdepsfilepath-behavior-change.md)]
1924

2025
***
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
### NETCOREAPP3_1 preprocessor symbol is not defined when targeting .NET 5
2+
3+
In .NET 5.0 RC2 and later versions, projects no longer define preprocessor symbols for earlier versions, but only for the version that they target. This is the same behavior as .NET Core 1.0 - 3.1.
4+
5+
#### Version introduced
6+
7+
5.0 RC2
8+
9+
#### Change description
10+
11+
In .NET 5.0 preview 7 through RC1, projects that target `net5.0` define both `NETCOREAPP3_1` and `NET5_0` preprocessor symbols. The intent behind this behavior change was that starting with .NET 5.0, conditional compilation [symbols would be cumulative](https://github.com/dotnet/designs/blob/main/accepted/2020/net5/net5.md#preprocessor-symbols).
12+
13+
In .NET 5.0 RC2 and later, projects only define symbols for the target framework monikers (TFM) that it targets and not for any earlier versions.
14+
15+
#### Reason for change
16+
17+
The change from preview 7 was reverted due to customer feedback. Defining symbols for earlier versions surprised and confused customers, and some assumed it was a bug in the C# compiler.
18+
19+
#### Recommended action
20+
21+
Make sure that your `#if` logic doesn't assume that `NETCOREAPP3_1` is defined when the project targets `net5.0` or higher. Instead, assume that `NETCOREAPP3_1` is only defined when the project explicitly targets `netcoreapp3.1`.
22+
23+
For example, if your project multitargets for .NET Core 2.1 and .NET Core 3.1 and you call APIs that were introduced in .NET Core 3.1, your `#if` logic should look as follows:
24+
25+
```csharp
26+
#if NETCOREAPP2_1 || NETCOREAPP3_0
27+
// Fallback behavior for old versions.
28+
#elif NETCOREAPP
29+
// Behavior for .NET Core 3.1 and later.
30+
#endif
31+
```
32+
33+
#### Category
34+
35+
MSBuild
36+
37+
#### Affected APIs
38+
39+
N/A
40+
41+
<!--
42+
43+
#### Affected APIs
44+
45+
Not detectable via API analysis.
46+
47+
-->

0 commit comments

Comments
 (0)