Skip to content

Compilation.GetTypeByMetadataName returns null even though the other ObsoleteAttribute is internal #52037

@jnm2

Description

@jnm2

Version Used: Visual Studio 16.9.2 (hosting analyzers), 3.9.0 NuGet packages (repro below)

Roslyn should be able to see that the internal System.ObsoleteAttribute declared by System.DirectoryServices is not accessible to the compilation. (System.DirectoryServices does not declare its internals visible to my project.) GetTypeByMetadataName should return the only public one.

The compilation succeeds, so there is a well-known way to resolve this conflict if it's even a conflict.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.IO;

var compilation = CSharpCompilation.Create(
    assemblyName: null,
    syntaxTrees: new[] { CSharpSyntaxTree.ParseText("[System.Obsolete] class C { }") },
    references: new[]
    {
        // These both get included in projects that target net5.0-windows
        MetadataReference.CreateFromFile(@"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0\ref\net5.0\System.Runtime.dll"),
        MetadataReference.CreateFromFile(@"C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\5.0.0\ref\net5.0\System.DirectoryServices.dll"),
    },
    new(OutputKind.DynamicallyLinkedLibrary));

Console.WriteLine("Compilation success: " + compilation.Emit(Stream.Null).Success);

var type = compilation.GetTypeByMetadataName(typeof(ObsoleteAttribute).FullName!);
Console.WriteLine("GetTypeByMetadataName returns null: " + (type is null));

Prints:

Compilation success: True
GetTypeByMetadataName returns null: True

In case this is "by design"

This is a pit of failure for people writing our own analyzers and source generators. For example, my analyzer stopped ignoring obsolete members as soon as it happened to be used in a net5.0-windows project. If there is some reason that this behavior can't be fixed, analyzer writers should get a warning if we try to use it. I think leaving things in the current pitfall-inducing state should not be seen as acceptable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-CompilersuntriagedIssues and PRs which have not yet been triaged by a lead

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions