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

Conversation

jjonescz
Copy link
Member

Resolves #49177.

I've also consolidated the project writing logic so each "section" is responsible for its own trailing blank line.

@jjonescz jjonescz added the Area-run-file Items related to the "dotnet run <file>" effort label Jul 14, 2025
@jjonescz jjonescz marked this pull request as ready for review July 14, 2025 11:45
@Copilot Copilot AI review requested due to automatic review settings July 14, 2025 11:45
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR centralizes default MSBuild properties into a single s_defaultProperties dictionary, applies file‐level directives to override them, and updates tests to reflect the new XML output format.

  • Extract default <PropertyGroup> values into s_defaultProperties and write only non‐overridden defaults.
  • Update RunFileTests and DotnetProjectConvertTests to remove hardcoded <TargetFramework> entries and remove interpolation.
  • Add a new Directives_AllDefaultOverridden test to verify that overriding all defaults yields exactly one <PropertyGroup>.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
test/dotnet.Tests/CommandTests/Run/RunFileTests.cs Removed the default <TargetFramework> line from the expected XML
test/dotnet.Tests/CommandTests/Project/Convert/DotnetProjectConvertTests.cs Switched expectedProject to a non‐interpolated literal and removed <TargetFramework>
src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs Consolidated default property output into s_defaultProperties and adjusted XML writing logic
Comments suppressed due to low confidence (2)

test/dotnet.Tests/CommandTests/Run/RunFileTests.cs:1860

  • The test no longer expects the default <TargetFramework> property, but the code always emits it from s_defaultProperties. Update this test to re‐introduce <TargetFramework>net10.0</TargetFramework> (or the current default) in the expected XML.
                        <OutputType>Exe</OutputType>

src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs:670

  • [nitpick] The indentation for these dynamically generated property tags may not align with other XML elements. Consider using a consistent approach like writer.WriteLine($" <{name}>{EscapeValue(value)}</{name}>"); to enforce two spaces for the element and two more for its children.
                            <{name}>{EscapeValue(value)}</{name}>

}
writer.WriteLine(" </PropertyGroup>");
writer.WriteLine();
}

if (isVirtualProject)
{
writer.WriteLine("""
Copy link
Preview

Copilot AI Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The code currently mixes raw string literals and direct WriteLine calls for XML fragments. For consistency, you could unify on one style (either always use raw string literals or always use WriteLine with interpolated strings).

Suggested change
writer.WriteLine("""
writer.WriteLine($"""

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would also agree with copilot here.

Copy link
Member Author

@jjonescz jjonescz Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either always use raw string literals or always use WriteLine with interpolated strings

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.

Copy link
Member

Choose a reason for hiding this comment

The 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

@jjonescz jjonescz requested a review from a team July 14, 2025 11:52
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).

#!/program
#:sdk Microsoft.NET.Web.Sdk
#:property OutputType=Exe
#:property TargetFramework=net11.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main for the SDK is not net11 yet.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 net472 to make that clearer.


<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@jjonescz
Copy link
Member Author

@jaredpar for a review, thanks

@jjonescz
Copy link
Member Author

@333fred for a review, thanks

@jjonescz jjonescz requested a review from 333fred July 22, 2025 13:45
@jjonescz jjonescz merged commit 8e70150 into dotnet:main Jul 22, 2025
27 checks passed
@jjonescz jjonescz deleted the sprint-overwrite branch July 22, 2025 16:23
@jjonescz
Copy link
Member Author

/backport to release/10.0.1xx-preview7

Copy link
Contributor

Started backporting to release/10.0.1xx-preview7: https://github.com/dotnet/sdk/actions/runs/16450060171

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-run-file Items related to the "dotnet run <file>" effort
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dotnet project convert works incorrectly for target framework other than default base framework net10.0
4 participants