@@ -294,32 +294,58 @@ private static Assembly CodeDomCompile(string source, string[] assemblyPaths, ou
294294 }
295295 }
296296
297+ // Compile to a controlled DLL path instead of in-memory. mcs prints a stray BOM line on
298+ // stdout that Mono's CodeDom can't parse, so it fabricates a bogus error (no error number,
299+ // text is just the BOM) and refuses to surface the assembly — even though mcs exits 0 and
300+ // wrote the DLL. We skip that bogus error and Assembly.Load the produced DLL ourselves.
301+ string outputAssemblyPath = Path . Combine ( Path . GetTempPath ( ) , $ "mcp-codedom-{ Guid . NewGuid ( ) : N} .dll") ;
297302 using ( var provider = new CSharpCodeProvider ( ) )
298303 {
299304 var parameters = new CompilerParameters
300305 {
301- GenerateInMemory = true ,
306+ GenerateInMemory = false ,
307+ OutputAssembly = outputAssemblyPath ,
302308 GenerateExecutable = false ,
303309 TreatWarningsAsErrors = false ,
304310 CompilerOptions = "@\" " + responseFilePath + "\" " ,
305311 } ;
306312
307- var results = provider . CompileAssemblyFromSource ( parameters , source ) ;
308-
309- if ( results . Errors . HasErrors )
313+ try
310314 {
315+ var results = provider . CompileAssemblyFromSource ( parameters , source ) ;
316+
317+ bool hasRealErrors = false ;
311318 foreach ( CompilerError error in results . Errors )
312319 {
313- if ( ! error . IsWarning )
314- {
315- int userLine = Math . Max ( 1 , error . Line - WrapperLineOffset ) ;
316- errors . Add ( $ "Line { userLine } : { error . ErrorText } ") ;
317- }
320+ if ( error . IsWarning )
321+ continue ;
322+
323+ // The bogus BOM "error": no error number, text is just the BOM/whitespace.
324+ string text = ( error . ErrorText ?? "" ) . Trim ( '\uFEFF ' , ' ' , '\t ' , '\r ' , '\n ' ) ;
325+ if ( string . IsNullOrEmpty ( error . ErrorNumber ) && string . IsNullOrEmpty ( text ) )
326+ continue ;
327+
328+ hasRealErrors = true ;
329+ int userLine = Math . Max ( 1 , error . Line - WrapperLineOffset ) ;
330+ errors . Add ( $ "Line { userLine } : { error . ErrorText } ") ;
318331 }
319- return null ;
320- }
321332
322- return results . CompiledAssembly ;
333+ if ( hasRealErrors )
334+ return null ;
335+
336+ if ( ! File . Exists ( outputAssemblyPath ) )
337+ {
338+ errors . Add ( "CodeDom reported success but produced no assembly." ) ;
339+ return null ;
340+ }
341+
342+ return Assembly . Load ( File . ReadAllBytes ( outputAssemblyPath ) ) ;
343+ }
344+ finally
345+ {
346+ try { if ( File . Exists ( outputAssemblyPath ) ) File . Delete ( outputAssemblyPath ) ; }
347+ catch { /* best effort cleanup */ }
348+ }
323349 }
324350 }
325351 finally
0 commit comments