Closed pnquest closed 1 year ago
As I'm using macOS I don't have access to VS and it works with the cli and in Rider.
Does it work again if you downgrade to 2.8.0
?
@latonz It works fine in 2.8.0
. This behavior only appears when using 3.0.0
. Definitely a weird one.
As I'm using macOS I don't have access to VS and it works with the cli and in Rider.
Just tried it in rider and it works with 3.0.0
public partial global::ToObject Map(global::FromObject input)
{
var target = new global::ToObject();
target.StringField = input.Stringfield;
return target;
}
I can try VS in a minute
a bit more digging and I was able to get a stack trace from the generator:
'System.InvalidOperationException: Could not resolve enum reflection type of PropertyNameMappingStrategy or Riok.Mapperly.Abstractions.PropertyNameMappingStrategy is not supported
at Riok.Mapperly.Configuration.AttributeDataAccessor.GetEnumValue(TypedConstant arg, Type targetType) in /_/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs:line 137
at Riok.Mapperly.Configuration.AttributeDataAccessor.BuildArgumentValue(TypedConstant arg, Type targetType) in /_/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs:line 96
at Riok.Mapperly.Configuration.AttributeDataAccessor.<Access>d__5`2.MoveNext() in /_/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs:line 56
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at Riok.Mapperly.Configuration.MapperConfiguration..ctor(SymbolAccessor symbolAccessor, ISymbol mapperSymbol) in /_/src/Riok.Mapperly/Configuration/MapperConfiguration.cs:line 16
at Riok.Mapperly.Descriptors.DescriptorBuilder..ctor(Compilation compilation, ClassDeclarationSyntax mapperSyntax, INamedTypeSymbol mapperSymbol, WellKnownTypes wellKnownTypes, SymbolAccessor symbolAccessor) in /_/src/Riok.Mapperly/Descriptors/DescriptorBuilder.cs:line 36
at Riok.Mapperly.MapperGenerator.BuildDescriptors(Compilation compilation, ImmutableArray`1 mappers, CancellationToken cancellationToken) in /_/src/Riok.Mapperly/MapperGenerator.cs:line 86
at Riok.Mapperly.MapperGenerator.<>c.<Initialize>b__4_1(ValueTuple`2 x, CancellationToken cancellationToken) in /_/src/Riok.Mapperly/MapperGenerator.cs:line 32
at Microsoft.CodeAnalysis.UserFunctionExtensions.<>c__DisplayClass0_0`2.<WrapUserFunction>b__0(TInput input, CancellationToken token)'.
Line numbers seem to have changed a bit, but that looks to be from here. https://github.com/riok/mapperly/blob/main/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs#L186
So it looks like this is returning null: https://github.com/riok/mapperly/blob/main/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs#L191
Maybe visual studio is loading libraries into the assembly differently than the cli.
Nice digging 👍
I can recreate the issue, VS doesn't run for me. Even after restarting
I'm assuming this applies to all Mapperly configurations enums, I'll experiment a little when I get the chance.
@latonz do you have a solution in mind? I'm considering:
I'm wondering what changed between 2.8.0 and 3.0.0 that this doesn't work anymore in VS 🤔 Riok.Mapperly does need a reference to Riok.Mapperly.Abstractions, it references and uses these enums directly.
I'm wondering what changed between 2.8.0 and 3.0.0 that this doesn't work anymore in VS 🤔
AttributeDataAccessor
was changed to convert types right? This became an issue because Mapperly started to use config objects. i.e. PropertyAccessor
-> PropertyAccessorConfig
Riok.Mapperly does need a reference to Riok.Mapperly.Abstractions, it references and uses these enums directly.
I had issues where the benchmarks would fail because of this.
But the way how enum reflection types are resolved didn't change since the initial commit: https://github.com/riok/mapperly/blob/ad539c824a6e2920d49a2fac860d832e3cf269fc/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs#L61-L65 I'm just not sure what the real root cause is and I'd like to know that before we implement a fix.
Did mapperly always convert enums inside the Mapper
attribute? I found that the MapEnum
attributes caused a conversion issue (causing the benchmarks to end prematurely). I don't think the other enums caused issues?
Around the same time I found the VS couldn't build the Mapperly project. I never knew if it was caused by a VS update or a change in Mapperly (might have been a change to the sln)
Edit: related see #486. I had the opposite problem, could resolve Mapperly
types but could not resolve foreign types.
This problem makes it difficult for me to find the cause of OPs issue in VS
I've tested it on a old VS Version (17.5), where the Code Snippet still works. After upgrading to 17,7 I also get that error
I've experimented with previous commits and found that Mapperly creates the below message between v2.8.0
and v2.9.0-next.1
. I think that the commit 11995f0d support derived tyoe mappings
may be the culprit. The error persists after rebuilding and restarting the IDE.
warning CS8785: Generator 'MapperGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'InvalidOperationException' with message 'Type Riok.Mapperly.Abstractions.EnumMappingStrategy, Riok.Mapperly.Abstractions not found'
I'd guess this is caused by the following:
IsExternaIInit
polyfillAttributeDataAccessor
is changed, (note that it has since changed but VS stopped working from this point on)
var attrFullName = attrType.FullName;
if (attrFullName == null)
yield break;
var attrSymbol = compilation.GetTypeByMetadataName(attrFullName); if (attrSymbol == null) yield break;
```C#
var attrType = typeof(TAttribute);
var attrSymbol = compilation.GetTypeByMetadataName($"{attrType.Namespace}.{attrType.Name}");
`v2.9.0-next.2
has the error `1>CSC : warning CS8785: Generator 'MapperGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'Object of type 'Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel.FieldSymbol' cannot be converted to type 'Riok.Mapperly.Abstractions.EnumMappingStrategy'.'
`v2.9.0-next.3
has the error `1>CSC : warning CS8785: Generator 'MapperGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'InvalidOperationException' with message 'Could not resolve enum reflection type of EnumMappingStrategy or Riok.Mapperly.Abstractions.EnumMappingStrategy is not supported'
~~This could be an unrelated problem but only impacts VS users ~~
Pretty sure adding the following will fix the issue:
private static object? GetEnumValue(TypedConstant arg, Type targetType)
{
if (arg.Value == null)
return null;
if (arg.Type?.ContainingAssembly.Name is "Riok.Mapperly.Abstractions")
return arg.Value;
This appears to fix the issue on v3.0.0 next 1
. I can't verify if it works on the latest commit because d0eb4714 prevents me from building this in Visual Studio.
Edit: I'm wrong. This lets you build and run the sample but the unit tests will fail
I think by directly casting without an assembly name check it should also work. I created #654 implementing this. Can you verify this? However, I still don't fully understand why and what exactly is happening. But without VS it is not really possible to dig into it...
If there seem to be more problems with the source generated polyfills in VS we may need to switch back to manually added polyfills. Otherwise all potential VS contributors would be locked out.
Findings with VS:
Riok.Mapperly.Sample
all types of Riok.Mapperly.Abstractions
are availableRiok.Mapperly.Sample
Project with VS (which calls msbuild /t:Build
) there are no accessible types from Riok.Mapperly.Abstractions
but types of Riok.Mapperly
are available (Tested by calling Type.GetType
)There is also an issue that Debugging the Source Generator in VS is only possible if you downgrade the Riok.Mapperly.Sample
project from net7.0
to net6.0
(don't know the reason for that yet).
If i want to build the Sample Project by using the VS msbuild
i get the following Output:
msbuild /t:Build
=>
Also tried out a dotnet msbuild /t:Build
with the latest 7 SDK (7.0.400, MSBuild 17.7.1), which produced following Output:
Looks like VS MsBuild is doing some magic stuff...
:tada: This issue has been resolved in version 3.1.0-next.2 :tada:
The release is available on:
v3.1.0-next.2
Your semantic-release bot :package::rocket:
@pnquest could you verify this issue is resolved with 3.1.0-next.2
?
@latonz Yep, that version works! Thanks!
:tada: This issue has been resolved in version 3.1.0 :tada:
The release is available on:
v3.1.0
Your semantic-release bot :package::rocket:
Describe the bug Starting with
3.0.0
the generator fails when usingPropertyNameMappingStrategy.CaseInsensitive
with error message.Cleaning and rebuilding as well as cleaning, closing VS and deleting the
.vs
folder and reopening did not resolve the issue. Everything seems to build fine using the dotnet cli.To Reproduce Try to build the sample code below from visual studio 2022.
Expected behavior The generator should complete without error and
Stringfield
should be mapped toStringField
Code snippets
Environment (please complete the following information):