-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rebuild compilations that use top-level statements #51845
Changes from 4 commits
e093816
c44eb97
a58b666
26e7d3e
8ab23fe
601e582
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,15 +136,9 @@ public OutputKind GetOutputKind() => | |
? OutputKind.ConsoleApplication | ||
: OutputKind.DynamicallyLinkedLibrary; | ||
|
||
public string? GetMainTypeName() => GetMainMethodInfo() is { } tuple | ||
? tuple.MainTypeName | ||
: null; | ||
public string? GetMainTypeName() => GetMainMethodInfo()?.MainTypeName; | ||
|
||
public string? GetMainMethodName() => GetMainMethodInfo() is { } tuple | ||
? tuple.MainMethodName | ||
: null; | ||
|
||
private (string MainTypeName, string MainMethodName)? GetMainMethodInfo() | ||
public (string MainTypeName, string MainMethodName)? GetMainMethodInfo() | ||
{ | ||
if (!(PdbReader.DebugMetadataHeader is { } header) || | ||
header.EntryPoint.IsNil) | ||
|
@@ -155,6 +149,15 @@ public OutputKind GetOutputKind() => | |
var mdReader = PeReader.GetMetadataReader(); | ||
var methodDefinition = mdReader.GetMethodDefinition(header.EntryPoint); | ||
var methodName = mdReader.GetString(methodDefinition.Name); | ||
|
||
// Here we only want to give the caller the main method name and containing type name if the method is named "Main" per convention. | ||
// If the main method has another name, we have to assume that specifying a main type name won't work. | ||
// For example, if the compilation uses top-level statements. | ||
if (methodName != WellKnownMemberNames.EntryPointMethodName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why return The current approach is in some ways hiding information and it's asking callers to infer behavior based on that information missing. Callers have to know that when this is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my view, the purpose of the method is to answer the question "what main type name should I pass to the compilation/emit options." If the answer is "none", then it's ok to return null. Would be happy to rename this to try and make that more explicit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is another way of phrasing my question: today
Should consumers react the same way to both situations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think if the tool can detect that the metadata is malformed, it might be good to throw and turn this into a misc error. But if the metadata is just absent, it feels ok for the consumer to do what they were doing before this PR--which is to react to the null by saying "ok, I just won't pass anything for the mainType or the debugEntryPoint." |
||
{ | ||
return null; | ||
} | ||
|
||
var typeHandle = methodDefinition.GetDeclaringType(); | ||
var typeDefinition = mdReader.GetTypeDefinition(typeHandle); | ||
var typeName = mdReader.GetString(typeDefinition.Name); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Reflection.PortableExecutable; | ||
using BuildValidator; | ||
using Microsoft.CodeAnalysis.CSharp.Test.Utilities; | ||
using Microsoft.CodeAnalysis.Emit; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Rebuild.UnitTests | ||
{ | ||
public class CSharpRebuildTests : CSharpTestBase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the new class here vs. adding onto the existing test case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I felt like "round tripping various compilation options" was pretty well delineated as a category of tests from "using various C# language features" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What type of test do you imagine won't involve a round trip at the end though? That in many ways is the ultimate test of the code here 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They all will test that round tripping works. But some tests are primarily about passing "unusual" compilation options while other tests are primarily about using "unusual" language features. That said, if you want me to put this in the existing file, please let me know and I will. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll let you decide. |
||
{ | ||
[Fact] | ||
public void TopLevelStatements() | ||
{ | ||
const string path = "test"; | ||
|
||
var original = CreateCompilation( | ||
@"System.Console.WriteLine(""I'm using top-level statements!"");", | ||
options: TestOptions.DebugExe); | ||
|
||
original.VerifyDiagnostics(); | ||
|
||
var originalBytes = original.EmitToArray(new EmitOptions(debugInformationFormat: DebugInformationFormat.Embedded)); | ||
var peReader = new PEReader(originalBytes); | ||
Assert.True(peReader.TryOpenAssociatedPortablePdb(path, path => null, out var provider, out _)); | ||
var pdbReader = provider!.GetMetadataReader(); | ||
|
||
var factory = LoggerFactory.Create(configure => { }); | ||
var logger = factory.CreateLogger(path); | ||
var bc = new BuildConstructor(logger); | ||
|
||
var optionsReader = new CompilationOptionsReader(logger, pdbReader, peReader); | ||
|
||
var sources = original.SyntaxTrees.Select(st => | ||
{ | ||
var text = st.GetText(); | ||
return new ResolvedSource(OnDiskPath: null, text, new SourceFileInfo(path, text.ChecksumAlgorithm, text.GetChecksum().ToArray(), text, embeddedCompressedHash: null)); | ||
}).ToImmutableArray(); | ||
var references = original.References.ToImmutableArray(); | ||
var (compilation, isError) = bc.CreateCompilation(optionsReader, path, sources, references); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this same pattern used in another test change? If so, could this be extracted to a helper method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will add helpers in the near future after we get byte-for-byte equality working in unit tests. |
||
|
||
Assert.False(isError); | ||
compilation.VerifyEmitDiagnostics(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a
public
method on apublic
type and hence tuples are generally discourage. Don't have to fix in this PR but feel like this will become arecord
before we can "ship" this library.