vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
454 stars 72 forks source link

[Issues]Version Issues for System.Runtime.CompilerServices.Unsafe #174

Closed jinjieTeam closed 10 months ago

jinjieTeam commented 1 year ago

First of all, I would like to thank the author for his excellent open source code. Unfortunately, I have a plug-in development for third-party programs. It can only use the. net framework platform (version 4.8). I have encountered some difficulties when using the System.Text.Json library. First, I updated all dependent libraries to the latest. There is no problem with compilation, but when I execute the program, I will be prompted that the dependent library which is “System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” cannot be found. Unfortunately,the System.Text.Json library depends on the System.Runtime.CompilerServices.Unsafe with Version 6.0.0.0. Now I am in a dilemma. I can reduce the System.Runtime.CompilerServices.Unsafe version to 4.5.3, which will clear the version error of the reference relationship, but this will lead to the abnormal operation of the System.Text.Json class library (its required System.Runtime.CompilerServices.Unsafe version 6.0.0.0). I wonder if there are other ways to solve the problem?

Other information:

The program exception error occurred in the SharpGLTF.Materials.MaterialValue.cs file of the SharpGLTF.Toolkit project, line 364.

 set
     {
       Span<float> tmp = stackalloc float[4];
       tmp[0] = value.X;
       tmp[1] = value.Y;
       tmp[2] = value.Z;
       tmp[3] = value.W;
       …………

Type initializer threw an exception: FileNotFoundException: Failed to load the file or assembly "System. Runtime. CompilerServices. Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" or one of its dependencies. The system cannot find the specified file.

vpenades commented 1 year ago

Hmm... that's a tricky one: Net48 is pulling the NetStandard2.1 assembly, which depends on System.Text.Json.

Notice that the I am using 5.0.2 of json deliberately, because I had problems in the past with newer versions, but maybe newer packages like 7.0.2 have fixed all these issues, so I could bump up the version.

I would suggest you to do this:

If that fixes your problem, I'll change the version for the next update.

Other than that, I don't know what else to do; the library already uses minimal dependencies.

jinjieTeam commented 1 year ago

The minimal dependencies is:

System.Text.Json System. Runtime. CompilerServices. Unsafe
5.0.2 5.0.0.0
7.0.2 6.0.0.0

I tried to adjust the System.Text.Json version referenced by the program to 5.0.2 and 7.0.2 respectively, and the following same error occurred.

image

vpenades commented 1 year ago

In my experience, this might be caused by another project you're consuming, that is needlessly referencing System.Runtime.CompilerServices.Unsafe

if you see my SharpGLTF csproj definitions, you'll noticed I only reference third party packages when needed and not by default.

The rule of thumb writing projects is that you only reference things that are missing in the current target framework.

Unfortunately, many developers don't take about this and reference lots of packages regardless of whether they're actually needed or not, so when they're used along other packages that do the same, that's where the problems happen.

more things you can try:

jinjieTeam commented 1 year ago

That is my project references:

image image image image

In my experience, this might be caused by another project you're consuming, that is needlessly referencing System.Runtime.CompilerServices.Unsafe

if you see my SharpGLTF csproj definitions, you'll noticed I only reference third party packages when needed and not by default.

The rule of thumb writing projects is that you only reference things that are missing in the current target framework.

Unfortunately, many developers don't take about this and reference lots of packages regardless of whether they're actually needed or not, so when they're used along other packages that do the same, that's where the problems happen.

more things you can try:

  • whenever possible, don't reference System.Runtime.CompilerServices.Unsafe in your other class library projects if it's not needed.
  • explicitly reference System.Runtime.CompilerServices.Unsafe in your main project (the project compiling the EXE)
vpenades commented 1 year ago

These screenshots are not very useful, SharpGLTF depends on what it needs to run, as any other library around. Conflicts happen when you mix multiple packages into a single project, and these two packages require different versions of the same dependency.

So if you have two conflicting projects, A and B, it's either modifying A to match B, or modifying B to match A.

sometimes people things that the last library they added to the project is the one responsible of the conflict, but it can happen that another library already in your project is not declaring the dependencies correctly, but as long as you don't add more packages, that issue on that library remains hidden.

They only solution I could think of is to include Net48 as targetframework on SharpGLTF and remove the System.Text.Json dependency if that's possible, so the library will use the default version used by net48.

In general, Libraries should not reference packages that are already included by default in a given target platform, but some packages do it anyway, and they're enforcing the whole solution to use the version they're referencing.

Managing conflicting references in a large project is always a pain, but I can't do much about it, since it's a general Net problem, not something specific to my library.

jinjieTeam commented 1 year ago

These screenshots are not very useful, SharpGLTF depends on what it needs to run, as any other library around. Conflicts happen when you mix multiple packages into a single project, and these two packages require different versions of the same dependency.

So if you have two conflicting projects, A and B, it's either modifying A to match B, or modifying B to match A.

sometimes people things that the last library they added to the project is the one responsible of the conflict, but it can happen that another library already in your project is not declaring the dependencies correctly, but as long as you don't add more packages, that issue on that library remains hidden.

They only solution I could think of is to include Net48 as targetframework on SharpGLTF and remove the System.Text.Json dependency if that's possible, so the library will use the default version used by net48.

In general, Libraries should not reference packages that are already included by default in a given target platform, but some packages do it anyway, and they're enforcing the whole solution to use the version they're referencing.

Managing conflicting references in a large project is always a pain, but I can't do much about it, since it's a general Net problem, not something specific to my library.

You are right. The radical cause is the problem of the System.Text.Json version in the. net 4.8 framework. I know that the version three years ago (alpha0013) is based on Newtonsoft.Json. If I replace System.Text.Json with Newtonsoft.Json in the source code of the latest version, do you think it is possible?

jinjieTeam commented 1 year ago

Today, I created a desktop exe program, called SharpGLTF, and performed the function perfectly by setting the runtime version of the dependent library in the app.config file. Disappointingly, the app.config file does not seem to work for the dll program.

jinjieTeam commented 10 months ago

Today, I tried another solution and it was successful. This solution bypasses the issues caused by the framework and referencing DLL versions. The main steps are as follows: I have also defined some classes to store node, material, and face point data in the 3D model. After obtaining this data through the DLL plugin, I converted the data object into a JSON file and used it as a parameter to call another exe program specifically used to generate. glb files.

vpenades commented 10 months ago

Thanks for letting me know.

As a side note, I would like you to be aware of some technical questions.

SharpGLTF does not reference CompilerServices.Unsafe directly,

SharpGLTF references System.Text.Json which then references CompilerServices.Unsafe, that's why I can't change its version.

What I can do, and what I've been doing since the last release, is to upgrade the version of System.Text.Json incrementally.

A few versions ago, I was using System.Text.Json 5.0.2

In the newest release I upgraded to 6.0.7

And for the next releases I plan to upgrade to 7.0.3

All these version of System.Text.Json are backwards compatible, but they pull newer versions of CompilerServices.Unsafe , so maybe one of these upgrades "magically" solves your problems.

jinjieTeam commented 10 months ago

Thank you for your reply. Looking forward to good performance after the update. Thank you again.

---Original--- From: "Vicente @.> Date: Tue, Aug 22, 2023 20:03 PM To: @.>; Cc: @.>;"State @.>; Subject: Re: [vpenades/SharpGLTF] [Issues]Version Issues forSystem.Runtime.CompilerServices.Unsafe (Issue #174)

Thanks for letting me know.

As a side note, I would like you to be aware of some technical questions.

SharpGLTF does not reference CompilerServices.Unsafe directly,

SharpGLTF references System.Text.Json which then references CompilerServices.Unsafe, that's why I can't change its version.

What I can do, and what I've been doing since the last release, is to upgrade the version of System.Text.Json incrementally.

A few versions ago, I was using System.Text.Json 5.0.2

In the newest release I upgraded to 6.0.7

And for the next releases I plan to upgrade to 7.0.3

All these version of System.Text.Json are backwards compatible, but they pull newer versions of CompilerServices.Unsafe , so maybe one of these upgrades "magically" solves your problems.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you modified the open/close state.Message ID: @.***>