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

Use #if with ! not operator and symbols for the 'Applies to' table rows consistenly #254

Open
StipoR opened this issue Dec 2, 2024 · 2 comments

Comments

@StipoR
Copy link

StipoR commented Dec 2, 2024

Describe the bug

When I want to test my shared project (.shproj) in my .NET Core 3.1 test project, I must also explicitly include .NET Standard 2.0 symbols (NETSTANDARD; NETSTANDARD2_0; NETSTANDARD2_0_OR_GREATER; NETSTANDARD1_6_OR_GREATER; NETSTANDARD1_5_OR_GREATER; NETSTANDARD1_4_OR_GREATER; NETSTANDARD1_3_OR_GREATER; NETSTANDARD1_2_OR_GREATER; NETSTANDARD1_1_OR_GREATER; NETSTANDARD1_0_OR_GREATER) in my test project because this shared project is part of a larger class library that targets also .NET Standard 2.0 and the code depends on these symbols being defined. This causes a build of my .NET Core 3.1 test project to fail with the following errors:

1>C:\Users\Stipo.nuget\packages\polyfill\7.5.0\contentFiles\cs\netstandard2.0\Polyfill\StringPolyfill.cs(23,16,23,20): error CS0121: The call is ambiguous between the following methods or properties: 'string.Join(char, params string?[])' and 'string.Join(string?, params string?[])'
1>C:\Users\Stipo.nuget\packages\polyfill\7.5.0\contentFiles\cs\netstandard2.0\Polyfill\StringPolyfill.cs(34,16,34,20): error CS0121: The call is ambiguous between the following methods or properties: 'string.Join(char, params object?[])' and 'string.Join(string?, params object?[])'
1>C:\Users\Stipo.nuget\packages\polyfill\7.5.0\contentFiles\cs\netstandard2.0\Polyfill\StringPolyfill.cs(45,16,45,20): error CS0121: The call is ambiguous between the following methods or properties: 'string.Join(char, string?[], int, int)' and 'string.Join(string?, string?[], int, int)'
1>C:\Users\Stipo.nuget\packages\polyfill\7.5.0\contentFiles\cs\netstandard2.0\Polyfill\StringPolyfill.cs(56,16,56,20): error CS0121: The call is ambiguous between the following methods or properties: 'string.Join(char, IEnumerable)' and 'string.Join(string?, IEnumerable)'

because .NET Core 3.1 has string.Join overloads with both string and char separator parameter.

One solution would be to use new string([separator]) instead of new([separator]) in the StringPolyfill.cs.

But the better solution would be to use the #if with the ! not operator and with the list of symbols for the 'Applies to' table rows consistently in the entire Polyfill source code.

So, #if NETSTANDARD2_0 || NETFRAMEWORK in the StringPolyfill.cs would be replaced
with #if !(NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER) because the 'Applies to' table of the string.Join(Char, String[]) method has the following rows:

.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard | 2.1

Another example is public static bool Contains(this string target, char value) from the Polyfill_String.cs.
Instead of #if NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0
use #if !(NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER) because the 'Applies to' table of the string.Contains(Char) method has the following rows:

.NET | Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard | 2.1

Using this approach consistently in the entire Polyfill source code aligns it better with the .NET documentation.

Minimal Repro

Extract the PolyfillSolution.zip and try building the MySharedProjectTest project.

@SimonCropp
Copy link
Owner

i though shproj were basically abandonware?

@StipoR
Copy link
Author

StipoR commented Dec 9, 2024

Shproj are still supported but they don't get much love since since the same thing can be done with globbing.

But that is irrelevant. We have a shared code that our class library imports and we have a test project that also imports and tests that shared code. Class library also targets .NET Standard 2.0 so we have NETSTANDARD* symbols in that shared code. In our test project we test the behavior of the .NET Standard shared code by targeting .NET Core 3.1. Since NETSTANDARD* symbols are not automatically defined in the test project we define them explicitly and that breaks the build of the Polyfill code since both NETSTANDARD* and NETCOREAPP* symbols are defined.

If the ! not operator and symbols for the 'Applies to' table rows would be used consistenly in the Polyfill source code, the problem would not occur.

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

No branches or pull requests

2 participants