Repository: godot
Issue: Nonexistent resource error when trying to use change_scene_to_packed() to change the scene to a PackedScene via an exported variable when the target scene also has a reference to the first scene
Labels: topic:core,documentation

### Godot version

4.0.2

### System information

Windows 10

### Issue description

I created a button that, when pressed, called change_scene_to_packed() with a PackedScene variable as the target. The variable is exported into the editor, where I can then drag and drop a scene into it for the button to change the scene to.

This works fine, the button changes the scene to the target scene. If the target scene also has another instance of the button in it linking to a third scene, that also works fine.

However, if one of the scenes links back to an earlier scene, it crashes and only produces an error in the console, not in the editor itself.

```
ERROR: res://scene2.tscn:4 - Parse Error: [ext_resource] referenced nonexistent resource at: res://scene1.tscn
   at: load (scene/resources/resource_format_text.cpp:490)
ERROR: Failed loading resource: res://scene2.tscn. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:222)
ERROR: res://scene1.tscn:4 - Parse Error: [ext_resource] referenced nonexistent resource at: res://scene2.tscn
   at: load (scene/resources/resource_format_text.cpp:490)
ERROR: Failed loading resource: res://scene1.tscn. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:222)
ERROR: Failed loading scene: res://scene1.tscn
   at: (main/main.cpp:2965)
ERROR: 2 RID allocations of type 'N10RendererRD12LightStorage11ShadowAtlasE' were leaked at exit.
ERROR: 1 RID allocations of type 'N10RendererRD12LightStorage15ReflectionAtlasE' were leaked at exit.
ERROR: 1 RID allocations of type 'N10RendererRD14TextureStorage12RenderTargetE' were leaked at exit.
ERROR: 2 RID allocations of type 'N10RendererRD14TextureStorage7TextureE' were leaked at exit.
ERROR: 1 RID allocations of type 'N18RendererCanvasCull6CanvasE' were leaked at exit.
ERROR: 1 RID allocations of type 'N16RendererViewport8ViewportE' were leaked at exit.
ERROR: 1 RID allocations of type 'N17RendererSceneCull8ScenarioE' were leaked at exit.
WARNING: 4 RIDs of type "Texture" were leaked.
     at: finalize (drivers/vulkan/rendering_device_vulkan.cpp:9301)
WARNING: ObjectDB instances leaked at exit (run with --verbose for details).
     at: cleanup (core/object/object.cpp:1982)
```

Here's the error when I have scene1 link to scene2, scene2 link to scene3, and scene3 link back to scene1:

```
ERROR: res://scene 3.tscn:4 - Parse Error: [ext_resource] referenced nonexistent resource at: res://scene1.tscn
   at: load (scene/resources/resource_format_text.cpp:490)
ERROR: Failed loading resource: res://scene 3.tscn. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:222)
ERROR: res://scene2.tscn:4 - Parse Error: [ext_resource] referenced nonexistent resource at: res://scene 3.tscn
   at: load (scene/resources/resource_format_text.cpp:490)
ERROR: Failed loading resource: res://scene2.tscn. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:222)
ERROR: res://scene1.tscn:4 - Parse Error: [ext_resource] referenced nonexistent resource at: res://scene2.tscn
   at: load (scene/resources/resource_format_text.cpp:490)
ERROR: Failed loading resource: res://scene1.tscn. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:222)
ERROR: Failed loading scene: res://scene1.tscn
   at: (main/main.cpp:2965)
ERROR: 2 RID allocations of type 'N10RendererRD12LightStorage11ShadowAtlasE' were leaked at exit.
ERROR: 1 RID allocations of type 'N10RendererRD12LightStorage15ReflectionAtlasE' were leaked at exit.
ERROR: 1 RID allocations of type 'N10RendererRD14TextureStorage12RenderTargetE' were leaked at exit.
ERROR: 2 RID allocations of type 'N10RendererRD14TextureStorage7TextureE' were leaked at exit.
ERROR: 1 RID allocations of type 'N18RendererCanvasCull6CanvasE' were leaked at exit.
ERROR: 1 RID allocations of type 'N16RendererViewport8ViewportE' were leaked at exit.
ERROR: 1 RID allocations of type 'N17RendererSceneCull8Sce
```

When I tried saving the project and reloading it, I got a "Load Error" dialog with an empty message box and this error in both the console and the debugger:

```
ERROR: res://scene2.tscn:20 - Parse Error: [ext_resource] referenced non-loaded resource at: res://scene1.tscn
   at: _parse_ext_resource (scene/resources/resource_format_text.cpp:174)
```

The button in the last scene is also stripped of its target scene. This happens whether or not I run the project after assigning the variables in the editor.

If I use change_scene_to_file() and use a file path string instead of a PackedScene variable, the scene loop works fine. This only happens when using change_scene_to_packed() and a PackedScene variable.

### Steps to reproduce

1. Create a button with the following GDScript code, with _on_pressed() connected to the pressed() signal node:

```
extends Button

@export var targetscene: PackedScene

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass


func _on_pressed():
	get_tree().change_scene_to_packed(targetscene)
```

2. Create a series of at least two scenes, each containing an instance of the button that targets the next scene, and the final scene containing an instance of the button that targets the first scene.
3. Run the project with the first scene as the starting scene.
4. Save and load the project.

The minimal reproduction project contains a version with two scenes. If you use it, make sure you reassign the button's targetscene variable on the second scene to scene1.tscn since Godot empties it upon loading.

### Minimal reproduction project

[repro test both scenes.zip](https://github.com/godotengine/godot/files/11475012/repro.test.both.scenes.zip)