Fix LibraryImportDiagnosticsAnalyzer missing SYSLIB1051 for StringBuilder when StringMarshalling is set#126691
Fix LibraryImportDiagnosticsAnalyzer missing SYSLIB1051 for StringBuilder when StringMarshalling is set#126691
Conversation
|
Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag |
|
It seems Copilot could not make a fix before timing out. |
Add tests verifying that SYSLIB1051 (ParameterTypeNotSupported) is correctly reported for StringBuilder parameters when StringMarshalling is set to Utf16 or Utf8, both as standalone parameters and alongside string parameters with [Out] attribute. Regression test for #126687 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@danmoseley I had copilot crank locally for quite a while and it couldn't figure it out but it did add some regression tests that should cover your scenario and pass without any product changes. Can you get me a complog of your build that didn't report the diagnostic? Is it possible that you have |
There was a problem hiding this comment.
Pull request overview
This PR targets a regression in LibraryImportDiagnosticsAnalyzer where SYSLIB1051 is not reported for StringBuilder parameters when LibraryImportAttribute.StringMarshalling is specified, leading to silently incorrect forwarder stub generation.
Changes:
- Added analyzer unit tests asserting SYSLIB1051 (
ParameterTypeNotSupported) is reported forStringBuilderparameters withStringMarshallingset (Utf16/Utf8) and without it. - Added a reproduction-style test case for
string+[Out] StringBuilderparameter combinations.
| [Theory] | ||
| [InlineData("StringMarshalling = StringMarshalling.Utf16")] | ||
| [InlineData("StringMarshalling = StringMarshalling.Utf8")] | ||
| [InlineData("")] | ||
| public async Task StringBuilderNotSupported_ReportsDiagnostic(string stringMarshallingArg) | ||
| { | ||
| string marshallingPart = string.IsNullOrEmpty(stringMarshallingArg) | ||
| ? "" | ||
| : $", {stringMarshallingArg}"; | ||
|
|
||
| // StringBuilder as a simple parameter | ||
| string source = $$""" | ||
|
|
||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| partial class Test | ||
| { | ||
| [LibraryImport("DoesNotExist"{{marshallingPart}})] | ||
| public static partial void Method(StringBuilder {|#0:sb|}); | ||
| } | ||
| """; | ||
|
|
||
| await VerifyCS.VerifyAnalyzerAsync(source, | ||
| VerifyCS.Diagnostic(GeneratorDiagnostics.ParameterTypeNotSupported) | ||
| .WithLocation(0) | ||
| .WithArguments("System.Text.StringBuilder", "sb")); | ||
| } |
There was a problem hiding this comment.
The PR title/description indicates this change fixes a missing SYSLIB1051 diagnostic, but this diff only adds new expectations in tests. As written, these tests will fail until the analyzer behavior is updated; consider including the analyzer fix in this PR (or adjusting the PR scope/title and not landing failing tests).
| [InlineData("StringMarshalling = StringMarshalling.Utf8")] | ||
| public async Task StringBuilderNotSupported_WithStringParam_ReportsDiagnostic(string stringMarshallingArg) | ||
| { | ||
| // StringBuilder with [Out] alongside a string parameter (repro from dotnet/runtime#126687) |
There was a problem hiding this comment.
Please avoid embedding a GitHub issue reference in the test comment ("#126687"). Prefer a self-contained description of the scenario so the test remains stable even if issue trackers/links change.
| // StringBuilder with [Out] alongside a string parameter (repro from dotnet/runtime#126687) | |
| // StringBuilder with [Out] alongside a string parameter |
|
Note This review was generated by Copilot. 🤖 Copilot Code Review — PR #126691Holistic AssessmentMotivation: The PR adds regression tests for issue #126687, verifying that SYSLIB1051 ( Approach: Adding Summary: Detailed Findings
|
@jkoritzinsky I updated the repro (top post #126687) to include a global.json -- can you still not repro with that? |
|
@jkoritzinsky also added the install command for preview 3. Using preview 2, it doesn't repro. I verified this by hand again, and it does repro for me with global.json pointing to 11.0.100-preview.3.26170.106 C:\temp\repro>type global.json | find /i "ver"
"version": "11.0.100-preview.3.26170.106",
C:\temp\repro>type program.cs
using System;
using System.Runtime.InteropServices;
using System.Text;
Console.WriteLine("If you see this, SYSLIB1051 was NOT reported.");
static partial class NativeMethods
{
[LibraryImport("kernel32.dll", StringMarshalling = StringMarshalling.Utf16)]
internal static partial int GetVolumeNameForVolumeMountPointW(
string volumeMountPoint,
[Out] StringBuilder volumeName,
int bufferLength);
}
C:\temp\repro>dotnet build
Restore complete (0.3s)
info NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
doit net11.0 succeeded (0.1s) → bin\Debug\net11.0\doit.dll
Build succeeded in 0.9s
and |
If I understand right then there are two bugs -- the error no longer firing, and the generated code not working (as I discovered in my original PR) the issue with the generated code pasted above (I think) is that it doesn't have |
Description
LibraryImportDiagnosticsAnalyzerfails to report SYSLIB1051 forStringBuilderparameters whenStringMarshalling = StringMarshalling.Utf16is set on[LibraryImport]. WithoutStringMarshalling, the error is correctly reported. This is a regression introduced in preview.3 when diagnostic reporting was moved fromLibraryImportGeneratorto the separateLibraryImportDiagnosticsAnalyzer(PR #123780). When the diagnostic is silently dropped, the generator produces a forwarder[DllImport]stub that omitsCharSet = CharSet.Unicode, causing incorrect runtime marshalling behavior.Reproduction:
Changes
LibraryImportDiagnosticsAnalyzer.CalculateDiagnosticscode path to identify why the diagnostic is lost whenStringMarshallingis setStringBuilderreceivesNoMarshallingInforegardless ofStringMarshalling, andNotSupportedResolvershould emit the diagnostic — the exact point of loss needs further pinpointingTesting
Diagnostics.csusingVerifyCS.VerifyAnalyzerAsynccoveringStringBuilderwithStringMarshalling.Utf16expectingParameterTypeNotSupported(SYSLIB1051)