andrewlock / StronglyTypedId

A Rosyln-powered generator for strongly-typed IDs
MIT License
1.5k stars 78 forks source link

Issue Defining Two or More Ids in the Same File #127

Closed evandcombs closed 6 months ago

evandcombs commented 6 months ago

I ran into an issue where when I define two ids in the same file the second id is generated outside of the namespace.

For example if I have a file with the code below.

namespace Example.Code;

[StronglyTypedId(backingType: StronglyTypedIdBackingType.Long,
    jsonConverter: StronglyTypedIdJsonConverter.SystemTextJson)]
public partial struct CategoryId {}

[StronglyTypedId(backingType: StronglyTypedIdBackingType.Long,
    jsonConverter: StronglyTypedIdJsonConverter.SystemTextJson)]
public partial struct CategoryGroupId {}

Generated Code

namespace ExampleCode
{
    [System.ComponentModel.TypeConverter(typeof(CategoryIdTypeConverter))]
    [System.Text.Json.Serialization.JsonConverter(typeof(CategoryIdSystemTextJsonConverter))]
    readonly partial struct CategoryId : System.IComparable<CategoryId>, System.IEquatable<CategoryId>
    {
       ...
    }
}

[System.ComponentModel.TypeConverter(typeof(CategoryIdTypeConverter))]
[System.Text.Json.Serialization.JsonConverter(typeof(CategoryIdSystemTextJsonConverter))]
readonly partial struct CategoryGroupId : System.IComparable<CategoryGroupId>, System.IEquatable<CategoryGroupId>
{
    ...
}
andrewlock commented 6 months ago

Hi there - I just tried to reproduce this with the latest version of the package, and it doesn't reproduce for me. I think this was related to using an older version of the Microsoft.CodeAnalysis.CSharp package. This was updated in 1.0.0-beta07 which seems to have resolved the issue.

Hope that helps - just FYI, note that 1.0.0-beta07 has a number of breaking changes!

evandcombs commented 6 months ago

That does seem to fix it, I forgot I needed to use prerelease for this project.