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

Add MSBuild project to solution and apply the change to Roslyn workspace as a unit #2314

Merged
Merged
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
43 changes: 25 additions & 18 deletions src/OmniSharp.MSBuild/ProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public ProjectToUpdate(string filePath, bool allowAutoRestore, ProjectIdInfo pro
private readonly ConcurrentDictionary<string, int/*unused*/> _projectsRequestedOnDemand;
private readonly ProjectLoader _projectLoader;
private readonly OmniSharpWorkspace _workspace;
private readonly object _workspaceGate = new();
private readonly ImmutableArray<IMSBuildEventSink> _eventSinks;
private const int LoopDelay = 100; // milliseconds
private readonly BufferBlock<ProjectToUpdate> _queue;
Expand Down Expand Up @@ -365,14 +366,17 @@ private void AddProject(ProjectFileInfo projectFileInfo)

var projectInfo = projectFileInfo.CreateProjectInfo(_analyzerAssemblyLoader);

var newSolution = _workspace.CurrentSolution.AddProject(projectInfo);
_workspace.AddDocumentInclusionRuleForProject(projectInfo.Id, (filePath) => projectFileInfo.IsFileIncluded(filePath));
lock (_workspaceGate)
{
var newSolution = _workspace.CurrentSolution.AddProject(projectInfo);
_workspace.AddDocumentInclusionRuleForProject(projectInfo.Id, (filePath) => projectFileInfo.IsFileIncluded(filePath));

SubscribeToAnalyzerReferenceLoadFailures(projectInfo.AnalyzerReferences.Cast<AnalyzerFileReference>(), _logger);
SubscribeToAnalyzerReferenceLoadFailures(projectInfo.AnalyzerReferences.Cast<AnalyzerFileReference>(), _logger);

if (!_workspace.TryApplyChanges(newSolution))
{
_logger.LogError($"Failed to add project to workspace: '{projectFileInfo.FilePath}'");
if (!_workspace.TryApplyChanges(newSolution))
{
_logger.LogError($"Failed to add project to workspace: '{projectFileInfo.FilePath}'");
}
}

WatchProjectFiles(projectFileInfo);
Expand Down Expand Up @@ -694,23 +698,26 @@ private void UpdateSourceFiles(Project project, IList<string> sourceFiles)

private void OnDirectoryFileChanged(string path, FileChangeType changeType)
{
// Hosts may not have passed through a file change type
if (changeType == FileChangeType.Unspecified && !File.Exists(path) || changeType == FileChangeType.Delete)
lock (_workspaceGate)
{
foreach (var documentId in _workspace.CurrentSolution.GetDocumentIdsWithFilePath(path))
// Hosts may not have passed through a file change type
if (changeType == FileChangeType.Unspecified && !File.Exists(path) || changeType == FileChangeType.Delete)
{
_workspace.RemoveDocument(documentId);
foreach (var documentId in _workspace.CurrentSolution.GetDocumentIdsWithFilePath(path))
{
_workspace.RemoveDocument(documentId);
}
}
}

if (changeType == FileChangeType.Unspecified || changeType == FileChangeType.Create || changeType == FileChangeType.Change)
{
// Only add cs files. Also, make sure the path is a file, and not a directory name that happens to end in ".cs"
if (string.Equals(Path.GetExtension(path), ".cs", StringComparison.CurrentCultureIgnoreCase) && File.Exists(path))
if (changeType == FileChangeType.Unspecified || changeType == FileChangeType.Create || changeType == FileChangeType.Change)
{
// 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 }, isCreate: changeType == FileChangeType.Create).Wait();
// Only add cs files. Also, make sure the path is a file, and not a directory name that happens to end in ".cs"
if (string.Equals(Path.GetExtension(path), ".cs", StringComparison.CurrentCultureIgnoreCase) && File.Exists(path))
{
// 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 }, isCreate: changeType == FileChangeType.Create).Wait();
}
}
}
}
Expand Down