public override ImmutableArray <PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
        {
            if (reference.StartsWith("nuget:", StringComparison.OrdinalIgnoreCase))
            {
                // HACK We need to return something here to "mark" the reference as resolved.
                // https://github.com/dotnet/roslyn/blob/master/src/Compilers/Core/Portable/ReferenceManager/CommonReferenceManager.Resolution.cs#L838
                return(ImmutableArray <PortableExecutableReference> .Empty.Add(
                           MetadataReference.CreateFromFile(typeof(NuGetMetadataReferenceResolver).GetTypeInfo().Assembly.Location)));
            }

            if (_cache == null)
            {
                return(_innerReferenceResolver.ResolveReference(reference, baseFilePath, properties));
            }

            if (_cache.TryGetValue(reference, out var result))
            {
                return(result);
            }

            result = _innerReferenceResolver.ResolveReference(reference, baseFilePath, properties);
            if (!result.IsDefaultOrEmpty)
            {
                _cache.TryAdd(reference, result);
            }

            return(result);
        }
Exemple #2
0
        public override ImmutableArray <PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
        {
            if (reference == null)
            {
                return(ImmutableArray <PortableExecutableReference> .Empty);
            }

            if (!HasValidAssemblyFileExtension(reference))
            {
                // Try to resolve using the default resolver (framework assemblies, e.g. System.Core, System.Xml, etc.)
                ImmutableArray <PortableExecutableReference> result = _scriptResolver.ResolveReference(reference, baseFilePath, properties);

                // If the default script resolver can't resolve the assembly
                // check if this is one of host's shared assemblies
                if (result.IsEmpty)
                {
                    Assembly assembly = null;

                    if (SharedAssemblyProviders.Any(p => p.TryResolveAssembly(reference, AssemblyLoadContext.Default, _logger, out assembly)) ||
                        _extensionSharedAssemblyProvider.TryResolveAssembly(reference, AssemblyLoadContext.Default, _logger, out assembly))
                    {
                        result = ImmutableArray.Create(MetadataReference.CreateFromFile(assembly.Location));
                    }
                }

                return(result);
            }

            return(GetMetadataFromReferencePath(reference));
        }
Exemple #3
0
        protected IEnumerable <MetadataReference> ResolveReferences(MetadataReferenceResolver metadataReferenceResolver)
        {
            List <MetadataReference> references = new List <MetadataReference>
            {
                MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location)
            };

            foreach (MetadataReference reference in MetadataReferences)
            {
                UnresolvedMetadataReference unresolved = reference as UnresolvedMetadataReference;

                if (unresolved != null)
                {
                    ImmutableArray <PortableExecutableReference> resolved =
                        metadataReferenceResolver.ResolveReference(unresolved.Reference, null, unresolved.Properties);

                    references.AddRange(resolved);
                }
                else
                {
                    references.Add(reference);
                }
            }

            return(references);
        }
            AssemblySymbol CreateAssemblyFromIdentity(MetadataReferenceResolver resolver, AssemblyIdentity identity, string basePath, List <PEModuleSymbol> modules)
            {
                if (!_observedMetadata.TryGetValue(identity, out var ass))
                {
                    // temporary: lookup ignoring version number
                    foreach (var pair in _observedMetadata)
                    {
                        if (IsIdentitySimilar(pair.Key, identity))
                        {
                            _observedMetadata[identity] = pair.Value;   // do not resolve this ever again
                            return(pair.Value);
                        }
                    }

                    //foreach (var m in modules)
                    //{
                    //    if (IsIdentitySimilar(m.ContainingAssembly.Identity, identity))
                    //    {
                    //        return m.ContainingAssembly as PEAssemblySymbol;
                    //    }
                    //}

                    //
                    if (resolver != null)
                    {
                        string keytoken = string.Join("", identity.PublicKeyToken.Select(b => b.ToString("x2")));
                        var    pes      = resolver.ResolveReference(identity.Name + ".dll", basePath, MetadataReferenceProperties.Assembly)
                                          .Concat(resolver.ResolveReference($"{identity.Name}/v4.0_{identity.Version}__{keytoken}/{identity.Name}.dll", basePath, MetadataReferenceProperties.Assembly));

                        var pe = pes.FirstOrDefault();
                        if (pe != null)
                        {
                            _observedMetadata[identity] = ass = PEAssemblySymbol.Create(pe, isLinked: false);
                            ass.SetCorLibrary(_lazyCorLibrary);
                            modules.AddRange(ass.Modules.Cast <PEModuleSymbol>());
                        }
                    }

                    if (ass == null)
                    {
                        //_diagnostics.Add(Location.None, Errors.ErrorCode.ERR_MetadataFileNotFound, identity);
                        return(new MissingAssemblySymbol(identity));
                    }
                }

                return(ass);
            }
            AssemblySymbol CreateAssemblyFromIdentity(MetadataReferenceResolver resolver, AssemblyIdentity identity, string basePath, List <PEModuleSymbol> modules)
            {
                PEAssemblySymbol ass;

                if (!_observedMetadata.TryGetValue(identity, out ass))
                {
                    // temporary: lookup ignoring minor version number
                    foreach (var pair in _observedMetadata)
                    {
                        // TODO: _identityComparer
                        if (pair.Key.Name.Equals(identity.Name, StringComparison.OrdinalIgnoreCase) &&
                            (pair.Key.Version.Major == identity.Version.Major || identity.Version == new Version(0, 0, 0, 0)))
                        {
                            _observedMetadata[identity] = pair.Value;
                            return(pair.Value);
                        }
                    }

                    //
                    if (resolver != null)
                    {
                        string keytoken = string.Join("", identity.PublicKeyToken.Select(b => b.ToString("x2")));
                        var    pes      = resolver.ResolveReference(identity.Name + ".dll", basePath, MetadataReferenceProperties.Assembly)
                                          .Concat(resolver.ResolveReference($"{identity.Name}/v4.0_{identity.Version}__{keytoken}/{identity.Name}.dll", basePath, MetadataReferenceProperties.Assembly));

                        var pe = pes.FirstOrDefault();
                        if (pe != null)
                        {
                            _observedMetadata[identity] = ass = PEAssemblySymbol.Create(pe, isLinked: false);
                            ass.SetCorLibrary(_lazyCorLibrary);
                            modules.AddRange(ass.Modules.Cast <PEModuleSymbol>());
                        }
                    }

                    if (ass == null)
                    {
                        //_diagnostics.Add(Location.None, Errors.ErrorCode.ERR_MetadataFileNotFound, identity);
                        return(new MissingAssemblySymbol(identity));
                    }
                }

                return(ass);
            }
Exemple #6
0
        public override ImmutableArray <PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
        {
            if (reference.StartsWith("nuget", StringComparison.OrdinalIgnoreCase))
            {
                // HACK We need to return something here to "mark" the reference as resolved.
                // https://github.com/dotnet/roslyn/blob/master/src/Compilers/Core/Portable/ReferenceManager/CommonReferenceManager.Resolution.cs#L838
                return(ImmutableArray <PortableExecutableReference> .Empty.Add(
                           MetadataReference.CreateFromFile(typeof(string).GetTypeInfo().Assembly.Location)));
            }
            var resolvedReference = metadataReferenceResolver.ResolveReference(reference, baseFilePath, properties);

            return(resolvedReference);
        }
Exemple #7
0
            PEAssemblySymbol CreateAssemblyFromIdentity(MetadataReferenceResolver resolver, AssemblyIdentity identity, string basePath, List <PEModuleSymbol> modules)
            {
                PEAssemblySymbol ass;

                if (!_observedMetadata.TryGetValue(identity, out ass))
                {
                    // temporary: lookup ignoring minor version number
                    foreach (var pair in _observedMetadata)
                    {
                        // TODO: _identityComparer
                        if (pair.Key.Name.Equals(identity.Name, StringComparison.OrdinalIgnoreCase) && pair.Key.Version.Major == identity.Version.Major)
                        {
                            _observedMetadata[identity] = pair.Value;
                            return(pair.Value);
                        }
                    }

                    //
                    string keytoken = string.Join("", identity.PublicKeyToken.Select(b => b.ToString("x2")));
                    var    pes      = resolver.ResolveReference(identity.Name + ".dll", basePath, MetadataReferenceProperties.Assembly)
                                      .Concat(resolver.ResolveReference($"{identity.Name}/v4.0_{identity.Version}__{keytoken}/{identity.Name}.dll", basePath, MetadataReferenceProperties.Assembly));

                    var pe = pes.FirstOrDefault();
                    if (pe != null)
                    {
                        _observedMetadata[identity] = ass = PEAssemblySymbol.Create(pe);
                        ass.SetCorLibrary(_lazyCorLibrary);
                        modules.AddRange(ass.Modules.Cast <PEModuleSymbol>());
                    }
                    else
                    {
                        // TODO: diagnostics
                        throw new DllNotFoundException(identity.GetDisplayName());
                    }
                }

                return(ass);
            }
Exemple #8
0
        public override ImmutableArray <PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
        {
            var key = $"{reference}-{baseFilePath}";

            if (DirectReferenceCache.TryGetValue(key, out var result))
            {
                return(result);
            }

            result = DefaultRuntimeResolver.ResolveReference(reference, baseFilePath, properties);
            if (result.Length > 0)
            {
                DirectReferenceCache[key] = result;
            }

            return(result);
        }
        public override ImmutableArray <PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
        {
            string cacheKey = $"{reference}:{baseFilePath}";

            if (_referenceCache.TryGetValue(cacheKey, out ImmutableArray <PortableExecutableReference> result))
            {
                return(result);
            }

            result = _innerResolver.ResolveReference(reference, baseFilePath, properties);

            if (result.Length > 0)
            {
                _referenceCache[cacheKey] = result;
            }

            return(result);
        }
        public override ImmutableArray <PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
        {
            var key = $"{reference}-{baseFilePath}";

            if (DirectReferenceCache.ContainsKey(key))
            {
                return(DirectReferenceCache[key]);
            }

            var result = _defaultReferenceResolver.ResolveReference(reference, baseFilePath, properties);

            if (result.Length > 0)
            {
                DirectReferenceCache[key] = result;
            }

            return(result);
        }
Exemple #11
0
            public override ImmutableArray <PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
            {
                // Check the previous resolver
                var result = previousResolver.ResolveReference(reference, baseFilePath, properties);

                if (result.Length > 0)
                {
                    return(result);
                }

                // Try to use file resolver
                string path = ResolvePath(reference, baseFilePath);

                if (!string.IsNullOrEmpty(path))
                {
                    return(ImmutableArray.Create(MetadataReference.CreateFromFile(path, properties)));
                }

                return(ImmutableArray <PortableExecutableReference> .Empty);
            }
Exemple #12
0
            /// <summary>
            /// Resolves the reference.
            /// </summary>
            /// <param name="reference">The reference.</param>
            /// <param name="baseFilePath">The base file path.</param>
            /// <param name="properties">The properties.</param>
            /// <returns></returns>
            public override ImmutableArray <PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
            {
                // Check if we are referencing CodeGen assembly
                ImportUserTypeAssembly codeGenAssembly;

                if (codeGenAssemblies.TryGetValue(reference, out codeGenAssembly))
                {
                    using (MemoryStream stream = new MemoryStream(codeGenAssembly.AssemblyBytes))
                    {
                        return(ImmutableArray.Create(MetadataReference.CreateFromStream(stream, properties, null, codeGenAssembly.AssemblyPath)));
                    }
                }

                // Check the previous resolver
                var result = previousResolver.ResolveReference(reference, baseFilePath, properties);

                if (result.Length > 0)
                {
                    return(result);
                }

                // Try to use file resolver
                try
                {
                    string path = ResolvePath(reference, baseFilePath);

                    if (!string.IsNullOrEmpty(path))
                    {
                        return(ImmutableArray.Create(MetadataReference.CreateFromFile(path, properties)));
                    }
                }
                catch
                {
                }

                // Check if reference holds xml for CodeGen
                ImportUserTypeOptions options = ImportUserTypeOptions.ParseString(reference);

                if (options != null)
                {
                    foreach (ImportUserTypeAssembly assembly in codeGenAssemblies.Values)
                    {
                        if (assembly.Options.Equals(options))
                        {
                            // TODO: Compare that used PDBs have same GUID.
                            codeGenAssembly = assembly;
                            break;
                        }
                    }

                    if (codeGenAssembly == null)
                    {
                        codeGenAssembly = GenerateAssembly(options);
                        AddAssembly(codeGenAssembly);
                    }

                    using (MemoryStream stream = new MemoryStream(codeGenAssembly.AssemblyBytes))
                    {
                        return(ImmutableArray.Create(MetadataReference.CreateFromStream(stream, properties, null, codeGenAssembly.AssemblyPath)));
                    }
                }

                return(ImmutableArray <PortableExecutableReference> .Empty);
            }