dotnet / project-system

The .NET Project System for Visual Studio
MIT License
970 stars 389 forks source link

ProjectReference to a Project with TargetType=module crashes VS #5380

Open vatsan-madhavan opened 5 years ago

vatsan-madhavan commented 5 years ago

Observed: VS will crash.

$exception
{"Can't create a reference to a module."}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233067
    HelpLink: null
    IPForWatsonBuckets: 0x2a586a41
    InnerException: null
    IsTransient: false
    Message: "Can't create a reference to a module."
    RemoteStackTrace: null
    Source: "Microsoft.CodeAnalysis"
    StackTrace: "   at Microsoft.CodeAnalysis.CompilationReference.GetProperties(Compilation compilation, ImmutableArray`1 aliases, Boolean embedInteropTypes)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.ToMetadataReference(ImmutableArray`1 aliases, Boolean embedInteropTypes)\r\n   at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.<GetMetadataReferenceAsync>d__32.MoveNext() in /_/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.CompilationTracker.cs:line 707"
    TargetSite: {Microsoft.CodeAnalysis.MetadataReferenceProperties GetProperties(Microsoft.CodeAnalysis.Compilation, System.Collections.Immutable.ImmutableArray`1[System.String], Boolean)}
    WatsonBuckets: null
    _HResult: -2146233067
    _className: "System.NotSupportedException"
    _data: {System.Collections.ListDictionaryInternal}
    _dynamicMethods: null
    _exceptionMethod: null
    _exceptionMethodString: null
    _helpURL: null
    _innerException: null
    _ipForWatsonBuckets: 0x2a586a41
    _message: "Can't create a reference to a module."
    _remoteStackIndex: 0
    _remoteStackTraceString: null
    _safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager}
    _source: null
    _stackTrace: {sbyte[96]}
    _stackTraceString: null
    _watsonBuckets: null
    _xcode: -532462766
    _xptrs: 0x00000000
"System.NotSupportedException: Can't create a reference to a module
at Microsoft.CodeAnalysis.CompilationReference.GetProperties(Compilation compilation, ImmutableArray`1 aliases, Boolean embedInteropTypes)
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.ToMetadataReference(ImmutableArray`1 aliases, Boolean embedInteropTypes)
at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.<GetMetadataReferenceAsync>d__32.MoveNext() in /_/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.CompilationTracker.cs:line 707"

This is what I'm seeing in CompilationReference.cs:

        internal static MetadataReferenceProperties GetProperties(Compilation compilation, ImmutableArray<string> aliases, bool embedInteropTypes)
        {
            if (compilation == null)
            {
                throw new ArgumentNullException(nameof(compilation));
            }

            if (compilation.IsSubmission)
            {
                throw new NotSupportedException(CodeAnalysisResources.CannotCreateReferenceToSubmission);
            }

            if (compilation.Options.OutputKind == OutputKind.NetModule)
            {
                throw new NotSupportedException(CodeAnalysisResources.CannotCreateReferenceToModule);
            }

            return new MetadataReferenceProperties(MetadataImageKind.Assembly, aliases, embedInteropTypes);
        }

dotnet/wpf repo is planning on using netmodules it as part of product-binaries, and this would imply that the solution in the repo can not be used/built in VS. It would be great if netmodule-references can be supported in Dev16.4.

module-crash-repro.zip

/cc @rladuca /cc @tgani-msft, @dotnet/wpf-developers

tgani-msft commented 5 years ago

who're working on the project system support for C++/CLI for .NET Core.

vatsan-madhavan commented 5 years ago

/cc @olgaark, @nguerrera

drewnoakes commented 5 years ago

Is this a project system issue? The code in question is part of Roslyn:

https://github.com/dotnet/roslyn/blob/38559118951cfc4599711141b9bf3bb7fd7b2dc6/src/Compilers/Core/Portable/MetadataReference/CompilationReference.cs#L25-L43

davkean commented 5 years ago

What's the expectation here? Clearly we should do better than a crash - but a reference to a module isn't valid.

vatsan-madhavan commented 5 years ago

What's the expectation here? Clearly we should do better than a crash - but a reference to a module isn't valid.

The attached repro project contains a ProjectReference to a project that produces a module, but it doesn't reference module. i.e., there is no (illegal) reference to the module.

Maybe the issue is that VS is is unable to see the the distinction that a ProjectReferencedoesn't always imply that there will be a reference to the projects's outputs?

davkean commented 5 years ago

Note that reference is marked as:

    <ProjectReference Include="..\module\module.csproj">
       <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
       <OutputItemType>AddModules</OutputItemType>
    </ProjectReference>

Which should mean that it isn't passed as reference, so we have a couple of bugs:

1) We shouldn't crash if someone doesn't specify ReferenceOutputAssembly 2) We shouldn't be passing this module to Roslyn when ReferenceOutputAssembly is specified.

Probably a combination of Roslyn, Project and MSBuild bugs.