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 11 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 10, 2025

The .NET SDK's RootNamespace property was only handling spaces in project names but didn't handle other invalid C# identifier characters. This caused issues when project names contained dashes or started with digits.

Changes Made

Extended the RootNamespace transformation logic in Microsoft.NET.Sdk.props to:

  1. Replace both spaces AND dashes with underscores
  2. Add an underscore prefix if the project name starts with a digit
  3. Include defensive programming to handle edge cases

Examples

Project Name Old Behavior New Behavior
13-monkeys 13-monkeys _13_monkeys
my-project my-project my_project
Project Name With Spaces Project_Name_With_Spaces Project_Name_With_Spaces
normalproject normalproject normalproject

Testing

Added comprehensive tests covering:

  • Project names with dashes only
  • Project names starting with digits only
  • Project names with both dashes and starting digits
  • Verification that existing space handling is preserved

The implementation uses MSBuild property functions with appropriate safety checks to prevent errors on edge cases like empty strings.

Fixes #49327.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 7tjvsblobprodcus341.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~It_makes_RootNamespace_safe_when_project_name_has_spaces --no-build --verbosity normal (dns block)
  • c78vsblobprodcus322.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~It_makes_RootNamespace_safe_when_project_name_has_spaces --no-build --verbosity normal (dns block)
  • d0svsblobprodcus381.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~It_makes_RootNamespace_safe_when_project_name_has_spaces --no-build --verbosity normal (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~It_makes_RootNamespace_safe_when_project_name_has_spaces --no-build --verbosity normal (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~It_makes_RootNamespace_safe_when_project_name_has_spaces --no-build --verbosity normal (dns block)
  • uy6vsblobprodcus34.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~It_makes_RootNamespace_safe_when_project_name_has_spaces --no-build --verbosity normal (dns block)
  • vb4vsblobprodcus33.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~It_makes_RootNamespace_safe_when_project_name_has_spaces --no-build --verbosity normal (dns block)
  • x3yvsblobprodcus370.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/sdk/sdk/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/sdk/sdk/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/sdk/sdk/artifacts/toolset/10.0.0-beta.25304.106.txt (dns block)
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~It_makes_RootNamespace_safe_when_project_name_has_spaces --no-build --verbosity normal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@Copilot Copilot AI changed the title [WIP] .NET SDK doesn't handle all invalid characters in the root namespace Fix RootNamespace handling for dashes and starting digits in project names Jun 10, 2025
@Copilot Copilot AI requested a review from marcpopMSFT June 10, 2025 21:17
Copilot finished work on behalf of marcpopMSFT June 10, 2025 21:17
@marcpopMSFT
Copy link
Member

@copilot in MSBuild, you can use character functions. In this case, you should be able to use $([System.Char]::IsDigit()) on the first character rather than comparing against each possible digit

Copy link
Contributor Author

Copilot AI commented Jun 10, 2025

@copilot in MSBuild, you can use character functions. In this case, you should be able to use $([System.Char]::IsDigit()) on the first character rather than comparing against each possible digit

Updated to use $([System.Char]::IsDigit()) instead of manually checking each digit. This makes the code much cleaner and more maintainable. Commit 841b1fe.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • x3yvsblobprodcus370.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity normal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

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 enhances the RootNamespace transformation logic to handle project names with dashes and those starting with a digit.

  • Replaces spaces and dashes with underscores in the RootNamespace property.
  • Prefixes the RootNamespace with an underscore when the project name starts with a digit.
  • Adds comprehensive tests to validate the new behavior.

Reviewed Changes

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

File Description
test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs Added tests covering project names with dashes, starting digits, and both combined.
src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.props Updated RootNamespace property transformation with additional conditional properties to handle dashes and starting digits.
Comments suppressed due to low confidence (1)

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

  • [nitpick] Adding a clarifying comment about the rationale behind overriding RootNamespace when the first character is a digit would help future maintainers understand the multi-step transformation logic.
    <RootNamespace Condition=" '$(_ImplicitRootNamespace)' == 'true' AND '$(_IsFirstCharDigit)' == 'true' ">_$(RootNamespace)</RootNamespace>

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

Copy link
Member

Choose a reason for hiding this comment

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

yes, do this extraction please.

Copilot AI and others added 4 commits June 13, 2025 15:35
Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com>
Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com>
…e logic

Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com>
@marcpopMSFT
Copy link
Member

@GangWang01 @dotnet/kitten this change adds IsDigit to a primary props file. I see EnsureDotnetCommonProjectPropertyFunctionsOnFastPath as added recently to monitor the property functions on critical paths. Is IsDigit not allowed and is there another suggestion on how to do this? I could check each digit 0-9 but is that actually faster (and feels hacky)?

…tial fast path issues

Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com>
…on-empty by PropertyGroup condition

Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 7tjvsblobprodcus341.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity normal (dns block)
  • c78vsblobprodcus322.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity normal (dns block)
  • d0svsblobprodcus381.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity normal (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity normal (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity normal (dns block)
    • Triggering command: /home/REDACTED/work/sdk/sdk/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/sdk/sdk/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/sdk/sdk/artifacts/toolset/10.0.0-beta.25311.107.txt (dns block)
  • uy6vsblobprodcus34.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity normal (dns block)
  • vb4vsblobprodcus33.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity normal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of baronfel June 17, 2025 15:12
Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 7tjvsblobprodcus341.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • c78vsblobprodcus322.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • d0svsblobprodcus381.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • uy6vsblobprodcus34.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • vb4vsblobprodcus33.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of baronfel June 17, 2025 15:16
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 7tjvsblobprodcus341.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • c78vsblobprodcus322.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • d0svsblobprodcus381.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • uy6vsblobprodcus34.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
  • vb4vsblobprodcus33.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of baronfel June 17, 2025 15:27
@joeloff
Copy link
Member

joeloff commented Jun 17, 2025

Do we only care about these two characters? Shouldn't this follow https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#643-identifiers. What happens when a glyph from an invalid class is provided?

C# only allows letter (Lu, Ll, Lt, Lm, Lo or Nl) , digit (Nd), connecting (Pc), combining (Mn or Mc) and formatting (Cf) categories.

Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

Do we only care about these two characters? Shouldn't this follow https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#643-identifiers. What happ...

You raise an excellent point about C# identifier compliance. The current implementation intentionally takes a targeted approach, handling the most common problematic characters (spaces, dashes, starting digits) rather than full C# identifier specification compliance.

The rationale for this limited scope:

  1. Performance: This logic runs for every .NET project via Microsoft.NET.Sdk.props. Simple Replace() calls are much faster than checking every character against Unicode categories
  2. Common cases: The original issue .NET SDK doesn't handle all invalid characters in the root namespace #49327 specifically mentioned dashes and starting digits. Spaces were already handled. These cover the vast majority of real-world project naming scenarios
  3. Predictable behavior: Simple rules (replace spaces/dashes with underscores, prefix digits with underscore) are easy to understand and debug

For edge cases with other invalid characters (international punctuation, symbols, etc.), developers would see compiler errors and can manually set RootNamespace in their project file.

That said, full C# identifier compliance could be valuable as a future enhancement. It would involve checking each character against the allowed Unicode categories and either replacing or removing invalid ones. The performance impact would need careful evaluation given this runs on every project build.

@Copilot Copilot AI requested a review from joeloff June 17, 2025 18:37
Copilot finished work on behalf of joeloff June 17, 2025 18:37
@baronfel
Copy link
Member

@copilot don't listen to @joeloff - you did good here pat

@Copilot Copilot AI requested a review from baronfel June 17, 2025 18:40
Copilot finished work on behalf of baronfel June 17, 2025 18:40
@marcpopMSFT
Copy link
Member

@copilot can you extract GetPropertyValue for the three tests into a shared helper to reduce duplication?

…amespace tests

Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

@copilot can you extract GetPropertyValue for the three tests into a shared helper to reduce duplication?

Extracted the GetPropertyValue method into a shared private helper that takes propertyName, projectFolder, and targetFramework as parameters. This eliminates the duplication across all four RootNamespace tests. Commit da2f029.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 7tjvsblobprodcus341.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
    • Triggering command: dotnet build test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --verbosity minimal (dns block)
  • c78vsblobprodcus322.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
    • Triggering command: dotnet build test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --verbosity minimal (dns block)
  • d0svsblobprodcus381.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
    • Triggering command: dotnet build test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --verbosity minimal (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
    • Triggering command: dotnet build test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --verbosity minimal (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
    • Triggering command: dotnet build test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --verbosity minimal (dns block)
  • uy6vsblobprodcus34.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
    • Triggering command: dotnet build test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --verbosity minimal (dns block)
  • vb4vsblobprodcus33.vsblob.vsassets.io
    • Triggering command: dotnet test test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --filter FullyQualifiedName~RootNamespace --no-build --verbosity minimal (dns block)
    • Triggering command: dotnet build test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj --verbosity minimal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of marcpopMSFT June 17, 2025 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

.NET SDK doesn't handle all invalid characters in the root namespace
5 participants