BUG: modifying the prefab with link to scriptable.

Hi there! I am using Unity 2021.3.15f1.

What am i doing: I generate the scriptable through the code. After that I save the link of scriptable in new created prefab (is nested from another one). Lets call the prefab with link is LinkPrefab. My workflow:

  1. (Code example below) Check the folder, where must be scriptable. If not empty - remove all. Else - creating empty directory.
  2. Than I generate the scriptable and save them in new directory.
  3. (Code example below) After that I should create nested prefab. I create that, and check FileSystem to old prefab existing. If not exist - create new. If exist - replace link on scriptable using EditPrefabContentsScope.

Problem reproduce:

  1. before generation, old prefab and old scriptable must be existed.
  2. I generate new scriptables, which replace old scriptables and change the link in LinkPrefab.
  3. If I entry in LinkPrefab - all correct. If I entry in another prefabs (lets call PrefabContainer), that contains LinkPrefab - all correct. Bit when I resave the PrefabContainer, exit from prefab edit mode and entry again in PrefabContainer - all links to scriptable in LinkPrefab will be null and marked modified. If i revert them, save and reenter in PrefabContainer - they all the same will be null. I opened the PrefabContainer through Notepad after reverting modifiers - the modifier didn’t revert (see attached image).

I didn’t check that with non nested prefab, but I think, that problem will be reproduced again.
Has anyone experienced this? Or maybe unity specialists can help me?

Paragraph 1 code.

StringBuilder builderScriptable = new StringBuilder("Assets/ScriptableObjects/Game/");
        builderScriptable.Append(transform.name);
        builderScriptable.Append("/{0}.asset");
     
        _folderName = builderScriptable.ToString();
        string directory = Path.GetDirectoryName(_folderName);
        if (Directory.Exists(directory))
        {
            string[] files = Directory.GetFiles(directory);
            for (int i = 0; i < files.Length; i++)
            {
                File.Delete(files[i]);
            }

            files = null;
        }
        else
        {
            Directory.CreateDirectory(directory);
        }

Paragraph 3.

TestPrefab newPrefab = (TestPrefab)PrefabUtility.InstantiatePrefab(_originalPrefab);
        newPrefab.transform.position = Vector3.zero;
        newPrefab.transform.rotation = Quaternion.identity;
        newPrefab.name = transform.name;
     
        string prefabPath = $"Assets/Prefabs/Game/{transform.name}.prefab";
        if (!File.Exists(prefabPath))
        {
            PrefabUtility.SaveAsPrefabAsset(newPrefab.gameObject, prefabPath);
        }
        else
        {
            using var editingScope = new PrefabUtility.EditPrefabContentsScope(prefabPath);
            TestPrefab prefab = editingScope.prefabContentsRoot.GetComponent<TestPrefab>();
            prefab.Info = info;
            EditorUtility.SetDirty(prefab);
            PrefabUtility.RecordPrefabInstancePropertyModifications(prefab);
        }
     
        DestroyImmediate(newPrefab.gameObject);
        DestroyImmediate(gameObject);

Also after generation I has next error in console.

NullReferenceException: SerializedObject of SerializedProperty has been Disposed.
UnityEditor.SerializedProperty.Verify (UnityEditor.SerializedProperty+VerifyFlags verifyFlags) (at /Users/bokken/build/output/unity/unity/Editor/Mono/SerializedProperty.bindings.cs:326)
UnityEditor.SerializedProperty.NextVisible (System.Boolean enterChildren) (at /Users/bokken/build/output/unity/unity/Editor/Mono/SerializedProperty.bindings.cs:337)
UnityEditor.GenericInspector.OnOptimizedInspectorGUI (UnityEngine.Rect contentRect) (at /Users/bokken/build/output/unity/unity/Editor/Mono/Inspector/GenericInspector.cs:120)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at /Users/bokken/build/output/unity/unity/ModuleOverrides/com.unity.ui/Editor/Inspector/InspectorElement.cs:629)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&) (at /Users/bokken/build/output/unity/unity/Modules/IMGUI/GUIUtility.cs:189)

I think, I found the problem. And it’s because in LinkPrefab saving wrong link. In Unity Inspector all visible correct, but when I open LinkPrefab through notepad - the link is empty. And the problem, to my mind, with the saving prefab through EditPrefabContentsScope or the creating asset and getting link to that. Code for creating asset is below.

Can anoyone help me?

string path = string.Format(_folderName, root.name);
AssetDatabase.CreateAsset(infoAsset, path);
info = infoAsset;