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

Cannot use MSBuildLocator with multiple versions of Visual Studio for different projects #71197

Closed
vladimirKa002 opened this issue Dec 9, 2023 · 7 comments
Assignees

Comments

@vladimirKa002
Copy link

Version Used: 4.8.0

Steps to Reproduce:

Tested C# projects (.csproj): project1 and project2.

Projects loading method:

private Project GetProject(string projectPath, MSBuildWorkspace workspace)
{
    return workspace.OpenProjectAsync(projectPath).Result;
}

Installed Visual Studio versions:

16.11.34301.259
17.8.34322.80

Dependecies:

<PackageReference Include="Microsoft.Build" Version="17.8.3" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.6.10" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.8.3" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.8.3" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.8.0" />

Expected Behavior: Projects should be loaded normally with all documents from the path.

Actual Behavior:

When I run the following code snippet at the start of the analysis, the first project has zero documents. However, the second one gets loaded normally:

if (!MSBuildLocator.IsRegistered)
{
    var vsi = MSBuildLocator.QueryVisualStudioInstances();
    MSBuildLocator.RegisterInstance(vsi.First()); // First installed version
}

But when I select the second VS version MSBuildLocator, the first project works fine, but the second one gets stuck forever.

Instead of this code, I also tried MSBuildLocator.RegisterDefaults();, but it behaved similarly to when the first version is selected.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 9, 2023
@genlu genlu added Question IDE-MSBuildWorkspace MSBuildWorkspace and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 15, 2023
@jasonmalinowski
Copy link
Member

So the fact you can't use different versions of MSBuild for different projects (at least with MSBuildLocator) would be expected, since once MSBuildLocator has loaded a VS version of MSBuild we can't unload it -- there's not AppDomain isolation or anything in the way we'd need to make that work. That said, this feels like a bit of a red herring -- I wouldn't expect a high version to not work with something that a lower version does.

@vladimirKa00217 I'd suggest trying one of two things:

  1. Once we get the 4.9.0-2.final packages of Workspaces.MSBuild uploaded, try those. Those move the actual invoking of MSBuild to a separate process where we ensure we get binding redirects and such set up. It also means you registering MSBuildLocator in process doesn't do anything anymore -- we're calling it in the other processes. Your SDK-style project would also get loaded in a .NET Core process which might help compatibility there since we'll use separate processes for each.
  2. If that doesn't work (or you want to try to analyze this a bit sooner), subscribe to MSBuildWorkspace.WorkspaceFailed as an event, and report back here what log messages get fired to that. That might give us a bit of a hint as to what's breaking here.

But there's a good chance the work we did for those newer packages might fix this.

@jasonmalinowski
Copy link
Member

Those packages are now up, so @vladimirKa002 want to give those a shot and report back?

@jasonmalinowski
Copy link
Member

@vladimirKa002 Any chance you can try the new packages and report back?

@vladimirKa002
Copy link
Author

@jasonmalinowski, thank you for your response. I tried a new package, and it solved the problem. Now I can perform the analysis of both projects normally.

@jasonmalinowski
Copy link
Member

Awesome, thanks for confirming! And to be clear: with the new packages you don't need MSBuildLocator at all anymore, so feel free to remove the package references and any use of the API.

@vladimirKa002
Copy link
Author

@jasonmalinowski, could you please check this issue. Seems like after upgrading the packages to a newer version, analyses stopped working in Docker environment.

@jasonmalinowski
Copy link
Member

@vladimirKa002 Thanks for the ping, since that issue had gotten closed we otherwise wouldn't have seen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants