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

Watch project.assets.json files for nuget restore #70602

Merged
merged 2 commits into from
Oct 27, 2023
Merged
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
Expand Up @@ -29,6 +29,7 @@ internal sealed class LoadedProject : IDisposable
/// The most recent version of the project design time build information; held onto so the next reload we can diff against this.
/// </summary>
private ProjectFileInfo? _mostRecentFileInfo;
private IWatchedFile? _mostRecentProjectAssetsFileWatcher;
private ImmutableArray<CommandLineReference> _mostRecentMetadataReferences = ImmutableArray<CommandLineReference>.Empty;
private ImmutableArray<CommandLineAnalyzerReference> _mostRecentAnalyzerReferences = ImmutableArray<CommandLineAnalyzerReference>.Empty;

Expand Down Expand Up @@ -182,6 +183,8 @@ public void Dispose()
document => _projectSystemProject.RemoveDynamicSourceFile(document.FilePath),
"Project {0} now has {1} dynamic file(s).");

WatchProjectAssetsFile(newProjectInfo, _fileChangeContext);

_mostRecentFileInfo = newProjectInfo;

Contract.ThrowIfNull(_projectSystemProject.CompilationOptions, "Compilation options cannot be null for C#/VB project");
Expand Down Expand Up @@ -216,6 +219,26 @@ void UpdateProjectSystemProjectCollection<T>(IEnumerable<T> loadedCollection, IE
if (newItems.Count != oldItemsCount)
logger.LogTrace(logMessage, projectFullPathWithTargetFramework, newItems.Count);
}

void WatchProjectAssetsFile(ProjectFileInfo currentProjectInfo, IFileChangeContext fileChangeContext)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to only watch the actual msbuild property for ProjectAssetsFile here - I took a look at a bit of the O# and CPS code, and it looked they were only looking at that specific property (not for any project.assets.json file in obj/).

{
if (_mostRecentFileInfo?.ProjectAssetsFilePath == currentProjectInfo.ProjectAssetsFilePath)
{
// The file path hasn't changed, just keep using the same watcher.
return;
}

// Dispose of the last once since we're changing the file we're watching.
_mostRecentProjectAssetsFileWatcher?.Dispose();

IWatchedFile? currentWatcher = null;
if (currentProjectInfo.ProjectAssetsFilePath != null)
{
currentWatcher = fileChangeContext.EnqueueWatchingFile(currentProjectInfo.ProjectAssetsFilePath);
}

_mostRecentProjectAssetsFileWatcher = currentWatcher;
}
}

private static bool TreatAsIsDynamicFile(DocumentFileInfo info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ internal static class PropertyNames
public const string PdbFile = nameof(PdbFile);
public const string PlatformTarget = nameof(PlatformTarget);
public const string Prefer32Bit = nameof(Prefer32Bit);
public const string ProjectAssetsFile = nameof(ProjectAssetsFile);
public const string ProvideCommandLineArgs = nameof(ProvideCommandLineArgs);
public const string RemoveIntegerChecks = nameof(RemoveIntegerChecks);
public const string ResolvedCodeAnalysisRuleSet = nameof(ResolvedCodeAnalysisRuleSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ private ProjectFileInfo CreateProjectFileInfo(MSB.Execution.ProjectInstance proj
intermediateOutputFilePath = GetAbsolutePathRelativeToProject(intermediateOutputFilePath);
}

var projectAssetsFilePath = project.ReadPropertyString(PropertyNames.ProjectAssetsFile);

// Right now VB doesn't have the concept of "default namespace". But we conjure one in workspace
// by assigning the value of the project's root namespace to it. So various feature can choose to
// use it for their own purpose.
Expand Down Expand Up @@ -174,6 +176,7 @@ private ProjectFileInfo CreateProjectFileInfo(MSB.Execution.ProjectInstance proj
defaultNamespace,
targetFramework,
targetFrameworkIdentifier,
projectAssetsFilePath,
commandLineArgs,
docs,
additionalDocs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ internal sealed class ProjectFileInfo
[DataMember(Order = 16)]
public bool IsSdkStyle { get; }

/// <summary>
/// The path to the project.assets.json path in obj/.
/// </summary>
[DataMember(Order = 17)]
public string? ProjectAssetsFilePath { get; }

public override string ToString()
=> RoslynString.IsNullOrWhiteSpace(TargetFramework)
? FilePath ?? string.Empty
Expand All @@ -142,6 +148,7 @@ public ProjectFileInfo(
string? defaultNamespace,
string? targetFramework,
string? targetFrameworkIdentifier,
string? projectAssetsFilePath,
ImmutableArray<string> commandLineArgs,
ImmutableArray<DocumentFileInfo> documents,
ImmutableArray<DocumentFileInfo> additionalDocuments,
Expand All @@ -162,6 +169,7 @@ public ProjectFileInfo(
this.DefaultNamespace = defaultNamespace;
this.TargetFramework = targetFramework;
this.TargetFrameworkIdentifier = targetFrameworkIdentifier;
this.ProjectAssetsFilePath = projectAssetsFilePath;
this.CommandLineArgs = commandLineArgs;
this.Documents = documents;
this.AdditionalDocuments = additionalDocuments;
Expand All @@ -181,6 +189,7 @@ public static ProjectFileInfo Create(
string? defaultNamespace,
string? targetFramework,
string? targetFrameworkIdentifier,
string? projectAssetsFilePath,
ImmutableArray<string> commandLineArgs,
ImmutableArray<DocumentFileInfo> documents,
ImmutableArray<DocumentFileInfo> additionalDocuments,
Expand All @@ -199,6 +208,7 @@ public static ProjectFileInfo Create(
defaultNamespace,
targetFramework,
targetFrameworkIdentifier,
projectAssetsFilePath,
commandLineArgs,
documents,
additionalDocuments,
Expand All @@ -219,6 +229,7 @@ public static ProjectFileInfo CreateEmpty(string language, string? filePath)
defaultNamespace: null,
targetFramework: null,
targetFrameworkIdentifier: null,
projectAssetsFilePath: null,
commandLineArgs: ImmutableArray<string>.Empty,
documents: ImmutableArray<DocumentFileInfo>.Empty,
additionalDocuments: ImmutableArray<DocumentFileInfo>.Empty,
Expand Down
Loading