Skip to content

Commit 217fe6e

Browse files
authored
Revert "Fix RootNamespace handling for dashes and starting digits in … (#50502)
2 parents 4e31699 + 9f76bf9 commit 217fe6e

File tree

2 files changed

+16
-138
lines changed

2 files changed

+16
-138
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.props

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,10 @@ Copyright (c) .NET Foundation. All rights reserved.
3939
<FileAlignment Condition=" '$(FileAlignment)' == '' ">512</FileAlignment>
4040
<ErrorReport Condition=" '$(ErrorReport)' == '' ">prompt</ErrorReport>
4141
<AssemblyName Condition=" '$(AssemblyName)' == '' ">$(MSBuildProjectName)</AssemblyName>
42+
<RootNamespace Condition=" '$(RootNamespace)' == '' ">$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
4243
<Deterministic Condition=" '$(Deterministic)' == '' ">true</Deterministic>
4344
</PropertyGroup>
4445

45-
<PropertyGroup Condition=" '$(RootNamespace)' == '' ">
46-
<!-- Transform project name for RootNamespace: replace spaces and dashes with underscores, prefix with underscore if starts with digit -->
47-
<RootNamespace>$(MSBuildProjectName.Replace(" ", "_").Replace("-", "_"))</RootNamespace>
48-
<RootNamespace Condition=" $([System.Char]::IsDigit($(RootNamespace), 0)) ">_$(RootNamespace)</RootNamespace>
49-
</PropertyGroup>
50-
5146
<!-- User-facing configuration-specific defaults -->
5247
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
5348
<DebugSymbols Condition=" '$(DebugSymbols)' == '' ">true</DebugSymbols>

test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs

Lines changed: 15 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,6 @@ internal static List<string> GetValuesFromTestLibrary(
105105
return itemValues;
106106
}
107107

108-
private string GetPropertyValue(string propertyName, string projectFolder, string targetFramework)
109-
{
110-
var getValuesCommand = new GetValuesCommand(Log, projectFolder,
111-
targetFramework, propertyName, GetValuesCommand.ValueType.Property)
112-
{
113-
Configuration = "Debug"
114-
};
115-
116-
getValuesCommand
117-
.Execute()
118-
.Should()
119-
.Pass();
120-
121-
var values = getValuesCommand.GetValues();
122-
values.Count.Should().Be(1);
123-
return values[0];
124-
}
125-
126108
private TestAsset CreateDocumentationFileLibraryAsset(bool? generateDocumentationFile, string documentationFile, string language, [CallerMemberName] string callingMethod = "")
127109
{
128110
string genDocFileIdentifier = generateDocumentationFile == null ? "null" : generateDocumentationFile.Value.ToString();
@@ -1068,124 +1050,25 @@ public class ProjectNameWithSpacesClass
10681050
.Should()
10691051
.Pass();
10701052

1071-
GetPropertyValue("RootNamespace", projectFolder, testProject.TargetFrameworks).Should().Be("Project_Name_With_Spaces");
1072-
}
1073-
1074-
[Theory]
1075-
[InlineData("netcoreapp3.1")]
1076-
[InlineData("netcoreapp5.0")]
1077-
public void It_makes_RootNamespace_safe_when_project_name_has_dashes(string targetFramework)
1078-
{
1079-
var testProject = new TestProject()
1080-
{
1081-
Name = "my-project-with-dashes",
1082-
TargetFrameworks = targetFramework,
1083-
};
1084-
1085-
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework);
1086-
1087-
// Overwrite the default file. CreateTestProject uses the defined project name for the namespace.
1088-
// We need a buildable project to extract the property to verify it
1089-
// since this issue only surfaces in VS when adding a new class through an item template.
1090-
File.WriteAllText(Path.Combine(testAsset.Path, testProject.Name, $"{testProject.Name}.cs"), @"
1091-
using System;
1092-
using System.Collections.Generic;
1093-
1094-
namespace MyProjectWithDashes
1095-
{
1096-
public class MyProjectWithDashesClass
1097-
{
1098-
public static string Name { get { return ""my-project-with-dashes""; } }
1099-
public static List<string> List { get { return null; } }
1100-
}
1101-
}");
1102-
string projectFolder = Path.Combine(testAsset.Path, testProject.Name);
1103-
1104-
var buildCommand = new BuildCommand(testAsset, $"{testProject.Name}");
1105-
buildCommand
1106-
.Execute()
1107-
.Should()
1108-
.Pass();
1109-
1110-
GetPropertyValue("RootNamespace", projectFolder, testProject.TargetFrameworks).Should().Be("my_project_with_dashes");
1111-
}
1112-
1113-
[Theory]
1114-
[InlineData("netcoreapp3.1")]
1115-
[InlineData("netcoreapp5.0")]
1116-
public void It_makes_RootNamespace_safe_when_project_name_starts_with_digit(string targetFramework)
1117-
{
1118-
var testProject = new TestProject()
1053+
string GetPropertyValue(string propertyName)
11191054
{
1120-
Name = "13monkeys",
1121-
TargetFrameworks = targetFramework,
1122-
};
1123-
1124-
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework);
1125-
1126-
// Overwrite the default file. CreateTestProject uses the defined project name for the namespace.
1127-
// We need a buildable project to extract the property to verify it
1128-
// since this issue only surfaces in VS when adding a new class through an item template.
1129-
File.WriteAllText(Path.Combine(testAsset.Path, testProject.Name, $"{testProject.Name}.cs"), @"
1130-
using System;
1131-
using System.Collections.Generic;
1132-
1133-
namespace _13monkeys
1134-
{
1135-
public class _13monkeysClass
1136-
{
1137-
public static string Name { get { return ""13monkeys""; } }
1138-
public static List<string> List { get { return null; } }
1139-
}
1140-
}");
1141-
string projectFolder = Path.Combine(testAsset.Path, testProject.Name);
1142-
1143-
var buildCommand = new BuildCommand(testAsset, $"{testProject.Name}");
1144-
buildCommand
1145-
.Execute()
1146-
.Should()
1147-
.Pass();
1148-
1149-
GetPropertyValue("RootNamespace", projectFolder, testProject.TargetFrameworks).Should().Be("_13monkeys");
1150-
}
1151-
1152-
[Theory]
1153-
[InlineData("netcoreapp3.1")]
1154-
[InlineData("netcoreapp5.0")]
1155-
public void It_makes_RootNamespace_safe_when_project_name_has_dashes_and_starts_with_digit(string targetFramework)
1156-
{
1157-
var testProject = new TestProject()
1158-
{
1159-
Name = "13-monkeys-project",
1160-
TargetFrameworks = targetFramework,
1161-
};
1162-
1163-
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework);
1164-
1165-
// Overwrite the default file. CreateTestProject uses the defined project name for the namespace.
1166-
// We need a buildable project to extract the property to verify it
1167-
// since this issue only surfaces in VS when adding a new class through an item template.
1168-
File.WriteAllText(Path.Combine(testAsset.Path, testProject.Name, $"{testProject.Name}.cs"), @"
1169-
using System;
1170-
using System.Collections.Generic;
1055+
var getValuesCommand = new GetValuesCommand(Log, projectFolder,
1056+
testProject.TargetFrameworks, propertyName, GetValuesCommand.ValueType.Property)
1057+
{
1058+
Configuration = "Debug"
1059+
};
11711060

1172-
namespace _13_monkeys_project
1173-
{
1174-
public class _13_monkeys_projectClass
1175-
{
1176-
public static string Name { get { return ""13-monkeys-project""; } }
1177-
public static List<string> List { get { return null; } }
1178-
}
1179-
}");
1180-
string projectFolder = Path.Combine(testAsset.Path, testProject.Name);
1061+
getValuesCommand
1062+
.Execute()
1063+
.Should()
1064+
.Pass();
11811065

1182-
var buildCommand = new BuildCommand(testAsset, $"{testProject.Name}");
1183-
buildCommand
1184-
.Execute()
1185-
.Should()
1186-
.Pass();
1066+
var values = getValuesCommand.GetValues();
1067+
values.Count.Should().Be(1);
1068+
return values[0];
1069+
}
11871070

1188-
GetPropertyValue("RootNamespace", projectFolder, testProject.TargetFrameworks).Should().Be("_13_monkeys_project");
1071+
GetPropertyValue("RootNamespace").Should().Be("Project_Name_With_Spaces");
11891072
}
11901073

11911074
[WindowsOnlyFact(Skip = "We need new SDK packages with different assembly versions to build this (.38 and .39 have the same assembly version)")]

0 commit comments

Comments
 (0)