Skip to content

godot-ts generate crash with GDScript files that have no parsed functions #217

@nedkelly

Description

@nedkelly

Hi! I'm using @godot-js/godot-ts in a Godot 4.5.1 project and ran into an issue after installing the Terrainy addon from the Godot Asset Library.

Environment:

  • Godot 4.5.1 stable
  • Windows
  • Node v23.7.0
  • @godot-js/godot-ts@0.0.8
  • Package manager: pnpm
  • Project also contains GDScript addon files from Terrainy

What happened

Running:

pnpm run generate

with:

"generate": "godot-ts generate"

failed with:

TypeError: e.functions is not iterable
    at Q (.../@godot-js/godot-ts/dist/index.js:20:45)
    at Command.ot (.../@godot-js/godot-ts/dist/index.js:133:232)

From debugging, it looks like godot-ts generate scans every .gd file, creates a descriptor for each one, and then assumes each descriptor has a functions array. Some valid GDScript files do not produce any parsed functions, so functions is undefined and generation crashes.

The relevant generated code path appears to be equivalent to:

for (let { name, returnType, params } of e.functions) {
  // ...
}

Expected behavior

godot-ts generate should handle GDScript descriptors with no functions, probably by treating missing functions as an empty array.

Something like:

for (let { name, returnType, params } of e.functions ?? []) {
  // ...
}

Local workaround

I worked around this by replacing the generator step with a small local script that:

  • scans .gd files
  • always initializes functions: []
  • writes the same generated gdscript.d.ts, gdscript.ts, and utils.ts files
  • compiles the generated helper TypeScript files into:
    • .godot/GodotJS/generated/gdscript.js
    • .godot/GodotJS/generated/utils.js

The final compile step was needed because GodotJS reported:

[jsb][Error] the javascript file is missing: res://.godot/GodotJS/generated/gdscript.js (source: res://generated/gdscript.ts)
[jsb][Error] the javascript file is missing: res://.godot/GodotJS/generated/utils.js (source: res://generated/utils.ts)

Related build issue

My original godot-ts config used:

"godot-ts": {
  "src": ".",
  "out": "./.godot/GodotJS",
  "minifyClasses": true
}

After adding a larger addon and generated files, godot-ts build started producing very noisy esbuild out-of-memory failures. Narrowing the script source directory fixed that for my project:

"godot-ts": {
  "src": "./src/scripts",
  "out": "./.godot/GodotJS/src/scripts",
  "minifyClasses": true
}

I also ended up using a small local esbuild wrapper for build/watch so only real game TypeScript files under src/scripts are compiled.

This may just be a documentation/default-project-layout issue, but it might be worth warning users that src: "." can become expensive or unstable once a Godot project contains addon files, generated files, tests, typings, or other non-game TypeScript.

Thanks for the project, it's really nice to be using TypeScript for game dev. Happy to share the workaround script if that helps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions