Skip to content

MTP incorrecrtly creates namespaces based on project name #7270

@agocke

Description

@agocke

Here's a simple example with SelfRegisteredExtensions:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by Microsoft.Testing.Platform.MSBuild
// </auto-generated>
//------------------------------------------------------------------------------

namespace System.Security.Cryptography.ProtectedData.Tests
{
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    internal static class SelfRegisteredExtensions
    {
        public static void AddSelfRegisteredExtensions(this global::Microsoft.Testing.Platform.Builder.ITestApplicationBuilder builder, string[] args)
        {
            global::Microsoft.Testing.Platform.MSBuild.TestingPlatformBuilderHook.AddExtensions(builder, args);
        }
    }
}

Note the namespace System.Security.Cryptography.ProtectedData.Tests -- this namespace doesn't appear anywhere in the project, it's synthesized from the project name. This is generally a bad idea. The problem is this:

  1. System.Security.Cryptography.ProtectedData.Tests references System.Security.Cryptography.ProtectedData.
  2. System.Security.Cryptography.ProtectedData doesn't define that namespace -- it defines System.Security.Cryptography
  3. S.S.C.ProtectedData contains a public type named ProtectedData.
  4. Usage of ProtectedData in S.S.C.ProtectedData.Tests now generates errors because there's an unresolvable ambiguity between the generated namespace (System.Security.Cryptography.ProtectedData) and the type (System.Security.Cryptography.ProtectedData).

Working around this requires extern aliases, which is a fairly uncommon feature and tricky to use.

My recommendation here would be to never synthesize new namespace names. If you're generating code for a particular type, you could consider putting more code inside that type's namespace. You could also use a namespace of your own, e.g. Microsoft.Testing.Platform. You could even put things in the global namespace. But synthesizing new namespaces is a recipe for tricky conflicts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: MTPBelongs to the Microsoft.Testing.Platform core library

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions