Repository: godot
Issue: [4.0] External changes to shader overwritten if builtin editor has ever shown that shader
Labels: bug,topic:editor,topic:shaders

**Godot version:**
4.0.dev.calinou.3ed5ff244

**OS/device including version:**
Windows 10.0.18362.900, NVIDIA / Vulkan

**Issue description:**
When a shader is changed from an external program, it is updated as expected. However, if the shader is opened in the text editor (double clicking the shader resource, or selecting it in a material), the original contents of the shader are displayed.

If the text editor is hidden/closed, the in-memory shader is replaced with the original version, even though the on-disk contents contain the external shader, and this state persists until the project is closed and later reopened. (When the project is closed or saved, the shader on disk is overwritten with an old copy.)

So basically, the behavior here depends on whether the shader code editor at the bottom has ever been viewed for a given shader (such as by expanding a ShaderMaterial) or not, within a given godot editor session.

**Steps to reproduce:**
(If using the attached project, open BUG.tscn and skip steps 1-2)
1. Create sprite with a simple shader, and save it to file:
```
shader_type canvas_item;
render_mode unshaded;
void fragment() {
	COLOR = vec4(1.0,1.0,0.0,1.0);
}
```
2. Quit to Project List and reopen (optional).
3. Open the shader in VS Code or another external text editor.
4. Open the scene, and observe the object is yellow.
5. In the external editor, modify the color to `vec4(0.0,1.0,0.0,1.0);` (green)
6. Alt-tab back to Godot, observe that the object is now green.
7. Double-click the shader, or expand the ShaderMaterial section of the inspector.
8. Observe that the code editor shows the new code: `vec4(0.0,1.0,0.0,1.0)` (green).
9. In the external editor, modify the color to `vec4(1.0,0.0,1.0,1.0);` (pink)
10. Alt-tab back to Godot, observe that the object is now pink.
11. Observe that the code editor still shows the previous code: `vec4(0.0,1.0,0.0,1.0)` (green).
12. select the root Node2d in the scene, such that the code editor closes.
13. The object color has reverted to green.
14. Quit to Project List and reopen. (Alternatively hit ctrl-S to save all resources).
15. The object in the scene is still green.
16. Observe that the shader on disk is overwritten with the old version previous visible in the code editor: both the code editor and the external editor show `vec4(0.0,1.0,0.0,1.0);` (green)
---
17. This process can now be repeated indefinitely: undo and save the old shader in the external editor, and observe that it is reflected.
18. Hit ctrl-s in godot editor and the shader is reverted both in scene and on disk.
etc.

19. Quit to Project List and reopen.
20. Now, never open the shader in the text editor (do not select the shader or expand the ShaderMaterial).
21. Change the shader externally and save.
22. Open godot and observe that the shader is updated.
23. Hit ctrl-s in godot. The shader on disk is *not* reverted, and nothing changes in the scene. This is the same result we observed in steps 5-8 earlier.

**Minimal reproduction project:**
[ExternalEditTest.zip](https://github.com/godotengine/godot/files/4927636/ExternalEditTest.zip)