Fix Compiler.config not being initialized by default

* Extended whitelist tests to use the other method of creating a compiler without explicit compiler settings
* Initialize Compiler._config as it's a struct so everything was zeroed before
This commit is contained in:
Matt Stevens
2025-12-17 11:07:19 +00:00
committed by GitHub
parent 9ad0943c6a
commit 8c25780848
5 changed files with 71 additions and 11 deletions

View File

@@ -1,3 +1,4 @@
using System;
using static System.Runtime.InteropServices.MemoryMarshal;
class UsingStaticTest

View File

@@ -9,6 +9,65 @@ namespace TestCompiler;
[TestClass]
public partial class BlacklistTest
{
[TestMethod]
public async Task DefaultCompilerFailsWhitelist()
{
var codePath = System.IO.Path.GetFullPath( "data/code/blacklist" );
var group = new CompileGroup( "TestWhitelist" );
var compiler = group.GetOrCreateCompiler( "test" );
compiler.AddSourcePath( codePath );
compiler.MarkForRecompile();
await group.BuildAsync();
// Verify compilation failed due to whitelist violations
var output = compiler.Output;
Assert.IsNotNull( output );
Assert.IsFalse( output.Successful, "Compiler should fail with default whitelist settings" );
Assert.IsTrue( output.Diagnostics.Count > 0, "Should have diagnostics for whitelist violations" );
}
[TestMethod]
public async Task CompilerWithWhitelistFails()
{
var codePath = System.IO.Path.GetFullPath( "data/code/blacklist" );
var group = new CompileGroup( "TestWhitelist" );
var compilerSettings = new Compiler.Configuration();
compilerSettings.Whitelist = true;
compilerSettings.Unsafe = false;
var compiler = group.CreateCompiler( "test", codePath, compilerSettings );
await group.BuildAsync();
// Verify compilation failed due to whitelist being enabled
var output = compiler.Output;
Assert.IsNotNull( output );
Assert.IsFalse( output.Successful, "Compiler should fail when whitelist is explicitly enabled" );
Assert.IsTrue( output.Diagnostics.Count > 0, "Should have diagnostics for whitelist violations" );
}
[TestMethod]
public async Task CompilerWithoutWhitelistSucceeds()
{
var codePath = System.IO.Path.GetFullPath( "data/code/blacklist" );
var group = new CompileGroup( "TestWhitelist" );
var compilerSettings = new Compiler.Configuration();
compilerSettings.Whitelist = false;
compilerSettings.Unsafe = true;
var compiler = group.CreateCompiler( "test", codePath, compilerSettings );
await group.BuildAsync();
// Verify compilation succeeded with whitelist disabled
var output = compiler.Output;
Assert.IsNotNull( output );
Assert.IsTrue( output.Successful, "Compiler should succeed when whitelist is disabled" );
Assert.IsNull( output.Exception, "Should not have any exceptions" );
}
[TestMethod]
public async Task EndToEndBuildFailure()
{

View File

@@ -114,10 +114,10 @@ partial class Compiler
var archive = new CodeArchive();
archive.CompilerName = Name;
archive.Configuration = config;
archive.Configuration = _config;
output.Archive = archive;
var parseOptions = config.GetParseOptions();
var parseOptions = _config.GetParseOptions();
//
// References
@@ -217,7 +217,7 @@ partial class Compiler
// check for blacklisted methods/types used in compilation
// we need this because the c# compiler will post optimize and use tons of blacklisted methods
// run this after generators because they can contain user inputs too
if ( config.Whitelist )
if ( _config.Whitelist )
{
RunBlacklistWalker( compiler, output );
@@ -242,7 +242,7 @@ partial class Compiler
peStream.Seek( 0, System.IO.SeekOrigin.Begin );
if ( config.Whitelist && Group.AccessControl is { } access )
if ( _config.Whitelist && Group.AccessControl is { } access )
{
var result = access.VerifyAssembly( peStream, out TrustedBinaryStream stream );
if ( !result.Success )

View File

@@ -78,7 +78,7 @@ partial class Compiler
/// </summary>
private string GetReplacementDirective( string filePath )
{
foreach ( var pair in config.ReplacementDirectives )
foreach ( var pair in _config.ReplacementDirectives )
{
if ( filePath.EndsWith( pair.Key, StringComparison.OrdinalIgnoreCase ) )
return pair.Value;
@@ -108,7 +108,7 @@ partial class Compiler
var pathFolders = folderName.Split( new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries );
// is this ignored
if ( pathFolders.Any( x => config.IgnoreFolders.Contains( x, StringComparer.OrdinalIgnoreCase ) ) )
if ( pathFolders.Any( x => _config.IgnoreFolders.Contains( x, StringComparer.OrdinalIgnoreCase ) ) )
return;
if ( pathFolders.Contains( "obj", StringComparer.OrdinalIgnoreCase ) )
@@ -156,7 +156,7 @@ partial class Compiler
{
tree = CSharpSyntaxTree.ParseText( text: sourceText, options: fileOptions, path: physicalPath );
if ( config.StripDisabledTextTrivia )
if ( _config.StripDisabledTextTrivia )
tree = StripDisabledTextTrivia( tree );
}
@@ -168,7 +168,7 @@ partial class Compiler
var wrappedSourceText = SourceText.From( wrappedText, Encoding.UTF8 );
tree = CSharpSyntaxTree.ParseText( wrappedSourceText, fileOptions, physicalPath );
if ( config.StripDisabledTextTrivia )
if ( _config.StripDisabledTextTrivia )
tree = StripDisabledTextTrivia( tree );
}

View File

@@ -85,7 +85,7 @@ public sealed partial class Compiler : IDisposable
/// <summary>
/// The compiler's settings.
/// </summary>
private Compiler.Configuration config;
private Compiler.Configuration _config = new();
/// <summary>
/// Should only ever get called from CompileGroup.
@@ -134,13 +134,13 @@ public sealed partial class Compiler : IDisposable
public void SetConfiguration( Compiler.Configuration newConfig )
{
config = newConfig;
_config = newConfig;
incrementalState.Reset();
}
public Configuration GetConfiguration()
{
return config;
return _config;
}
/// <summary>