CoplayDev/unity-mcp · error · Exception

Failed to write config file '{path}': {ex.Message}

Error message

Failed to write config file '{path}': {ex.Message}

What it means

McpConfigurationHelper writes the config atomically: it backs up the original, writes a temp file, then replaces the target. If any step throws, the catch block restores the backup (only if the final write did not complete) and re-throws a generic Exception whose message embeds the original error and whose InnerException is the real cause. The 'finally' cleans up temp/backup files defensively.

Source

Thrown at MCPForUnity/Editor/Helpers/McpConfigurationHelper.cs:274

                        }
                        catch { }
                        File.Move(path, backup);
                    }
                    File.Move(tmp, path);
                    writeDone = true;
                }
            }
            catch (Exception ex)
            {
                try
                {
                    if (!writeDone && File.Exists(backup))
                    {
                        try { File.Copy(backup, path, true); } catch { }
                    }
                }
                catch { }
                throw new Exception($"Failed to write config file '{path}': {ex.Message}", ex);
            }
            finally
            {
                try { if (File.Exists(tmp)) File.Delete(tmp); } catch { }
                try { if (writeDone && File.Exists(backup)) File.Delete(backup); } catch { }
            }
        }
    }
}

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Close any editor or process holding the config file open, then retry.
  2. Ensure write permission on the config path and that the disk has free space.
  3. Inspect the InnerException for the true underlying error rather than the wrapper message.
  4. Avoid running two instances that write the same config concurrently.
Defensive patterns

Strategy: retry

Validate before calling

// Pre-check the config path is writable before attempting the atomic write.
string dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
string probe = Path.Combine(dir, $".writeprobe_{Guid():N}");
File.WriteAllText(probe, ""); File.Delete(probe);

Try / catch

for (int attempt = 0; attempt < 3; attempt++)
{
    try { McpConfigurationHelper.WriteConfig(path, json); break; }
    catch (Exception ex) when (IsTransientFileError(ex))
    {
        if (attempt == 2) throw;
        await Task.Delay(250 * (attempt + 1));
    }
}
// Inspect ex.InnerException for the real cause when rethrown.

Prevention

When it happens

Trigger: The config file is locked by another process; the disk is full; the path is read-only or permission-denied; the path exceeds OS length limits; antivirus quarantines the temp file mid-write.

Common situations: Config file left open in a text editor; another Unity/editor instance holding a write lock; a read-only project checkout; concurrent writes from two processes to the same config; transient AV interference.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/7351febf7017d295. Report an issue: GitHub.