|
| 1 | +@tool |
| 2 | +extends EditorPlugin |
| 3 | + |
| 4 | +# Synthetic test for #245: macOS Godot 4.6.2-mono leaves a newly-injected |
| 5 | +# Dictionary field with null _p after hot-reloading the script class on a |
| 6 | +# live instance. Models the v2.1.1 → v2.1.2 self-update path of godot-ai. |
| 7 | +# |
| 8 | +# Sequence on _enter_tree (deferred so editor is fully booted): |
| 9 | +# 1) stage repro_v1.gd content into res://repro.gd |
| 10 | +# 2) construct an instance of v1 |
| 11 | +# 3) overwrite res://repro.gd with v2 content (adds Dictionary field) |
| 12 | +# 4) trigger filesystem rescan + script reload |
| 13 | +# 5) read instance.injected_dict.keys() — bug fires here |
| 14 | + |
| 15 | +var _instance: RefCounted |
| 16 | + |
| 17 | + |
| 18 | +func _enter_tree() -> void: |
| 19 | + print("[repro] _enter_tree — scheduling test") |
| 20 | + get_tree().create_timer(2.0).timeout.connect(_step_1_stage_v1) |
| 21 | + |
| 22 | + |
| 23 | +func _exit_tree() -> void: |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +func _read_text(p: String) -> String: |
| 28 | + var f := FileAccess.open(p, FileAccess.READ) |
| 29 | + if f == null: |
| 30 | + print("[repro] FAIL: cannot read %s" % p) |
| 31 | + return "" |
| 32 | + var s := f.get_as_text() |
| 33 | + f.close() |
| 34 | + return s |
| 35 | + |
| 36 | + |
| 37 | +func _write_text(p: String, content: String) -> void: |
| 38 | + var f := FileAccess.open(p, FileAccess.WRITE) |
| 39 | + if f == null: |
| 40 | + print("[repro] FAIL: cannot open %s for write" % p) |
| 41 | + return |
| 42 | + f.store_string(content) |
| 43 | + f.close() |
| 44 | + |
| 45 | + |
| 46 | +func _step_1_stage_v1() -> void: |
| 47 | + print("[repro] step 1: stage v1 at res://repro.gd") |
| 48 | + var v1: String = _read_text("res://repro_v1.gd") |
| 49 | + _write_text("res://repro.gd", v1) |
| 50 | + EditorInterface.get_resource_filesystem().scan() |
| 51 | + get_tree().create_timer(2.0).timeout.connect(_step_2_construct_v1) |
| 52 | + |
| 53 | + |
| 54 | +func _step_2_construct_v1() -> void: |
| 55 | + print("[repro] step 2: construct v1 instance") |
| 56 | + var script_v1: GDScript = load("res://repro.gd") as GDScript |
| 57 | + if script_v1 == null: |
| 58 | + print("[repro] FAIL: v1 class did not load") |
| 59 | + return |
| 60 | + _instance = script_v1.new() |
| 61 | + if _instance == null: |
| 62 | + print("[repro] FAIL: v1 instance is null") |
| 63 | + return |
| 64 | + print("[repro] v1 instance: %s" % _instance) |
| 65 | + var s: Variant = _instance.call("say") |
| 66 | + print("[repro] v1 .say() -> %s" % str(s)) |
| 67 | + var prop_names: Array[String] = [] |
| 68 | + for p in _instance.get_property_list(): |
| 69 | + prop_names.append(p["name"]) |
| 70 | + print("[repro] v1 property names: %s" % str(prop_names)) |
| 71 | + get_tree().create_timer(1.0).timeout.connect(_step_3_overwrite_to_v2) |
| 72 | + |
| 73 | + |
| 74 | +func _step_3_overwrite_to_v2() -> void: |
| 75 | + print("[repro] step 3: overwrite res://repro.gd with v2 (adds Dictionary field)") |
| 76 | + var v2: String = _read_text("res://repro_v2.gd") |
| 77 | + _write_text("res://repro.gd", v2) |
| 78 | + EditorInterface.get_resource_filesystem().scan() |
| 79 | + get_tree().create_timer(3.0).timeout.connect(_step_4_force_reload) |
| 80 | + |
| 81 | + |
| 82 | +func _step_4_force_reload() -> void: |
| 83 | + print("[repro] step 4: force script reload + reimport") |
| 84 | + EditorInterface.get_resource_filesystem().reimport_files(PackedStringArray(["res://repro.gd"])) |
| 85 | + get_tree().create_timer(2.0).timeout.connect(_step_5_probe) |
| 86 | + |
| 87 | + |
| 88 | +func _step_5_probe() -> void: |
| 89 | + print("[repro] step 5: probe instance after hot-reload") |
| 90 | + if _instance == null: |
| 91 | + print("[repro] FAIL: instance was GC'd") |
| 92 | + _quit_with(1) |
| 93 | + return |
| 94 | + var prop_names: Array[String] = [] |
| 95 | + for p in _instance.get_property_list(): |
| 96 | + prop_names.append(p["name"]) |
| 97 | + print("[repro] post-reload property names: %s" % str(prop_names)) |
| 98 | + |
| 99 | + var v: Variant = _instance.get("injected_dict") |
| 100 | + print("[repro] inst.get('injected_dict'): typeof=%d value=%s" % [typeof(v), str(v)]) |
| 101 | + |
| 102 | + if typeof(v) == TYPE_DICTIONARY: |
| 103 | + var d: Dictionary = v |
| 104 | + print("[repro] is Dictionary; calling .keys()...") |
| 105 | + # THIS is where the bug would crash (Dictionary::keys on null _p) |
| 106 | + var k: Array = d.keys() |
| 107 | + print("[repro] .keys() returned %s (size=%d)" % [str(k), k.size()]) |
| 108 | + else: |
| 109 | + print("[repro] field is not Dictionary; type=%d" % typeof(v)) |
| 110 | + |
| 111 | + # Call the v2 .say() method which itself does injected_dict.keys() |
| 112 | + print("[repro] calling inst.say() (v2 body)...") |
| 113 | + var msg: Variant = _instance.call("say") |
| 114 | + print("[repro] inst.say() -> %s" % str(msg)) |
| 115 | + |
| 116 | + print("[repro] DONE — no SIGABRT. Bug did NOT reproduce in synthetic.") |
| 117 | + _quit_with(0) |
| 118 | + |
| 119 | + |
| 120 | +func _quit_with(code: int) -> void: |
| 121 | + get_tree().create_timer(0.5).timeout.connect(func(): get_tree().quit(code)) |
0 commit comments