Skip to content

Commit 498e904

Browse files
committed
fix(compiler): prevent frame update write loop, atomic diagnostics writes, and thread safety
1 parent a9b11e4 commit 498e904

2 files changed

Lines changed: 39 additions & 13 deletions

File tree

Packages/com.pereviader.unityclirunner/Editor/PollRefreshHandler.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ private string GetRefreshPollResponse()
3232
if (UnityCliCompilationTracker.ScriptCompilationFailed)
3333
{
3434
string diagnosticsPath = Path.Combine(Directory.GetCurrentDirectory(), "Temp", "unity_compilation_errors.txt");
35-
if (!File.Exists(diagnosticsPath) || new FileInfo(diagnosticsPath).Length == 0)
36-
{
37-
UnityCliCompilationTracker.WriteActiveErrorsToFile();
38-
}
39-
4035
if (File.Exists(diagnosticsPath) && new FileInfo(diagnosticsPath).Length > 0)
4136
{
4237
return "COMPILATION_ERROR";

Packages/com.pereviader.unityclirunner/Editor/UnityCliCompilationTracker.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public static bool CompilationRequested
4242
if (value)
4343
{
4444
s_CompilationRequestTime = EditorApplication.timeSinceStartup;
45-
s_ScriptCompilationFailed = false;
4645
}
4746
}
4847
}
@@ -114,6 +113,11 @@ private static string FormatCompilerDiagnostic(string rawMessage, string file, i
114113

115114
if (System.Text.RegularExpressions.Regex.IsMatch(msg, @"^([a-zA-Z]:)?[a-zA-Z0-9_./\\ -]+\([0-9]+,[0-9]+\):\s*(error|warning)\s+[a-zA-Z0-9]+:", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
116115
{
116+
int newlineIdx = msg.IndexOfAny(new[] { '\r', '\n' });
117+
if (newlineIdx >= 0)
118+
{
119+
msg = msg.Substring(0, newlineIdx).Trim();
120+
}
117121
return msg;
118122
}
119123

@@ -127,6 +131,12 @@ private static string FormatCompilerDiagnostic(string rawMessage, string file, i
127131
msg = msg.Substring(8).TrimStart();
128132
}
129133

134+
int nlIdx = msg.IndexOfAny(new[] { '\r', '\n' });
135+
if (nlIdx >= 0)
136+
{
137+
msg = msg.Substring(0, nlIdx).Trim();
138+
}
139+
130140
if (!string.IsNullOrEmpty(file) && line > 0)
131141
{
132142
return $"{file}({line},{column}): {typeStr} {msg}";
@@ -155,13 +165,9 @@ public static void UpdateCompilationState()
155165
else if (EditorApplication.timeSinceStartup - s_CompilationRequestTime > 1.5)
156166
{
157167
s_CompilationRequested = false;
168+
WriteActiveErrorsToFile();
158169
}
159170
}
160-
161-
if (!s_IsCompiling && !s_CompilationRequested && !s_RefreshPending)
162-
{
163-
WriteActiveErrorsToFile();
164-
}
165171
}
166172

167173
public static void ClearActiveEntries()
@@ -297,7 +303,7 @@ public static void WriteActiveErrorsToFile()
297303

298304
if (diagnostics.Count > 0)
299305
{
300-
File.WriteAllLines(errorsPath, diagnostics, new UTF8Encoding(false));
306+
WriteDiagnosticsFileAtomically(errorsPath, diagnostics);
301307
}
302308
else if (EditorUtility.scriptCompilationFailed)
303309
{
@@ -315,6 +321,31 @@ public static void WriteActiveErrorsToFile()
315321
}
316322
}
317323

324+
private static void WriteDiagnosticsFileAtomically(string errorsPath, IEnumerable<string> lines)
325+
{
326+
string tmpPath = errorsPath + "." + Guid.NewGuid().ToString("N") + ".tmp";
327+
try
328+
{
329+
File.WriteAllLines(tmpPath, lines, new UTF8Encoding(false));
330+
if (File.Exists(errorsPath))
331+
{
332+
File.Delete(errorsPath);
333+
}
334+
File.Move(tmpPath, errorsPath);
335+
}
336+
catch (Exception e)
337+
{
338+
Debug.LogError($"UnityCliRunner: Failed to write diagnostics file atomically to {errorsPath}: {e}");
339+
}
340+
finally
341+
{
342+
if (File.Exists(tmpPath))
343+
{
344+
try { File.Delete(tmpPath); } catch { }
345+
}
346+
}
347+
}
348+
318349
private static void WriteFallbackDiagnosticsIfCompilationFailed(string message)
319350
{
320351
try
@@ -327,7 +358,7 @@ private static void WriteFallbackDiagnosticsIfCompilationFailed(string message)
327358

328359
string diagnosticsPath = Path.Combine(GetTempDirectory(), CompilationDiagnosticsFileName);
329360
string diagnostic = $"UnityCliRunner(1,1): error UC0001: {message}";
330-
File.WriteAllText(diagnosticsPath, diagnostic + Environment.NewLine, new UTF8Encoding(false));
361+
WriteDiagnosticsFileAtomically(diagnosticsPath, new[] { diagnostic });
331362
}
332363
catch(Exception e)
333364
{

0 commit comments

Comments
 (0)