Skip to content

Conversation

@teo-tsirpanis
Copy link
Contributor

This PR moves the logic that sets the implicit framework defines to the newly added GenerateTargetFrameworkDefineConstants target. Instead of directly modifying the DefineConstants property, this target modifies the _ImplicitDefineConstant item like the other targets do, which causes the defines to be set even if DefineConstants is specified as a global property. Language-specific logic was also deduplicated.

Fixes #45638. Validated locally by building the repro attached in the issue with a patched installation of the .NET SDK.

@Sergio0694
Copy link
Contributor

Would this (or, would you want to help) fix #43908 too by any chance? 😅

@marcpopMSFT marcpopMSFT requested a review from dsplaisted March 27, 2025 23:10
@marcpopMSFT marcpopMSFT removed the untriaged Request triage from a team member label Mar 27, 2025
Copy link
Member

@dsplaisted dsplaisted left a comment

Choose a reason for hiding this comment

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

This looks like it could be good, but I'm not aware that setting properties in a target lets you override global properties. @rainersigwald, Do you know if this is the case?

I know that you can use the TreatAsLocalProperty attribute on the Project element to override global properties during evaluation. It sounds like moving these to a target may have other benefits so if the target approach works probably it's best to stick with that.

It would also be nice to add a test case covering this. Would you like some help with that?

Copilot AI review requested due to automatic review settings August 24, 2025 17:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR moves the logic for generating implicit framework define constants from individual language-specific targets to a centralized GenerateTargetFrameworkDefineConstants target. This resolves an issue where framework defines were not being set when DefineConstants was specified as a global property.

  • Centralizes implicit framework define generation in a new target instead of directly modifying DefineConstants in language-specific files
  • Uses the _ImplicitDefineConstant item pattern consistently with other targets
  • Adds test coverage to verify the fix works when DefineConstants is provided via CLI

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Microsoft.NET.Sdk.BeforeCommon.targets Moves framework define logic to new GenerateTargetFrameworkDefineConstants target and updates target dependencies
Microsoft.NET.Sdk.CSharp.targets Removes duplicated framework define logic
Microsoft.NET.Sdk.FSharp.targets Removes duplicated framework define logic
Microsoft.NET.Sdk.VisualBasic.targets Removes duplicated framework define logic
GivenThatWeWantToBuildALibrary.cs Adds test cases to verify framework defines work with CLI-provided DefineConstants

.Pass();

var definedConstants = getValuesCommand.GetValues();
var expectedConstants = expectedDefines.Concat(addDefineFromCli ? ["HELLOWORLD"] : ["DEBUG", "TRACE"]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if skipping TRACE and the configuration define is the best possible behavior, but I'm not going to change it in this PR. We can either open an issue about it, or issue guidance to discourage people from setting DefineConstants from the command line.

@teo-tsirpanis
Copy link
Contributor Author

@dsplaisted I added tests and CI is passing. Can you take another look?

Copy link
Member

@dsplaisted dsplaisted left a comment

Choose a reason for hiding this comment

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

LGTM. @rainersigwald or @baronfel might want to take a quick look at this in case I'm missing anything.

If you want this to go into .NET 10, it needs to be retargeted or backported to the release/10.0.1xx branch.

@baronfel
Copy link
Member

This LGTM - it's important that we try and separate the user-influenceable knobs like DefineConstant from critical functionality, like the TFM/Runtime-specific constants, and this is a good step!

@dsplaisted dsplaisted changed the base branch from main to release/10.0.1xx August 26, 2025 13:40
@dsplaisted dsplaisted requested review from a team and tmat as code owners August 26, 2025 13:40
@dsplaisted dsplaisted changed the base branch from release/10.0.1xx to main August 26, 2025 13:40
@dsplaisted dsplaisted merged commit 4b4953e into dotnet:main Aug 26, 2025
27 checks passed
@dsplaisted
Copy link
Member

/backport to release/10.0.1xx

@github-actions
Copy link
Contributor

Started backporting to release/10.0.1xx: https://github.com/dotnet/sdk/actions/runs/17239981884

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconcistent definition of implicit framework defines when using /p:DefineConstants.

6 participants