GlitchEnzo / NuGetForUnity

A NuGet Package Manager for Unity
MIT License
3.13k stars 310 forks source link

"(assembly) is already imported in engine" - but I don't think it is? #627

Open tattyd opened 7 months ago

tattyd commented 7 months ago

Description

Hi there,

I'm trying to install the R3 package via NuGetforUnity.

It fails to install all transitive dependencies, specifically, System.Runtime.Compilerservices.Unsafe. On the console, I see the message:

Is package 'System.Runtime.CompilerServices.Unsafe' already imported? True
Package System.Runtime.CompilerServices.Unsafe.6.0.0 is already imported in engine, skipping install.

However, then Unity fails to compile saying that assembly is missing. From some quick experimentation:

public class Assemblies
{
    // menu item to print all assemblies
    [UnityEditor.MenuItem("Tools/Print Assemblies")]
    public static void PrintAssemblies()
    {
        var assemblies = CompilationPipeline.GetAssemblies(AssembliesType.Editor);
        foreach (var assembly in assemblies)
        {
            Debug.Log(assembly.name);
            Debug.Log(assembly.outputPath);
        }
    }
}

Is it possible that Nuget is mistaken? Is there any other way I can truly verify whether the dependency is already present, and if so where it is?

I don't see System.Runtime.CompilerServices.Unsafe anywhere in the output.

JoC0de commented 7 months ago

Hi @tattyd thanks for the issue. Especially in Unity versions > 2023 Unity has different assemblies that are available in Editor / Player. NuGet for unity uses the following to determine the assemblies: https://github.com/GlitchEnzo/NuGetForUnity/blob/25dcde8a309d6f273c82699af7967ef46048e5a1/src/NuGetForUnity/Editor/UnityPreImportedLibraryResolver.cs#L83-L85

If you try to use the importer NuGet package in a Editor assembly you probably can fix it by changing the Editor Assembly Compatibility Level in the Unity settings so it matches the one used in runtime.