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

Don't lock Cake host object assembly. #1044

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/OmniSharp.Abstractions/Services/IAssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IAssemblyLoader

IReadOnlyList<Assembly> LoadAllFrom(string folderPath);

Assembly LoadFrom(string assemblyPath);
Assembly LoadFrom(string assemblyPath, bool dontLockAssemblyOnDisk = false);
}

public static class IAssemblyLoaderExtensions
Expand Down
12 changes: 10 additions & 2 deletions src/OmniSharp.Cake/CakeProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void Initalize(IConfiguration configuration)
FileName = cakePath,
FromDisk = true
});

var project = GetProject(cakeScript, cakePath);

// add Cake project to workspace
Expand Down Expand Up @@ -205,8 +206,15 @@ private ProjectInfo GetProject(CakeScript cakeScript, string filePath)
{
var name = Path.GetFileName(filePath);

var assembly = _assemblyLoader.LoadFrom(cakeScript.Host.AssemblyPath);
var hostObjectType = Type.GetType(cakeScript.Host.TypeName, a => assembly, null, true);
if (!File.Exists(cakeScript.Host.AssemblyPath))
{
throw new FileNotFoundException($"Cake is not installed. Path {cakeScript.Host.AssemblyPath} does not exist.");
}
var hostObjectType = Type.GetType(cakeScript.Host.TypeName, a => _assemblyLoader.LoadFrom(cakeScript.Host.AssemblyPath, dontLockAssemblyOnDisk: true), null, false);
if (hostObjectType == null)
{
throw new InvalidOperationException($"Could not get host object type: {cakeScript.Host.TypeName}.");
}

return ProjectInfo.Create(
id: ProjectId.CreateNewId(Guid.NewGuid().ToString()),
Expand Down
32 changes: 23 additions & 9 deletions src/OmniSharp.Host/Services/AssemblyLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.IO;
Expand All @@ -9,6 +10,7 @@ namespace OmniSharp.Services
{
internal class AssemblyLoader : IAssemblyLoader
{
private static readonly ConcurrentDictionary<string, Assembly> AssemblyCache = new ConcurrentDictionary<string, Assembly>(StringComparer.OrdinalIgnoreCase);
private readonly ILogger _logger;

public AssemblyLoader(ILoggerFactory loggerFactory)
Expand Down Expand Up @@ -59,19 +61,31 @@ public IReadOnlyList<Assembly> LoadAllFrom(string folderPath)
}
}

public Assembly LoadFrom(string assemblyPath)
public Assembly LoadFrom(string assemblyPath, bool dontLockAssemblyOnDisk = false)
{
if (string.IsNullOrWhiteSpace(assemblyPath)) return null;

Assembly assembly = null;

try
if (!AssemblyCache.TryGetValue(assemblyPath, out var assembly))
{
assembly = Assembly.LoadFrom(assemblyPath);
}
catch (Exception ex)
{
_logger.LogError(ex, $"Failed to load assembly from path: {assemblyPath}");
try
{
if (dontLockAssemblyOnDisk)
{
var bytes = File.ReadAllBytes(assemblyPath);
assembly = Assembly.Load(bytes);
}
else
{
assembly = Assembly.LoadFrom(assemblyPath);
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"Failed to load assembly from path: {assemblyPath}");
return assembly;
}

AssemblyCache.AddOrUpdate(assemblyPath, assembly, (k, v) => assembly);
}

_logger.LogTrace($"Assembly loaded from path: {assemblyPath}");
Expand Down