Godot 4 headless –script tests cannot see class_name classes on a fresh clone, and –check-only cannot see autoloads

My headless test suite passed on my machine and failed on a fresh clone with Identifier not found: GridModel, a class I had declared with class_name in the same project. Later, a syntax check of my UI scripts failed with Identifier not found: Router, an autoload the project defines. Both errors come from the same place: what the GDScript parser can resolve depends on how you launched Godot, not on what the project contains.

TL;DR — Scripts run with godot --headless --script file.gd only resolve class_name identifiers if .godot/global_script_class_cache.cfg already exists, and --check-only --script never registers autoload singletons. Reference scripts through preload() constants, keep autoload-dependent code out of --script runs, and drive in-game automation from the main scene with a user argument instead.

The setup

A puzzle game in Godot 4 with a pure-logic model (grid_model.gd, declared class_name GridModel), a test script that extends SceneTree and runs with --script, and a handful of autoload singletons (Router, SaveData) that the UI scripts call by name. Tests run in CI-style: clone, run godot --headless --script tests/test_grid_model.gd, read the exit code. No editor involved.

# tests/test_grid_model.gd (first version)
extends SceneTree

func _initialize() -> void:
    var m := GridModel.new(6, 8, 10)   # class_name declared in scripts/model/grid_model.gd
    assert(m.cols == 6)
    quit(0)

What I expected

class_name makes a script a global type. The editor shows it in the create-node dialog, and running the project resolves it everywhere. I expected the same from a headless --script run: same project, same files.

What actually happens

On a clone without a .godot/ directory the run fails at parse time:

SCRIPT ERROR: Parse Error: Cannot infer the type of "m" variable because the value doesn't have a set type.
SCRIPT ERROR: Compile Error: Identifier not found: GridModel
ERROR: Failed to load script "res://tests/test_grid_model.gd" with error "Compilation failed".

Run the editor once (or godot --headless --path . --import), which writes .godot/global_script_class_cache.cfg, and the identical command succeeds. Delete .godot/ and it fails again. The global class list is a cache built by the editor’s filesystem scan; --script mode loads your one script and its dependencies and never scans the project.

Autoloads are a second, separate gap. With Router="*res://autoload/router.gd" in project.godot, a plain project run resolves Router.go(...) fine, but a syntax check of a script that uses it reports:

$ godot --headless --path . --check-only --script scripts/ui/main_menu.gd
SCRIPT ERROR: Compile Error: Identifier not found: Router
SCRIPT ERROR: Compile Error: Failed to compile depended scripts.

The script is correct. --check-only --script compiles it without instantiating the scene tree, so the singleton globals that project.godot promises do not exist at that moment.

Three ways to launch Godot and which identifiers each one can resolve: a project run resolves class_name and autoloads, a --script run resolves class_name only with the .godot cache present, and --check-only resolves neither reliably.

The fix

For classes: never rely on class_name from a --script entry point. Preload the script into a constant and use that. The class_name line can stay for editor convenience, but do not give the constant the same name, because a constant that shadows a global class is itself a parse error.

# tests/test_grid_model.gd
extends SceneTree

const GridModelScript = preload("res://scripts/model/grid_model.gd")

func _initialize() -> void:
    var m = GridModelScript.new(6, 8, 10)   # untyped: the type name is not resolvable here
    assert(m.cols == 6)
    quit(0)

Inside the model itself, avoid self-references by name too. A clone() method written as GridModel.new(...) fails the same way; get_script().new(...) does not:

func clone():
    var m = get_script().new(cols, rows, max_moves)
    # copy fields...
    return m

For autoloads: keep --script runs for pure logic, and drive anything that needs the singletons from the project’s main scene. I replaced a SceneTree-based screenshot tool with a plain Node that the main scene adds when a user argument is present:

# scripts/ui/main.gd
func _ready() -> void:
    if "--capture" in OS.get_cmdline_user_args() and ResourceLoader.exists("res://tools/capture_driver.gd"):
        add_child(load("res://tools/capture_driver.gd").new())
        return
    Router.go("MainMenu")
godot --path . -- --capture --level=51 --steps=red_1:up,tap:HUD/PauseButton

Everything after -- reaches OS.get_cmdline_user_args(), the autoloads exist because the project is running normally, and ResourceLoader.exists keeps the exported build safe when tools/ is excluded from the export.

Why it works

preload() is resolved from the file path at parse time, independently of any cache. The global class list, by contrast, is .godot/global_script_class_cache.cfg, produced by the editor’s import scan; --script skips that scan because it does not open the project as the editor would. Autoloads are instantiated by the main loop when the project starts its main scene; a --script run replaces the main loop with your script, and --check-only stops after compilation.

What I did not test

Godot 4.4 to 4.6, Windows and macOS. Whether --script mode instantiates autoloads at runtime (as opposed to compile time in --check-only) I did not need to find out: moving the automation into the main scene sidestepped the question. The class_name cache behaviour was observed on 4.3 and reproduced on 4.7.1.

Facts

context: Godot 4 project with class_name scripts and autoload singletons, headless --script test entry points
problem: "Identifier not found" for class_name globals on a fresh clone and for autoloads under --check-only --script
solution: reference scripts via preload() constants (never a same-named constant), use get_script().new() for self-references, run autoload-dependent automation from the main scene via a user argument
verified_on: 2026-09-06
applies_to: [Godot 4.3.stable, Godot 4.7.1.stable, Linux, headless]
does_not_apply_to: [scripts run through the editor or the project's main scene, where both resolve]

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Retour en haut