Skip to content

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

Merged
merged 4 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 58 additions & 42 deletions src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Copy link
Member

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.

Copy link
Member Author

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).

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.
Expand Down Expand Up @@ -645,34 +657,33 @@ 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>
""");

if (isVirtualProject)
// Write default and custom properties.
{
writer.WriteLine("""

<PropertyGroup>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>
""");
}

if (propertyDirectives.Any())
{
writer.WriteLine("""
// First write the default properties except those specified by the user.
var customPropertyNames = propertyDirectives.Select(d => d.Name).ToHashSet(StringComparer.OrdinalIgnoreCase);
foreach (var (name, value) in s_defaultProperties)
{
if (!customPropertyNames.Contains(name))
{
writer.WriteLine($"""
<{name}>{EscapeValue(value)}</{name}>
""");
}
}

<PropertyGroup>
""");
// Write virtual-only properties.
if (isVirtualProject)
{
writer.WriteLine("""
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
""");
}

// Write custom properties.
foreach (var property in propertyDirectives)
{
writer.WriteLine($"""
Expand All @@ -682,24 +693,23 @@ public static void WriteProjectFile(
processedDirectives++;
}

writer.WriteLine(" </PropertyGroup>");
}
// Write virtual-only properties which cannot be overridden.
if (isVirtualProject)
{
writer.WriteLine("""
<Features>$(Features);FileBasedProgram</Features>
""");
}

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>
""");

Expand All @@ -721,13 +731,15 @@ public static void WriteProjectFile(
processedDirectives++;
}

writer.WriteLine(" </ItemGroup>");
writer.WriteLine("""
</ItemGroup>

""");
}

if (projectDirectives.Any())
{
writer.WriteLine("""

<ItemGroup>
""");

Expand All @@ -740,7 +752,10 @@ public static void WriteProjectFile(
processedDirectives++;
}

writer.WriteLine(" </ItemGroup>");
writer.WriteLine("""
</ItemGroup>

""");
}

Debug.Assert(processedDirectives + directives.OfType<CSharpDirective.Shebang>().Count() == directives.Length);
Expand All @@ -750,7 +765,6 @@ public static void WriteProjectFile(
Debug.Assert(targetFilePath is not null);

writer.WriteLine($"""

<ItemGroup>
<Compile Include="{EscapeValue(targetFilePath)}" />
</ItemGroup>
Expand All @@ -761,12 +775,12 @@ public static void WriteProjectFile(
{
var targetDirectory = Path.GetDirectoryName(targetFilePath) ?? "";
writer.WriteLine($"""
<ItemGroup>
<RuntimeHostConfigurationOption Include="EntryPointFilePath" Value="{EscapeValue(targetFilePath)}" />
<RuntimeHostConfigurationOption Include="EntryPointFileDirectoryPath" Value="{EscapeValue(targetDirectory)}" />
</ItemGroup>
<ItemGroup>
<RuntimeHostConfigurationOption Include="EntryPointFilePath" Value="{EscapeValue(targetFilePath)}" />
<RuntimeHostConfigurationOption Include="EntryPointFileDirectoryPath" Value="{EscapeValue(targetDirectory)}" />
</ItemGroup>

""");
""");
}

foreach (var sdk in sdkDirectives)
Expand All @@ -782,12 +796,14 @@ public static void WriteProjectFile(
""");
}

writer.WriteLine();
writer.WriteLine(TargetOverrides);
writer.WriteLine($"""

{TargetOverrides}

""");
}

writer.WriteLine("""

</Project>
""");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,26 +635,22 @@ public void Directives()
#!/program
#:sdk Microsoft.NET.Sdk
#:sdk Aspire.Hosting.Sdk@9.1.0
#:property TargetFramework=net11.0
#:property TargetFramework=net472
#:package System.CommandLine@2.0.0-beta4.22272.1
#: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>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>net11.0</TargetFramework>
<TargetFramework>net472</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>

Expand All @@ -670,6 +666,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=net472
#: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>net472</TargetFramework>
<Nullable>disable</Nullable>
<PublishAot>false</PublishAot>
<Custom>1</Custom>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>

</Project>

""",
expectedCSharp: """
Console.WriteLine();
""");
}

[Fact]
public void Directives_Variable()
{
Expand All @@ -687,9 +721,6 @@ public void Directives_Variable()
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<PropertyGroup>
<MyProp>MyValue</MyProp>
</PropertyGroup>

Expand Down Expand Up @@ -765,9 +796,6 @@ public void Directives_Separators()
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<PropertyGroup>
<Prop1>One=a/b</Prop1>
<Prop2>Two/a=b</Prop2>
</PropertyGroup>
Expand Down Expand Up @@ -877,9 +905,6 @@ public void Directives_Escaping()
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<PropertyGroup>
<Prop>&lt;test&quot;&gt;</Prop>
</PropertyGroup>

Expand Down Expand Up @@ -914,9 +939,6 @@ public void Directives_Whitespace()
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<PropertyGroup>
<Name>Value</Name>
<NugetPackageDescription>&quot;My package with spaces&quot;</NugetPackageDescription>
</PropertyGroup>
Expand Down Expand Up @@ -961,9 +983,6 @@ public void Directives_AfterToken()
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<PropertyGroup>
<Prop1>1</Prop1>
<Prop2>2</Prop2>
</PropertyGroup>
Expand Down Expand Up @@ -1010,9 +1029,6 @@ public void Directives_AfterIf()
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<PropertyGroup>
<Prop1>1</Prop1>
<Prop2>2</Prop2>
</PropertyGroup>
Expand Down Expand Up @@ -1056,9 +1072,6 @@ public void Directives_Comments()
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<PropertyGroup>
<Prop1>1</Prop1>
<Prop2>2</Prop2>
</PropertyGroup>
Expand Down
22 changes: 0 additions & 22 deletions test/dotnet.Tests/CommandTests/Run/RunFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1858,22 +1858,12 @@ public void Api()

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>{ToolsetInfo.CurrentTargetFramework}</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<PropertyGroup>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>net11.0</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<PropertyGroup>
<Features>$(Features);FileBasedProgram</Features>
</PropertyGroup>

Expand Down Expand Up @@ -1940,13 +1930,7 @@ public void Api_Diagnostic_01()
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<PropertyGroup>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>

<PropertyGroup>
<Features>$(Features);FileBasedProgram</Features>
</PropertyGroup>

Expand Down Expand Up @@ -2012,13 +1996,7 @@ public void Api_Diagnostic_02()
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<PropertyGroup>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>

<PropertyGroup>
<Features>$(Features);FileBasedProgram</Features>
</PropertyGroup>

Expand Down