GeezerVison is showing github.com — Analyzers (and possibly AOT) adds requirement for Key/ctor ordering · Issue #1993 · MessagePack-CSharp/MessagePack-CSharp
Analyzers (and possibly AOT) adds requirement for Key/ctor ordering #1993DescriptionI have tried MessagePack 3.0.134-beta on my project. So far new analyzer detections force a very strict style that requires a lot of refactoring to get it going. This might be not the best code, but on large code base refactoring is always a matter of time you can spend for this. Analyzer for 2.5.172 was fine with that code.
Questions:
Test1 - Why are we forced to start with 0 for constructor params? For me it is not always the case and I cannot change that.
Test2, Test3 - Requirement to make partial class doesn't make sense. Code gen still has to use constructor.
Test4 - Maybe analyzer should detect private member not annotated with attributes only when class is partial?
Common patterns detected:
[MessagePackObject]
public class Test1
{
[Key(1)]
//[Key("1")]// works fine with string keys
public string Name { get; }
[SerializationConstructor]
public Test1(string name) => Name = name;
}
[MessagePackObject]
public class Test2
{
private readonly string _name;
[Key(0)]
public string Name => _name;
[SerializationConstructor]
public Test2(string name) => _name = name;
}
// another way Test2 can implemented
[MessagePackObject]
public class Test3
{
[Key(0)]
public string Name { get; private set; }
[SerializationConstructor]
public Test3(string name) => Name = name;
}
[MessagePackObject]
public class Test4
{
private int? _cachedValue;// not serialized by design
[Key(0)]
public string Name { get; set; }
}ActivityAArnott commented on Oct 10, 2024 Thanks for trying out the beta and sharing your feedback. This is great to hear as we're closing down v3 and we want to identify adoption blockers.
Test1 - Why are we forced to start with 0 for constructor params? For me it is not always the case and I cannot change that.
I don't recall intentionally changing this behavior, but as I'm surprised that what you had worked in the first place, I may have inadvertently made the change.
I thought for ordinal indexed members, the indexes had to match the constructor parameter positions.
And apparently the diagnostic is simply in error, because on v3 it works as you have it written. But that's using the DynamicObjectResolver. Maybe the AOT source generation has more strict requirements. I'll have to look into that.
Test2, Test3 - Requirement to make partial class doesn't make sense. Code gen still has to use constructor.
Quite right. And I'm pleased to say that I just recently made fixes that makes these tests work the way you expect without any warnings. (#1990)
Test4 - Maybe analyzer should detect private member not annotated with attributes only when class is partial?
Also fixed by #1990. Not the way you propose, but the attributes are only required on public members by default. Once you start annotating non-public members, or use [MessagePackObject(AllowPrivate=true)], diagnostics for the non-public members start asking for you to apply them to all private members.
So all up, not too bad. Just Test1 survives as an issue to be fixed. :) changed the title [-]Analyzer v3 detections for constructors/private members[/-] [+]Analyzers (and possibly AOT) adds requirement for Key/ctor ordering[/+] on Oct 10, 2024AArnott commented on Oct 10, 2024 Actually, Test3 still repros. I wasn't testing with the analyzers turned on, because evidently we have a bug where the analyzers aren't always brought in. But I agree we should get that fixed too. pkindruk commented on Oct 10, 2024 ContributorAuthor More actions Good to hear that some problems were already fixed. It would be nice to have a fresh beta since a lot of work is done. I wasn't able to check half of the project due to analyzer errors, VS just won't let me build further.
I wasn't testing with the analyzers turned on, because evidently we have a bug where the analyzers aren't always brought in.
Yeah this is annoying, I tried using direct reference to source code from develop, but analyzer didn't work at all.
I thought for ordinal indexed members, the indexes had to match the constructor parameter positions.
DynamicObjectResolver does a little trick to remove the gaps.
|
IReadOnlyDictionary<int, EmittableMember> ctorParamIndexIntMembersDictionary = intMembers.OrderBy(x => x.Key).Select((x, i) => (Key: x.Value, Index: i)).ToDictionary(x => x.Index, x => x.Key); |
pkindruk commented on Oct 10, 2024 ContributorAuthor More actions I think it would be nice to have a project (somewhere in sandbox dir) for quick check on source generator changes as you are editing the code. This could be done with something like this:
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\tests\SourceGeneratorConsumer.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<GeneratedFolder>gen</GeneratedFolder>
<CompilerGeneratedFilesOutputPath>$(GeneratedFolder)\$(TargetFramework)</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<Compile Remove="$(GeneratedFolder)/**/*.cs" />
</ItemGroup>
</Project>AArnott commented on Oct 11, 2024 We have several test projects already (under the test/ folder) that we use to test. Some are essentially ordinary consumers and test execution behavior. Others focus on testing diagnostics, compilable generated code and code fixes. And another one 'records' source generated output and then tests verify it hasn't changes, so we have something of an LKG that reveals in git diff views what effect our changes have. Each approach has its pros and cons, so we have them all and try to use each one for its strengths. pkindruk commented on Oct 11, 2024 ContributorAuthor More actions I was talking of dummy project where you can quickly write some dirty code and see generated output. I'm not very familiar with source generators testing. I was looking for a project with EmitCompilerGeneratedFiles, but didn't find any))
After taking some time to get a base understanding of how this works I found a way to achieve what I want. The closest alternative would be to create a dummy test and enable WRITE_EXPECTED inside MessagePack.SourceGenerators.Tests. It seems more complicated that first approach, though it is a matter of taste. AArnott commented on Oct 12, 2024 @pkindruk You can also just declare a [MessagePackObject] attributed class in the ExecutionTests project. That runs the source generator in its most natural form, where you have to drill into the analyzers node in Solution Explorer to find the generated code.
The downside to this project is the source generator that runs is the version that was built at the time you opened Visual Studio. If you change the source generator, you don't see changes in the IDE for this ExecutionTests project until you build the source generator and restart the IDE. pkindruk commented on Oct 12, 2024 ContributorAuthor More actions Some blog posts about developing source generators mention this downside and project template like that is what proposed to overcome this limitation, so you don't have restart Visual Studio each time you make a little change in source generator.
Any way I don't insist it should be included. I can always set it up when I need to and just don't include changes on commit. AArnott commented on Oct 13, 2024
project template like that is what proposed to overcome this limitation
What project template are you talking about? pkindruk commented on Oct 14, 2024 ContributorAuthor More actions Template I mentioned above #1993 (comment). When you use EmitCompilerGeneratedFiles changes to generator are applied and generated files get updated when you build project without restarting VS. It is a bit of a hack where you have to look in generated folder rather than Analyzers node, but can be useful during development. AArnott commented on Oct 15, 2024 Oh, I see. Interesting technique. I'll have to stew on it for a bit to see how I'd use it in my workflow compared to the other techniques, but I didn't know about this, so I appreciate you sharing it with me.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|