Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# code generation breaks on Resources with >2 levels of nesting #83071

Closed
donovani opened this issue Oct 9, 2023 · 1 comment · Fixed by #83532
Closed

C# code generation breaks on Resources with >2 levels of nesting #83071

donovani opened this issue Oct 9, 2023 · 1 comment · Fixed by #83532

Comments

@donovani
Copy link

donovani commented Oct 9, 2023

Godot version

4.1.1

System information

Godot v4.1.1.stable.mono - Windows 10.0.19045 - Vulkan (Forward+) - dedicated NVIDIA GeForce GTX 970 (NVIDIA; 31.0.15.3713) - Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz (8 Threads)

Issue description

It appears nesting classes too deeply in a file breaks the code generator on C# projects (unsure if this is applicable to GDScript).

Problem Description

If you have a single file with deeply nested classes, the code generator can fail to build.

See reproduction steps for an example of what breaks it, as well as the log messages.

Expected Behavior

Ideally, you should be able to nest resources arbitrarily. Oftentimes, the reason a class is nested is because it will never be used elsewhere. The source generator should not care about the structure of my code, as long as it is valid code.

If this is something that should not be allowed, I would at the very least expect a build error message that conveys that. The errors does not make it clear what actionable steps can be taken to resolve the issue.

Workaround

Currently the only way to pass the build is to refactor files so that there is no more than one level of nesting.

That said, I find that approach can lead to cluttering namespaces or needing to write more verbose names.

Steps to reproduce

Attempt to build the following Resource:

using Godot;

public partial class Foo: Resource
{
  public partial class Bar: Resource
  {
    public partial class Baz: Resource
    {
      [Export]
      public string key;
      [Export]
      public bool value;
    }
    
    [Export]
    public Baz[] bazList;
  }
  
  [Export]
  public Bar[] barList;
}

It should fail with the following errors (... is meant to represent the project root dir):

D:\...\Godot.SourceGenerators\Godot.SourceGenerators.ScriptPropertiesGenerator\Foo.Bar.Baz_ScriptProperties.generated.cs(15,29): error CS0115: 'Bar.Foo.Baz.SetGodotClassPropertyValue(in godot_string_name, in godot_variant)': no suitable method found to override [D:\...\Example.csproj]
D:\...\Godot.SourceGenerators\Godot.SourceGenerators.ScriptPropertiesGenerator\Foo.Bar.Baz_ScriptProperties.generated.cs(27,29): error CS0115: 'Bar.Foo.Baz.GetGodotClassPropertyValue(in godot_string_name, out godot_variant)': no suitable method found to override [D:\...\Example.csproj]
D:\...\Godot.SourceGenerators\Godot.SourceGenerators.ScriptSerializationGenerator\Foo.Bar.Baz_ScriptSerialization.generated.cs(10,29): error CS0115: 'Bar.Foo.Baz.SaveGodotObjectData(GodotSerializationInfo)': no suitable method found to override [D:\...\Example.csproj]
D:\...\Godot.SourceGenerators\Godot.SourceGenerators.ScriptSerializationGenerator\Foo.Bar.Baz_ScriptSerialization.generated.cs(16,29): error CS0115: 'Bar.Foo.Baz.RestoreGodotObjectData(GodotSerializationInfo)': no suitable method found to override [D:\...\Example.csproj]

However, if you remove Foo, and simply nest once, this error does not occur despite the logic not changing.

using Godot;
public partial class Bar: Resource
{
  public partial class Baz: Resource
  {
    [Export]
    public string key;
    [Export]
    public bool value;
  }
  
  [Export]
  public Baz[] bazList;
}

Minimal reproduction project

Here's an example project. It really doesn't have anything except the script in the reproduction steps.
Example.zip

@donovani donovani changed the title C# code generation breaks on resources with >2 levels of nested Resources C# code generation breaks on Resources with >2 levels of nesting Oct 11, 2023
@raulsntos
Copy link
Member

It seems the generator is generating the partial class nesting in the wrong order:

partial class Bar
{
	partial class Foo
	{
		partial class Baz { /* ... */ }
	}
}

When it should be:

partial class Foo
{
	partial class Bar
	{
		partial class Baz { /* ... */ }
	}
}

That said, nested classes can't be used as scripts. Script classes must be a top-level class and its name must match the name of the file where they are declared.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants