dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.07k stars 4.04k forks source link

MSBuildWorkspace should have an API to reload a project #7842

Open erik-kallen opened 8 years ago

erik-kallen commented 8 years ago

I need to monitor a solution for changes. I do this by opening the solution using MSBuildWorkspace.Create().OpenSolutionAsync(solutionPath). I then start a FileSystemWatcher that watches all files in the solution, and whenever I detect a file to a .cs file, I use

foreach (var docId in solution.GetDocumentIdsWithFilePath(changedPath)
    solution = solution.WithDocumentText(docId, File.ReadAllText(changedPath));

I have two questions:

  1. Is this the best way of watching for changes to a solution? There seems to once upon a time have been an additional parameter to OpenSolution called enableFileTracking (http://stackoverflow.com/questions/11732067/detect-iworkspace-changes-by-vs-from-outside-vs-host-environment), but it does not seem to exist anymore.
  2. How to handle changes to .csproj? solution.GetDocumentIdsWithFilePath() returns nothing for those files. Ideally I want to reload the project that changed, and only that project, when this happens. How can I do this?
davkean commented 8 years ago

@mattwar or @jasonmalinowski Can you answer this?

mattwar commented 8 years ago

The file tracking logic was removed because it didn't fit the usage scenarios for MSBuildWorkspace, which is intended for simple analysis or transformation command line tools, not for live edit sessions like with VS editor.

erik-kallen commented 8 years ago

So is there a better workspace for my use case? Or, if not, is there any way to reload a single project?

jasonmalinowski commented 8 years ago

There's nothing better, no.

erik-kallen commented 8 years ago

In that case, please consider this a feature request for a feature that allows for just a single project to be reloaded

fred-perkins commented 7 years ago

Hi, bit late the party, just ran into this issue myself. @erik-kallen did you ever find a way to resolve question 2? I have a use case where i need to process a project using CodeAnalysis with some additional changes to the csproj files externally. I'd like to reload just the project instead of reloading the whole solution.

Thanks in advance :)

erik-kallen commented 7 years ago

@fred-perkins No, I did not. I had to resort to reloading the entire solution when a project file changed

miyu commented 4 years ago

@mattwar @jasonmalinowki so how does Visual Studio / VSCode live-monitor changes in files? Is there any alternative we can pursue for quickly monitoring and live-analyzing a C# project/solution?

CyrusNajmabadi commented 4 years ago

@mattwar @jasonmalinowki so how does Visual Studio / VSCode live-monitor changes in files? Is there any alternative we can pursue for quickly monitoring and live-analyzing a C# project/solution?

We don't use MSBuildWorkspace for this. We use VisualStudioWorkspace. This type hooks in appropriately to VS (including their own directory/file watching facilities).

JoeRobich commented 3 years ago

@jmarolf Is this covered by your PR https://github.com/dotnet/roslyn/pull/52554

tobyash86 commented 2 years ago

PR has been rejected. Are there any plans to support it?

jasonmalinowski commented 2 years ago

We're going to be needing to implement this (or something like it) soon, so we'll see what that ultimately looks like.

tobyash86 commented 1 year ago

Hi, any progress on this? What do you mean by saying "soon"?