godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.44k stars 21.27k forks source link

Command line --check-only gives a false compilation error #78587

Open d0n3val opened 1 year ago

d0n3val commented 1 year ago

Godot version

4.0.3-stable

System information

Windows 10

Issue description

When checking a gd script using the command line, it give a false compilation error. It seems to not be able to discover the autoload setup and fails to find defined autoloads, generating this error:

SCRIPT ERROR: Compile Error: Identifier not found: singleton

I thought that compilation does not check for this, but if I intentionally add "class_name singleton" I get this very correct compilation error:

SCRIPT ERROR: Parse Error: Class "singleton" hides an autoload singleton.

This means the compilation process knows the autoloads but still fails to identify them as already defined class. This is a language feature as per official documentation:

From https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html:

"If the Enable column is checked (which is the default), then the singleton can be accessed directly without requiring get_node():"

I've attached a minimal project to demo the bug. In the editor works correctly, but checking compilation errors on the command line fails to find the autoload identifier.

Steps to reproduce

Execute the .bat associated in the attached .zip to compile the file and see the compilation error that should not happen. You might need to edit it to point to your godot executable. It just executes:

"c:\Program Files\Godot\Godot_v4-stable_win64.exe" --headless --check_only -s scene.gd

If you open the project in the editor the code compiles and works correctly

Minimal reproduction project

compilation_bug_test.zip

heppocogne commented 1 year ago

"c:\Program Files\Godot\Godot_v4-stable_win64.exe" --headless -s scene.gd" result in the same error. This issue happens not only when "--check_only" is given.

And even if I run godot in a project folder or give --path path/to/project argument, godot does not load autoload scripts if --script option is given. I think godot should always load autoload scripts when a project is detected.

I thought that compilation does not check for this, but if I intentionally add "class_name singleton" I get this very correct compilation error: SCRIPT ERROR: Parse Error: Class "singleton" hides an autoload singleton.

When godot does not know project path or .godot folder is removed, this does not happen. Cache files may have something to do with this.

Calinou commented 1 year ago

"c:\Program Files\Godot\Godot_v4-stable_win64.exe" --headless -s scene.gd" result in the same error. This issue happens not only when "--check_only" is given.

That particular issue is being tracked in https://github.com/godotengine/godot/issues/80319. Were project autoloads ever loaded when using --script/-s (especially in 3.x)?

monitz87 commented 9 months ago

Were project autoloads ever loaded when using --script/-s (especially in 3.x)?

I have a working standalone script in 3.x that depends on an autoload fwiw

monitz87 commented 9 months ago

@Calinou looking into the source, it seems like autoloads are being loaded just fine*. The problem is that the script is loaded (and thus compiled) before that happens, so compilation fails because the autoload global names aren't populated yet. I'm not sure why this was working in 3.x but I assume compilation worked differently in those versions.

I'm working on a fix that basically populates the autoload map before assigning the main loop, and only adds them to the tree if said loop is a scene tree. I'll submit a PR shortly to get guidance on whether my approach is sound

*: this can be corroborated by creating an external script that extends SceneTree but doesn't depend on any global autoload names. The autoloads will load (e.g. they will print things when they're ready) after the script has been instantiated.