/// <summary> /// Dispose of resources being used by the <see cref="MSBuildEngineFixture"/>. /// </summary> public void Dispose() { if (MSBuildLocator.IsRegistered) { MSBuildLocator.Unregister(); } }
private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { _workspace.Dispose(); MSBuildLocator.Unregister(); } disposedValue = true; } }
public void Dispose() { MSBuildLocator.Unregister(); }
public void Dispose() { MSBuildLocator.Unregister(); GC.SuppressFinalize(this); }
/// <summary> /// Find and use the latest version of the MSBuild engine compatible with the current SDK. /// </summary> /// <param name="baseDirectory"> /// An optional base directory where dotnet.exe should be run (this may affect the version it reports due to global.json). /// </param> /// <param name="logger"> /// An optional <see cref="ILogger"/> to use for diagnostic purposes (if not specified, the static <see cref="Log.Logger"/> will be used). /// </param> public static void DiscoverMSBuildEngine(string baseDirectory = null, ILogger logger = null) { if (MSBuildLocator.IsRegistered) { MSBuildLocator.Unregister(); } _registeredMSBuildInstance = null; // Assume working directory is VS code's current working directory (i.e. the workspace root). // // Really, until we figure out a way to change the version of MSBuild we're using after the server has started, // we're still going to have problems here. // // In the end we will probably wind up having to move all the MSBuild stuff out to a separate process, and use something like GRPC (or even Akka.NET's remoting) to communicate with it. // It can be stopped and restarted by the language server (even having different instances for different SDK / MSBuild versions). // // This will also ensure that the language server's model doesn't expose any MSBuild objects anywhere. // // For now, though, let's choose the dumb option. DotNetRuntimeInfo runtimeInfo = DotNetRuntimeInfo.GetCurrent(baseDirectory, logger); // SDK versions are in SemVer format... SemanticVersion targetSdkSemanticVersion; if (!SemanticVersion.TryParse(runtimeInfo.SdkVersion, out targetSdkSemanticVersion)) { throw new Exception($"Cannot determine SDK version information for current .NET SDK (located at '{runtimeInfo.BaseDirectory}')."); } // ...which MSBuildLocator does not understand. Version targetSdkVersion = new Version( major: targetSdkSemanticVersion.Major, minor: targetSdkSemanticVersion.Minor, build: targetSdkSemanticVersion.Patch ); var queryOptions = new VisualStudioInstanceQueryOptions { // We can only load the .NET Core MSBuild engine DiscoveryTypes = DiscoveryType.DotNetSdk }; VisualStudioInstance[] allInstances = MSBuildLocator .QueryVisualStudioInstances(queryOptions) .ToArray(); VisualStudioInstance latestInstance = allInstances .OrderByDescending(instance => instance.Version) .FirstOrDefault(instance => // We need a version of MSBuild for the currently-supported SDK instance.Version == targetSdkVersion ); if (latestInstance == null) { string foundVersions = String.Join(", ", allInstances.Select(instance => instance.Version)); throw new Exception($"Cannot locate MSBuild engine for .NET SDK v{targetSdkVersion}. This probably means that MSBuild Project Tools cannot find the MSBuild for the current project instance. It did find the following version(s), though: [{foundVersions}]."); } MSBuildLocator.RegisterInstance(latestInstance); _registeredMSBuildInstance = latestInstance; }