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

Use message pack for project.razor.* configuration file #9270

Merged
merged 4 commits into from
Sep 12, 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 @@ -9,7 +9,6 @@
using Microsoft.AspNetCore.Razor.ProjectEngineHost;
using Microsoft.AspNetCore.Razor.ProjectSystem;
using Microsoft.AspNetCore.Razor.Serialization;
using Microsoft.AspNetCore.Razor.Serialization.Json;
using Microsoft.AspNetCore.Razor.Telemetry;
using Microsoft.AspNetCore.Razor.Utilities;
using Microsoft.CodeAnalysis;
Expand All @@ -35,7 +34,7 @@ static RazorProjectInfoSerializer()
s_projectEngineFactories = ProjectEngineFactories.Factories.Select(f => (f.Item1.Value, f.Item2)).ToArray();
}

public static async Task SerializeAsync(Project project, string projectRazorJsonFileName, CancellationToken cancellationToken)
public static async Task SerializeAsync(Project project, string configurationFileName, CancellationToken cancellationToken)
{
var projectPath = Path.GetDirectoryName(project.FilePath);
if (projectPath is null)
Expand Down Expand Up @@ -88,17 +87,17 @@ public static async Task SerializeAsync(Project project, string projectRazorJson

var projectWorkspaceState = new ProjectWorkspaceState(tagHelpers, csharpLanguageVersion);

var jsonFilePath = Path.Combine(intermediateOutputPath, projectRazorJsonFileName);
var configurationFilePath = Path.Combine(intermediateOutputPath, configurationFileName);

var projectInfo = new RazorProjectInfo(
serializedFilePath: jsonFilePath,
serializedFilePath: configurationFilePath,
filePath: project.FilePath!,
configuration: configuration,
rootNamespace: defaultNamespace,
projectWorkspaceState: projectWorkspaceState,
documents: documents);

WriteJsonFile(jsonFilePath, projectInfo);
WriteToFile(configurationFilePath, projectInfo);
}

private static RazorConfiguration ComputeRazorConfigurationOptions(AnalyzerConfigOptionsProvider options, out string defaultNamespace)
Expand Down Expand Up @@ -126,11 +125,11 @@ private static RazorConfiguration ComputeRazorConfigurationOptions(AnalyzerConfi
return razorConfiguration;
}

private static void WriteJsonFile(string publishFilePath, RazorProjectInfo projectInfo)
private static void WriteToFile(string configurationFilePath, RazorProjectInfo projectInfo)
{
// We need to avoid having an incomplete file at any point, but our
// project configuration is large enough that it will be written as multiple operations.
var tempFilePath = string.Concat(publishFilePath, ".temp");
var tempFilePath = string.Concat(configurationFilePath, ".temp");
var tempFileInfo = new FileInfo(tempFilePath);

if (tempFileInfo.Exists)
Expand All @@ -141,18 +140,18 @@ private static void WriteJsonFile(string publishFilePath, RazorProjectInfo proje

// This needs to be in explicit brackets because the operation needs to be completed
// by the time we move the temp file into its place
using (var writer = tempFileInfo.CreateText())
using (var stream = tempFileInfo.Create())
{
JsonDataConvert.SerializeObject(writer, projectInfo, ObjectWriters.WriteProperties);
projectInfo.SerializeTo(stream);
}

var fileInfo = new FileInfo(publishFilePath);
var fileInfo = new FileInfo(configurationFilePath);
if (fileInfo.Exists)
{
fileInfo.Delete();
}

File.Move(tempFileInfo.FullName, publishFilePath);
File.Move(tempFileInfo.FullName, configurationFilePath);
}

private static ImmutableArray<DocumentSnapshotHandle> GetDocuments(Project project, string projectPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static class LanguageServerConstants
{
public const int VSCompletionItemKindOffset = 118115;

public const string DefaultProjectConfigurationFile = "project.razor.json";
public const string DefaultProjectConfigurationFile = "project.razor.bin";

public const string RazorSemanticTokensLegendEndpoint = "_vs_/textDocument/semanticTokensLegend";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public override void RemoveDocument(string filePath)

// If the document is open, we can't remove it, because we could still get a request for it, and that
// request would fail. Instead we move it to the miscellaneous project, just like if we got notified of
// a remove via the project.razor.json
// a remove via the project.razor.bin
if (_projectSnapshotManagerAccessor.Instance.IsDocumentOpen(textDocumentPath))
{
_logger.LogInformation("Moving document '{textDocumentPath}' from project '{projectKey}' to misc files because it is open.", textDocumentPath, projectSnapshot.Key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.IO;
using Microsoft.AspNetCore.Razor.ProjectSystem;
using Microsoft.AspNetCore.Razor.Serialization.Json;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Serialization;

Expand All @@ -18,11 +17,10 @@ private RazorProjectInfoDeserializer()
public RazorProjectInfo? DeserializeFromFile(string filePath)
{
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using var reader = new StreamReader(stream);

try
{
return JsonDataConvert.DeserializeObject(reader, ObjectReaders.ReadProjectInfoFromProperties);
return RazorProjectInfo.DeserializeFrom(stream);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Immutable;
using System.IO;
using MessagePack;
using MessagePack.Resolvers;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Serialization;
using Microsoft.AspNetCore.Razor.Serialization.MessagePack.Resolvers;
using Microsoft.CodeAnalysis;

namespace Microsoft.AspNetCore.Razor.ProjectSystem;

internal sealed class RazorProjectInfo
{
private static readonly MessagePackSerializerOptions s_options = MessagePackSerializerOptions.Standard
.WithResolver(CompositeResolver.Create(
RazorProjectInfoResolver.Instance,
StandardResolver.Instance));

public string SerializedFilePath { get; }
public string FilePath { get; }
public RazorConfiguration? Configuration { get; }
Expand All @@ -31,4 +41,10 @@ public RazorProjectInfo(
ProjectWorkspaceState = projectWorkspaceState;
Documents = documents.NullToEmpty();
}

public void SerializeTo(Stream stream)
=> MessagePackSerializer.Serialize(stream, this, s_options);

public static RazorProjectInfo? DeserializeFrom(Stream stream)
=> MessagePackSerializer.Deserialize<RazorProjectInfo>(stream, s_options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private CachedStringFormatter()
{
}

protected override string? Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
public override string? Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
{
if (reader.NextMessagePackType == MessagePackType.Integer)
{
Expand All @@ -31,7 +31,7 @@ private CachedStringFormatter()
return result;
}

protected override void Serialize(ref MessagePackWriter writer, string? value, SerializerCachingOptions options)
public override void Serialize(ref MessagePackWriter writer, string? value, SerializerCachingOptions options)
{
if (value is null)
{
Expand All @@ -47,4 +47,20 @@ protected override void Serialize(ref MessagePackWriter writer, string? value, S
options.Strings.Add(value);
}
}

public override void Skim(ref MessagePackReader reader, SerializerCachingOptions options)
{
if (reader.NextMessagePackType == MessagePackType.Integer)
{
reader.Skip(); // Reference Id
return;
}

var result = reader.ReadString();

if (result is not null)
{
options.Strings.Add(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private DocumentSnapshotHandleFormatter()
{
}

protected override DocumentSnapshotHandle Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
public override DocumentSnapshotHandle Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
{
reader.ReadArrayHeaderAndVerify(3);

Expand All @@ -24,7 +24,7 @@ protected override DocumentSnapshotHandle Deserialize(ref MessagePackReader read
return new DocumentSnapshotHandle(filePath, targetPath, fileKind);
}

protected override void Serialize(ref MessagePackWriter writer, DocumentSnapshotHandle value, SerializerCachingOptions options)
public override void Serialize(ref MessagePackWriter writer, DocumentSnapshotHandle value, SerializerCachingOptions options)
{
writer.WriteArrayHeader(3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal static class ExtraExtensions
{
// C# allows extension method overloads to differ only by generic constraints, but they must be declared on
// different classes, since they'll have the same signature.
public static void SerializeObject<T>(this ref MessagePackWriter writer, T value, MessagePackSerializerOptions options)
public static void Serialize<T>(this ref MessagePackWriter writer, T value, MessagePackSerializerOptions options)
where T : struct
{
options.Resolver.GetFormatterWithVerify<T>().Serialize(ref writer, value, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ private FetchTagHelpersResultFormatter()
{
}

protected override FetchTagHelpersResult Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
public override FetchTagHelpersResult Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
{
var tagHelpers = reader.Deserialize<ImmutableArray<TagHelperDescriptor>>(options);

return new(tagHelpers);
}

protected override void Serialize(ref MessagePackWriter writer, FetchTagHelpersResult value, SerializerCachingOptions options)
public override void Serialize(ref MessagePackWriter writer, FetchTagHelpersResult value, SerializerCachingOptions options)
{
writer.SerializeObject(value.TagHelpers, options);
writer.Serialize(value.TagHelpers, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private ProjectSnapshotHandleFormatter()
{
}

protected override ProjectSnapshotHandle Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
public override ProjectSnapshotHandle Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
{
reader.ReadArrayHeaderAndVerify(3);

Expand All @@ -29,7 +29,7 @@ protected override ProjectSnapshotHandle Deserialize(ref MessagePackReader reade
return new(projectId, configuration, rootNamespace);
}

protected override void Serialize(ref MessagePackWriter writer, ProjectSnapshotHandle value, SerializerCachingOptions options)
public override void Serialize(ref MessagePackWriter writer, ProjectSnapshotHandle value, SerializerCachingOptions options)
{
writer.WriteArrayHeader(3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
using System.Collections.Immutable;
using MessagePack;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.ProjectSystem;
using Microsoft.AspNetCore.Razor.Serialization.MessagePack.Formatters.TagHelpers;
using Microsoft.AspNetCore.Razor.Utilities;
using Microsoft.CodeAnalysis.CSharp;
using Checksum = Microsoft.AspNetCore.Razor.Utilities.Checksum;

namespace Microsoft.AspNetCore.Razor.Serialization.MessagePack.Formatters;

Expand All @@ -17,21 +21,46 @@ private ProjectWorkspaceStateFormatter()
{
}

protected override ProjectWorkspaceState Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
public override ProjectWorkspaceState Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
{
reader.ReadArrayHeaderAndVerify(2);
reader.ReadArrayHeaderAndVerify(3);

var tagHelpers = reader.Deserialize<ImmutableArray<TagHelperDescriptor>>(options);
var checksums = reader.Deserialize<ImmutableArray<Checksum>>(options);

reader.ReadArrayHeaderAndVerify(checksums.Length);

using var builder = new PooledArrayBuilder<TagHelperDescriptor>(capacity: checksums.Length);
var cache = TagHelperCache.Default;

foreach (var checksum in checksums)
{
if (!cache.TryGet(checksum, out var tagHelper))
{
tagHelper = TagHelperFormatter.Instance.Deserialize(ref reader, options);
cache.TryAdd(checksum, tagHelper);
}
else
{
TagHelperFormatter.Instance.Skim(ref reader, options);
}

builder.Add(tagHelper);
}

var tagHelpers = builder.DrainToImmutable();
var csharpLanguageVersion = (LanguageVersion)reader.ReadInt32();

return new ProjectWorkspaceState(tagHelpers, csharpLanguageVersion);
}

protected override void Serialize(ref MessagePackWriter writer, ProjectWorkspaceState value, SerializerCachingOptions options)
public override void Serialize(ref MessagePackWriter writer, ProjectWorkspaceState value, SerializerCachingOptions options)
{
writer.WriteArrayHeader(2);
writer.WriteArrayHeader(3);

var checksums = value.TagHelpers.SelectAsArray(x => x.GetChecksum());

writer.SerializeObject(value.TagHelpers, options);
writer.Serialize(checksums, options);
writer.Serialize(value.TagHelpers, options);
writer.Write((int)value.CSharpLanguageVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private RazorConfigurationFormatter()
{
}

protected override RazorConfiguration Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
public override RazorConfiguration Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
{
var count = reader.ReadArrayHeader();

Expand All @@ -41,7 +41,7 @@ protected override RazorConfiguration Deserialize(ref MessagePackReader reader,
return RazorConfiguration.Create(languageVersion, configurationName, extensions);
}

protected override void Serialize(ref MessagePackWriter writer, RazorConfiguration value, SerializerCachingOptions options)
public override void Serialize(ref MessagePackWriter writer, RazorConfiguration value, SerializerCachingOptions options)
{
// Write two values + one value per extension.
var extensions = value.Extensions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private RazorDiagnosticFormatter()
{
}

protected override RazorDiagnostic Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
public override RazorDiagnostic Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
{
reader.ReadArrayHeaderAndVerify(8);

Expand All @@ -41,7 +41,7 @@ static Func<string> MessageFormat(string message)
}
}

protected override void Serialize(ref MessagePackWriter writer, RazorDiagnostic value, SerializerCachingOptions options)
public override void Serialize(ref MessagePackWriter writer, RazorDiagnostic value, SerializerCachingOptions options)
{
writer.WriteArrayHeader(8);

Expand All @@ -56,4 +56,19 @@ protected override void Serialize(ref MessagePackWriter writer, RazorDiagnostic
writer.Write(span.CharacterIndex);
writer.Write(span.Length);
}

public override void Skim(ref MessagePackReader reader, SerializerCachingOptions options)
{
reader.ReadArrayHeaderAndVerify(8);

CachedStringFormatter.Instance.Skim(ref reader, options); // Id
reader.Skip(); // Severity
CachedStringFormatter.Instance.Skim(ref reader, options); // Message

CachedStringFormatter.Instance.Skim(ref reader, options); // FilePath
reader.Skip(); // AbsoluteIndex
reader.Skip(); // LineIndex
reader.Skip(); // CharacterIndex
reader.Skip(); // Length
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private RazorProjectInfoFormatter()
{
}

protected override RazorProjectInfo Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
public override RazorProjectInfo Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
{
reader.ReadArrayHeaderAndVerify(7);

Expand All @@ -37,7 +37,7 @@ protected override RazorProjectInfo Deserialize(ref MessagePackReader reader, Se
return new RazorProjectInfo(serializedFilePath, filePath, configuration, rootNamespace, projectWorkspaceState, documents);
}

protected override void Serialize(ref MessagePackWriter writer, RazorProjectInfo value, SerializerCachingOptions options)
public override void Serialize(ref MessagePackWriter writer, RazorProjectInfo value, SerializerCachingOptions options)
{
writer.WriteArrayHeader(7);

Expand All @@ -47,6 +47,6 @@ protected override void Serialize(ref MessagePackWriter writer, RazorProjectInfo
writer.Serialize(value.Configuration, options);
writer.Serialize(value.ProjectWorkspaceState, options);
CachedStringFormatter.Instance.Serialize(ref writer, value.RootNamespace, options);
writer.SerializeObject(value.Documents, options);
writer.Serialize(value.Documents, options);
}
}
Loading
Loading