-
Notifications
You must be signed in to change notification settings - Fork 734
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change format of requests to show module sources (#12351)
Doing this because: 1) want easier way to change the title of a source document displayed in vscode 2) local cache file path that's currently in the URI is not needed 3) need to be able to request a specific file from the module source zip Old format of the URI we send to vscode to display the source of a module (unencoded): bicep-cache:{fullyQualifiedReference}#{localCacheFullPath} Example: bicep-cache:br:myregistry.azurecr.io/myrepo:v1#/Users/MyUserName/.bicep/br/registry.azurecr.io/myrepo/v1$/entrypoint.bicep New format (unencoded): bicep-extsrc:{title-to-display-for-document}?{fullyQualifiedReference}#{source-file-to-retrieve-relative-path} Example: bicep-extsrc:br:myregistry.azurecr.io/myrepo:main.bicep (v1)?br:myregistry.azurecr.io/myrepo:v1#main.bicep No fragment in URI if just requesting the main.json file Also changed other names, like: BicepRegistryCacheRequestHandler -> BicepExternalSourceRequestHandler Example of source document titles (exact format still being decided): <img width="658" alt="image" src="https://github.com/Azure/bicep/assets/6913354/4e36bd8c-4718-42cc-83d6-9113a5c5860b"><img width="771" alt="image" src="https://github.com/Azure/bicep/assets/6913354/8084f618-513e-44ec-89f4-93ad03c8021a"> ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/12351) --------- Co-authored-by: Stephen Weatherford <Stephen.Weatherford.com>
- Loading branch information
1 parent
433eac7
commit 2f25f04
Showing
16 changed files
with
490 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 203 additions & 18 deletions
221
.../BicepRegistryCacheRequestHandlerTests.cs → ...BicepExternalSourceRequestHandlerTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
src/Bicep.LangServer/Handlers/BicepExternalSourceRequestHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Bicep.Core.Diagnostics; | ||
using Bicep.Core.FileSystem; | ||
using Bicep.Core.Registry; | ||
using Bicep.Core.Registry.Oci; | ||
using Bicep.Core.SourceCode; | ||
using MediatR; | ||
using OmniSharp.Extensions.JsonRpc; | ||
|
||
namespace Bicep.LanguageServer.Handlers | ||
{ | ||
[Method(BicepExternalSourceRequestHandler.BicepExternalSourceLspMethodName, Direction.ClientToServer)] | ||
public record BicepExternalSourceParams( | ||
string Target // The module reference to display sources for | ||
) : IRequest<BicepExternalSourceResponse>; | ||
|
||
public record BicepExternalSourceResponse(string Content); | ||
|
||
/// <summary> | ||
/// Handles textDocument/bicepExternalSource LSP requests. These are sent by clients that are resolving contents of document URIs using the bicep-extsrc: scheme. | ||
/// The BicepDefinitionHandler returns such URIs when definitions are inside modules that reside in the local module cache. | ||
/// </summary> | ||
public class BicepExternalSourceRequestHandler : IJsonRpcRequestHandler<BicepExternalSourceParams, BicepExternalSourceResponse> | ||
{ | ||
public const string BicepExternalSourceLspMethodName = "textDocument/bicepExternalSource"; | ||
|
||
private readonly IModuleDispatcher moduleDispatcher; | ||
private readonly IFileResolver fileResolver; | ||
|
||
public BicepExternalSourceRequestHandler(IModuleDispatcher moduleDispatcher, IFileResolver fileResolver) | ||
{ | ||
this.moduleDispatcher = moduleDispatcher; | ||
this.fileResolver = fileResolver; | ||
} | ||
|
||
public Task<BicepExternalSourceResponse> Handle(BicepExternalSourceParams request, CancellationToken cancellationToken) | ||
{ | ||
// If any of the following paths result in an exception being thrown (and surfaced client-side to the user), | ||
// it indicates a code defect client or server-side. | ||
// In normal operation, the user should never see them regardless of how malformed their code is. | ||
|
||
if (!moduleDispatcher.TryGetArtifactReference(ArtifactType.Module, request.Target, new Uri("file:https:///no-parent-file-is-available")).IsSuccess(out var moduleReference)) | ||
{ | ||
throw new InvalidOperationException( | ||
$"The client specified an invalid module reference '{request.Target}'."); | ||
} | ||
|
||
if (!moduleReference.IsExternal) | ||
{ | ||
throw new InvalidOperationException( | ||
$"The specified module reference '{request.Target}' refers to a local module which is not supported by {BicepExternalSourceLspMethodName} requests."); | ||
} | ||
|
||
if (this.moduleDispatcher.GetArtifactRestoreStatus(moduleReference, out _) != ArtifactRestoreStatus.Succeeded) | ||
{ | ||
throw new InvalidOperationException( | ||
$"The module '{moduleReference.FullyQualifiedReference}' has not yet been successfully restored."); | ||
} | ||
|
||
if (!moduleDispatcher.TryGetLocalArtifactEntryPointUri(moduleReference).IsSuccess(out var uri)) | ||
{ | ||
throw new InvalidOperationException( | ||
$"Unable to obtain the entry point URI for module '{moduleReference.FullyQualifiedReference}'."); | ||
} | ||
|
||
if (moduleDispatcher.TryGetModuleSources(moduleReference) is SourceArchive sourceArchive) | ||
{ | ||
// TODO: For now, we just proffer the main source file | ||
var entrypointFile = sourceArchive.SourceFiles.Single(f => f.Path == sourceArchive.EntrypointRelativePath); | ||
return Task.FromResult(new BicepExternalSourceResponse(entrypointFile.Contents)); | ||
} | ||
|
||
// No sources available, just retrieve the JSON source | ||
if (!this.fileResolver.TryRead(uri).IsSuccess(out var contents, out var failureBuilder)) | ||
{ | ||
var message = failureBuilder(DiagnosticBuilder.ForDocumentStart()).Message; | ||
throw new InvalidOperationException($"Unable to read file '{uri}'. {message}"); | ||
} | ||
|
||
return Task.FromResult(new BicepExternalSourceResponse(contents)); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a bicep-extsrc: URI for a given module's source file to give to the client to use as a document URI. (Client will then | ||
/// ask for us the source code via a textDocument/externalSource request). | ||
/// </summary> | ||
/// <param name="reference">The module reference</param> | ||
/// <param name="sourceArchive">The source archive for the module, if sources are available</param> | ||
/// <returns>A bicep-extsrc: URI</returns> | ||
public static Uri GetExternalSourceLinkUri(OciArtifactReference reference, SourceArchive? sourceArchive) | ||
{ | ||
string entrypoint; | ||
string? requestedSourceFile; | ||
|
||
if (sourceArchive is { }) | ||
{ | ||
// We have Bicep source code available. | ||
entrypoint = sourceArchive.EntrypointRelativePath; | ||
requestedSourceFile = entrypoint; | ||
} | ||
else | ||
{ | ||
// Just showing the main.json | ||
entrypoint = "main.json"; | ||
requestedSourceFile = null; | ||
} | ||
|
||
var fullyQualifiedReference = reference.FullyQualifiedReference; | ||
var version = reference.Tag is string ? $":{reference.Tag}" : $"@{reference.Digest}"; | ||
|
||
var shortDocumentTitle = $"{entrypoint} ({Path.GetFileName(reference.Repository)}{version})"; | ||
var fullDocumentTitle = $"{reference.Scheme}:{reference.Registry}/{reference.Repository}{version}/{shortDocumentTitle}"; | ||
|
||
// Encode the module reference as a query and the file to retrieve as a fragment. | ||
// Vs Code will strip the fragment and query and use the main part of the uri as the document title. | ||
// The Bicep extension will use the fragment to make a call to use via textDocument/bicepExternalSource request (see BicepExternalSourceHandler) | ||
// to get the actual source code contents to display. | ||
// | ||
// Example: | ||
// | ||
// source available (will be encoded): | ||
// bicep-extsrc:br:myregistry.azurecr.io/myrepo:main.bicep (v1)?br:myregistry.azurecr.io/myrepo:v1#main.bicep | ||
// | ||
// source not available, showing just JSON (will be encoded) | ||
// bicep-extsrc:br:myregistry.azurecr.io/myrepo:main.json (v1)?br:myregistry.azurecr.io/myrepo:v1 | ||
// | ||
var uri = new UriBuilder($"bicep-extsrc:{Uri.EscapeDataString(fullDocumentTitle)}"); | ||
uri.Query = Uri.EscapeDataString(fullyQualifiedReference); | ||
uri.Fragment = requestedSourceFile is null ? null : Uri.EscapeDataString(requestedSourceFile); | ||
return uri.Uri; | ||
} | ||
} | ||
} |
92 changes: 0 additions & 92 deletions
92
src/Bicep.LangServer/Handlers/BicepRegistryCacheRequestHandler.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.