From a86c531d143c45a7a7dfc2dc66f3f0ca32a674cc Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 02:08:30 +0000 Subject: [PATCH] Seed the class registry on a cold .godot cache before import A cold .godot/ has no global_script_class_cache.cfg, so the engine boots with no class_name scripts registered, and a plugin's custom ResourceFormatLoader is missing when the first import parses that plugin's resources. parse.sh now runs a seed import first when the registry is missing, logs it on its own and fails if the seed writes no registry. gdk_rebuild_import_cache (make import) runs two passes on a cold registry. Refs #188 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0177RxMb4U6YGaGVPmUNstnQ --- .../godot/installables/gdk_runners.sh | 66 ++++++++++++------- .../godot/installables/import_cache.sh | 4 +- src/godot_devkit/godot/installables/parse.sh | 24 ++++++- tests/test_runners_installable.py | 23 ++++++- 4 files changed, 87 insertions(+), 30 deletions(-) diff --git a/src/godot_devkit/godot/installables/gdk_runners.sh b/src/godot_devkit/godot/installables/gdk_runners.sh index aa99294f..a163e760 100644 --- a/src/godot_devkit/godot/installables/gdk_runners.sh +++ b/src/godot_devkit/godot/installables/gdk_runners.sh @@ -892,38 +892,43 @@ gdk_sweep_failed_paths() { # gdk_sandbox_home — the pass boots the project. GDK_REBUILD_IMPORT_CACHE_TIMEOUT="${GDK_REBUILD_IMPORT_CACHE_TIMEOUT:-60}" +# The script class registry. The engine registers every `class_name` script — +# and with it a plugin's custom ResourceFormatLoader — from this file at boot, +# BEFORE it parses a resource. A cold `.godot/` has none, so the first import +# parses the plugin's resources with no loader for them; the pass writes the +# registry, and a second boot reads it. gdk_cold_class_registry is that test. +GDK_CLASS_REGISTRY=".godot/global_script_class_cache.cfg" +gdk_cold_class_registry() { + [ ! -f "$GDK_CLASS_REGISTRY" ] +} + # gdk_rebuild_import_cache [seconds] # With no inherited lease it takes EVERY engine slot (GDK_ENGINE_GATE_WEIGHT=all, # #47): the pass rewrites, in place, the .godot/ a peer run in this checkout -# reads. +# reads. On a cold registry it runs the pass TWICE, each up to [seconds]: the +# first seeds the registry, the second imports with the plugin loaders. gdk_rebuild_import_cache() { - local secs="${1:-$GDK_REBUILD_IMPORT_CACHE_TIMEOUT}" - if [ -n "$GDK_TIMEOUT" ]; then - gdk_engine_gate_held 2>/dev/null; local held=$? + local secs="${1:-$GDK_REBUILD_IMPORT_CACHE_TIMEOUT}" passes=1 held + gdk_cold_class_registry && passes=2 + # No timeout binary. This is best-effort recovery, so it still runs — but + # it runs UNBOUNDED, and a caller that printed "up to ${secs}s" would be + # stating a bound nothing enforces. gdk_run_bounded REFUSES in the same + # situation; the difference is deliberate and is why this says it out loud. + [ -n "$GDK_TIMEOUT" ] || echo "$GDK_LIB_TAG: no timeout/gtimeout on PATH — the import pass runs UNBOUNDED" >&2 + local -a bound=() + [ -z "$GDK_TIMEOUT" ] || bound=("$GDK_TIMEOUT" --kill-after="$GDK_TIMEOUT_KILL_AFTER" "${secs}s") + while [ "$passes" -gt 0 ]; do + passes=$((passes - 1)) + gdk_engine_gate_held 2>/dev/null; held=$? [ "$held" -ne 2 ] || { echo "$GDK_LIB_TAG: refusing import pass with invalid inherited engine lease" >&2; return 2; } if [ "$held" -eq 1 ]; then GDK_ENGINE_GATE_WEIGHT=all gdk_engine_gate_run import-cache -- bash -c '"$@" >/dev/null 2>&1 || true' _ \ - "$GDK_TIMEOUT" --kill-after="$GDK_TIMEOUT_KILL_AFTER" "${secs}s" \ - "$GDK_GODOT" --path . --headless --import || return $? + ${bound[@]+"${bound[@]}"} "$GDK_GODOT" --path . --headless --import || return $? else - "$GDK_TIMEOUT" --kill-after="$GDK_TIMEOUT_KILL_AFTER" "${secs}s" \ - "$GDK_GODOT" --path . --headless --import >/dev/null 2>&1 || true + ${bound[@]+"${bound[@]}"} "$GDK_GODOT" --path . --headless --import >/dev/null 2>&1 || true fi - return 0 - fi - # No timeout binary. This is best-effort recovery, so it still runs — but - # it runs UNBOUNDED, and a caller that printed "up to ${secs}s" would be - # stating a bound nothing enforces. gdk_run_bounded REFUSES in the same - # situation; the difference is deliberate and is why this says it out loud. - echo "$GDK_LIB_TAG: no timeout/gtimeout on PATH — the import pass runs UNBOUNDED" >&2 - gdk_engine_gate_held 2>/dev/null; local held=$? - [ "$held" -ne 2 ] || { echo "$GDK_LIB_TAG: refusing import pass with invalid inherited engine lease" >&2; return 2; } - if [ "$held" -eq 1 ]; then - GDK_ENGINE_GATE_WEIGHT=all gdk_engine_gate_run import-cache -- bash -c '"$@" >/dev/null 2>&1 || true' _ \ - "$GDK_GODOT" --path . --headless --import - else - "$GDK_GODOT" --path . --headless --import >/dev/null 2>&1 || true - fi + done + return 0 } # --- gdk_cpu_count — cores this process may use ------------------------------ @@ -1184,6 +1189,19 @@ second line' "$(cat "$log")" esac _gdk_st_true 'rebuild_import_cache warns when nothing can bound it' "$status" + # --- a cold registry is seeded by a first pass, a warm one is not -------- + # shellcheck disable=SC2016 # the stub expands $GDK_ST_PASSES, not this shell + printf '#!/bin/sh\necho pass >> "$GDK_ST_PASSES"\nmkdir -p .godot && : > %s\n' \ + "$GDK_CLASS_REGISTRY" > "$scratch/stub-bin/godot" + mkdir -p "$scratch/cold-project" + for want in 2 1; do + : > "$scratch/passes" + ( cd "$scratch/cold-project" && PATH="$scratch/stub-bin:$PATH" GDK_TIMEOUT='' GDK_GODOT='godot' \ + GDK_ST_PASSES="$scratch/passes" gdk_rebuild_import_cache 5 2>/dev/null ) + _gdk_st_eq "rebuild_import_cache runs $want pass(es) ($([ "$want" -eq 2 ] && echo cold || echo warm) registry)" \ + "$want" "$(grep -c pass "$scratch/passes")" + done + # --- gdk_timeout_is_hang: only the two timeout codes are a hang ---------- status=0; gdk_timeout_is_hang "$GDK_EXIT_SIGTERM_TIMEOUT" || status=1 _gdk_st_true 'timeout_is_hang recognises 124' "$status" @@ -1418,7 +1436,7 @@ gdk_engine_gate_run, gdk_engine_gate_held, gdk_receipt_key, gdk_receipt_hit, gdk_receipt_write, gdk_timeout_is_hang, gdk_restore_project_file, gdk_gate_log, gdk_gate_capture, gdk_gate_publish, gdk_gate_verdict, gdk_sweep_result_line, gdk_sweep_result_field, -gdk_sweep_failed_paths, gdk_rebuild_import_cache. +gdk_sweep_failed_paths, gdk_cold_class_registry, gdk_rebuild_import_cache. Engine admission defaults to the account home across checkouts. GDK_ENGINE_GATE_HOME explicitly selects an isolated domain for test fixtures. diff --git a/src/godot_devkit/godot/installables/import_cache.sh b/src/godot_devkit/godot/installables/import_cache.sh index 4973b915..7939a521 100644 --- a/src/godot_devkit/godot/installables/import_cache.sh +++ b/src/godot_devkit/godot/installables/import_cache.sh @@ -21,7 +21,9 @@ # native parse/import target also refreshes them. # - after adding / deleting / moving `.tscn` / `.tres` / asset files. # - a cold checkout with no `.godot/` (the native parse/import target also -# prepares a cold checkout). +# prepares a cold checkout). A cold registry gets two passes: the first +# seeds the `class_name` registry, so the second boots with a plugin's +# resource loaders registered before it parses their resources. # - `invalid UID … using text path instead` warnings on a cold run. # - after editing a `.png`/`.tres` asset, before a screenshot capture — # otherwise the capture renders the OLD art. diff --git a/src/godot_devkit/godot/installables/parse.sh b/src/godot_devkit/godot/installables/parse.sh index 8c8cb1e4..aa3fa0bc 100644 --- a/src/godot_devkit/godot/installables/parse.sh +++ b/src/godot_devkit/godot/installables/parse.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash # parse.sh — native headless import, bounded and isolated from player user://. -# Godot imports assets and registers named classes, then the compile helper +# Godot imports assets and registers named classes (a cold cache seeds the +# registry with a first import), then the compile helper # loads all scripts and boots the main scene. A 4.7.2 probe confirmed import # alone misses an unloaded script with a syntax error, so the sweep remains. set -uo pipefail @@ -20,7 +21,9 @@ usage() { usage: parse.sh [--help] [--self-test] Import the project, then compile every script and boot the main scene, all -inside the HOME sandbox. Fail on runtime engine errors, UID drift, missing +inside the HOME sandbox. A cold cache (no class registry) first gets a seed +import, so plugin resource loaders are registered before the checked import. +Fail on runtime engine errors, UID drift, missing sweep results, nonzero exits or timeouts. Exact post-completion shutdown resource diagnostics stay visible without changing the compile verdict. Writes import caches and sidecars normally. @@ -84,6 +87,23 @@ LOG="$(gdk_gate_log "$GATE_SLOT")" # Import writes shared cache state, so exclude concurrent engine readers. export GDK_ENGINE_GATE_WEIGHT=all export GDK_GATE_VERDICT=FAIL +# A cold cache seeds the class registry first, so the classified import boots +# with every plugin resource loader registered. The seed's own errors are the +# cold boot's, and stay in their own transcript. +if gdk_cold_class_registry; then + SEED_LOG="${LOG%.log}-seed.log" + : > "$SEED_LOG" + gdk_gate_capture "$SEED_LOG" -- gdk_run_bounded "$TIMEOUT_SECONDS" -- \ + "$GDK_GODOT" --path . --headless --import + if gdk_timeout_is_hang "$GDK_GATE_EXIT"; then + GDK_GATE_VERDICT=HANG + gdk_gate_verdict "$GATE_TAG" "FAIL (cold-cache seed import timeout)" "$SEED_LOG"; exit 1 + fi + if gdk_cold_class_registry; then + gdk_gate_verdict "$GATE_TAG" "FAIL (cold-cache seed wrote no class registry $GDK_CLASS_REGISTRY, exit $GDK_GATE_EXIT)" "$SEED_LOG"; exit 1 + fi + echo "[$GATE_TAG] cold cache: seeded the class registry — seed log: $SEED_LOG" +fi gdk_gate_capture "$LOG" -- gdk_run_bounded "$TIMEOUT_SECONDS" -- \ "$GDK_GODOT" --path . --headless --import ENGINE_EXIT="$GDK_GATE_EXIT" diff --git a/tests/test_runners_installable.py b/tests/test_runners_installable.py index 619c54c1..3c56fe04 100644 --- a/tests/test_runners_installable.py +++ b/tests/test_runners_installable.py @@ -393,9 +393,14 @@ def test_parse_imports_then_compiles_and_fails_on_engine_errors(tmp_path): root = tmp_path / 'repo' _project(root, PARSE) starts = tmp_path / 'starts.log' + # A cold cache: until the class registry exists, a plugin's resource + # loader is unregistered and the import errors; the import writes it. + registry = '.godot/global_script_class_cache.cfg' stub = _stub_engine(tmp_path, '#!/usr/bin/env bash\n' 'echo "$*" >> "$GDK_STUB_LOG"\n' - 'case "$*" in *--import*) echo "[ DONE ] loading_editor_layout";; ' + 'case "$*" in *--import*)\n' + f' [ -f {registry} ] || echo "ERROR: No loader found for resource: res://a.plugdata"\n' + f' mkdir -p .godot; : > {registry}; echo "[ DONE ] loading_editor_layout";;\n' '*) echo "SWEEP_RESULT 1 1";; esac\n') env = {'PATH': f'{stub}:/usr/bin:/bin:{Path(sys.executable).parent}', 'HOME': str(tmp_path / 'home'), 'GDK_RECEIPTS': '0', @@ -406,9 +411,21 @@ def parse(): text=True, capture_output=True, env=env) done = parse() assert done.returncode == 0, done.stdout + done.stderr + assert '[PARSE] cold cache: seeded the class registry' in done.stdout, done.stdout + assert 'No loader found' in (root / '.gate-reports/parse-seed.log').read_text(encoding='utf-8') + sweep = '--path . --headless -s res://tools/dev/runners/compile_sweep.gd' assert starts.read_text(encoding='utf-8').splitlines() == [ - '--path . --headless --import', - '--path . --headless -s res://tools/dev/runners/compile_sweep.gd'] + '--path . --headless --import', '--path . --headless --import', sweep] + starts.unlink() + done = parse() + assert done.returncode == 0, done.stdout + done.stderr + assert starts.read_text(encoding='utf-8').splitlines() == ['--path . --headless --import', sweep] + (root / registry).unlink() + (stub / 'godot').write_text('#!/usr/bin/env bash\necho "[ DONE ] loading_editor_layout"\n', encoding='utf-8') + done = parse() + assert done.returncode == 1 and 'seed wrote no class registry' in done.stdout, done.stdout + done.stderr + (root / '.godot').mkdir(exist_ok=True) + (root / registry).write_text('', encoding='utf-8') (stub / 'godot').write_text('#!/usr/bin/env bash\n' 'case "$*" in *--import*) echo "[ DONE ] loading_editor_layout"; exit 0;; esac\n' 'echo "SWEEP_FAIL res://bad.gd"\necho "SWEEP_RESULT 0 1"\n', encoding='utf-8')