Skip to content

Saving to a read-only file or unwritable folder crashes Schema.Editor instead of showing the save-failed popup #255

Description

@matt-edmondson

What's wrong

SchemaFile.TrySave (Schema.Editor/SchemaFile.cs, the try around Directory.CreateDirectory / File.WriteAllText) catches only JsonException and IOException. Both of those calls throw UnauthorizedAccessException when the target file is read-only or the directory isn't writable, and UnauthorizedAccessException is not an IOException, so it escapes.

SchemaFile.Load in the same file already catches UnauthorizedAccessException, so the load and save paths handle the same failure differently.

There is nothing above the save path to catch it. SaveToCurrentPath (Schema.Editor/SchemaEditor.Files.cs) expects TrySave to return false and then shows the "Failed to save schema to '...'" popup. That popup is never reached.

Failure scenario

  1. Open a .schema.json that is read-only, for example a Perforce or other VCS checkout that isn't opened for edit, or a file with attrib +r. Alternatively, use Save As into a directory you can't write to.
  2. Make edits.
  3. Press Ctrl+S, choose File > Save, or click "Save" on the unsaved-changes prompt that appears when you close the editor.
  4. UnauthorizedAccessException escapes the ImGui frame callback and the editor terminates. The edits the user was trying to save are lost.

Secondary problem in the same flow

SaveAs assigns CurrentSchemaPath = filePath before it attempts the save. Even once the crash is fixed, a Save As that fails leaves the document pointing at the path that failed. The window title shows that path, and the next Ctrl+S writes there instead of to the previous good path.

Suggested fix

  • Catch UnauthorizedAccessException in TrySave. Ideally, return the failure reason the way Load does (Message = ...), so the popup can say why the save failed (read-only, access denied) instead of giving a generic message.
  • In SaveAs, assign CurrentSchemaPath only after the save succeeds, or restore the previous path when it fails.
  • Add a SchemaFileTests case that saves over a read-only file and asserts that TrySave returns false instead of throwing.

Related but distinct: #243 covers the same exception class in GenerateToDisk / kschema, not the editor's document save.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions