C
C#16mo ago
Foxtrek_64

✅ Multi-target, wrong version

I am multi-targeting a project for NetFx and Net6/7. Specifically, there are four projects in play here: Remora.Results - Provides the base Result and ResultError types for Net6/7 Tafs.Activities.Results - Provides the base Result and ResultError types for NetFx Tafs.Activities.Results.Extensions - Conditionally includes Remora.Results or Tafs.Activities.Results based on framework version. Tafs.Activities.Serialization - Targets Remora.Results or Tafs.Activities.Results based on target version. Additionally targets Tafs.Activities.Results.Extensions for extra error types. My SocialSecurityNumber class relies on Tafs.Activities.Results.Extensions.Errors.ValidationError. This is happy on NetFx, but on Net6/7, I get an error saying I cannot implicitly convert it to Remora.Results.Result. I would expect this error if it picked the wrong version of the error to provide. The source code of the ValidationError is this:
#if NET461_OR_GREATER
using Tafs.Activities.Results;
#else
using Remora.Results;
#endif

namespace Tafs.Activities.Results.Extensions.Errors
{
public sealed record class ValidationError(string Message) : ResultError(Message);
}
#if NET461_OR_GREATER
using Tafs.Activities.Results;
#else
using Remora.Results;
#endif

namespace Tafs.Activities.Results.Extensions.Errors
{
public sealed record class ValidationError(string Message) : ResultError(Message);
}
Whether it implements Tafs.Activities.Results.ResultError or Remora.Results.ResultError should in theory depend on the dotnet version, but it does not appear to be behaving as such. To make things even stranger, this framework comparison extends to the csproj too. So in theory, the Net6/7 version shouldn't even be aware of Tafs.Activities.Results.
<ItemGroup Condition="'$(TargetFramework)' == 'net461' Or '$(TargetFramework)' == 'net462'>
<ProjectReference Include="..\Tafs.Activities.Results\Tafs.Activities.Results.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'Net6.0' Or '$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Remora.Results" Version="7.2.3" />
<PackageReference Include="Remora.Results.Analyzers" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net461' Or '$(TargetFramework)' == 'net462'>
<ProjectReference Include="..\Tafs.Activities.Results\Tafs.Activities.Results.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'Net6.0' Or '$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Remora.Results" Version="7.2.3" />
<PackageReference Include="Remora.Results.Analyzers" Version="1.0.0" />
</ItemGroup>
Any thoughts as to what might be going on here?
1 Reply
Foxtrek_64
Foxtrek_6416mo ago
Oh, since it would be helpful- the error I'm getting for Net6.0 and Net7.0 targets is: "Cannot implicitly convert type 'Tafs.Activities.Results.Extensions.Errors.ValidationError' to 'Remora.Results.Result'. If I eliminate the explicit conversion and use the generic Result.FromError<TError>() method, the error changes to this: "The type 'Tafs.Activities.Results.Extensions.Errors.ValidationError' cannot be used as type parameter 'TError' in the generic type or method 'Result.FromError<TError>(TError)'. There is no implicit conversion from 'Tafs.Activities.Results.Extensions.Errors.ValidationError' to 'Remora.Results.IResultError'." This tells me that the Net6/7 version of ValidationError is actually implementing Tafs.Activities.Results.ResultError, even though there should be no way that it actually sees it. Because Remora.Results.ResultError implements Remora.Results.IResultError, the only way there would not be an implicit conversion would be if one tried to implicitly convert Tafs.Activities.Results.ResultError to Remora.Results.IResultError.