Skip to content
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

Transitive references are not considered by Roslyns Semantic model? #3249

Open
Inspyro opened this issue Jun 2, 2015 · 6 comments
Open

Transitive references are not considered by Roslyns Semantic model? #3249

Inspyro opened this issue Jun 2, 2015 · 6 comments
Labels
Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Question
Milestone

Comments

@Inspyro
Copy link

Inspyro commented Jun 2, 2015

Assume the following project layout:
ProjectA declares a custom attribute
ProjectB references ProjectA and uses the custom attribute on ClassA
ProjectC references ProjectB and uses ClassA in ClassB

Now we want get the AttributeData (compiled version of the custom attribute) by using the semantic model of ProjectC, however we only get an ErrorType as long as ProjectC does not reference ProjectA (the one that contains the custom attribute).

Is this a Roslyn bug? Or is there a way to retrieve the AttributeData without adding a reference to ProjectA.

Here is the source code to reproduce the issue

private static void Main ()
    {
      using (var workspace = new AdhocWorkspace())
      {
        var libraryUsedByA = AddProject (workspace.CurrentSolution, "LibraryUsedByA")
            .AddDocument (
                "SomeAttribute.cs",
                "[System.AttributeUsage(System.AttributeTargets.Class)] public class SomeAttribute : System.Attribute { }").Project;

        var projectA = AddProject (libraryUsedByA.Solution, "ProjectA")
            .AddProjectReference (new ProjectReference (libraryUsedByA.Id))
            .AddDocument ("SomeClass.cs", "[SomeAttribute] public class SomeClass { }").Project;

        var someComposedClass = AddProject (projectA.Solution, "ProjectB")
            .AddProjectReference (new ProjectReference (projectA.Id))
            //.AddProjectReference(new ProjectReference(libraryUsedByA.Id)) //Why is this needed?
            .AddDocument ("SomeComposedClass.cs", "public class SomeComposedClass { SomeClass _someClass; }");

        var semanticModelOfProjectB = someComposedClass.GetSemanticModelAsync().Result;
        var someClassField = semanticModelOfProjectB.SyntaxTree.GetRoot().DescendantNodes().OfType<FieldDeclarationSyntax>().Single();

        var fieldSymbol = semanticModelOfProjectB.GetSymbolInfo (someClassField.Declaration.Type).Symbol;
        var someAttribute = fieldSymbol.GetAttributes().Single();

        Trace.Assert (someAttribute.AttributeConstructor != null);
      }
    }

    private static Project AddProject (Solution solution, string projectName)
    {
      return solution.AddProject (projectName, projectName, LanguageNames.CSharp)
          .AddMetadataReference (MetadataReference.CreateFromAssembly (typeof (string).Assembly))
          .WithCompilationOptions (
              new CSharpCompilationOptions (OutputKind.DynamicallyLinkedLibrary, assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default));
    }
@Inspyro
Copy link
Author

Inspyro commented Jun 2, 2015

The above example throws in the trace assert, as it cannot find the AttributeConstructor (it only finds an ErrorType for someAttribute)

@Pilchie Pilchie added Question Concept-API This issue involves adding, removing, clarification, or modification of an API. Area-Compilers labels Jun 2, 2015
@gafter gafter added this to the Unknown milestone Jun 2, 2015
@matkoch
Copy link

matkoch commented Jun 10, 2015

+1 I'm facing the same problem.

@fschmied
Copy link

fschmied commented Sep 4, 2015

@gafter, @Pilchie Seems like a bug to me, shouldn't this have the "Bug" label rather than the "Questions" label?

@gafter
Copy link
Member

gafter commented Sep 4, 2015

shouldn't this have the "Bug" label rather than the "Questions" label?

@fschmied That is the question. I believe the answer is "no", but it requires investigation.

@j3parker
Copy link

Any recent thoughts on this?

@CyrusNajmabadi
Copy link
Member

@j3parker It's safe to assume that if there hasn't been any further discussion or links here then nothing really had changed. IMO, this is not a bug. It's an intrinsic way the Compilation/SemanticModel apis work. Barring any major shifts in thinking here, the right way to deal with this would be to add the appropriate references to the compilation, then ask semantic model questions. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Question
Projects
None yet
Development

No branches or pull requests

7 participants