Godot headless test exits 0 on a parse error
Godot 4 headless can finish with exit code 0 after a GDScript parse error. Check for an explicit PASS line instead, and read "Could not resolve external class member" as a parse failure in another script.
Contents
We run our Godot 4 tests headless, from a shell, and we first trusted the exit code. Then a test script failed to parse, Godot printed the error, and the shell still reported success. This post shows what that looks like, how to make a test runner that cannot be fooled by it, and a second, confusing error from the same session that had a simple cause.
The run that said exit 0
The command was a plain headless script run:
godot --headless --path godot --script res://scripts/presentation/tests/presenter_test.gd \
2>&1 | grep -vE '^Godot Engine|^\s*$'
echo "exit=$?"
The output was:
SCRIPT ERROR: Parse Error: Cannot infer the type of "system_lines" variable because the value doesn't have a set type.
at: GDScript::reload (res://scripts/presentation/tests/presenter_test.gd:40)
ERROR: Failed to load script "res://scripts/presentation/tests/presenter_test.gd" with error "Parse error".
at: load (modules/gdscript/gdscript_resource_format.cpp:46)
exit=0
Two things to see. One, the test did not run: the script failed to load, so none of its
checks executed. Two, exit=0. Be careful with that number. Our command piped Godot through
grep, and in a pipe $? is the status of the last command, which is grep here. So we
cannot say from this run alone what Godot's own exit status was. What we can say is that the
shell reported success for a run in which no test ran, and that is the failure we care about.
The cause of this particular error was a line of this shape:
var system_lines := p.lines.filter(is_system_line)
filter returns an untyped Array, so := has nothing to infer from. The fix is an explicit
type:
var system_lines: Array = p.lines.filter(is_system_line)
Check for an explicit PASS line
If a failed load can look like success, the exit code does not tell you the tests passed. We changed the
rule: every test script prints a final line starting with PASS when all its checks succeed,
and the runner treats a missing PASS as a failure.
out=$(godot --headless --path godot --script "res://scripts/presentation/tests/$t.gd" 2>&1)
if ! printf '%s\n' "$out" | grep -q '^PASS'; then
printf '%s\n' "$out"
echo "FAIL: $t printed no PASS line"
exit 1
fi
This turns "the script did not run" into a failure for the same reason as "a check failed": the success line never appeared. It costs one line per test and it covers parse errors, missing resources and crashes before the first check.
Two small things to keep with it. Print the PASS line last, after all checks, so a test that
dies halfway does not leave one behind. And have a failing check print a different word,
because a grep PASS that also matches "PASS 3 of 5 checks, 2 FAIL" is a new way to lie.
The pipe masks the code, too
The sample above pipes through grep -v to strip the engine banner. In a shell pipe, the
exit status is the one from the last command, unless you set pipefail. So even a Godot that
exits 1 would be hidden by the filter. Our own notes at the time said Godot "exited 0 despite
the parse error", and the pipe is the first thing we would check before believing that. We ran into the same effect in another place the
same day: a lint error was masked by a pipe, and we only saw it because the output was read.
The PASS-line check does not depend on the exit code at all, so the pipe cannot hide
anything. If you rely on exit codes in other tools, set set -o pipefail in the script, or
capture the output to a variable first as above.
The second trap: "Could not resolve external class member"
Earlier that session, a different error stopped us for a while:
SCRIPT ERROR: Parse Error: Could not resolve external class member "set_identity".
at: GDScript::reload (res://scripts/presentation/pd_stage.gd:113)
ERROR: Failed to load script "res://scripts/presentation/pd_stage.gd" with error "Parse error".
Line 113 of the stage script called a method on a character object, and the character script
had set_identity. The message suggested the method was missing. It was not missing from
what we wrote.
The useful reading of "Could not resolve external class member" is that a script you
preload has failed to parse, so the class it defines cannot be resolved. The error is
reported in the script that uses the class, not in the one with the fault. We ran
--check-only on each script by itself:
for f in characters/blocky_agent.gd operator/operator_controls.gd presentation/pd_stage.gd; do
echo "== $f"
godot --headless --path godot --check-only --script res://scripts/$f 2>&1 \
| grep -vE '^Godot Engine|^\s*$' | head -6
done
That pointed at the character script. In Godot 4.7.2 Node3D already has a native
set_identity(), and our class extends it. Our method had the same name, and that
collision made the character script fail to parse. We renamed it:
sed -i '' 's/func set_identity(/func configure(/' godot/scripts/characters/blocky_agent.gd
After renaming the call sites, all three scripts passed the check.
The checklist we use now
- A headless Godot test passes when it prints
PASS, not when the process exits 0. - A parse error in one script can surface as an error in another. When you see "could not
resolve", check each preloaded script alone with
--check-only. - Before naming a method on a node subclass, search the engine class reference for that name. A native method with the same name can break the parse.
- Keep the engine version in the log. We saw these on 4.7.2 and have not checked other versions.
Why we did not just add pipefail
set -o pipefail is a good habit, and we use it where a script calls other tools. It fixes
the pipe. It does not fix the larger question, which is whether the process status says
anything about the tests. A test runner can fail to load a script in more ways than we have
listed here, and each of those is a way to get a clean status with no checks run.
The PASS line asks the question we care about directly: did the checks run to the end and all succeed? Everything that goes wrong before that, whatever its cause, leaves the line missing. That is why we trust it more than a number the engine hands back.
What it cost
One line per test script to print PASS, and a few lines in the runner to look for it. A
parse error now shows up as a named failure and not as a green run with nothing in it.