Skip to content

Fix RootNamespace handling for dashes and starting digits in project names #49328

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ Copyright (c) .NET Foundation. All rights reserved.
<FileAlignment Condition=" '$(FileAlignment)' == '' ">512</FileAlignment>
<ErrorReport Condition=" '$(ErrorReport)' == '' ">prompt</ErrorReport>
<AssemblyName Condition=" '$(AssemblyName)' == '' ">$(MSBuildProjectName)</AssemblyName>
<RootNamespace Condition=" '$(RootNamespace)' == '' ">$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
<!-- Transform project name for RootNamespace: replace spaces and dashes with underscores, prefix with underscore if starts with digit -->
<_ImplicitRootNamespace Condition=" '$(RootNamespace)' == '' ">true</_ImplicitRootNamespace>
<RootNamespace Condition=" '$(_ImplicitRootNamespace)' == 'true' ">$(MSBuildProjectName.Replace(" ", "_").Replace("-", "_"))</RootNamespace>
<_FirstChar Condition=" '$(RootNamespace)' != '' ">$(RootNamespace.Substring(0,1))</_FirstChar>
<_IsFirstCharDigit Condition=" '$(_FirstChar)' != '' AND $([System.Char]::IsDigit($(_FirstChar))) ">true</_IsFirstCharDigit>
<RootNamespace Condition=" '$(_ImplicitRootNamespace)' == 'true' AND '$(_IsFirstCharDigit)' == 'true' ">_$(RootNamespace)</RootNamespace>
<Deterministic Condition=" '$(Deterministic)' == '' ">true</Deterministic>
</PropertyGroup>

Expand Down
171 changes: 171 additions & 0 deletions test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,177 @@ string GetPropertyValue(string propertyName)
GetPropertyValue("RootNamespace").Should().Be("Project_Name_With_Spaces");
}

[Theory]
[InlineData("netcoreapp3.1")]
[InlineData("netcoreapp5.0")]
public void It_makes_RootNamespace_safe_when_project_name_has_dashes(string targetFramework)
{
var testProject = new TestProject()
{
Name = "my-project-with-dashes",
TargetFrameworks = targetFramework,
};

var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework);

// Overwrite the default file. CreateTestProject uses the defined project name for the namespace.
// We need a buildable project to extract the property to verify it
// since this issue only surfaces in VS when adding a new class through an item template.
File.WriteAllText(Path.Combine(testAsset.Path, testProject.Name, $"{testProject.Name}.cs"), @"
using System;
using System.Collections.Generic;

namespace MyProjectWithDashes
{
public class MyProjectWithDashesClass
{
public static string Name { get { return ""my-project-with-dashes""; } }
public static List<string> List { get { return null; } }
}
}");
string projectFolder = Path.Combine(testAsset.Path, testProject.Name);

var buildCommand = new BuildCommand(testAsset, $"{testProject.Name}");
buildCommand
.Execute()
.Should()
.Pass();

string GetPropertyValue(string propertyName)
Copy link
Preview

Copilot AI Jun 12, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider extracting the common GetPropertyValue logic from the three tests into a shared helper method to reduce duplication and improve maintainability.

Copilot uses AI. Check for mistakes.

{
var getValuesCommand = new GetValuesCommand(Log, projectFolder,
testProject.TargetFrameworks, propertyName, GetValuesCommand.ValueType.Property)
{
Configuration = "Debug"
};

getValuesCommand
.Execute()
.Should()
.Pass();

var values = getValuesCommand.GetValues();
values.Count.Should().Be(1);
return values[0];
}

GetPropertyValue("RootNamespace").Should().Be("my_project_with_dashes");
}

[Theory]
[InlineData("netcoreapp3.1")]
[InlineData("netcoreapp5.0")]
public void It_makes_RootNamespace_safe_when_project_name_starts_with_digit(string targetFramework)
{
var testProject = new TestProject()
{
Name = "13monkeys",
TargetFrameworks = targetFramework,
};

var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework);

// Overwrite the default file. CreateTestProject uses the defined project name for the namespace.
// We need a buildable project to extract the property to verify it
// since this issue only surfaces in VS when adding a new class through an item template.
File.WriteAllText(Path.Combine(testAsset.Path, testProject.Name, $"{testProject.Name}.cs"), @"
using System;
using System.Collections.Generic;

namespace _13monkeys
{
public class _13monkeysClass
{
public static string Name { get { return ""13monkeys""; } }
public static List<string> List { get { return null; } }
}
}");
string projectFolder = Path.Combine(testAsset.Path, testProject.Name);

var buildCommand = new BuildCommand(testAsset, $"{testProject.Name}");
buildCommand
.Execute()
.Should()
.Pass();

string GetPropertyValue(string propertyName)
{
var getValuesCommand = new GetValuesCommand(Log, projectFolder,
testProject.TargetFrameworks, propertyName, GetValuesCommand.ValueType.Property)
{
Configuration = "Debug"
};

getValuesCommand
.Execute()
.Should()
.Pass();

var values = getValuesCommand.GetValues();
values.Count.Should().Be(1);
return values[0];
}

GetPropertyValue("RootNamespace").Should().Be("_13monkeys");
}

[Theory]
[InlineData("netcoreapp3.1")]
[InlineData("netcoreapp5.0")]
public void It_makes_RootNamespace_safe_when_project_name_has_dashes_and_starts_with_digit(string targetFramework)
{
var testProject = new TestProject()
{
Name = "13-monkeys-project",
TargetFrameworks = targetFramework,
};

var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework);

// Overwrite the default file. CreateTestProject uses the defined project name for the namespace.
// We need a buildable project to extract the property to verify it
// since this issue only surfaces in VS when adding a new class through an item template.
File.WriteAllText(Path.Combine(testAsset.Path, testProject.Name, $"{testProject.Name}.cs"), @"
using System;
using System.Collections.Generic;

namespace _13_monkeys_project
{
public class _13_monkeys_projectClass
{
public static string Name { get { return ""13-monkeys-project""; } }
public static List<string> List { get { return null; } }
}
}");
string projectFolder = Path.Combine(testAsset.Path, testProject.Name);

var buildCommand = new BuildCommand(testAsset, $"{testProject.Name}");
buildCommand
.Execute()
.Should()
.Pass();

string GetPropertyValue(string propertyName)
{
var getValuesCommand = new GetValuesCommand(Log, projectFolder,
testProject.TargetFrameworks, propertyName, GetValuesCommand.ValueType.Property)
{
Configuration = "Debug"
};

getValuesCommand
.Execute()
.Should()
.Pass();

var values = getValuesCommand.GetValues();
values.Count.Should().Be(1);
return values[0];
}

GetPropertyValue("RootNamespace").Should().Be("_13_monkeys_project");
}

[WindowsOnlyFact(Skip = "We need new SDK packages with different assembly versions to build this (.38 and .39 have the same assembly version)")]
public void It_errors_on_windows_sdk_assembly_version_conflicts()
{
Expand Down
Loading