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

Workspace create file workaround #2019

Merged
merged 3 commits into from
Nov 19, 2020
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
3 changes: 2 additions & 1 deletion src/OmniSharp.Cake/CakeProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using OmniSharp.Cake.Services;
Expand Down Expand Up @@ -126,7 +127,7 @@ private void AddCakeFile(string cakeFilePath)
// add Cake project to workspace
_workspace.AddProject(project);
var documentId = DocumentId.CreateNewId(project.Id);
var loader = new CakeTextLoader(cakeFilePath, _scriptService);
var loader = TextLoader.From(TextAndVersion.Create(SourceText.From(cakeScript.Source), VersionStamp.Create(DateTime.UtcNow)));
var documentInfo = DocumentInfo.Create(
documentId,
cakeFilePath,
Expand Down
55 changes: 0 additions & 55 deletions src/OmniSharp.Cake/CakeTextLoader.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public async Task<object> Handle(UpdateBufferRequest request)
NewText = change.NewText
});
}

fileChange.FromDisk = false;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/OmniSharp.MSBuild/ProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ private void OnDirectoryFileChanged(string path, FileChangeType changeType)
{
// Use the buffer manager to add the new file to the appropriate projects
// Hosts that don't pass the FileChangeType may wind up updating the buffer twice
_workspace.BufferManager.UpdateBufferAsync(new UpdateBufferRequest() { FileName = path, FromDisk = true }).Wait();
_workspace.BufferManager.UpdateBufferAsync(new UpdateBufferRequest() { FileName = path, FromDisk = true }, isCreate: changeType == FileChangeType.Create).Wait();
}
}
}
Expand Down
28 changes: 23 additions & 5 deletions src/OmniSharp.Roslyn/BufferManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.Logging;
using OmniSharp.FileWatching;
using OmniSharp.Models;
using OmniSharp.Models.ChangeBuffer;
Expand All @@ -20,14 +21,14 @@ public class BufferManager
private readonly ISet<DocumentId> _transientDocumentIds = new HashSet<DocumentId>();
private readonly object _lock = new object();
private readonly IFileSystemWatcher _fileSystemWatcher;
private readonly Action<string, FileChangeType> _onFileChanged;
private readonly ILogger<BufferManager> _logger;

public BufferManager(OmniSharpWorkspace workspace, IFileSystemWatcher fileSystemWatcher)
public BufferManager(OmniSharpWorkspace workspace, ILoggerFactory loggerFactory, IFileSystemWatcher fileSystemWatcher)
{
_workspace = workspace;
_workspace.WorkspaceChanged += OnWorkspaceChanged;
_fileSystemWatcher = fileSystemWatcher;
_onFileChanged = OnFileChanged;
_logger = loggerFactory.CreateLogger<BufferManager>();
}

public bool IsTransientDocument(DocumentId documentId)
Expand All @@ -38,14 +39,15 @@ public bool IsTransientDocument(DocumentId documentId)
}
}

public async Task UpdateBufferAsync(Request request)
public async Task UpdateBufferAsync(Request request, bool isCreate = false)
{
var buffer = request.Buffer;
var changes = request.Changes;

if (request is UpdateBufferRequest updateRequest && updateRequest.FromDisk)
{
buffer = File.ReadAllText(updateRequest.FileName);
_logger.LogDebug("Read file contents:\n{0}", buffer);
}

if (request.FileName == null || (buffer == null && changes == null))
Expand All @@ -64,7 +66,20 @@ public async Task UpdateBufferAsync(Request request)

foreach (var documentId in documentIds)
{
solution = solution.WithDocumentText(documentId, sourceText);
var document = solution.GetDocument(documentId);

// This can happen when the client sends a create request for a file that we created on the server,
// such as RunCodeAction. Unfortunately, previous attempts to have this fully controlled by the vscode
// client (such that it sent both create event and then updated existing text) wasn't successful:
// vscode seems to always trigger an update buffer event before triggering the create event.
if (isCreate && string.IsNullOrEmpty(buffer) && (await document.GetTextAsync()).Length > 0)
{
_logger.LogDebug("File was created with content in workspace, ignoring disk update");
continue;
}

solution = document.WithText(sourceText).Project.Solution;
_logger.LogDebug("Updating file {0} with new text:\n{1}", request.FileName, sourceText);
}
}
else
Expand Down Expand Up @@ -100,6 +115,8 @@ public async Task UpdateBufferAsync(Request request)
}
}

_logger.LogDebug("Updating file {0} with new text:\n{1}", document.FilePath, sourceText);

solution = solution.WithDocumentText(documentId, sourceText);
}
}
Expand All @@ -108,6 +125,7 @@ public async Task UpdateBufferAsync(Request request)
}
else if (buffer != null)
{
_logger.LogDebug("Adding transient file for {0}\n{1}", request.FileName, buffer);
TryAddTransientDocument(request.FileName, buffer);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/OmniSharp.Roslyn/OmniSharpWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public bool Initialized
public OmniSharpWorkspace(HostServicesAggregator aggregator, ILoggerFactory loggerFactory, IFileSystemWatcher fileSystemWatcher)
: base(aggregator.CreateHostServices(), "Custom")
{
BufferManager = new BufferManager(this, fileSystemWatcher);
BufferManager = new BufferManager(this, loggerFactory, fileSystemWatcher);
_logger = loggerFactory.CreateLogger<OmniSharpWorkspace>();
fileSystemWatcher.WatchDirectories(OnDirectoryRemoved);
}
Expand Down
4 changes: 2 additions & 2 deletions test-assets/test-projects/CakeProject/tools/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<packages>
<package id="Cake" version="0.27.2" />
<package id="Cake.Bakery" version="0.3.0" />
</packages>
<package id="Cake.Bakery" version="0.5.0" />
</packages>