Repository: godotengine/godot (language: cpp)

Issue:
Editor wants to reload GDScript files constantly
### Tested versions

- Reproducible in v4.4.beta2.mono.official [a013481b0]
- NOT Reproducible in v4.4.beta1.mono.official [d33da79d3]

### System information

Godot v4.4.beta2.mono - Windows 10 (build 19045) - Multi-window, 1 monitor - Vulkan (Forward+) - dedicated NVIDIA GeForce GTX 1060 6GB (NVIDIA; 32.0.15.6094) - Intel(R) Core(TM) i5-3570K CPU @ 3.40GHz (4 threads)

### Issue description

It seems that even if Godot is able to handle external changes to files through the language server, changes are still treated like they were "external changes". Even after told to reload from disk, the file is considered being changed and "Files have been modified outside Godot" dialog is displayed whenever Godot gets focus.

My "Auto Reload Scripts on External Change" setting is disabled (default value). If I change that setting to enabled, the problem goes away, but my understanding is that the language server should handle this situation with the default setting (no auto reload) and auto reload should only be used when the external editor doesn't support language server.

### Steps to reproduce

1. Open Godot and start a new project
1. Open external editor, eg. vscode. Make sure external editor connects to the language server
1. Keep both Godot and external editor visible
1. Add a GDScript file to the project 
1. Edit GDScript file using the external editor => You can see the change appearing to Godot's code editor as expected
1. Switch focus back to Godot 
=> **"Files have been modified outside Godot" BUG This shouldn't happen.**

1. Click "Reload from disk" 
1. Switch focus out from Godot (doesn't need to focus to the external editor)
1. Switch focus back to Godot
=> **"Files have been modified outside Godot" BUG This happens again.**

### Minimal reproduction project (MRP)

N/A


Discussion / hints:
Likely related to this: https://github.com/godotengine/godot/pull/101616
should be the same cause as #101615
Can't be the same cause if it does not happen in beta1.
Fair point, tested with my local revert of #96499, it does not affect this
Yes I click outside Godot window, then back in and it shows the "Files have been modified outside Godot". Then I click "Reload from disk". Then I click outside godot window and back to godot window. Then the dialog pops up again "Files have been modified outside Godot" for the same file. This happens in a forever loop. 

This did not happen in Godot 4.4 Beta 1. 

Restarting whole godot makes it go away, but the moment I modify a .gd file again outside godot, the problem occurss again.
Don't think this is related only to GDScript but any file.

This only happens, if the file was modified externally while it was also open in Godot's script editor. (You don't have to be on `Script` tab you could switch to any other tab as long as file is open there)

Also it doesn't matter if external editor is connected to language server and Godot and external editor doesn't need to be both visible to reproduce it.

Prompt is also triggered when you are stopping running project, and sometimes it prevents you from stopping running project (you have to click stop button and close prompt dialog several times to stop the project, couldn't find exact way to reproduce it but it sometimes it happens)

But If you edit file externally while project was running, prompt won't appear (when changing window focus) until you stop running the project, then it starts appearing again
@AThousandShips think this shouldn't be in GDScript Issue Triage
I'll leave it there for now and let the team take a look just to be safe

Fix patch:
diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp
index 1a1e5697c2d2..a6c18d3b31c2 100644
--- a/editor/plugins/script_editor_plugin.cpp
+++ b/editor/plugins/script_editor_plugin.cpp
@@ -2826,19 +2826,19 @@ void ScriptEditor::_reload_scripts(bool p_refresh_only) {
 		}
 
 		if (!p_refresh_only) {
-			uint64_t last_date = edited_res->get_last_modified_time();
+			uint64_t last_date = se->edited_file_data.last_modified_time;
 			uint64_t date = FileAccess::get_modified_time(edited_res->get_path());
 
 			if (last_date == date) {
 				continue;
 			}
+			se->edited_file_data.last_modified_time = date;
 
 			Ref<Script> scr = edited_res;
 			if (scr.is_valid()) {
 				Ref<Script> rel_scr = ResourceLoader::load(scr->get_path(), scr->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
 				ERR_CONTINUE(rel_scr.is_null());
 				scr->set_source_code(rel_scr->get_source_code());
-				scr->set_last_modified_time(rel_scr->get_last_modified_time());
 				scr->reload(true);
 
 				update_docs_from_script(scr);
@@ -2849,7 +2849,6 @@ void ScriptEditor::_reload_scripts(bool p_refresh_only) {
 				Ref<JSON> rel_json = ResourceLoader::load(json->get_path(), json->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
 				ERR_CONTINUE(rel_json.is_null());
 				json->parse(rel_json->get_parsed_text(), true);
-				json->set_last_modified_time(rel_json->get_last_modified_time());
 			}
 
 			Ref<TextFile> text_file = edited_res;


Test patch: