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

Duplicate definition error in VS 2022 #10

Closed
Dimension4 opened this issue Nov 11, 2021 · 25 comments · Fixed by #11
Closed

Duplicate definition error in VS 2022 #10

Dimension4 opened this issue Nov 11, 2021 · 25 comments · Fixed by #11

Comments

@Dimension4
Copy link

I'm trying to upgrade my solution to VS 2022, but if I build it I'm getting duplicate definition errors that weren't there before with VS 2019. I guess this is caused by the new .net 6 sdk?

I think a simple future proof fix would be to wrap the source code in another #if block

#if !NET
...
#endif

since NET is defined for .net 5 and later.

@i2van
Copy link

i2van commented Nov 14, 2021

This also doesn't work in VS 2022 for netcoreapp3.1:

<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'">
  <PackageReference Include="IsExternalInit" Version="1.0.1">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  </PackageReference>
</ItemGroup>
Error CS0579: Duplicate 'DebuggerNonUserCode' attribute (58, 32)
Error CS0579: Duplicate 'ExcludeFromCodeCoverage' attribute (58, 7)
Error CS0101: The namespace 'System.Runtime.CompilerServices' already contains a definition for 'IsExternalInit' (60, 28)

@manuelroemer
Copy link
Owner

manuelroemer commented Nov 15, 2021

Hey, I tried to reproduce the issue, but cannot, at the moment. I created a new console app, added the package and the following code:

Console.WriteLine(new Message("Hello, World!"));

public record Message(string Mesage);

So far, everything works on my side. I successfully ran it using the netcoreapp3.1, net5.0 and net6.0 TFMs. I'm at .NET 6.0.100 and VS 17.1.0 Preview 1.0 (I only have the preview version installed right now, maybe it is that?).

Could you maybe upload a minimal example project which causes issues for you? Thanks a lot!

@i2van
Copy link

i2van commented Nov 15, 2021

@manuelroemer Could you please create alpha/beta package with fix proposed by topic starter (see 1st line of this file) and I'll test it on this project. master can not be built using VS 2022. net6_support branch - can.

@Dimension4
Copy link
Author

We're multi targeting .net framework 4.5.2, 4.8 and .net core 3.1. I can't do more testing at the moment because I had to uninstall VS 2022 since it also broke the builds on VS 2019.

@SWeini
Copy link
Contributor

SWeini commented Nov 15, 2021

I installed the .NET 6 SDK (no VS 2022 yet) and this makes my compilation fail as well. The project is a WPF project (UseWPF=true). Works like a charm when removing the Workaround from the .targets file.

Looks like they changed something in .NET 6, see dotnet/wpf#810

@manuelroemer
Copy link
Owner

manuelroemer commented Nov 15, 2021

Yeah, it seems like something in the SDK/tooling changed/got broken.
Not sure how to tackle this generally, but in the meantime I am totally happy with conditionally excluding the file's content via the proposed NET toggle. That might fix some of the issues without being a problem in the future. @i2van I uploaded a new package for testing it: 1.0.2-pre1. (Branch: https://github.com/manuelroemer/IsExternalInit/tree/hotfix/v1-0-2-pre/src)

In any case, if something is wrong with the SDK, there is not much I can do at the moment. I also don't have any time for investigating where the issue is rooted. Either someone else has an idea/makes a PR for this repo or the issue gets fixed upstream by the .NET guys.

Works like a charm when removing the Workaround from the .targets file.

@SWeini That's an interesting clue, thanks a lot! Just to clarify, do you mean these definitions here? If these are an issue, we could maybe remove them altogether (I think the issue that they worked around was eventually fixed in some .NET 5 SDK update).

@i2van
Copy link

i2van commented Nov 15, 2021

@manuelroemer It doesn't compile with new package out of the box. Looks like global.json sdk.version setting also matter. Adding file locally solves issues for both VS 2019 and 2022. Project I mentioned also uses WPF and has builds for 4.61, .NET Core 3.1, .NET 5/6

@i2van
Copy link

i2van commented Nov 15, 2021

global.json sdk.version build with new package under VS 2022:

.NET Result
5.0.400 OK
6.0.100 Error CS0101: The namespace 'System.Runtime.CompilerServices' already contains a definition for 'IsExternalInit'

@SWeini
Copy link
Contributor

SWeini commented Nov 16, 2021

We could also remove the .targets files completely. That's how the issue seems to be solved in https://github.com/manuelroemer/Nullable
Use .NET 5 SDK and set IncludePackageReferencesDuringMarkupCompilation to true or use .NET 6 SDK and use true by default.

If you want to be backwards-compatible to SDKs before .NET 5 you might still need that workaround. I haven't tested this with all possible versions.

@SWeini That's an interesting clue, thanks a lot! Just to clarify, do you mean these definitions here? If these are an issue, we could maybe remove them altogether (I think the issue that they worked around was eventually fixed in some .NET 5 SDK update).

@manuelroemer
Copy link
Owner

We could also remove the .targets files completely. [...] If you want to be backwards-compatible to SDKs before .NET 5 you might still need that workaround.

Yes, I think that removing the workaround is the best course of action. And I think the assumption that users have an up-to-date SDK (be it .NET 5 or .NET6+) is reasonable enough. Would you like to make this change in your PR? Would be much appreciated! 🍻
(Thanks for opening your current PR, by the way!)

@i2van
Copy link

i2van commented Nov 16, 2021

Could you upload package for testing, please?

@manuelroemer
Copy link
Owner

You mean with the change proposed by @SWeini? If so, yes, I'd upload a prerelease for testing before creating a "full" release.

@Dimension4
Copy link
Author

@manuelroemer sorry for bumping this, but what's the plan now? I see there's a PR waiting for approval, is there anything that needs to be done?

@manuelroemer
Copy link
Owner

manuelroemer commented Nov 22, 2021

@manuelroemer sorry for bumping this, but what's the plan now?

No worries, bumping is more than fair here! I merged #11 and uploaded the next prerelease version for testing: https://www.nuget.org/packages/IsExternalInit/1.0.2-pre2

If that still doesn't make things work, my last remaining hope is to revert #5 and #6, but ideally we can avoid that.

@Dimension4
Copy link
Author

@manuelroemer can confirm it works for me now 👍

@Dimension4
Copy link
Author

Will the fix be released as a proper version 1.0.2 or do we keep the 1.0.2-pre2 tag? Just so I know when to merge back into master in our code base :)

@i2van
Copy link

i2van commented Nov 23, 2021

Works for me.

@manuelroemer
Copy link
Owner

Will the fix be released as a proper version 1.0.2 or do we keep the 1.0.2-pre2 tag?

I'll release a 1.0.2 very soon!

@manuelroemer
Copy link
Owner

Done. Thanks to all of you for helping resolve this issue!

@Dimension4
Copy link
Author

Thanks for providing and updating the package!

@Dimension4
Copy link
Author

Dimension4 commented Nov 23, 2021

I think we've broken the old behavior now. If you try to use the new nuget package without having .net 6 sdk installed (but rather .net 5 sdk), builds break on VS 2019 with the same error

Error CS0518: Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported

@Dimension4
Copy link
Author

Ok it looks like it's not really broken, but the behavior changed a bit. Previously, we were able to just reference the nuget package (with .net 5) and it just worked. But now we also have to add

<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>

to make it work with .net 5. So now I can confirm it works with .net 6 as well as .net 5.

Sorry for the confusion 😅

@johanneshoehn
Copy link

@manuelroemer 1.0.1 is still the newest version on nuget.org. Was the 1.0.2 pulled, or something?

@i2van
Copy link

i2van commented Dec 13, 2021

@manuelroemer 1.0.1 is still the newest version on nuget.org. Was the 1.0.2 pulled, or something?

https://www.nuget.org/packages/IsExternalInit/1.0.2

It was unlisted for some reason. Now it's back.

@manuelroemer
Copy link
Owner

Hey, as @i2van said, it was unlisted, probably by accident when I unlisted the prerelease versions. In any case the full 1.0.2 is back now.

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