From 16f87df671a84c2959e3fa5fe11e3952b609ed6d Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 27 Jun 2017 15:52:45 +0200 Subject: [PATCH 1/3] improve the logic of adding default assembly references --- src/OmniSharp.Script/ScriptProjectSystem.cs | 67 +++++++++++++++------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/src/OmniSharp.Script/ScriptProjectSystem.cs b/src/OmniSharp.Script/ScriptProjectSystem.cs index e093b262f3..f67f1da5ca 100644 --- a/src/OmniSharp.Script/ScriptProjectSystem.cs +++ b/src/OmniSharp.Script/ScriptProjectSystem.cs @@ -73,6 +73,7 @@ public void Initalize(IConfiguration configuration) x.Name.ToLowerInvariant().StartsWith("system.valuetuple"))); var runtimeContexts = File.Exists(Path.Combine(_env.TargetDirectory, "project.json")) ? ProjectContext.CreateContextForEachTarget(_env.TargetDirectory) : null; + var runtimeContext = runtimeContexts?.FirstOrDefault(); var commonReferences = new HashSet(); @@ -87,24 +88,19 @@ public void Initalize(IConfiguration configuration) } // if we have no context, then we also have no dependencies - // we can assume desktop framework - // and add mscorlib - if (runtimeContexts == null || runtimeContexts.Any() == false) + // we will assume desktop framework + // and add default CLR references + // same applies for having a context that is not a .NET Core app + AddDefaultClrMetadataReferences(runtimeContext, commonReferences); + if (runtimeContext == null) { - _logger.LogInformation("Unable to find project context for CSX files. Will default to non-context usage."); - - AddMetadataReference(commonReferences, typeof(object).GetTypeInfo().Assembly.Location); - AddMetadataReference(commonReferences, typeof(Enumerable).GetTypeInfo().Assembly.Location); - - inheritedCompileLibraries.AddRange(DependencyContext.Default.CompileLibraries.Where(x => - x.Name.ToLowerInvariant().StartsWith("system.runtime"))); + _logger.LogInformation("Unable to find project context for CSX files. Will default to non-context usage (Destkop CLR scripts)."); } // otherwise we will grab dependencies for the script from the runtime context else { // assume the first one - var runtimeContext = runtimeContexts.First(); - _logger.LogInformation($"Found script runtime context '{runtimeContext?.TargetFramework.Framework}' for '{runtimeContext.ProjectFile.ProjectFilePath}'."); + _logger.LogInformation($"Found script runtime context '{runtimeContext.TargetFramework.Framework}' for '{runtimeContext.ProjectFile.ProjectFilePath}'."); var projectExporter = runtimeContext.CreateExporter("Release"); var projectDependencies = projectExporter.GetDependencies(); @@ -116,14 +112,6 @@ public void Initalize(IConfiguration configuration) _logger.LogDebug("Discovered script compilation assembly reference: " + compilationAssembly.ResolvedPath); AddMetadataReference(commonReferences, compilationAssembly.ResolvedPath); } - - // for non .NET Core, include System.Runtime - if (runtimeContext.TargetFramework.Framework != ".NETCoreApp") - { - - inheritedCompileLibraries.AddRange(DependencyContext.Default.CompileLibraries.Where(x => - x.Name.ToLowerInvariant().StartsWith("system.runtime"))); - } } // inject all inherited assemblies @@ -155,6 +143,45 @@ public void Initalize(IConfiguration configuration) } } + private void AddDefaultClrMetadataReferences(ProjectContext projectContext, HashSet commonReferences) + { + if (projectContext == null || projectContext.TargetFramework?.Framework != ".NETCoreApp") + { + var assemblies = new[] + { + typeof(object).GetTypeInfo().Assembly, + typeof(Enumerable).GetTypeInfo().Assembly, + typeof(Stack<>).GetTypeInfo().Assembly, + typeof(Lazy<,>).GetTypeInfo().Assembly, + FromName("System.Runtime"), + FromName("mscorlib") + }; + + var references = assemblies + .Where(a => a != null) + .Select(a => a.Location) + .Distinct() + .Select(l => _metadataFileReferenceCache.GetMetadataReference(l)); + + foreach (var reference in references) + { + commonReferences.Add(reference); + } + + Assembly FromName(string assemblyName) + { + try + { + return Assembly.Load(new AssemblyName(assemblyName)); + } + catch + { + return null; + } + } + } + } + private IEnumerable TryCreateRuntimeContextsFromScriptFiles() { _logger.LogInformation($"Attempting to create runtime context from script files. Default target framework {_targetFrameWork.Value}"); From d07f01251371319d5d54c5e78de9b2f4b68345b1 Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 27 Jun 2017 15:53:50 +0200 Subject: [PATCH 2/3] correct comment --- src/OmniSharp.Script/ScriptProjectSystem.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OmniSharp.Script/ScriptProjectSystem.cs b/src/OmniSharp.Script/ScriptProjectSystem.cs index f67f1da5ca..dab7763415 100644 --- a/src/OmniSharp.Script/ScriptProjectSystem.cs +++ b/src/OmniSharp.Script/ScriptProjectSystem.cs @@ -64,7 +64,7 @@ public void Initalize(IConfiguration configuration) _logger.LogInformation($"Found {allCsxFiles.Length} CSX files."); - // explicitly inherit scripting library references to all global script object (InteractiveScriptGlobals) to be recognized + // explicitly inherit scripting library references to all global script object (CommandLineScriptGlobals) to be recognized var inheritedCompileLibraries = DependencyContext.Default.CompileLibraries.Where(x => x.Name.ToLowerInvariant().StartsWith("microsoft.codeanalysis")).ToList(); From cd7b3c369b7c77540aee5d8eec7924a8c59d414d Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 27 Jun 2017 16:44:29 +0200 Subject: [PATCH 3/3] use assembly loader and fixed issues after rebase --- src/OmniSharp.Script/ScriptProjectSystem.cs | 29 +++++++-------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/OmniSharp.Script/ScriptProjectSystem.cs b/src/OmniSharp.Script/ScriptProjectSystem.cs index dab7763415..d1b188d741 100644 --- a/src/OmniSharp.Script/ScriptProjectSystem.cs +++ b/src/OmniSharp.Script/ScriptProjectSystem.cs @@ -30,15 +30,19 @@ public class ScriptProjectSystem : IProjectSystem private readonly OmniSharpWorkspace _workspace; private readonly IOmniSharpEnvironment _env; private readonly ILogger _logger; + private readonly IAssemblyLoader _assemblyLoader; + private readonly IScriptProjectProvider _scriptProjectProvider; private static readonly Lazy _targetFrameWork = new Lazy(ResolveTargetFramework); [ImportingConstructor] - public ScriptProjectSystem(OmniSharpWorkspace workspace, IOmniSharpEnvironment env, ILoggerFactory loggerFactory, MetadataFileReferenceCache metadataFileReferenceCache) + public ScriptProjectSystem(OmniSharpWorkspace workspace, IOmniSharpEnvironment env, ILoggerFactory loggerFactory, + MetadataFileReferenceCache metadataFileReferenceCache, IAssemblyLoader assemblyLoader) { _metadataFileReferenceCache = metadataFileReferenceCache; _workspace = workspace; _env = env; + _assemblyLoader = assemblyLoader; _logger = loggerFactory.CreateLogger(); _projects = new Dictionary(); _scriptProjectProvider = ScriptProjectProvider.Create(loggerFactory); @@ -73,10 +77,6 @@ public void Initalize(IConfiguration configuration) x.Name.ToLowerInvariant().StartsWith("system.valuetuple"))); var runtimeContexts = File.Exists(Path.Combine(_env.TargetDirectory, "project.json")) ? ProjectContext.CreateContextForEachTarget(_env.TargetDirectory) : null; - var runtimeContext = runtimeContexts?.FirstOrDefault(); - - var commonReferences = new HashSet(); - if (!bool.TryParse(configuration["enableScriptNuGetReferences"], out var enableScriptNuGetReferences)) { enableScriptNuGetReferences = false; @@ -87,6 +87,9 @@ public void Initalize(IConfiguration configuration) runtimeContexts = TryCreateRuntimeContextsFromScriptFiles(); } + var runtimeContext = runtimeContexts?.FirstOrDefault(); + var commonReferences = new HashSet(); + // if we have no context, then we also have no dependencies // we will assume desktop framework // and add default CLR references @@ -153,8 +156,8 @@ private void AddDefaultClrMetadataReferences(ProjectContext projectContext, Hash typeof(Enumerable).GetTypeInfo().Assembly, typeof(Stack<>).GetTypeInfo().Assembly, typeof(Lazy<,>).GetTypeInfo().Assembly, - FromName("System.Runtime"), - FromName("mscorlib") + _assemblyLoader.Load("System.Runtime"), + _assemblyLoader.Load("mscorlib") }; var references = assemblies @@ -167,18 +170,6 @@ private void AddDefaultClrMetadataReferences(ProjectContext projectContext, Hash { commonReferences.Add(reference); } - - Assembly FromName(string assemblyName) - { - try - { - return Assembly.Load(new AssemblyName(assemblyName)); - } - catch - { - return null; - } - } } }