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

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

Open
teo-tsirpanis opened this issue Dec 27, 2024 · 1 comment
Labels
Area-NetSDK untriaged Request triage from a team member

Comments

@teo-tsirpanis
Copy link

Describe the bug

When building a project by setting DefineConstants from the command line, the NET***_OR_GREATER symbols are defined, but NET***, NET, and NETCOREAPP are not.

To Reproduce

Build a .NET 9 library with the following file with dotnet build -f net9.0 /p:DefineConstants=HELLOWORLD:

public class MyClass
{
#if NET
    public string _myString;
#endif

#if NET9_0_OR_GREATER
    public string MyString => _myString;
#endif
}

Exceptions (if any)

  net-sdk-repro failed with 1 error(s) (2,9s)
    C:\Users\teo\code\ephemeral\net-sdk-repro\Class1.cs(8,31): error CS0103: The name '_myString' does not exist in the current context

Build failed with 1 error(s) in 3,9s

Further technical details

I'm using .NET SDK 9.0.101.

After some investigation, the NET*** constants are defined here, and the NET***_OR_GREATER constants here. The former has no effect because because we are reassigning a global property, but the latter succeeds because we can reassign global properties inside a Target?

One way of fixing this is to update the NET*** constant definition to use the _ImplicitDefineConstant item, and move it to BeforeCommon.targets, which will also deduplicate the logic from C#, F# and VB. If that sounds good I can open a PR.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-NetSDK untriaged Request triage from a team member labels Dec 27, 2024
@baronfel
Copy link
Member

Good observation - this is one of a category of problems that we have due to the duplicate use of global properties. Your proposal sounds good to me, but there are a number of other places that the Roslyn compiler especially is called (Razor and WPF are the main ones I'm thinking of) so we'd need to coordinate/sequence the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-NetSDK untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

2 participants