-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Override default properties with custom file-level directives #49767
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
Changes from 1 commit
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 | ||||
---|---|---|---|---|---|---|
|
@@ -61,6 +61,18 @@ internal sealed class VirtualProjectBuildingCommand : CommandBase | |||||
"MSBuild.rsp", | ||||||
]; | ||||||
|
||||||
/// <remarks> | ||||||
/// Kept in sync with the default <c>dotnet new console</c> project file (enforced by <c>DotnetProjectAddTests.SameAsTemplate</c>). | ||||||
/// </remarks> | ||||||
private static readonly FrozenDictionary<string, string> s_defaultProperties = FrozenDictionary.Create<string, string>(StringComparer.OrdinalIgnoreCase, | ||||||
[ | ||||||
new("OutputType", "Exe"), | ||||||
new("TargetFramework", "net10.0"), | ||||||
new("ImplicitUsings", "enable"), | ||||||
new("Nullable", "enable"), | ||||||
new("PublishAot", "true"), | ||||||
]); | ||||||
|
||||||
internal static readonly string TargetOverrides = """ | ||||||
<!-- | ||||||
Override targets which don't work with project files that are not present on disk. | ||||||
|
@@ -645,33 +657,37 @@ public static void WriteProjectFile( | |||||
writer.WriteLine(); | ||||||
} | ||||||
|
||||||
// Kept in sync with the default `dotnet new console` project file (enforced by `DotnetProjectAddTests.SameAsTemplate`). | ||||||
writer.WriteLine($""" | ||||||
<PropertyGroup> | ||||||
<OutputType>Exe</OutputType> | ||||||
<TargetFramework>net10.0</TargetFramework> | ||||||
<ImplicitUsings>enable</ImplicitUsings> | ||||||
<Nullable>enable</Nullable> | ||||||
<PublishAot>true</PublishAot> | ||||||
</PropertyGroup> | ||||||
"""); | ||||||
// Write default properties unless they are overridden by custom directives. | ||||||
var propertyDirectiveNames = propertyDirectives.Select(d => d.Name).ToHashSet(StringComparer.OrdinalIgnoreCase); | ||||||
if (!s_defaultProperties.Keys.All(propertyDirectiveNames.Contains)) | ||||||
333fred marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{ | ||||||
writer.WriteLine(" <PropertyGroup>"); | ||||||
foreach (var (name, value) in s_defaultProperties) | ||||||
{ | ||||||
if (!propertyDirectiveNames.Contains(name)) | ||||||
{ | ||||||
writer.WriteLine($""" | ||||||
<{name}>{EscapeValue(value)}</{name}> | ||||||
"""); | ||||||
} | ||||||
} | ||||||
writer.WriteLine(" </PropertyGroup>"); | ||||||
writer.WriteLine(); | ||||||
} | ||||||
|
||||||
if (isVirtualProject) | ||||||
{ | ||||||
writer.WriteLine(""" | ||||||
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. [nitpick] The code currently mixes raw string literals and direct
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback 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. Personally, I would also agree with copilot 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.
I'm not sure what is being suggested here. Why would I use WriteLine with interpolated strings when I don't need anything interpolated? And when I'm using interpolated strings, they are raw string literals. 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. Oh, the suggested fix is bogus. I'm talking about the mix of raw vs non-raw |
||||||
|
||||||
<PropertyGroup> | ||||||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems> | ||||||
</PropertyGroup> | ||||||
|
||||||
"""); | ||||||
} | ||||||
|
||||||
if (propertyDirectives.Any()) | ||||||
{ | ||||||
writer.WriteLine(""" | ||||||
|
||||||
<PropertyGroup> | ||||||
"""); | ||||||
writer.WriteLine(" <PropertyGroup>"); | ||||||
|
||||||
foreach (var property in propertyDirectives) | ||||||
{ | ||||||
|
@@ -683,25 +699,23 @@ public static void WriteProjectFile( | |||||
} | ||||||
|
||||||
writer.WriteLine(" </PropertyGroup>"); | ||||||
writer.WriteLine(); | ||||||
} | ||||||
|
||||||
if (isVirtualProject) | ||||||
{ | ||||||
// After `#:property` directives so they don't override this. | ||||||
writer.WriteLine(""" | ||||||
|
||||||
<PropertyGroup> | ||||||
<Features>$(Features);FileBasedProgram</Features> | ||||||
</PropertyGroup> | ||||||
|
||||||
"""); | ||||||
} | ||||||
|
||||||
if (packageDirectives.Any()) | ||||||
{ | ||||||
writer.WriteLine(""" | ||||||
|
||||||
<ItemGroup> | ||||||
"""); | ||||||
writer.WriteLine(" <ItemGroup>"); | ||||||
|
||||||
foreach (var package in packageDirectives) | ||||||
{ | ||||||
|
@@ -722,14 +736,12 @@ public static void WriteProjectFile( | |||||
} | ||||||
|
||||||
writer.WriteLine(" </ItemGroup>"); | ||||||
writer.WriteLine(); | ||||||
} | ||||||
|
||||||
if (projectDirectives.Any()) | ||||||
{ | ||||||
writer.WriteLine(""" | ||||||
|
||||||
<ItemGroup> | ||||||
"""); | ||||||
writer.WriteLine(" <ItemGroup>"); | ||||||
|
||||||
foreach (var projectReference in projectDirectives) | ||||||
{ | ||||||
|
@@ -741,6 +753,7 @@ public static void WriteProjectFile( | |||||
} | ||||||
|
||||||
writer.WriteLine(" </ItemGroup>"); | ||||||
writer.WriteLine(); | ||||||
} | ||||||
|
||||||
Debug.Assert(processedDirectives + directives.OfType<CSharpDirective.Shebang>().Count() == directives.Length); | ||||||
|
@@ -750,7 +763,6 @@ public static void WriteProjectFile( | |||||
Debug.Assert(targetFilePath is not null); | ||||||
|
||||||
writer.WriteLine($""" | ||||||
|
||||||
<ItemGroup> | ||||||
<Compile Include="{EscapeValue(targetFilePath)}" /> | ||||||
</ItemGroup> | ||||||
|
@@ -784,12 +796,10 @@ public static void WriteProjectFile( | |||||
|
||||||
writer.WriteLine(); | ||||||
writer.WriteLine(TargetOverrides); | ||||||
writer.WriteLine(); | ||||||
} | ||||||
|
||||||
writer.WriteLine(""" | ||||||
|
||||||
</Project> | ||||||
"""); | ||||||
writer.WriteLine("</Project>"); | ||||||
|
||||||
static string EscapeValue(string value) => SecurityElement.Escape(value); | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -640,14 +640,13 @@ public void Directives() | |
#:property LangVersion=preview | ||
Console.WriteLine(); | ||
""", | ||
expectedProject: $""" | ||
expectedProject: """ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Sdk Name="Aspire.Hosting.Sdk" Version="9.1.0" /> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>{ToolsetInfo.CurrentTargetFramework}</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<PublishAot>true</PublishAot> | ||
|
@@ -670,6 +669,44 @@ public void Directives() | |
"""); | ||
} | ||
|
||
/// <summary> | ||
/// There should be only one <c>PropertyGroup</c> element when the default properties are overridden. | ||
/// </summary> | ||
[Fact] | ||
public void Directives_AllDefaultOverridden() | ||
{ | ||
VerifyConversion( | ||
inputCSharp: """ | ||
#!/program | ||
#:sdk Microsoft.NET.Web.Sdk | ||
#:property OutputType=Exe | ||
#:property TargetFramework=net11.0 | ||
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.
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 know, this is supposed to be a different version for testing, I can probably use something like |
||
#:property Nullable=disable | ||
#:property PublishAot=false | ||
#:property Custom=1 | ||
#:property ImplicitUsings=disable | ||
Console.WriteLine(); | ||
""", | ||
expectedProject: """ | ||
<Project Sdk="Microsoft.NET.Web.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net11.0</TargetFramework> | ||
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. Same here. |
||
<Nullable>disable</Nullable> | ||
<PublishAot>false</PublishAot> | ||
<Custom>1</Custom> | ||
<ImplicitUsings>disable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
</Project> | ||
|
||
""", | ||
expectedCSharp: """ | ||
Console.WriteLine(); | ||
"""); | ||
} | ||
|
||
[Fact] | ||
public void Directives_Variable() | ||
{ | ||
|
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.
Any reason we can't use
ToolsetInfo.CurrentTargetFramework
here? I think that's the right value.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.
ToolsetInfo
is only available in tests. I'm introducing a product equivalent in #49528 and once that's merged I expect it to be used here (there will be conflicts that will remind me to do that).