zzzprojects / System.Linq.Dynamic.Core

The .NET Standard / .NET Core version from the System Linq Dynamic functionality.
https://dynamic-linq.net/
Apache License 2.0
1.56k stars 228 forks source link

FileNotFoundException for file: System.ComponentModel.TypeConverter.dll when used in TargetFramework .net7.0 with SelfContained set to true #706

Open Kl-Li opened 1 year ago

Kl-Li commented 1 year ago

Hello, Allow me to start by saying thank you for this library. I believe I have found an issue when migrating some code from .net6.0 to .net7.0. If this is an issue on my end (eg wrong version etc please feel free to correct me)

Description: Changing TargetFramework from net6.0 to net7.0 with SelfContained set to true seems to cause an Unhandled exception. of type: System.IO.FileNotFoundException. Looking at the directory I can see that the file is there but has a different version (please see attached picture).

Exception message: Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'System.ComponentModel.TypeConverter, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. File name: 'System.ComponentModel.TypeConverter, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseComparisonOperator() at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLogicalAndOrOperator() at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseIn() at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAndOperator() at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseOrOperator() at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLambdaOperator() at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseNullCoalescingOperator() at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseConditionalOperator() at System.Linq.Dynamic.Core.Parser.ExpressionParser.Parse(Type resultType, Boolean createParameterCtor) at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(Type delegateType, ParsingConfig parsingConfig, Boolean createParameterCtor, ParameterExpression[] parameters, Type resultType, String expression, Object[] values) at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(ParsingConfig parsingConfig, Boolean createParameterCtor, ParameterExpression[] parameters, Type resultType, String expression, Object[] values) at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda[T,TResult](ParsingConfig parsingConfig, Boolean createParameterCtor, String expression, Object[] values) at Program.Main(String[] args) in ...

Sample code: Sample code can be found here: https://dotnetfiddle.net/wnh2gk

csproj configuration:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework> <!-- net6.0 -->
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <PublishSingleFile>true</PublishSingleFile>
    <SelfContained>true</SelfContained> <!-- false -->
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.2" />
  </ItemGroup>

</Project>
TargetFramework SelfContained Exception
net6.0 false No
net6.0 true No
net7.0 false No
net7.0 true Yes

image

StefH commented 1 year ago

I think it's related to :

And I wonder if this can be solved in System.Linq.Dynamic.Core

Maybe you can create an issue for dotnet ?

Kl-Li commented 1 year ago

Hello, Yesterday I was able to spend some more time. It seems the problem isn't related to the single executable as such but to the RID. Below are my findings:

Running with :

    <PublishSingleFile>true</PublishSingleFile>
    <SelfContained>true</SelfContained>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>

The single executable found in the publish directory works as expected. The issue appears if we run the "non single exe file". eg default debug inside the IDE

I also tried running with:

    <!-- <PublishSingleFile>true</PublishSingleFile>
    <SelfContained>true</SelfContained> -->
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>

and indeed the same issue appears (both in the debug and publish directory this time)

@StefH Due to the above i don't think it's related to the trimming. However from you links I noticed the below: https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview?tabs=cli#api-incompatibility

API | Note -- | -- Assembly.Location | Returns an empty string.

From a quick look at the code it seems that Assembly.Location is used (eg https://github.com/zzzprojects/System.Linq.Dynamic.Core/blob/4cc72c4e6387fbeb4cbd493c38df63a618a3809b/src/System.Linq.Dynamic.Core/DefaultAssemblyHelper.cs#L25 Not sure if it's related (since those are APIs incompatibilities with single file deployment) but i thought it's worth mentioning.

I will try and do some more tests/reading and update accordingly.

StefH commented 1 week ago

@Kl-Li Were you able to create a reproducible project for this issue?