-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Allow dotnet-watch to use MSBuildProjectLoader without launching out-of-process build hosts #80359
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
Open
DustinCampbell
wants to merge
6
commits into
dotnet:main
Choose a base branch
from
DustinCampbell:msbuild-project-loader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f8402c7
Refactor PathResolver in MSBuildWorkspace
DustinCampbell ef47b0a
Make some MSBuildProjectLoader.Worker fields parameters to LoadAsync
DustinCampbell 1b9413e
Introduce ProjectLoadOperationRunner helper struct
DustinCampbell 24c087e
Extract BuildHostProjectFileInfoLoader from MSBuildProjectLoader.Worker
DustinCampbell 705b691
Introduce IProjectFileInfoLoaderFactory abstraction
DustinCampbell 56b71a0
Add external access API for dotnet-watch to MS.CA.Workspaces.MSBuild
DustinCampbell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/Workspaces/MSBuild/Core/ExternalAccess/Watch/Api/IWatchProjectFileInfoLoader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.MSBuild.ExternalAccess.Watch.Api; | ||
|
|
||
| internal interface IWatchProjectFileInfoLoader | ||
| { | ||
| Task<LoadProjectFileInfosResult> LoadProjectFileInfosAsync( | ||
| string projectFilePath, | ||
| string languageName, | ||
| CancellationToken cancellationToken); | ||
|
|
||
| Task<string?> TryGetProjectOutputPathAsync(string projectFilePath, CancellationToken cancellationToken); | ||
| } | ||
|
|
||
| internal readonly record struct LoadProjectFileInfosResult( | ||
| ImmutableArray<WatchProjectFileInfo> ProjectFileInfos, | ||
| ImmutableArray<WatchDiagnosticLogItem> DiagnosticItems); | ||
12 changes: 12 additions & 0 deletions
12
src/Workspaces/MSBuild/Core/ExternalAccess/Watch/Api/IWatchProjectFileInfoLoaderFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Collections.Immutable; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.MSBuild.ExternalAccess.Watch.Api; | ||
|
|
||
| internal interface IWatchProjectFileInfoLoaderFactory | ||
| { | ||
| IWatchProjectFileInfoLoader Create(ImmutableDictionary<string, string> properties); | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/Workspaces/MSBuild/Core/ExternalAccess/Watch/Api/WatchDiagnosticLogItem.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| namespace Microsoft.CodeAnalysis.MSBuild.ExternalAccess.Watch.Api; | ||
|
|
||
| internal readonly struct WatchDiagnosticLogItem | ||
| { | ||
| internal DiagnosticLogItem UnderlyingObject { get; } | ||
|
|
||
| internal WatchDiagnosticLogItem(DiagnosticLogItem underlyingObject) | ||
| { | ||
| UnderlyingObject = underlyingObject; | ||
| } | ||
|
|
||
| public WatchDiagnosticLogItem(WatchDiagnosticLogItemKind kind, string message, string projectFilePath) | ||
| : this(new DiagnosticLogItem((DiagnosticLogItemKind)kind, message, projectFilePath)) | ||
| { | ||
| } | ||
|
|
||
| public WatchDiagnosticLogItemKind Kind => (WatchDiagnosticLogItemKind)UnderlyingObject.Kind; | ||
|
|
||
| public string Message => UnderlyingObject.Message; | ||
|
|
||
| public string ProjectFilePath => UnderlyingObject.ProjectFilePath; | ||
|
|
||
| public override string ToString() => Message; | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/Workspaces/MSBuild/Core/ExternalAccess/Watch/Api/WatchDiagnosticLogItemKind.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| namespace Microsoft.CodeAnalysis.MSBuild.ExternalAccess.Watch.Api; | ||
|
|
||
| internal enum WatchDiagnosticLogItemKind | ||
| { | ||
| Error = DiagnosticLogItemKind.Error, | ||
| Warning = DiagnosticLogItemKind.Warning, | ||
| } |
53 changes: 53 additions & 0 deletions
53
src/Workspaces/MSBuild/Core/ExternalAccess/Watch/Api/WatchDocumentFileInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Collections.Immutable; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.MSBuild.ExternalAccess.Watch.Api; | ||
|
|
||
| /// <summary> | ||
| /// Represents a source file that is part of a project file. | ||
| /// </summary> | ||
| internal readonly struct WatchDocumentFileInfo | ||
| { | ||
| internal DocumentFileInfo UnderlyingObject { get; } | ||
|
|
||
| internal WatchDocumentFileInfo(DocumentFileInfo underlyingObject) | ||
| { | ||
| UnderlyingObject = underlyingObject; | ||
| } | ||
|
|
||
| public WatchDocumentFileInfo(string filePath, string logicalPath, bool isLinked, bool isGenerated, ImmutableArray<string> folders) | ||
| : this(new(filePath, logicalPath, isLinked, isGenerated, folders)) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The absolute path to the document file on disk. | ||
| /// </summary> | ||
| public string FilePath => UnderlyingObject.FilePath; | ||
|
|
||
| /// <summary> | ||
| /// A fictional path to the document, relative to the project. | ||
| /// The document may not actually exist at this location, and is used | ||
| /// to represent linked documents. This includes the file name. | ||
| /// </summary> | ||
| public string LogicalPath => UnderlyingObject.LogicalPath; | ||
|
|
||
| /// <summary> | ||
| /// True if the document has a logical path that differs from its | ||
| /// absolute file path. | ||
| /// </summary> | ||
| public bool IsLinked => UnderlyingObject.IsLinked; | ||
|
|
||
| /// <summary> | ||
| /// True if the file was generated during build. | ||
| /// </summary> | ||
| public bool IsGenerated => UnderlyingObject.IsGenerated; | ||
|
|
||
| /// <summary> | ||
| /// Containing folders of the document relative to the containing project root path. | ||
| /// </summary> | ||
| public ImmutableArray<string> Folders => UnderlyingObject.Folders; | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/Workspaces/MSBuild/Core/ExternalAccess/Watch/Api/WatchFileGlobs.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Collections.Immutable; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.MSBuild.ExternalAccess.Watch.Api; | ||
|
|
||
| internal readonly struct WatchFileGlobs | ||
| { | ||
| internal FileGlobs UnderlyingObject { get; } | ||
|
|
||
| internal WatchFileGlobs(FileGlobs underlyingObject) | ||
| { | ||
| UnderlyingObject = underlyingObject; | ||
| } | ||
|
|
||
| public WatchFileGlobs(ImmutableArray<string> includes, ImmutableArray<string> excludes, ImmutableArray<string> removes) | ||
| : this(new(includes, excludes, removes)) | ||
| { | ||
|
|
||
| } | ||
|
|
||
| public ImmutableArray<string> Includes => UnderlyingObject.Includes; | ||
| public ImmutableArray<string> Excludes => UnderlyingObject.Excludes; | ||
| public ImmutableArray<string> Removes => UnderlyingObject.Removes; | ||
| } |
71 changes: 71 additions & 0 deletions
71
src/Workspaces/MSBuild/Core/ExternalAccess/Watch/Api/WatchMSBuildProjectLoader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Collections.Immutable; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.MSBuild.ExternalAccess.Watch.Api; | ||
|
|
||
| internal static class WatchMSBuildProjectLoader | ||
| { | ||
| public static MSBuildProjectLoader Create( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove this overload. |
||
| Workspace workspace, | ||
| IWatchProjectFileInfoLoaderFactory loaderFactory) | ||
| => Create(workspace, properties: null, loaderFactory); | ||
|
|
||
| public static MSBuildProjectLoader Create( | ||
| Workspace workspace, | ||
| ImmutableDictionary<string, string>? properties, | ||
| IWatchProjectFileInfoLoaderFactory loaderFactory) | ||
| => new(workspace, properties, new WatchProjectFileInfoLoaderFactory(loaderFactory)); | ||
|
|
||
| private sealed class WatchProjectFileInfoLoaderFactory(IWatchProjectFileInfoLoaderFactory loaderFactory) : IProjectFileInfoLoaderFactory | ||
| { | ||
| public ProjectFileInfoLoader Create( | ||
| ImmutableDictionary<string, string> properties, | ||
| ProjectFileExtensionRegistry projectFileExtensionRegistry, | ||
| ProjectLoadOperationRunner operationRunner, | ||
| DiagnosticReporter diagnosticReporter, | ||
| ILoggerFactory loggerFactory) | ||
| => new WatchProjectFileInfoLoader(diagnosticReporter, projectFileExtensionRegistry, operationRunner, loaderFactory.Create(properties)); | ||
| } | ||
|
|
||
| private sealed class WatchProjectFileInfoLoader( | ||
| DiagnosticReporter diagnosticReporter, | ||
| ProjectFileExtensionRegistry projectFileExtensionRegistry, | ||
| ProjectLoadOperationRunner operationRunner, | ||
| IWatchProjectFileInfoLoader loader) | ||
| : ProjectFileInfoLoader(diagnosticReporter, projectFileExtensionRegistry, operationRunner) | ||
| { | ||
| private readonly IWatchProjectFileInfoLoader _loader = loader; | ||
|
|
||
| public override ValueTask DisposeAsync() | ||
| => (_loader as IAsyncDisposable)?.DisposeAsync() ?? default; | ||
|
|
||
| public override async Task<ImmutableArray<ProjectFileInfo>> LoadProjectFileInfosAsync( | ||
| string projectFilePath, | ||
| DiagnosticReportingOptions reportingOptions, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| if (!TryGetLanguageNameFromProjectPath(projectFilePath, reportingOptions, out var languageName)) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| var result = await _loader | ||
| .LoadProjectFileInfosAsync(projectFilePath, languageName, cancellationToken) | ||
| .ConfigureAwait(false); | ||
|
|
||
| ReportDiagnostics(result.DiagnosticItems.SelectAsArray(static x => x.UnderlyingObject)); | ||
|
|
||
| return result.ProjectFileInfos.SelectAsArray(static x => x.UnderlyingObject); | ||
| } | ||
|
|
||
| public override Task<string?> TryGetProjectOutputPathAsync(string projectFilePath, CancellationToken cancellationToken) | ||
| => _loader.TryGetProjectOutputPathAsync(projectFilePath, cancellationToken); | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
src/Workspaces/MSBuild/Core/ExternalAccess/Watch/Api/WatchPackageReference.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| namespace Microsoft.CodeAnalysis.MSBuild.ExternalAccess.Watch.Api; | ||
|
|
||
| internal readonly record struct WatchPackageReference | ||
| { | ||
| internal PackageReference UnderlyingObject { get; } | ||
|
|
||
| internal WatchPackageReference(PackageReference underlyingObject) | ||
| { | ||
| UnderlyingObject = underlyingObject; | ||
| } | ||
|
|
||
| public WatchPackageReference(string name, string versionRange) | ||
| : this(new(name, versionRange)) | ||
| { | ||
| } | ||
|
|
||
| public string Name => UnderlyingObject.Name; | ||
| public string VersionRange => UnderlyingObject.VersionRange; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't language name implied by project file path?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but there's a
ProjectFileExtensionRegistrythat gets used in MSBuild workspace. I'd prefer to leave that as an implementation detail and not expose it via the external access. So, by the time this method is called, the language name has already been retrieved from the project path.