|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Immutable; |
| 6 | +using System.Security; |
| 7 | +using Microsoft.CodeAnalysis.Features.Workspaces; |
| 8 | +using Microsoft.CodeAnalysis.Host; |
| 9 | +using Microsoft.CodeAnalysis.LanguageServer.HostWorkspace.ProjectTelemetry; |
| 10 | +using Microsoft.CodeAnalysis.MetadataAsSource; |
| 11 | +using Microsoft.CodeAnalysis.MSBuild; |
| 12 | +using Microsoft.CodeAnalysis.Options; |
| 13 | +using Microsoft.CodeAnalysis.ProjectSystem; |
| 14 | +using Microsoft.CodeAnalysis.Shared.Extensions; |
| 15 | +using Microsoft.CodeAnalysis.Shared.TestHooks; |
| 16 | +using Microsoft.CodeAnalysis.Text; |
| 17 | +using Microsoft.CodeAnalysis.Workspaces.ProjectSystem; |
| 18 | +using Microsoft.CommonLanguageServerProtocol.Framework; |
| 19 | +using Microsoft.Extensions.Logging; |
| 20 | +using Microsoft.VisualStudio.Composition; |
| 21 | +using Roslyn.LanguageServer.Protocol; |
| 22 | +using Roslyn.Utilities; |
| 23 | +using static Microsoft.CodeAnalysis.MSBuild.BuildHostProcessManager; |
| 24 | + |
| 25 | +namespace Microsoft.CodeAnalysis.LanguageServer.HostWorkspace; |
| 26 | + |
| 27 | +/// <summary>Handles loading both miscellaneous files and file-based program projects.</summary> |
| 28 | +internal sealed class FileBasedProgramsProjectSystem : LanguageServerProjectLoader, ILspMiscellaneousFilesWorkspaceProvider |
| 29 | +{ |
| 30 | + private readonly ILspServices _lspServices; |
| 31 | + private readonly ILogger<FileBasedProgramsProjectSystem> _logger; |
| 32 | + private readonly IMetadataAsSourceFileService _metadataAsSourceFileService; |
| 33 | + |
| 34 | + public FileBasedProgramsProjectSystem( |
| 35 | + ILspServices lspServices, |
| 36 | + IMetadataAsSourceFileService metadataAsSourceFileService, |
| 37 | + LanguageServerWorkspaceFactory workspaceFactory, |
| 38 | + IFileChangeWatcher fileChangeWatcher, |
| 39 | + IGlobalOptionService globalOptionService, |
| 40 | + ILoggerFactory loggerFactory, |
| 41 | + IAsynchronousOperationListenerProvider listenerProvider, |
| 42 | + ProjectLoadTelemetryReporter projectLoadTelemetry, |
| 43 | + ServerConfigurationFactory serverConfigurationFactory, |
| 44 | + BinlogNamer binlogNamer) |
| 45 | + : base( |
| 46 | + workspaceFactory.FileBasedProgramsProjectFactory, |
| 47 | + workspaceFactory.TargetFrameworkManager, |
| 48 | + workspaceFactory.ProjectSystemHostInfo, |
| 49 | + fileChangeWatcher, |
| 50 | + globalOptionService, |
| 51 | + loggerFactory, |
| 52 | + listenerProvider, |
| 53 | + projectLoadTelemetry, |
| 54 | + serverConfigurationFactory, |
| 55 | + binlogNamer) |
| 56 | + { |
| 57 | + _lspServices = lspServices; |
| 58 | + _logger = loggerFactory.CreateLogger<FileBasedProgramsProjectSystem>(); |
| 59 | + _metadataAsSourceFileService = metadataAsSourceFileService; |
| 60 | + } |
| 61 | + |
| 62 | + public Workspace Workspace => ProjectFactory.Workspace; |
| 63 | + |
| 64 | + private string GetDocumentFilePath(DocumentUri uri) => uri.ParsedUri is { } parsedUri ? ProtocolConversions.GetDocumentFilePathFromUri(parsedUri) : uri.UriString; |
| 65 | + |
| 66 | + public async ValueTask<TextDocument?> AddMiscellaneousDocumentAsync(DocumentUri uri, SourceText documentText, string languageId, ILspLogger logger) |
| 67 | + { |
| 68 | + var documentFilePath = GetDocumentFilePath(uri); |
| 69 | + |
| 70 | + // https://github.com/dotnet/roslyn/issues/78421: MetadataAsSource should be its own workspace |
| 71 | + if (_metadataAsSourceFileService.TryAddDocumentToWorkspace(documentFilePath, documentText.Container, out var documentId)) |
| 72 | + { |
| 73 | + var metadataWorkspace = _metadataAsSourceFileService.TryGetWorkspace(); |
| 74 | + Contract.ThrowIfNull(metadataWorkspace); |
| 75 | + return metadataWorkspace.CurrentSolution.GetRequiredDocument(documentId); |
| 76 | + } |
| 77 | + |
| 78 | + var primordialDoc = AddPrimordialDocument(uri, documentText, languageId); |
| 79 | + Contract.ThrowIfNull(primordialDoc.FilePath); |
| 80 | + |
| 81 | + var doDesignTimeBuild = uri.ParsedUri?.IsFile is true |
| 82 | + && primordialDoc.Project.Language == LanguageNames.CSharp |
| 83 | + && GlobalOptionService.GetOption(LanguageServerProjectSystemOptionsStorage.EnableFileBasedPrograms); |
| 84 | + await BeginLoadingProjectWithPrimordialAsync(primordialDoc.FilePath, primordialProjectId: primordialDoc.Project.Id, doDesignTimeBuild); |
| 85 | + |
| 86 | + return primordialDoc; |
| 87 | + |
| 88 | + TextDocument AddPrimordialDocument(DocumentUri uri, SourceText documentText, string languageId) |
| 89 | + { |
| 90 | + var languageInfoProvider = _lspServices.GetRequiredService<ILanguageInfoProvider>(); |
| 91 | + if (!languageInfoProvider.TryGetLanguageInformation(uri, languageId, out var languageInformation)) |
| 92 | + { |
| 93 | + Contract.Fail($"Could not find language information for {uri} with absolute path {documentFilePath}"); |
| 94 | + } |
| 95 | + |
| 96 | + var workspace = Workspace; |
| 97 | + var sourceTextLoader = new SourceTextLoader(documentText, documentFilePath); |
| 98 | + var projectInfo = MiscellaneousFileUtilities.CreateMiscellaneousProjectInfoForDocument( |
| 99 | + workspace, documentFilePath, sourceTextLoader, languageInformation, documentText.ChecksumAlgorithm, workspace.Services.SolutionServices, []); |
| 100 | + |
| 101 | + ProjectFactory.ApplyChangeToWorkspace(workspace => workspace.OnProjectAdded(projectInfo)); |
| 102 | + |
| 103 | + // https://github.com/dotnet/roslyn/pull/78267 |
| 104 | + // Work around an issue where opening a Razor file in the misc workspace causes a crash. |
| 105 | + if (languageInformation.LanguageName == LanguageInfoProvider.RazorLanguageName) |
| 106 | + { |
| 107 | + var docId = projectInfo.AdditionalDocuments.Single().Id; |
| 108 | + return workspace.CurrentSolution.GetRequiredAdditionalDocument(docId); |
| 109 | + } |
| 110 | + |
| 111 | + var id = projectInfo.Documents.Single().Id; |
| 112 | + return workspace.CurrentSolution.GetRequiredDocument(id); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public async ValueTask TryRemoveMiscellaneousDocumentAsync(DocumentUri uri, bool removeFromMetadataWorkspace) |
| 117 | + { |
| 118 | + var documentPath = GetDocumentFilePath(uri); |
| 119 | + if (removeFromMetadataWorkspace && _metadataAsSourceFileService.TryRemoveDocumentFromWorkspace(documentPath)) |
| 120 | + { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + await UnloadProjectAsync(documentPath); |
| 125 | + } |
| 126 | + |
| 127 | + protected override async Task<(RemoteProjectFile projectFile, bool hasAllInformation, BuildHostProcessKind preferred, BuildHostProcessKind actual)?> TryLoadProjectInMSBuildHostAsync( |
| 128 | + BuildHostProcessManager buildHostProcessManager, string documentPath, CancellationToken cancellationToken) |
| 129 | + { |
| 130 | + const BuildHostProcessKind buildHostKind = BuildHostProcessKind.NetCore; |
| 131 | + var buildHost = await buildHostProcessManager.GetBuildHostAsync(buildHostKind, cancellationToken); |
| 132 | + |
| 133 | + var loader = ProjectFactory.CreateFileTextLoader(documentPath); |
| 134 | + var textAndVersion = await loader.LoadTextAsync(new LoadTextOptions(SourceHashAlgorithms.Default), cancellationToken); |
| 135 | + var (virtualProjectContent, isFileBasedProgram) = VirtualCSharpFileBasedProgramProject.MakeVirtualProjectContent(documentPath, textAndVersion.Text); |
| 136 | + |
| 137 | + // When loading a virtual project, the path to the on-disk source file is not used. Instead the path is adjusted to end with .csproj. |
| 138 | + // This is necessary in order to get msbuild to apply the standard c# props/targets to the project. |
| 139 | + var virtualProjectPath = VirtualCSharpFileBasedProgramProject.GetVirtualProjectPath(documentPath); |
| 140 | + var loadedFile = await buildHost.LoadProjectAsync(virtualProjectPath, virtualProjectContent, languageName: LanguageNames.CSharp, cancellationToken); |
| 141 | + return (loadedFile, hasAllInformation: isFileBasedProgram, preferred: buildHostKind, actual: buildHostKind); |
| 142 | + } |
| 143 | +} |
0 commit comments