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

Detect when files move to empty directories #1368

Merged
merged 10 commits into from
Jan 15, 2019
4 changes: 2 additions & 2 deletions src/OmniSharp.MSBuild/ProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ private void ProcessQueue(CancellationToken cancellationToken)
{
_processingQueue = false;
}

_fileSystemWatcher.Watch(".cs", _onDirectoryFileChanged);
}

private (ProjectFileInfo, ProjectLoadedEventArgs) LoadProject(string projectFilePath)
Expand Down Expand Up @@ -421,8 +423,6 @@ private void UpdateSourceFiles(Project project, IList<string> sourceFiles)
// Add source files to the project.
foreach (var sourceFile in sourceFiles)
{
_fileSystemWatcher.Watch(Path.GetDirectoryName(sourceFile), _onDirectoryFileChanged);

// If a document for this source file already exists in the project, carry on.
if (currentDocuments.Remove(sourceFile))
{
Expand Down
31 changes: 31 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FilesChangedFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,37 @@ public async Task TestFileAddedToMSBuildWorkspaceOnCreation()
}
}

[Fact]
public async Task TestFileMovedToPreviouslyEmptyDirectory()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectAndSolution"))
using (var host = CreateOmniSharpHost(testProject.Directory))
{
var watcher = host.GetExport<IFileSystemWatcher>();

var projectDirectory = Path.GetDirectoryName(host.Workspace.CurrentSolution.Projects.First().FilePath);
const string filename = "FileName.cs";
var filePath = Path.Combine(projectDirectory, filename);
File.WriteAllText(filePath, "text");
var handler = GetRequestHandler(host);
await handler.Handle(new[] { new FilesChangedRequest() { FileName = filePath, ChangeType = FileChangeType.Create } });

Assert.Contains(host.Workspace.CurrentSolution.Projects.First().Documents, d => d.Name == filePath);

var nestedDirectory = Path.Combine(projectDirectory, "Nested");
Directory.CreateDirectory(nestedDirectory);

var destinationPath = Path.Combine(nestedDirectory, filename);
File.Move(filePath, destinationPath);

await handler.Handle(new[] { new FilesChangedRequest() { FileName = filePath, ChangeType = FileChangeType.Delete } });
await handler.Handle(new[] { new FilesChangedRequest() { FileName = destinationPath, ChangeType = FileChangeType.Create } });

Assert.Contains(host.Workspace.CurrentSolution.Projects.First().Documents, d => d.Name == destinationPath);
Assert.DoesNotContain(host.Workspace.CurrentSolution.Projects.First().Documents, d => d.Name == filePath);
}
}

[Fact]
public void TestMultipleDirectoryWatchers()
{
Expand Down