Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

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?

Copy link
Member Author

@DustinCampbell DustinCampbell Sep 19, 2025

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 ProjectFileExtensionRegistry that 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.

CancellationToken cancellationToken);

Task<string?> TryGetProjectOutputPathAsync(string projectFilePath, CancellationToken cancellationToken);
}

internal readonly record struct LoadProjectFileInfosResult(
ImmutableArray<WatchProjectFileInfo> ProjectFileInfos,
ImmutableArray<WatchDiagnosticLogItem> DiagnosticItems);
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);
}
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;
}
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,
}
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;
}
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;
}
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(
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
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;
}
Loading
Loading