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

Namespace collision when used with WinUI 3 #219

Closed
wjk opened this issue Mar 19, 2021 · 10 comments
Closed

Namespace collision when used with WinUI 3 #219

wjk opened this issue Mar 19, 2021 · 10 comments

Comments

@wjk
Copy link

wjk commented Mar 19, 2021

When you link WinUI 3 into your app, it defines a namespace called Microsoft.System. CsWin32 outputs go into Microsoft.Windows.Sdk. Notice the common prefix. The generated code (ab)uses a strange quirk of the C# language by placing its using statements inside the namespace body, which changes the algorithm used to look up types. This results in using System.Runtime.InteropServices resolving to the namespace Microsoft.System.Runtime.InteropServices because of the common prefix. Of course, this namespace doesn’t exist, which breaks the build. Hence, CsWin32 and WinUI 3 cannot coexist in the same project.

The fix here is dead simple: Move the using statements to the top of the file, outside the namespace, which is where they should have been in the first place. When you do this, the using statements will be resolved exactly as they are specified. Thanks!

@AArnott
Copy link
Member

AArnott commented Mar 19, 2021

The fix here is dead simple: Move the using statements to the top of the file, outside the namespace, which is where they should have been in the first place.

That's one option. There are actually using directives that only work when inside the namespace, just FYI. That isn't the case here, but we already avoid this problem using the global:: prefix on our using directives.

What version of CsWin32 are you using that has this bug? It doesn't repro on 0.1.422-beta, I think.

image

@AArnott
Copy link
Member

AArnott commented Mar 19, 2021

See #124 for the original report and fix for the namespace collision.

@wjk
Copy link
Author

wjk commented Mar 19, 2021

You are correct. This has been fixed since I last used CsWin32 and WinUI 3 together. Thank you for your assistance.

@wjk wjk closed this as completed Mar 19, 2021
@dotMorten
Copy link

dotMorten commented Mar 31, 2021

Still seeing this issue. Repro steps:
1 Install Project Reunion Project Templates: https://marketplace.visualstudio.com/items?itemName=ProjectReunion.MicrosoftProjectReunion
2. Create a new Blank App, Packaged (WinUI 3 in Desktop) project.
3. Add CsWin32 nuget v0.1.422-beta
4. Add NativeMethods.txt and some interop methods to it.
5. Open MainWindow.xaml.cs, and in the constructor add a pinvoke method call.
6. Compile
7. Observe errors
image

Seems to happen when there's an XAML file in the project. Doesn't happen if there's no xaml in the project.

Repro sample:
App39.zip

Using VS16.10.p1

/cc @AArnott

@AArnott AArnott reopened this Mar 31, 2021
@AArnott
Copy link
Member

AArnott commented Mar 31, 2021

@dotMorten, can you navigate to the generated file and line that produces the error and copy the code snippet into the issue?

@dotMorten
Copy link

dotMorten commented Mar 31, 2021

@AArnott Here: Doesn't show any squiggles, so think it is still the xaml precompile step causing it
image

I figured changing the default namespace would fix the problem, but that doesn't help, so it isn't a namespace collision.

@AArnott
Copy link
Member

AArnott commented Mar 31, 2021

oh, it's complaining about your code, @dotMorten ? I don't know there's anything we can do about that. Try adding global:: in front of your fully-qualified types on that line.

@dotMorten
Copy link

@AArnott Well it's complaining about calling "your" code :-) It's not a namespace collision issue (see last line in my above comment).
I think the xaml precompile step compiles this code prior to the generated code getting included.

@AArnott
Copy link
Member

AArnott commented Mar 31, 2021

Ok, so this looks like a project reunion flavor of #7, but the WPF workaround is apparently not effective in Reunion. I filed https://github.com/dotnet/roslyn/issues/52306 to get this fixed.

@AArnott
Copy link
Member

AArnott commented Mar 31, 2021

This isn't a namespace collision with WinUI which is what this bug was tracking, so I'm going to re-close this issue. We'll open another issue in this repo to track the dotnet/roslyn one.

@AArnott AArnott closed this as completed Mar 31, 2021
AArnott pushed a commit that referenced this issue Jan 4, 2024
Bumps [xunit](https://github.com/xunit/xunit) from 2.5.0 to 2.5.1.
- [Commits](xunit/xunit@2.5.0...2.5.1)

---
updated-dependencies:
- dependency-name: xunit
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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

3 participants