-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Enable the ConvertToLibraryImport diagnostic by default #72819
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
Enable the ConvertToLibraryImport diagnostic by default #72819
Conversation
We want to enable this diagnostic by default so that users can see it in VS as a suggestion. Otherwise, users will not see the code fix to convert to source-generated interop unless they add an .editorconfig file and modify it to explicitly set a severity for the diagnostic.
|
Tagging subscribers to this area: @dotnet/interop-contrib Issue DetailsWe want to enable this diagnostic by default so that users can see it in VS as a suggestion. Otherwise, users will not see the code fix to convert to source-generated interop unless they add an .editorconfig file and modify it to explicitly set a severity for the diagnostic.
|
|
I'm all for this. @stephentoub Do you have any concerns on this front? |
|
Does it have any / many false positives? Is there any significant performance impact to the analyzer itself? And I assume this maps to our guidance moving forward which is that every use of DllImport in C# should be changed to LibraryImport unless there's a technical reason it can't (in which case the analyzer also won't fire)? |
Depends on your definition of false positive. If your definition is "will the build break after running the fix", then the answer is "sometimes". If the user doesn't have If your definition is "will the code-fix convert any P/Invokes using custom MarshalAs marshalling that is not supported by LibraryImport" (aka the special rules for built-in types that we got rid of like SafeArray or CustomMarshaler), then the answer is no. We do not have any false positives in this area. The only places where the user will have to change anything afterwards to get their code building is the above cases.
No
Yes, this maps to our guidance. |
|
Thanks. There are three levels here. For any given location: It seems like it's technically possible for every DllImport to be translated to a LibraryImport with enough work on the user's part, so (a) seems unnecessary to consider. But I'm very much against offering a fixer that results in the user's code breaking / failing to build. There are multiple ways to achieve that (e.g. multiple diagnostic IDs to differentiate), and if this is going to start making such suggestions to developers, and especially if there's a bulk auto-fix available (which would be nice to enable and looks like exists), we should ensure we're not causing the developer undue pain. For the AllowUnsafeBlocks not set case, the auto-fixer offered in that situation could even handle enabling it. |
This does seem to have negative impact. The issue is suppressions that get added and then never reviewed, which is very common. The issue is a user upgrades to NET 7 and sees a bunch of issues/noise. They then say, "not right now" and add a suppression for I'm leery of pushing something that could produce a bad first experience. Perhaps we should wait for turning this on by default. |
|
I agree with Stephen that option a) is not the best choice. If we don't have the analyzer on by default, people won't ever even notice that converting to LibraryImport unless they personally read the release notes for .NET 7. I strongly believe this will lead to a significant reduction in uptake of LibraryImport usage. The diagnostic from the analyzer doesn't break the build and doesn't show up in a command line build (only in VS and as a message), so it's quite non-intrusive if a user chooses not to use the code fix. I'm okay updating the code fix to automatically add the We can also update the analyzer/fixer to conditionally offer the fix so when the user is using a non-blittable user-defined type, we don't offer it (so option b). Also, we could add a warning annotation to the code-fix so the user can see which parameters they'll need to manually add marshalling support for after converting to LibraryImport (a variant on option c). |
What about warnings as errors? Is this the same thing or is a suggestion different? |
|
info/suggestion are the blue squiggles in VS. Info-level analyzers are design-time and aren't run as part of actual builds. They're a lower severity than warnings. |
If we can measure that sure, but the argument "it will hurt usage" with no way to measure it doesn't seem valid. |
Perfect. I'm fine with this then. |
|
I'd like to merge this in as-is and and open an issue for the following follow-up items:
@stephentoub what do you think of this plan? |
Can we get to those follow-ups, and in particular the second one (avoid breaking), as part of the same RC release as this PR will be in? If so, sounds good. |
|
Yes, we can get these follow ups in for RC1. We have enough time until the cut-off to get it in. |
We want to enable this diagnostic by default so that users can see it in VS as a suggestion. Otherwise, users will not see the code fix to convert to source-generated interop unless they add an .editorconfig file and modify it to explicitly set a severity for the diagnostic.