Repository navigation
Stream project.assets.json when checking for unresolved package references #84647
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
Changes from 1 commit
e5a1fa0
5c5e467
b062e52
1f224c3
d93255e
965e389
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| // 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.Text; | ||
| using System.Text.Json; | ||
| using Microsoft.CodeAnalysis.LanguageServer.HostWorkspace; | ||
| using Microsoft.CodeAnalysis.Test.Utilities; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests; | ||
|
|
||
| public sealed class ProjectDependencyHelperTests : IDisposable | ||
| { | ||
| private readonly TempRoot _tempRoot = new(); | ||
|
|
||
| public void Dispose() | ||
| => _tempRoot.Dispose(); | ||
|
|
||
| [Fact] | ||
| public void NeedsRestore_MissingAssetsFile() | ||
| { | ||
| var projectAssetsPath = Path.Combine(_tempRoot.CreateDirectory().Path, "missing.assets.json"); | ||
|
|
||
| Assert.True(NeedsRestore(projectAssetsPath, ("Package", "1.0.0"))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NeedsRestore_NoPackageReferencesDoesNotParseAssetsFile() | ||
| { | ||
| var projectAssetsPath = WriteAssetsFile("not json"); | ||
|
|
||
| Assert.False(NeedsRestore(projectAssetsPath)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NeedsRestore_MalformedAssetsFileThrows() | ||
| { | ||
| var projectAssetsPath = WriteAssetsFile("""{"libraries":{"Package/1.0.0":{}}"""); | ||
|
|
||
| Assert.ThrowsAny<JsonException>(() => NeedsRestore(projectAssetsPath, ("Package", "1.0.0"))); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("Package", "1.0.0", "Package/1.0.0", false)] | ||
| [InlineData("PACKAGE", "1.0.0", "package/1.0.0", false)] | ||
| [InlineData("Package", "[1.0.0]", "Package/1.0.0", false)] | ||
| [InlineData("Package", "[1.0.0,2.0.0)", "Package/1.5.0", false)] | ||
| [InlineData("Package", "(1.0.0,2.0.0)", "Package/1.0.0", true)] | ||
| [InlineData("Package", "[2.0.0]", "Package/1.0.0", true)] | ||
| [InlineData("Package", "not a range", "Package/1.0.0", false)] | ||
| [InlineData("Other", "1.0.0", "Package/1.0.0", true)] | ||
| public void NeedsRestore_PackageNameAndVersion( | ||
| string packageName, | ||
| string versionRange, | ||
| string restoredLibrary, | ||
| bool expectedNeedsRestore) | ||
| { | ||
| var projectAssetsPath = WriteAssetsFile($"{{\"version\":3,\"libraries\":{{\"{restoredLibrary}\":{{\"type\":\"package\"}}}}}}"); | ||
|
|
||
| Assert.Equal(expectedNeedsRestore, NeedsRestore(projectAssetsPath, (packageName, versionRange))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NeedsRestore_UsesTopLevelLibrariesFromRealisticAssetsFile() | ||
| { | ||
| var projectAssetsPath = WriteAssetsFile(""" | ||
| { | ||
| "version": 3, | ||
| "targets": { | ||
| "net10.0": { | ||
| "Misleading.Package/9.0.0": { | ||
| "type": "package", | ||
| "compile": {} | ||
| } | ||
| } | ||
| }, | ||
| "libraries": { | ||
| "Newtonsoft.Json/13.0.3": { | ||
| "sha512": "hash", | ||
| "type": "package", | ||
| "path": "newtonsoft.json/13.0.3", | ||
| "files": [ | ||
| ".nupkg.metadata", | ||
| "lib/net6.0/Newtonsoft.Json.dll" | ||
| ] | ||
| }, | ||
| "Microsoft.CodeAnalysis.Common/5.0.0": { | ||
| "sha512": "hash", | ||
| "type": "package", | ||
| "path": "microsoft.codeanalysis.common/5.0.0", | ||
| "files": [] | ||
| } | ||
| }, | ||
| "projectFileDependencyGroups": { | ||
| "net10.0": [ | ||
| "Newtonsoft.Json >= 13.0.0" | ||
| ] | ||
| }, | ||
| "packageFolders": { | ||
| "C:\\Users\\test\\.nuget\\packages\\": {} | ||
| }, | ||
| "project": { | ||
| "version": "1.0.0", | ||
| "restore": { | ||
| "projectName": "TestProject" | ||
| } | ||
| } | ||
| } | ||
| """); | ||
|
|
||
| Assert.False(NeedsRestore( | ||
| projectAssetsPath, | ||
| ("newtonsoft.json", "[13.0.0,14.0.0)"), | ||
| ("Microsoft.CodeAnalysis.Common", "[5.0.0]"))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NeedsRestore_HandlesLibraryNameAcrossBufferBoundary() | ||
| { | ||
| // Large enough that the library name lands in a later read, but still smaller than the read buffer. | ||
| var padding = new string('x', 12 * 1024); | ||
| var projectAssetsPath = WriteAssetsFile($"{{\"padding\":\"{padding}\",\"libraries\":{{\"Package/1.0.0\":{{}}}}}}"); | ||
|
|
||
| Assert.False(NeedsRestore(projectAssetsPath, ("Package", "1.0.0"))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NeedsRestore_HandlesTokenLargerThanBuffer() | ||
| { | ||
| // A single token too large for the initial buffer, which forces the read buffer to grow. | ||
| var padding = new string('x', 64 * 1024); | ||
| var projectAssetsPath = WriteAssetsFile($"{{\"padding\":\"{padding}\",\"libraries\":{{\"Package/1.0.0\":{{}}}}}}"); | ||
|
|
||
| Assert.False(NeedsRestore(projectAssetsPath, ("Package", "1.0.0"))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NeedsRestore_HandlesLibraryNameLargerThanBuffer() | ||
| { | ||
| // The grown buffer has to preserve the library key itself, not just skip past oversized values. | ||
| var packageName = new string('x', 64 * 1024); | ||
| var projectAssetsPath = WriteAssetsFile($"{{\"libraries\":{{\"{packageName}/1.0.0\":{{}}}}}}"); | ||
|
|
||
| Assert.False(NeedsRestore(projectAssetsPath, (packageName, "1.0.0"))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NeedsRestore_SkipsByteOrderMark() | ||
| { | ||
| var file = _tempRoot.CreateFile(); | ||
| File.WriteAllBytes( | ||
| file.Path, | ||
| [.. Encoding.UTF8.Preamble, .. """{"version":3,"libraries":{"Package/1.0.0":{"type":"package"}}}"""u8]); | ||
|
|
||
| Assert.False(NeedsRestore(file.Path, ("Package", "1.0.0"))); | ||
| } | ||
|
|
||
| private string WriteAssetsFile(string contents) | ||
| { | ||
| var file = _tempRoot.CreateFile(); | ||
| file.WriteAllText(contents); | ||
| return file.Path; | ||
| } | ||
|
|
||
| private static bool NeedsRestore(string projectAssetsPath, params (string Name, string VersionRange)[] packageReferences) | ||
| => ProjectDependencyHelper.TestAccessor.CheckProjectAssetsForUnresolvedDependencies( | ||
| projectAssetsPath, packageReferences, NullLogger.Instance); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| // 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.Buffers; | ||
| using System.Text.Json; | ||
| using NuGet.Versioning; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.LanguageServer.HostWorkspace; | ||
|
|
||
| /// <summary> | ||
| /// Reads the <c>libraries</c> section of a <c>project.assets.json</c> file to discover which packages a | ||
| /// restore has already resolved. Only the library keys are inspected, so the file is streamed through a | ||
| /// small fixed buffer instead of being materialized into a <c>LockFile</c> model. | ||
| /// </summary> | ||
| internal static class ProjectAssetsReader | ||
| { | ||
| /// <summary> | ||
| /// Initial size of the read buffer. It grows if a single JSON token does not fit, which real assets files | ||
| /// never require; the longest token in one is a few hundred bytes. | ||
| /// </summary> | ||
| private const int BufferSize = 16 * 1024; | ||
|
|
||
| /// <summary> | ||
| /// Upper bound on the length of a library key decoded on the stack. Keys are a package id (which NuGet | ||
| /// limits to 100 characters) plus a version, so realistic keys stay well below this. | ||
| /// </summary> | ||
| private const int MaxStackAllocatedKeyLength = 512; | ||
|
|
||
| private static ReadOnlySpan<byte> Utf8Bom => [0xEF, 0xBB, 0xBF]; | ||
|
Check failure on line 30 in src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace/ProjectAssetsReader.cs
|
||
|
|
||
| /// <summary> | ||
| /// Sets the entry in <paramref name="resolvedReferences"/> for each item in | ||
| /// <paramref name="packageReferences"/> that the assets file lists at a version satisfying its range. | ||
| /// Entries are only ever set to <see langword="true"/>, so <paramref name="resolvedReferences"/> must | ||
| /// start cleared. | ||
| /// </summary> | ||
| public static void FindResolvedPackageReferences( | ||
|
dibarbet marked this conversation as resolved.
Resolved
|
||
| string projectAssetsPath, | ||
| ReadOnlySpan<PackageReferenceItem> packageReferences, | ||
| Span<bool> resolvedReferences) | ||
| { | ||
| Contract.ThrowIfFalse(packageReferences.Length == resolvedReferences.Length); | ||
|
|
||
| // A buffer size of 1 disables FileStream's internal buffer; this reader does its own buffering. | ||
| using var stream = new FileStream(projectAssetsPath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1); | ||
| var buffer = ArrayPool<byte>.Shared.Rent(BufferSize); | ||
| try | ||
| { | ||
| var state = new JsonReaderState(); | ||
| var isFinalBlock = false; | ||
|
|
||
| // Utf8JsonReader does not consume a byte order mark, so strip one before parsing begins. | ||
| var bufferedCount = stream.ReadAtLeast(buffer, Utf8Bom.Length, throwOnEndOfStream: false); | ||
| if (buffer.AsSpan(0, bufferedCount).StartsWith(Utf8Bom)) | ||
| { | ||
| bufferedCount -= Utf8Bom.Length; | ||
| buffer.AsSpan(Utf8Bom.Length, bufferedCount).CopyTo(buffer); | ||
| } | ||
|
|
||
| // Depth of the object value of the top level "libraries" property, or -1 while outside of it. | ||
| var librariesDepth = -1; | ||
| var atLibrariesValue = false; | ||
|
|
||
| while (!isFinalBlock) | ||
| { | ||
| if (bufferedCount == buffer.Length) | ||
| { | ||
| // The buffer holds a single incomplete token, so it has to grow for that token to be read. | ||
| var grownBuffer = ArrayPool<byte>.Shared.Rent(checked(buffer.Length * 2)); | ||
| buffer.AsSpan(0, bufferedCount).CopyTo(grownBuffer); | ||
| ArrayPool<byte>.Shared.Return(buffer); | ||
| buffer = grownBuffer; | ||
| } | ||
|
|
||
| var readCount = stream.Read(buffer, bufferedCount, buffer.Length - bufferedCount); | ||
| bufferedCount += readCount; | ||
| isFinalBlock = readCount == 0; | ||
|
|
||
| var reader = new Utf8JsonReader(buffer.AsSpan(0, bufferedCount), isFinalBlock, state); | ||
| while (reader.Read()) | ||
|
dibarbet marked this conversation as resolved.
Resolved
|
||
| { | ||
| if (atLibrariesValue) | ||
| { | ||
| atLibrariesValue = false; | ||
| if (reader.TokenType == JsonTokenType.StartObject) | ||
| librariesDepth = reader.CurrentDepth; | ||
| } | ||
|
dibarbet marked this conversation as resolved.
Resolved
|
||
| else if (librariesDepth >= 0) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.PropertyName && reader.CurrentDepth == librariesDepth + 1) | ||
| MarkResolvedPackageReferences(ref reader, packageReferences, resolvedReferences); | ||
| else if (reader.TokenType == JsonTokenType.EndObject && reader.CurrentDepth == librariesDepth) | ||
| librariesDepth = -1; | ||
| } | ||
| else if (reader.TokenType == JsonTokenType.PropertyName && | ||
| reader.CurrentDepth == 1 && | ||
| reader.ValueTextEquals("libraries")) | ||
| { | ||
| atLibrariesValue = true; | ||
| } | ||
| } | ||
|
|
||
| state = reader.CurrentState; | ||
|
|
||
|
Comment on lines
+131
to
+134
|
||
| // Keep the trailing partial token so the next read can complete it. | ||
| var consumedCount = checked((int)reader.BytesConsumed); | ||
| bufferedCount -= consumedCount; | ||
| buffer.AsSpan(consumedCount, bufferedCount).CopyTo(buffer); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| ArrayPool<byte>.Shared.Return(buffer); | ||
| } | ||
| } | ||
|
|
||
| private static void MarkResolvedPackageReferences( | ||
| ref Utf8JsonReader reader, | ||
| ReadOnlySpan<PackageReferenceItem> packageReferences, | ||
| Span<bool> resolvedReferences) | ||
| { | ||
| // Decoding to UTF-16 gives escaped and unescaped keys a single code path. A decoded key is never | ||
| // longer than its raw UTF-8 form, so only implausibly long keys fall back to allocating. | ||
| Span<char> keyBuffer = stackalloc char[MaxStackAllocatedKeyLength]; | ||
| var libraryKey = reader.ValueSpan.Length <= keyBuffer.Length | ||
| ? keyBuffer[..reader.CopyString(keyBuffer)] | ||
| : reader.GetString().AsSpan(); | ||
|
|
||
| // Keys are "PackageId/Version"; anything else (including project libraries) cannot match a package. | ||
|
dibarbet marked this conversation as resolved.
Resolved
|
||
| var separatorIndex = libraryKey.LastIndexOf('/'); | ||
| if (separatorIndex <= 0 || separatorIndex == libraryKey.Length - 1) | ||
| return; | ||
|
|
||
| var packageId = libraryKey[..separatorIndex]; | ||
| var resolvedVersion = libraryKey[(separatorIndex + 1)..]; | ||
| for (var i = 0; i < packageReferences.Length; i++) | ||
| { | ||
| if (resolvedReferences[i] || !packageId.Equals(packageReferences[i].Name, StringComparison.OrdinalIgnoreCase)) | ||
| continue; | ||
|
|
||
| // Only packages the project references get this far, so versions are allocated a handful of times. | ||
| if (NuGetVersion.TryParse(resolvedVersion.ToString(), out var version) && | ||
| GetVersionRange(packageReferences[i]).Satisfies(version)) | ||
| { | ||
| resolvedReferences[i] = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static VersionRange GetVersionRange(PackageReferenceItem reference) | ||
| => VersionRange.TryParse(reference.VersionRange, out var versionRange) ? versionRange : VersionRange.All; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.