Entitas
Entitas copied to clipboard
NullReferenceException when generating code
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.
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 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
- Windows 10.
- Install Entitas 1.4.2.
- Install Visual Studio Community 2017.
- Open Unity Editor 2017.4.23f1.
- Editor preferences: External Tools: Select Visual Studio Community 2017.
- Open C# file in Visual Studio.
- 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
-
JetBrains Rider seems stable.
-
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
-
Restart Unity Editor.
-
Build Settings : Project Settings
-
Change framework from .NET 4.6 to .NET 3.5.
- If you're already at 3.5 you can skip this switch.
-
Restart Unity Editor.
-
Build Settings : Project Settings
-
Change framework from .NET 3.5 to .NET 4.6.
-
Restart Unity Editor.
-
If in doubt, observe 4.6 was configured in the C# project file:
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion> <TargetFrameworkProfile></TargetFrameworkProfile> <LangVersion>6</LangVersion>
-
At the command line, run Jenny again.
-
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.
-
Open Unity Editor 2017.4.23f1.
-
Editor preferences: External Tools: Select Visual Studio Community 2017.
-
Project Settings, configure .NET framework to 3.5.
-
Restart the editor.
-
Open the Visual Studio C# project file (same name as the Unity project file).
-
Delete the lines:
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <TargetFrameworkProfile>Unity Full v3.5</TargetFrameworkProfile>
-
Save the C# project.
-
Run Jenny again.
-
To recover, restore the target framework profile, or delete the C# project and open Unity editor again to regenerate the C# project.
-
In version 3.5, incompatible Entitas code was generated. For example, Entitas "Events" folder is deleted.
-
To recover apply Workaround with Windows Visual Studio in the section above.
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 did you find a workaround for this? I have the exact same problem using MessagePack and Entitas.
Fixed in Entitas 1.6.0 to automatically remove
<TargetFrameworkProfile>Unity Full v3.5</TargetFrameworkProfile>
in TargetFrameworkProfilePreProcessor
See #721