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

Mix the source generator version info into the dependent checksum we get for projects #77273

Merged
merged 1 commit into from
Feb 20, 2025
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
2 changes: 2 additions & 0 deletions src/Workspaces/Core/Portable/Workspace/Solution/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ public Task<VersionStamp> GetSemanticVersionAsync(CancellationToken cancellation
/// <see cref="Project.GetDependentSemanticVersionAsync(CancellationToken)"/> changes as the project is removed, then added resulting in a version change.</item>
/// </list>
/// </para>
/// This checksum is also affected by the <see cref="SourceGeneratorExecutionVersion"/> for this project.
/// As such, it is not usable across different sessions of a particular host.
/// </remarks>
internal Task<Checksum> GetDependentChecksumAsync(CancellationToken cancellationToken)
=> Solution.CompilationState.GetDependentChecksumAsync(this.Id, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,40 +1096,50 @@ public Task<Checksum> GetDependentChecksumAsync(
Interlocked.CompareExchange(
ref _lazyDependentChecksum,
AsyncLazy.Create(static (arg, c) =>
arg.self.ComputeDependentChecksumAsync(arg.SolutionState, c),
arg: (self: this, compilationState.SolutionState)),
arg.self.ComputeDependentChecksumAsync(arg.compilationState, c),
arg: (self: this, compilationState)),
null);
}

return _lazyDependentChecksum.GetValueAsync(cancellationToken);
}

private async Task<Checksum> ComputeDependentChecksumAsync(SolutionState solution, CancellationToken cancellationToken)
private async Task<Checksum> ComputeDependentChecksumAsync(
SolutionCompilationState solution, CancellationToken cancellationToken)
{
using var _ = ArrayBuilder<Checksum>.GetInstance(out var tempChecksumArray);

// Mix in the SG information for this project. That way if it changes, we will have a different
// checksum (since semantics could have changed because of this).
if (solution.SourceGeneratorExecutionVersionMap.Map.TryGetValue(this.ProjectState.Id, out var executionVersion))
tempChecksumArray.Add(executionVersion.Checksum);

// Get the checksum for the project itself.
var projectChecksum = await this.ProjectState.GetChecksumAsync(cancellationToken).ConfigureAwait(false);
tempChecksumArray.Add(projectChecksum);

// Calculate a checksum this project and for each dependent project that could affect semantics for
// this project. Ensure that the checksum calculation orders the projects consistently so that we get
// the same checksum across sessions of VS. Note: we use the project filepath+name as a unique way
// to reference a project. This matches the logic in our persistence-service implemention as to how
// information is associated with a project.
var transitiveDependencies = solution.GetProjectDependencyGraph().GetProjectsThatThisProjectTransitivelyDependsOn(this.ProjectState.Id);
// Calculate a checksum this project and for each dependent project that could affect semantics for this
// project. We order the projects so that we are resilient to the underlying in-memory graph structure
// changing this arbitrarily. We do not want that to cause us to change our semantic version.. Note: we
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// changing this arbitrarily. We do not want that to cause us to change our semantic version.. Note: we
// changing this arbitrarily. We do not want that to cause us to change our semantic version. Note: we

// use the project filepath+name as a unique way to reference a project. This matches the logic in our
// persistence-service implementation as to how information is associated with a project.
var transitiveDependencies = solution.SolutionState.GetProjectDependencyGraph().GetProjectsThatThisProjectTransitivelyDependsOn(this.ProjectState.Id);
Copy link
Member

Choose a reason for hiding this comment

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

Asking a general question which doesn't need to impact this PR:

Is there a reason this gets the full transitive list versus just asking the immediate project references and mixing in their checksums, which would themselves depend on their immediate project references...etc? Are we going to be generating extra dependency graph entries here even though we don't need to?

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 have a followup pr that does that :)

var orderedProjectIds = transitiveDependencies.OrderBy(id =>
Copy link
Member

Choose a reason for hiding this comment

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

Asking a general question which doesn't need to impact this PR:

Why do we sort this way versus just sorting by GUID or something?

Copy link
Member Author

Choose a reason for hiding this comment

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

that would work as well!

{
var depProject = solution.GetRequiredProjectState(id);
var depProject = solution.SolutionState.GetRequiredProjectState(id);
return (depProject.FilePath, depProject.Name);
});

foreach (var projectId in orderedProjectIds)
{
var referencedProject = solution.GetRequiredProjectState(projectId);
// Mix in the SG information for the dependent project. That way if it changes, we will have a
// different checksum (since semantics could have changed because of this).
if (solution.SourceGeneratorExecutionVersionMap.Map.TryGetValue(projectId, out executionVersion))
tempChecksumArray.Add(executionVersion.Checksum);

// Note that these checksums should only actually be calculated once, if the project is unchanged
// the same checksum will be returned.
var referencedProject = solution.SolutionState.GetRequiredProjectState(projectId);
var referencedProjectChecksum = await referencedProject.GetChecksumAsync(cancellationToken).ConfigureAwait(false);
tempChecksumArray.Add(referencedProjectChecksum);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static SourceGeneratorExecutionVersion ReadFrom(ObjectReader reader)

public override string ToString()
=> $"{MajorVersion}.{MinorVersion}";

public Checksum Checksum => new(MajorVersion, MinorVersion);
}

/// <summary>
Expand Down
Loading