Skip to content

Commit 9d94e51

Browse files
authored
Merge branch 'release/10.0.2xx' into merge/release/10.0.1xx-to-release/10.0.2xx
2 parents 1f0bf2f + 03bac48 commit 9d94e51

File tree

5 files changed

+76
-44
lines changed

5 files changed

+76
-44
lines changed

documentation/general/dotnet-run-file.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ Additionally, the implicit project file has the following customizations:
5656
in case there is a project or solution in the same directory as the file-based app.
5757
This ensures that items from nested projects and artifacts are not included by the app.
5858

59+
- `EnableDefaultEmbeddedResourceItems` and `EnableDefaultNoneItems` properties are set to `false` if the default SDK (`Microsoft.NET.Sdk`) is being used.
60+
This avoids including files like `./**/*.resx` in simple file-based apps where users usually don't expect that.
61+
5962
## Grow up
6063

6164
When file-based programs reach an inflection point where build customizations in a project file are needed,

sdk.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<Project Path="src/Cli/dotnet/dotnet.csproj" />
6565
<Project Path="src/Cli/Microsoft.DotNet.Cli.Utils/Microsoft.DotNet.Cli.Utils.csproj" />
6666
<Project Path="src/Cli/Microsoft.DotNet.Configurer/Microsoft.DotNet.Configurer.csproj" />
67+
<Project Path="src/Cli/Microsoft.DotNet.FileBasedPrograms/Microsoft.DotNet.FileBasedPrograms.Package.csproj" />
6768
<Project Path="src/Cli/Microsoft.DotNet.InternalAbstractions/Microsoft.DotNet.InternalAbstractions.csproj" />
6869
<Project Path="src/Cli/Microsoft.TemplateEngine.Cli/Microsoft.TemplateEngine.Cli.csproj" />
6970
</Folder>

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,7 @@ public static void WriteProjectFile(
11571157
var packageDirectives = directives.OfType<CSharpDirective.Package>();
11581158
var projectDirectives = directives.OfType<CSharpDirective.Project>();
11591159

1160+
const string defaultSdkName = "Microsoft.NET.Sdk";
11601161
string firstSdkName;
11611162
string? firstSdkVersion;
11621163

@@ -1168,7 +1169,7 @@ public static void WriteProjectFile(
11681169
}
11691170
else
11701171
{
1171-
firstSdkName = "Microsoft.NET.Sdk";
1172+
firstSdkName = defaultSdkName;
11721173
firstSdkVersion = null;
11731174
}
11741175

@@ -1192,6 +1193,18 @@ public static void WriteProjectFile(
11921193
<DisableDefaultItemsInProjectFolder>true</DisableDefaultItemsInProjectFolder>
11931194
""");
11941195

1196+
// Only set these to false when using the default SDK with no additional SDKs
1197+
// to avoid including .resx and other files that are typically not expected in simple file-based apps.
1198+
// When other SDKs are used (e.g., Microsoft.NET.Sdk.Web), keep the default behavior.
1199+
bool usingOnlyDefaultSdk = firstSdkName == defaultSdkName && sdkDirectives.Count() <= 1;
1200+
if (usingOnlyDefaultSdk)
1201+
{
1202+
writer.WriteLine($"""
1203+
<EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
1204+
<EnableDefaultNoneItems>false</EnableDefaultNoneItems>
1205+
""");
1206+
}
1207+
11951208
// Write default properties before importing SDKs so they can be overridden by SDKs
11961209
// (and implicit build files which are imported by the default .NET SDK).
11971210
foreach (var (name, value) in DefaultProperties)
@@ -1400,9 +1413,9 @@ public static void WriteProjectFile(
14001413

14011414
if (!sdkDirectives.Any())
14021415
{
1403-
Debug.Assert(firstSdkName == "Microsoft.NET.Sdk" && firstSdkVersion == null);
1404-
writer.WriteLine("""
1405-
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
1416+
Debug.Assert(firstSdkName == defaultSdkName && firstSdkVersion == null);
1417+
writer.WriteLine($"""
1418+
<Import Project="Sdk.targets" Sdk="{defaultSdkName}" />
14061419
""");
14071420
}
14081421

test/dotnet.Tests/CommandTests/Project/Convert/DotnetProjectConvertTests.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,14 @@ public void NestedDirectory()
395395
}
396396

397397
/// <summary>
398-
/// Default items like <c>None</c> or <c>Content</c> are copied over.
398+
/// Default items like <c>None</c> or <c>Content</c> are copied over if non-default SDK is used.
399399
/// </summary>
400400
[Fact]
401-
public void DefaultItems()
401+
public void DefaultItems_NonDefaultSdk()
402402
{
403403
var testInstance = _testAssetsManager.CreateTestDirectory();
404404
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """
405+
#:sdk Microsoft.NET.Sdk.Web
405406
Console.WriteLine();
406407
""");
407408
File.WriteAllText(Path.Join(testInstance.Path, "my.json"), "");
@@ -434,6 +435,8 @@ public void DefaultItems_MoreIncluded()
434435
var testInstance = _testAssetsManager.CreateTestDirectory();
435436
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """
436437
#:property EnableDefaultCompileItems=true
438+
#:property EnableDefaultEmbeddedResourceItems=true
439+
#:property EnableDefaultNoneItems=true
437440
Console.WriteLine();
438441
""");
439442
File.WriteAllText(Path.Join(testInstance.Path, "my.json"), "");
@@ -488,6 +491,8 @@ public void DefaultItems_ExcludedViaMetadata()
488491
{
489492
var testInstance = _testAssetsManager.CreateTestDirectory();
490493
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """
494+
#:property EnableDefaultEmbeddedResourceItems=true
495+
#:property EnableDefaultNoneItems=true
491496
Console.WriteLine();
492497
""");
493498
File.WriteAllText(Path.Join(testInstance.Path, "my.json"), "");
@@ -523,6 +528,7 @@ public void DefaultItems_ImplicitBuildFileInDirectory()
523528
{
524529
var testInstance = _testAssetsManager.CreateTestDirectory();
525530
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """
531+
#:sdk Microsoft.NET.Sdk.Web
526532
Console.WriteLine(Util.GetText());
527533
""");
528534
File.WriteAllText(Path.Join(testInstance.Path, "Util.cs"), """
@@ -678,6 +684,8 @@ public void DefaultItems_AlongsideProj([CombinatorialValues("sln", "slnx", "cspr
678684

679685
var testInstance = _testAssetsManager.CreateTestDirectory();
680686
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """
687+
#:property EnableDefaultEmbeddedResourceItems=true
688+
#:property EnableDefaultNoneItems=true
681689
Console.WriteLine();
682690
""");
683691
File.WriteAllText(Path.Join(testInstance.Path, "my.json"), "");

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

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ public void Verbosity_CompilationDiagnostics()
13641364
}
13651365

13661366
/// <summary>
1367-
/// Default projects include embedded resources by default.
1367+
/// File-based projects using the default SDK do not include embedded resources by default.
13681368
/// </summary>
13691369
[Fact]
13701370
public void EmbeddedResource()
@@ -1373,6 +1373,21 @@ public void EmbeddedResource()
13731373
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), s_programReadingEmbeddedResource);
13741374
File.WriteAllText(Path.Join(testInstance.Path, "Resources.resx"), s_resx);
13751375

1376+
// By default, with the default SDK, embedded resources are not included.
1377+
new DotnetCommand(Log, "run", "Program.cs")
1378+
.WithWorkingDirectory(testInstance.Path)
1379+
.Execute()
1380+
.Should().Pass()
1381+
.And.HaveStdOut("""
1382+
Resource not found
1383+
""");
1384+
1385+
// This behavior can be overridden to enable embedded resources.
1386+
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), $"""
1387+
#:property EnableDefaultEmbeddedResourceItems=true
1388+
{s_programReadingEmbeddedResource}
1389+
""");
1390+
13761391
new DotnetCommand(Log, "run", "Program.cs")
13771392
.WithWorkingDirectory(testInstance.Path)
13781393
.Execute()
@@ -1381,9 +1396,23 @@ public void EmbeddedResource()
13811396
[MyString, TestValue]
13821397
""");
13831398

1384-
// This behavior can be overridden.
1399+
// When using a non-default SDK, embedded resources are included by default.
13851400
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), $"""
1386-
#:property EnableDefaultEmbeddedResourceItems=false
1401+
#:sdk Microsoft.NET.Sdk.Web
1402+
{s_programReadingEmbeddedResource}
1403+
""");
1404+
1405+
new DotnetCommand(Log, "run", "Program.cs")
1406+
.WithWorkingDirectory(testInstance.Path)
1407+
.Execute()
1408+
.Should().Pass()
1409+
.And.HaveStdOut("""
1410+
[MyString, TestValue]
1411+
""");
1412+
1413+
// When using the default SDK explicitly, embedded resources are not included.
1414+
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), $"""
1415+
#:sdk Microsoft.NET.Sdk
13871416
{s_programReadingEmbeddedResource}
13881417
""");
13891418

@@ -1406,7 +1435,10 @@ public void EmbeddedResource_AlongsideProj([CombinatorialValues("sln", "slnx", "
14061435
bool considered = ext is "sln" or "slnx" or "csproj";
14071436

14081437
var testInstance = _testAssetsManager.CreateTestDirectory();
1409-
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), s_programReadingEmbeddedResource);
1438+
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), $"""
1439+
#:property EnableDefaultEmbeddedResourceItems=true
1440+
{s_programReadingEmbeddedResource}
1441+
""");
14101442
File.WriteAllText(Path.Join(testInstance.Path, "Resources.resx"), s_resx);
14111443
File.WriteAllText(Path.Join(testInstance.Path, $"repo.{ext}"), "");
14121444

@@ -3152,6 +3184,7 @@ public void UpToDate_DefaultItems(bool optOut)
31523184
var testInstance = _testAssetsManager.CreateTestDirectory();
31533185
var code = $"""
31543186
{(optOut ? "#:property FileBasedProgramCanSkipMSBuild=false" : "")}
3187+
#:property EnableDefaultEmbeddedResourceItems=true
31553188
{s_programReadingEmbeddedResource}
31563189
""";
31573190
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), code);
@@ -3172,50 +3205,20 @@ public void UpToDate_DefaultItems(bool optOut)
31723205
Build(testInstance, BuildLevel.All, ["--no-cache"], expectedOutput: "[MyString, UpdatedValue]");
31733206
}
31743207

3175-
/// <summary>
3176-
/// Combination of <see cref="UpToDate_DefaultItems"/> with <see cref="CscOnly"/> optimization.
3177-
/// </summary>
3178-
[Theory, CombinatorialData] // https://github.com/dotnet/sdk/issues/50912
3179-
public void UpToDate_DefaultItems_CscOnly(bool optOut)
3180-
{
3181-
var testInstance = _testAssetsManager.CreateTestDirectory(baseDirectory: OutOfTreeBaseDirectory);
3182-
var code = $"""
3183-
{(optOut ? "#:property FileBasedProgramCanSkipMSBuild=false" : "")}
3184-
{s_programReadingEmbeddedResource}
3185-
""";
3186-
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), code);
3187-
File.WriteAllText(Path.Join(testInstance.Path, "Resources.resx"), s_resx);
3188-
3189-
Build(testInstance, optOut ? BuildLevel.All : BuildLevel.Csc, expectedOutput: optOut ? "[MyString, TestValue]" : "Resource not found");
3190-
3191-
// Update the RESX file.
3192-
File.WriteAllText(Path.Join(testInstance.Path, "Resources.resx"), s_resx.Replace("TestValue", "UpdatedValue"));
3193-
3194-
Build(testInstance, optOut ? BuildLevel.All : BuildLevel.None, expectedOutput: optOut ? "[MyString, UpdatedValue]" : "Resource not found");
3195-
3196-
// Update the C# file.
3197-
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), "//v2\n" + code);
3198-
3199-
Build(testInstance, optOut ? BuildLevel.All : BuildLevel.Csc, expectedOutput: optOut ? "[MyString, UpdatedValue]" : "Resource not found");
3200-
3201-
Build(testInstance, BuildLevel.All, ["--no-cache"], expectedOutput: "[MyString, UpdatedValue]");
3202-
3203-
// Update the C# file.
3204-
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), "//v3\n" + code);
3205-
3206-
Build(testInstance, optOut ? BuildLevel.All : BuildLevel.Csc, expectedOutput: "[MyString, UpdatedValue]");
3207-
}
3208-
32093208
/// <summary>
32103209
/// Combination of <see cref="UpToDate_DefaultItems"/> with <see cref="CscOnly_AfterMSBuild"/> optimization.
32113210
/// </summary>
3211+
/// <remarks>
3212+
/// Note: we cannot test <see cref="CscOnly"/> because that optimization doesn't support neither <c>#:property</c> nor <c>#:sdk</c> which we need to enable default items.
3213+
/// </remarks>
32123214
[Theory, CombinatorialData] // https://github.com/dotnet/sdk/issues/50912
32133215
public void UpToDate_DefaultItems_CscOnly_AfterMSBuild(bool optOut)
32143216
{
32153217
var testInstance = _testAssetsManager.CreateTestDirectory(baseDirectory: OutOfTreeBaseDirectory);
32163218
var code = $"""
32173219
#:property Configuration=Release
32183220
{(optOut ? "#:property FileBasedProgramCanSkipMSBuild=false" : "")}
3221+
#:property EnableDefaultEmbeddedResourceItems=true
32193222
{s_programReadingEmbeddedResource}
32203223
""";
32213224
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), code);
@@ -3897,6 +3900,8 @@ public void Api_Diagnostic_01()
38973900
<FileBasedProgram>true</FileBasedProgram>
38983901
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
38993902
<DisableDefaultItemsInProjectFolder>true</DisableDefaultItemsInProjectFolder>
3903+
<EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
3904+
<EnableDefaultNoneItems>false</EnableDefaultNoneItems>
39003905
<OutputType>Exe</OutputType>
39013906
<TargetFramework>{ToolsetInfo.CurrentTargetFramework}</TargetFramework>
39023907
<ImplicitUsings>enable</ImplicitUsings>
@@ -3965,6 +3970,8 @@ public void Api_Diagnostic_02()
39653970
<FileBasedProgram>true</FileBasedProgram>
39663971
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
39673972
<DisableDefaultItemsInProjectFolder>true</DisableDefaultItemsInProjectFolder>
3973+
<EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
3974+
<EnableDefaultNoneItems>false</EnableDefaultNoneItems>
39683975
<OutputType>Exe</OutputType>
39693976
<TargetFramework>{ToolsetInfo.CurrentTargetFramework}</TargetFramework>
39703977
<ImplicitUsings>enable</ImplicitUsings>

0 commit comments

Comments
 (0)