GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #1993

New issue
Copy link
New issue
Copy link
Closed
Closed
Analyzers (and possibly AOT) adds requirement for Key/ctor ordering#1993
Copy link
Assignees
AArnott
Labels
analyzersource generation
Milestone
v3.0

Description

@pkindruk
pkindruk
opened on Oct 9, 2024
Issue body actions

I 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:

  1. 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.
  2. Test2, Test3 - Requirement to make partial class doesn't make sense. Code gen still has to use constructor.
  3. 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; }
}

Activity

  1. AArnott commented on Oct 10, 2024

    @AArnott
    AArnott
    on Oct 10, 2024
    Collaborator
    More actions

    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. :)

  2. AArnott
    added this to the v3.0 milestone on Oct 10, 2024
  3. AArnott
    self-assigned this
    on Oct 10, 2024
  4. AArnott
    changed the title [-]Analyzer v3 detections for constructors/private members[/-] [+]Analyzers (and possibly AOT) adds requirement for Key/ctor ordering[/+] on Oct 10, 2024
  5. AArnott
    added
    source generation
    analyzer
    on Oct 10, 2024
  6. AArnott commented on Oct 10, 2024

    @AArnott
    AArnott
    on Oct 10, 2024
    Collaborator
    More actions

    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.

  7. AArnott
    added a commit that references this issue on Oct 10, 2024

    Add tests for adoption friction reported in #1993

    78289a0
  8. pkindruk commented on Oct 10, 2024

    @pkindruk
    pkindruk
    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.

    MessagePack-CSharp/src/MessagePack/Resolvers/DynamicObjectResolver.cs

    Line 1697 in 90203d8

    IReadOnlyDictionary<int, EmittableMember> ctorParamIndexIntMembersDictionary = intMembers.OrderBy(x => x.Key).Select((x, i) => (Key: x.Value, Index: i)).ToDictionary(x => x.Index, x => x.Key);

    Reacted by Andrew Arnott
  9. pkindruk commented on Oct 10, 2024

    @pkindruk
    pkindruk
    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>
  10. AArnott commented on Oct 11, 2024

    @AArnott
    AArnott
    on Oct 11, 2024
    Collaborator
    More actions

    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.

  11. pkindruk commented on Oct 11, 2024

    @pkindruk
    pkindruk
    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.

  12. AArnott commented on Oct 12, 2024

    @AArnott
    AArnott
    on Oct 12, 2024
    Collaborator
    More actions

    @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.

  13. pkindruk commented on Oct 12, 2024

    @pkindruk
    pkindruk
    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.

  14. AArnott commented on Oct 13, 2024

    @AArnott
    AArnott
    on Oct 13, 2024
    Collaborator
    More actions

    project template like that is what proposed to overcome this limitation

    What project template are you talking about?

  15. pkindruk commented on Oct 14, 2024

    @pkindruk
    pkindruk
    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.

  16. AArnott commented on Oct 15, 2024

    @AArnott
    AArnott
    on Oct 15, 2024
    Collaborator
    More actions

    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.

  17. AArnott
    closed this as completedon Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @AArnott
    AArnott

Labels

analyzersource generation

Type

No type

Projects

No projects

    Milestone

    • v3.0

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.