-
Notifications
You must be signed in to change notification settings - Fork 6.1k
[Breaking change] DefineConstants for target frameworks not available at evaluation time
#52279
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
Open
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-defineconstants-evaluation-time
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
docs/core/compatibility/sdk/10.0/defineconstants-not-available-at-evaluation.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| --- | ||
| title: "Breaking change: `DefineConstants` for target frameworks not available at evaluation time" | ||
| description: "Learn about the breaking change in .NET 10 SDK where DefineConstants items for target frameworks are no longer available at MSBuild evaluation time." | ||
| ms.date: 03/12/2026 | ||
| ai-usage: ai-assisted | ||
| ms.custom: https://github.com/dotnet/docs/issues/51763 | ||
| --- | ||
|
|
||
| # `DefineConstants` for target frameworks not available at evaluation time | ||
|
|
||
| The target-framework-related `DefineConstants` items (such as `NET`, `NET9_0_OR_GREATER`, `NETSTANDARD2_0`, and similar constants) are no longer computed at MSBuild evaluation time. Using these constants in MSBuild `Condition` attributes in your project file will no longer work as expected. | ||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Version introduced | ||
|
|
||
| .NET 10 | ||
|
|
||
| ## Previous behavior | ||
|
|
||
| Previously, `DefineConstants` values like `NET`, `NETFRAMEWORK`, and `NETSTANDARD` (with optional version and `_OR_GREATER` suffixes, such as `NET452_OR_GREATER` or `NET9_0_OR_GREATER`) were available during MSBuild evaluation. This allowed you to use them directly in `Condition` attributes in your project file: | ||
|
|
||
| ```xml | ||
| <ItemGroup Condition="$(DefineConstants.Contains('NET9_0_OR_GREATER'))"> | ||
| <PackageReference Include="SomePackage" Version="1.0.0" /> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| ## New behavior | ||
|
|
||
| Starting in .NET 10, the target-framework `DefineConstants` values are computed in a `Target`, not during MSBuild evaluation. As a result, `Condition` attributes that inspect `DefineConstants` for TFM-related values evaluate before those values are set and no longer trigger as expected. | ||
|
|
||
| For example, the following condition no longer evaluates correctly because `NET9_0_OR_GREATER` isn't present in `DefineConstants` at evaluation time: | ||
|
|
||
| ```xml | ||
| <!-- This no longer works as expected --> | ||
| <ItemGroup Condition="$(DefineConstants.Contains('NET9_0_OR_GREATER'))"> | ||
| <PackageReference Include="SomePackage" Version="1.0.0" /> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| ## Type of breaking change | ||
|
|
||
| This change is a [behavioral change](../../categories.md#behavioral-change). | ||
|
|
||
| ## Reason for change | ||
|
|
||
| Direct access or manipulation of `DefineConstants` led to users accidentally overwriting the values. In addition, moving the computation into a `Target` enables more complex MSBuild orchestration scenarios required by other tools. | ||
|
|
||
| ## Recommended action | ||
|
|
||
| - **Do not** inspect `DefineConstants` for TFM-related values (for example, `NET9_0_OR_GREATER`, `NETSTANDARD2_0`, and similar) in MSBuild `Condition` attributes at evaluation time. | ||
| - **Do** use the documented [MSBuild `TargetFramework` and `TargetPlatform` functions](/visualstudio/msbuild/property-functions#msbuild-targetframework-and-targetplatform-functions) to check for target-framework compatibility. For example: | ||
|
|
||
| ```xml | ||
| <!-- Condition on net9.0 or later (frameworks compatible with net9.0) --> | ||
| <ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))"> | ||
| <PackageReference Include="SomePackage" Version="1.0.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <!-- Condition on frameworks not compatible with net8.0 --> | ||
| <ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))"> | ||
| <PackageReference Include="LegacyPackage" Version="2.0.0" /> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| - **Continue** to use `DefineConstants` in `Condition` attributes for constants that you define and control yourself. This change only affects the TFM-related constants that the SDK sets automatically. | ||
|
|
||
| ## Affected APIs | ||
|
|
||
| None. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.