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

PreprocessorSymbolNames are missing for .NET 5+ projects #191

Closed
richardwerkman opened this issue Dec 10, 2021 · 2 comments · Fixed by #192
Closed

PreprocessorSymbolNames are missing for .NET 5+ projects #191

richardwerkman opened this issue Dec 10, 2021 · 2 comments · Fixed by #192

Comments

@richardwerkman
Copy link

As mentioned on this page https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives I can use new symobols in preprocessor if like NETCOREAPP3_1_OR_GREATER which should be added if the targetframework is greater than 3.1.

If I use this in a project and build with Visual Studio the code is included. But when I analyse that same project with buildalyzer the preprocessor symbols are not added. Only the default symbols.

Reproduction

Reference .NET 6 and add to a file

    public int Bit()
    {
#if NETCOREAPP3_1_OR_GREATER
        return 1 ^ 2;
#endif
    }

This will compile in VS and dotnet build, but not in buildalyzer

Context

I'm not sure yet why they are missing in Buildalyzer. They should be added on SDK style projects. But there is a message saying: For traditional, non-SDK-style projects, you have to manually configure the conditional compilation symbols for the different target frameworks in Visual Studio via the project's properties pages.

So it might just be the case some SDK assembly is not loaded? Or if we have bad luck this is custom logic in MSBuild thats used in VS and the dotnet CLI.

Either way I can't compile the project that builalyzer returns... I also see no workaround as the symbols are added at parse time and can't be changed once the syntax tree has been parsed.

Issue was found in stryker-mutator/stryker-net#1828

@richardwerkman
Copy link
Author

I think I found where the preprocessor symbols are added in MSbuild: https://github.com/dotnet/sdk/blob/997376c6bc798af22633a8eb36074a0acec8a200/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets

@richardwerkman
Copy link
Author

I have found the issue. We create our syntax trees with ParseOptions based on what Buildalyzer tells us. I see Builalyzer does the same internally here https://github.com/daveaglick/Buildalyzer/blob/b2bd62140b2073d8a1ef4b30cecd1a2ce34038b5/src/Buildalyzer.Workspaces/AnalyzerResultExtensions.cs#L174

The issue is that the DefineConstants property is lacking the *_OR_GREATER constants. But I have found a property that contains all constants we need. It's private so we can't access it.

image

I'll try to create a PR that makes this public and will also fix the internal usage so Buildalyzer support the new preprocessor symbols from now on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant