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

Check if file exist before trying to create MetadataReference. #1045

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 31 additions & 9 deletions src/OmniSharp.Cake/CakeProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using OmniSharp.Cake.Services;
using OmniSharp.Helpers;
using OmniSharp.Models.WorkspaceInformation;
using OmniSharp.Roslyn.Utilities;
using OmniSharp.Services;

namespace OmniSharp.Cake
Expand All @@ -23,6 +24,7 @@ namespace OmniSharp.Cake
public class CakeProjectSystem : IProjectSystem
{
private readonly OmniSharpWorkspace _workspace;
private readonly MetadataFileReferenceCache _metadataReferenceCache;
private readonly IOmniSharpEnvironment _environment;
private readonly IAssemblyLoader _assemblyLoader;
private readonly ICakeScriptService _scriptService;
Expand All @@ -37,12 +39,14 @@ public class CakeProjectSystem : IProjectSystem
[ImportingConstructor]
public CakeProjectSystem(
OmniSharpWorkspace workspace,
MetadataFileReferenceCache metadataReferenceCache,
IOmniSharpEnvironment environment,
IAssemblyLoader assemblyLoader,
ICakeScriptService scriptService,
ILoggerFactory loggerFactory)
{
_workspace = workspace ?? throw new ArgumentNullException(nameof(workspace));
_metadataReferenceCache = metadataReferenceCache ?? throw new ArgumentNullException(nameof(metadataReferenceCache));
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
_assemblyLoader = assemblyLoader ?? throw new ArgumentNullException(nameof(assemblyLoader));
_scriptService = scriptService ?? throw new ArgumentNullException(nameof(scriptService));
Expand Down Expand Up @@ -148,15 +152,27 @@ private void ScriptReferencesChanged(object sender, ReferencesChangedEventArgs e
var document = solution.GetDocument(documentId);
var project = document.Project;

var metadataReferences = e.References.Select(reference => MetadataReference.CreateFromFile(reference, documentation: GetDocumentationProvider(reference)));
var fileReferencesToRemove = project.MetadataReferences;
var metadataReferences = GetMetadataReferences(e.References);
var referencesToRemove = new HashSet<MetadataReference>(project.MetadataReferences, MetadataReferenceEqualityComparer.Instance);
var referencesToAdd = new HashSet<MetadataReference>(MetadataReferenceEqualityComparer.Instance);

foreach (var reference in metadataReferences)
{
if (referencesToRemove.Remove(reference))
{
continue;
}

if (referencesToAdd.Contains(reference))
{
continue;
}

_workspace.AddMetadataReference(project.Id, reference);
referencesToAdd.Add(reference);
}

foreach (var reference in fileReferencesToRemove)
foreach (var reference in referencesToRemove)
{
_workspace.RemoveMetadataReference(project.Id, reference);
}
Expand Down Expand Up @@ -217,18 +233,24 @@ private ProjectInfo GetProject(CakeScript cakeScript, string filePath)
language: LanguageNames.CSharp,
compilationOptions: cakeScript.Usings == null ? _compilationOptions.Value : _compilationOptions.Value.WithUsings(cakeScript.Usings),
parseOptions: new CSharpParseOptions(LanguageVersion.Default, DocumentationMode.Parse, SourceCodeKind.Script),
metadataReferences: cakeScript.References.Select(reference => MetadataReference.CreateFromFile(reference, documentation: GetDocumentationProvider(reference))),
metadataReferences: GetMetadataReferences(cakeScript.References),
// TODO: projectReferences?
isSubmission: true,
hostObjectType: hostObjectType);
}

private static DocumentationProvider GetDocumentationProvider(string assemblyPath)
private IEnumerable<MetadataReference> GetMetadataReferences(IEnumerable<string> references)
{
var assemblyDocumentationPath = Path.ChangeExtension(assemblyPath, ".xml");
return File.Exists(assemblyDocumentationPath)
? XmlDocumentationProvider.CreateFromFile(assemblyDocumentationPath)
: DocumentationProvider.Default;
foreach (var reference in references)
{
if (!File.Exists(reference))
{
_logger.LogWarning($"Unable to create MetadataReference. File {reference} does not exist.");
continue;
}

yield return _metadataReferenceCache.GetMetadataReference(reference);
}
}

private static CSharpCompilationOptions CreateCompilationOptions()
Expand Down
2 changes: 1 addition & 1 deletion src/OmniSharp.Cake/Services/CakeScriptService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public CakeScript Generate(FileChange fileChange)
if (!cakeScript.References.SetEquals(references))
{
_cachedReferences[fileChange.FileName] = cakeScript.References;
OnReferencesChanged(new ReferencesChangedEventArgs(fileChange.FileName, cakeScript.References.ToList()));
OnReferencesChanged(new ReferencesChangedEventArgs(fileChange.FileName, cakeScript.References));
}

// Check if usings changed
Expand Down
6 changes: 3 additions & 3 deletions src/OmniSharp.Cake/Services/ICakeScriptService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ protected ScriptChangedEventArgs(string scriptPath)

public class ReferencesChangedEventArgs : ScriptChangedEventArgs
{
public IReadOnlyCollection<string> References { get; }
public ISet<string> References { get; }

public ReferencesChangedEventArgs(string scriptPath, IReadOnlyCollection<string> references) : base(scriptPath)
public ReferencesChangedEventArgs(string scriptPath, ISet<string> references) : base(scriptPath)
{
References = references ?? new List<string>();
References = references ?? new HashSet<string>();
}
}

Expand Down