C# scripts get reset/cleared/reverted by godot in certain circumstances
Bugsquad note: This issue has been confirmed several times already. No need to confirm it further.
Godot version
3.3.2.stable.mono.official
System information
Windows 10
Issue description
As the title says, sometimes, godot resets/reverts/clears c# script file contents for reasons unknown. It's hard to pinpoint exactly where or why it happened (not the first time i experienced this), but i recorded for a while and tried to reproduce it and 'luckily' caught it 2 times in a row - See video:
https://user-images.githubusercontent.com/8841352/124391409-ccbafb80-dcf0-11eb-85fc-49913536b3e9.mp4
Here it seems to have been triggered by opening the project, using the 'Build' option, then doing a 'Save All' (ctrl-s). In my case rider (IDE) then reloaded the changed file from disk, so i reverted it (undo) to reclaim my missing script content (but it's not related to rider as i can see the content vanishing even from other editors as well).
Steps to reproduce
Not sure, in my case here 3 specific files seemed affected but no other script. These are used in resources and are toolscripts, maybe that matters somehow, but others that are also used in resources didn't seem to be affected. And i found no steps to reliably trigger it, but it happens somewhat frequently. I'll try a bit more to see if i can find a reliable MRP
Minimal reproduction project
No response
Confirming that it is also happening on my projects, I use the same godot version. I also have no idea what triggers it :(
After testing a bit further, the SourceCode property of the script resource in question becomes empty when this issue arises. So it seems the editor is actually persisting this empty SourceCode property into the file, while it shouldn't.
Can you reproduce this in previous Godot versions such as 3.2.3?
Yes (3.2.3), it seems to happen quite frequently when building, then saving all.
It happens with you when moving files between folders. And do the deletes files.
Still an issue in 3.3.4
Still occurs in 3.4.2 when moving a script in the editor. A simple workaround for that is to always work outside the editor when it comes to CSharp stuff. I have only hit this bug twice in my 6 months of work.
Steps to reproduce (I think reliably?? NOTE: Do not deviate! Do not set up any project stuff.)
- Start the editor and create a brand new project.
- Create folders
res://a/andres://b/ - In the editor, create a new CSharp script at
res://a/MyScript.cs. - Open
res://a/MyScript.csin VSCode and replace the contents of the class with a comment like// Now you see me - Return to Godot Editor and build.
- In the Editor's FileSystem move
res://a/MyScript.csto folderres://b/. - The project should still not have any scenes set. Click
Node2Din the Scene dock and then save the scene asMain.tscn - Hit
ctrl-sto save all. - Return to the script and see that it's contents are replaced by the default stub.
It has taken a while to find that set of steps. It seems even a little deviation will make it not happen. This bug triggers in the most random ways... fun. Hopefully it's just one bug and not something that needs to be fixed in a dozen different locations.
I encounter an equivalent problem by generate c# classes over my GdUnit3 c# TestSuite builder
I do as save by using the script editor to save possible changes before extending an existing script script_editor._menu_option(EDITOR_ACTIONS.FILE_SAVE)
My code works fine to generate the new scrip but the editor shows still the old content.
After comment out the save action the script is "reload" correctly.
So seams to be the save action is not syncronzed and is overlapping with external script changes.
The worst part of this is that it also devours the local history for the file, making it impossible to recover with Undo/Redo in VSCode. Bumping this because I lost 5 hours of C# after moving a cs file from "res://Scenes/" to "res://Scripts" because Scenes is Godot's favorite directory to put new scripts for some reason.
EDIT: It is possible with VSCode to recover your work! Move the file back to where it came from, then open the local history panel and copy/paste to somewhere safe!
Pictured: The timeline is in the bottom left, and has access to all edits made at that exact path.
This is still happening in 4.0
To clarify, this will still happen even when not moving a file.
This happened to me in 4.0 without even moving the file. I will try to reproduce it.
I believe that it has something to do with "state." because when I close the engine and reopen it in another process, The bug does not happen anymore.
Still happening on 4.1.1 .Net version
I don't know how to make the issue START to happen, but once it happens, it is persistent and is 100% predictable.
I am using Win10 22H2, C# with VSCode
Here's how it happens:
- Open your Godot project
- Double click source code from Godot file explorer.
- Make any change to source code and save your source code in the code editor
- Go back to Godot, and just Save the project. You don't even need to compile. Just save.
- Go back to source code, and find that your change has been undone.
- Press Ctrl-Z in source code to bring back your change, then re-save source code.
- Go back to Godot and save.
- Go back to the code editor and see that your code change stayed. Good!
So here's what I've gathered from this, and I have observed this pattern 100% of the time.
- Issue only happens the first time you save the project in any Godot session. A Godot session begins when you open the project file, and ends when you close it.
- If Godot settings are set to Save first before Run (should be by default), then this issue occurs the first time you run the project in that Godot session because it saves the project first before running.
- Issue does not happen if you make a new Godot project (at least until that project gets infected some how).
EDIT I found the culprit, but I don't understand why it causes the issue.
- Inside the root of your game project folder, navigate to .Godot -> Editor
- There are a bunch of config files. Look for files along the lines of {myScriptName}.cs-folding-aLongStringHere.cfg
- You may have multiple. You'll have to remove them and test your project to find which file is the culprit. For me, the culprit was called "Game.cs-folding-04c57d74a5067c4e8f7056d760dc179c.cfg"
- Below is the full code within the .CFG that is causing the issue. I don't know how to use the code formatter on GitHub. "[folding] sections_unfolded=PackedStringArray("Resource") " https://pastebin.com/4gNMrqLe
- I have tested, and removing the line where it says "sections_unfolded..." resolves the code undo issue. Alternatively, just delete the whole file and it resolves as well. Alternatively, delete all content inside .Godot (except for the editor-layout.cfg to keep custom editor layouts) and it will resolve as well.
I'm still getting this in 4.1 w/ Rider. I havent yet divined the cause of it, but its super frusturating.
I havent moved any files. I dont really understand why saving in godot overwrites the file if I havent got it open in Godot. That seems like some sort of cache whackiness.
I'd move to V Studio but I cant get that to work for the mac (It doesnt seem to understand the solution files. Visual Studio code opens fine, but the code completion seems less than desirable, and I rather do enjoy IntelliJs IDEs.
edit: Damn, reading the comments, it isnt an IDE problem but a Godot problem.
Can this bug be prioritized please, its a show stopper and its deeply frusturating to have a bug wipe your work out.
It could be fixed if someone managed to reproduce the steps to trigger this bug. In the past, I have tried to locate it, but it only occurs under certain conditions. Due to this instability, no one knows how it has happened, which is why it hasn't been resolved yet. My personal idea is to add a debug trace that throws an exception and catch it when the function performs the cleanup; then, we can write the stack trace to a file and report it here.
My second idea is to compile the Godot Engine from its source code. While developing our game, we can utilize the debugging features in Visual Studio. This way, if the reset/clear bug occurs, we'll be able to set a breakpoint and begin debugging promptly to capture the issue. As for me, I'm currently busy with a full-time job and studying AI-related topics to incorporate into my game, so I won't be significantly involved in working with Godot, at least until next year. However, you can take the lead.
You can find the script to build the Godot Engine from source, enabling C# support and generating a Visual Studio Project, in this link. To run it, use PowerShell: Shift + right-click and choose Open PowerShell window here. Note that using other shells won't work and will fail at the dotnet nuget add source stage. After placing the script inside the godot folder, execute it using the following command: python3 mono_compile.py. Remember, the purpose of this script is to allow you to quickly execute these commands without having to copy and paste them every time you pull the latest code from upstream. It has only been tested on Windows 10, 64-bit.
Can confirm It happens constantly using Rider & Godot .Net 4.1 . It is pretty frustating.
I have been hit by the bug again, and I was again able to resolve it by clearing out all the files inside the \.godot\editor folder, except for editor_layout.cfg in order to preserve the editor layout.
There are precisely zero circumstances where the godot engine should EVER be writing to those C# sciptts if you are using an IDE. Start with that.....
Happened on Godot 4.1, twice Code revert to some random old version hours ago Not sure what cause it exacly, I have renamed the file around to test how godot handles script renaming
I just think that the editor should not have the access or cache the script anyhow. This bug should be marked top priority:
- easily destroy hours of work
- violate the common sense that only myself and git can mess with my code
- still not fixed and even not replicatable from 3.x
which means that simple opening the project somehow could ruin your code totally not acceptable and kills the "so far so good" atmosphere for a Unity switcher
It could be fixed if someone managed to reproduce the steps to trigger this bug. In the past, I have tried to locate it, but it only occurs under certain conditions. Due to this instability, no one knows how it has happened, which is why it hasn't been resolved yet. My personal idea is to add a debug trace that throws an exception and catch it when the function performs the cleanup; then, we can write the stack trace to a file and report it here.
My second idea is to compile the Godot Engine from its source code. While developing our game, we can utilize the debugging features in Visual Studio. This way, if the reset/clear bug occurs, we'll be able to set a breakpoint and begin debugging promptly to capture the issue. As for me, I'm currently busy with a full-time job and studying AI-related topics to incorporate into my game, so I won't be significantly involved in working with Godot, at least until next year. However, you can take the lead.
You can find the script to build the Godot Engine from source, enabling C# support and generating a Visual Studio Project, in this link. To run it, use PowerShell:
Shift + right-clickand chooseOpen PowerShell window here. Note that using other shells won't work and will fail at thedotnet nuget add sourcestage. After placing the script inside thegodotfolder, execute it using the following command:python3 mono_compile.py. Remember, the purpose of this script is to allow you to quickly execute these commands without having to copy and paste them every time you pull the latest code from upstream. It has only been tested on Windows 10, 64-bit.
If user dont intent to use the embeded script editor, can you simple provide an option to not allow any writing to code files overall? So that only external applications can make changes to the code files. That can prevent the damage before actually locating the actual cause