Entitas icon indicating copy to clipboard operation
Entitas copied to clipboard

NullReferenceException when generating code

Open wpp1983 opened this issue 5 years ago • 4 comments

Hi,

I found a bug want to contribute.

I upgrade entitas.

and on my teammate's compute , there is a NullReferenceException when Jenny is generating code.

here is the error code. error

wpp1983 avatar Mar 01 '19 10:03 wpp1983

Hi, it looks like there is an issue with the EntityIndexDataProvider.

Can you double check if all your components are correctly using the entity index attribute?

sschmid avatar Mar 14 '19 19:03 sschmid

@sschmid Do you know how to resolve that stack trace when only happening with Visual Studio? Sometimes, with unknown steps, I encounter a similar stack trace. Below is a report.

Jenny fails with Windows Visual Studio

The basic workaround is to toggle the project from .NET 4.6 to 3.5 and back again to .NET 4.6.

Steps To Verify

  1. Windows 10.
  2. Install Entitas 1.4.2.
  3. Install Visual Studio Community 2017.
  4. Open Unity Editor 2017.4.23f1.
  5. Editor preferences: External Tools: Select Visual Studio Community 2017.
  6. Open C# file in Visual Studio.
  7. Run CodeGenerator/Jenny.exe gen Roslyn.properties

Actual Result

  • Sometimes script crashes or never completes.

Expected Result

  • Entitas code regenerates.

Diagnosis

Run verbose mode:

    CodeGenerator/Jenny.exe -v gen Roslyn.properties
    ...
    Complete MyProject
    Creating model: Entity Index (Roslyn) (16%)
    System.NullReferenceException: Object reference not set to an instance of an object.
       at DesperateDevs.Roslyn.SymbolExtension.ToCompilableString(ISymbol symbol)
       ...

Workaround with Windows JetBrains Rider

  1. JetBrains Rider seems stable.

  2. Use a properties file with no difference, except targeting JetBrain Rider's C# project file (Assembly-CSharp.csproj). Example:

     CodeGenerator/Jenny.exe -v gen RoslynJB.properties
    

Workaround with Windows Visual Studio

  1. Restart Unity Editor.

  2. Build Settings : Project Settings

  3. Change framework from .NET 4.6 to .NET 3.5.

    • If you're already at 3.5 you can skip this switch.
  4. Restart Unity Editor.

  5. Build Settings : Project Settings

  6. Change framework from .NET 3.5 to .NET 4.6.

  7. Restart Unity Editor.

  8. If in doubt, observe 4.6 was configured in the C# project file:

     <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
     <TargetFrameworkProfile></TargetFrameworkProfile>
     <LangVersion>6</LangVersion>
    
  9. At the command line, run Jenny again.

  10. Code appears to be generated well.

Stack Trace of Crash

Run verbose mode:

    CodeGenerator/Jenny.exe -v gen Roslyn.properties

Tail of output:

    ...
    Opened MyProject
    Parsing MyProject
    Complete MyProject
    Creating model: Entity Index (Roslyn) (16%)
    System.NullReferenceException: Object reference not set to an instance of an object.
       at DesperateDevs.Roslyn.SymbolExtension.ToCompilableString(ISymbol symbol)
       at DesperateDevs.Roslyn.SymbolExtension.isAttributeType[T](AttributeData attr, Boolean inherit)
       at System.Linq.Enumerable.WhereArrayIterator`1.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at DesperateDevs.Roslyn.SymbolExtension.GetAttributes[T](ISymbol type, Boolean inherit)
       at Entitas.Roslyn.CodeGeneration.Plugins.EntityIndexDataProvider.<>c.<GetData>b__19_9(ISymbol symbol)
       at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
       at Entitas.Roslyn.CodeGeneration.Plugins.EntityIndexDataProvider.<>c.<GetData>b__19_4(KeyValuePair`2 kv)
       at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
       at System.Linq.Enumerable.<SelectManyIterator>d__17`2.MoveNext()
       at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at DesperateDevs.CodeGeneration.CodeGenerator.CodeGenerator.generate(String messagePrefix, IPreProcessor[] preProcessors, IDataProvider[] dataProviders, ICodeGenerator[] codeGenerators, IPostProcessor[] postProcessors)
       at DesperateDevs.CodeGeneration.CodeGenerator.CodeGenerator.Generate()
       at DesperateDevs.Serialization.CLI.Utils.AbstractPreferencesCommand.Run(String[] args)
       at DesperateDevs.CLI.Utils.CLIProgram.runCommand(String[] args)

Triggering a Code Generation Failure

Here's some steps that trigger the above error on my Windows machine.

  1. Open Unity Editor 2017.4.23f1.

  2. Editor preferences: External Tools: Select Visual Studio Community 2017.

  3. Project Settings, configure .NET framework to 3.5.

  4. Restart the editor.

  5. Open the Visual Studio C# project file (same name as the Unity project file).

  6. Delete the lines:

     <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
     <TargetFrameworkProfile>Unity Full v3.5</TargetFrameworkProfile>
    
  7. Save the C# project.

  8. Run Jenny again.

  9. To recover, restore the target framework profile, or delete the C# project and open Unity editor again to regenerate the C# project.

  10. In version 3.5, incompatible Entitas code was generated. For example, Entitas "Events" folder is deleted.

  11. To recover apply Workaround with Windows Visual Studio in the section above.

ethankennerly avatar Jul 13 '19 01:07 ethankennerly

I encounter the bug,too.

In my project,I use MessagePack as a Component Serialization Tool. When I Use the MessagePack.KeyAttribute to Mark My Component Field, I Got The Bug Error When Code Generating.

    [Spawner]
    [MessagePackObject]
    public class SpawnDataKey : IComponent
    {
        [Key(0)]
        public string value;
    }

I think there a Bug in EntityIndexDataProvider.cs, method GetData(). In GetData() Method, It try to Get Attributes.

Wish You Happy Day!

willjey avatar Dec 09 '21 15:12 willjey

@willjey did you find a workaround for this? I have the exact same problem using MessagePack and Entitas.

aroman avatar Jan 03 '22 19:01 aroman

Fixed in Entitas 1.6.0 to automatically remove

<TargetFrameworkProfile>Unity Full v3.5</TargetFrameworkProfile>

in TargetFrameworkProfilePreProcessor

See #721

sschmid avatar Sep 07 '22 08:09 sschmid