diff --git a/src/Bicep.Cli/Commands/PublishCommand.cs b/src/Bicep.Cli/Commands/PublishCommand.cs index 2a34f22df10..7b8efad7cdc 100644 --- a/src/Bicep.Cli/Commands/PublishCommand.cs +++ b/src/Bicep.Cli/Commands/PublishCommand.cs @@ -13,7 +13,6 @@ using Bicep.Core.Features; using Bicep.Core.FileSystem; using Bicep.Core.Registry; -using Bicep.Core.Registry.Oci; using Bicep.Core.SourceCode; namespace Bicep.Cli.Commands @@ -99,7 +98,7 @@ private async Task PublishModuleAsync(ArtifactReference target, Stream compiledA private ArtifactReference ValidateReference(string targetModuleReference, Uri targetModuleUri) { - if (!this.moduleDispatcher.TryGetArtifactReference(targetModuleReference, "module", targetModuleUri).IsSuccess(out var moduleReference, out var failureBuilder)) + if (!this.moduleDispatcher.TryGetModuleReference(targetModuleReference, targetModuleUri).IsSuccess(out var moduleReference, out var failureBuilder)) { // TODO: We should probably clean up the dispatcher contract so this sort of thing isn't necessary (unless we change how target module is set in this command) var message = failureBuilder(DiagnosticBuilder.ForDocumentStart()).Message; diff --git a/src/Bicep.Core.IntegrationTests/RegistryTests.cs b/src/Bicep.Core.IntegrationTests/RegistryTests.cs index d8b0863322c..7d2747ac298 100644 --- a/src/Bicep.Core.IntegrationTests/RegistryTests.cs +++ b/src/Bicep.Core.IntegrationTests/RegistryTests.cs @@ -182,7 +182,7 @@ public async Task ModuleRestoreContentionShouldProduceConsistentState(bool publi var moduleReferences = dataSet.RegistryModules.Values .OrderBy(m => m.Metadata.Target) - .Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'.")) + .Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'.")) .ToImmutableList(); moduleReferences.Should().HaveCount(7); @@ -238,7 +238,7 @@ public async Task ModuleRestoreWithStuckFileLockShouldFailAfterTimeout(IEnumerab var configuration = BicepTestConstants.BuiltInConfigurationWithAllAnalyzersDisabled; var moduleReferences = moduleInfos .OrderBy(m => m.Metadata.Target) - .Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'.")) + .Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'.")) .ToImmutableList(); moduleReferences.Should().HaveCount(moduleCount); @@ -307,7 +307,7 @@ public async Task ForceModuleRestoreWithStuckFileLockShouldFailAfterTimeout(IEnu var moduleReferences = moduleInfos .OrderBy(m => m.Metadata.Target) - .Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'.")) + .Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'.")) .ToImmutableList(); moduleReferences.Should().HaveCount(moduleCount); @@ -384,7 +384,7 @@ public async Task ForceModuleRestoreShouldRestoreAllModules(IEnumerable m.Metadata.Target) - .Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'.")) + .Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'.")) .ToImmutableList(); moduleReferences.Should().HaveCount(moduleCount); diff --git a/src/Bicep.Core.Samples/DataSetsExtensions.cs b/src/Bicep.Core.Samples/DataSetsExtensions.cs index 5a8985db5d0..0680d2ff325 100644 --- a/src/Bicep.Core.Samples/DataSetsExtensions.cs +++ b/src/Bicep.Core.Samples/DataSetsExtensions.cs @@ -73,7 +73,7 @@ public static Mock CreateMockRegistryClients(Im { var target = publishInfo.Metadata.Target; - if (!dispatcher.TryGetArtifactReference(target, "module", RandomFileUri()).IsSuccess(out var @ref) || @ref is not OciArtifactReference targetReference) + if (!dispatcher.TryGetModuleReference(target, RandomFileUri()).IsSuccess(out var @ref) || @ref is not OciArtifactReference targetReference) { throw new InvalidOperationException($"Module '{moduleName}' has an invalid target reference '{target}'. Specify a reference to an OCI artifact."); } @@ -149,7 +149,7 @@ public static ITemplateSpecRepositoryFactory CreateMockTemplateSpecRepositoryFac foreach (var (moduleName, templateSpecInfo) in templateSpecs) { - if (!dispatcher.TryGetArtifactReference(templateSpecInfo.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) || @ref is not TemplateSpecModuleReference reference) + if (!dispatcher.TryGetModuleReference(templateSpecInfo.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) || @ref is not TemplateSpecModuleReference reference) { throw new InvalidOperationException($"Module '{moduleName}' has an invalid target reference '{templateSpecInfo.Metadata.Target}'. Specify a reference to a template spec."); } @@ -191,7 +191,7 @@ public static async Task PublishModuleToRegistryAsync(IContainerRegistryClientFa .AddSingleton(featureProviderFactory) ).Construct(); - var targetReference = dispatcher.TryGetArtifactReference(target, "TODO", RandomFileUri()).IsSuccess(out var @ref) ? @ref + var targetReference = dispatcher.TryGetModuleReference(target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new InvalidOperationException($"Module '{moduleName}' has an invalid target reference '{target}'. Specify a reference to an OCI artifact."); var result = CompilationHelper.Compile(moduleSource); diff --git a/src/Bicep.Core.UnitTests/Registry/ArtifactDispatcherTests.cs b/src/Bicep.Core.UnitTests/Registry/ArtifactDispatcherTests.cs index 36140a9940e..6d326645db8 100644 --- a/src/Bicep.Core.UnitTests/Registry/ArtifactDispatcherTests.cs +++ b/src/Bicep.Core.UnitTests/Registry/ArtifactDispatcherTests.cs @@ -89,20 +89,20 @@ public async Task MockRegistries_ModuleLifecycle() var validRefUri = RandomFileUri(); ArtifactReference? validRef = new MockModuleReference("validRef", validRefUri); - mock.Setup(m => m.TryParseArtifactReference(null, "module", "validRef", validRefUri)).Returns(ResultHelper.Create(validRef, @null)); + mock.Setup(m => m.TryParseArtifactReference(null, ArtifactType.Module, "validRef")).Returns(ResultHelper.Create(validRef, @null)); var validRefUri2 = RandomFileUri(); ArtifactReference? validRef2 = new MockModuleReference("validRef2", validRefUri2); - mock.Setup(m => m.TryParseArtifactReference(null, "module", "validRef2", validRefUri2)).Returns(ResultHelper.Create(validRef2, @null)); + mock.Setup(m => m.TryParseArtifactReference(null, ArtifactType.Module, "validRef2")).Returns(ResultHelper.Create(validRef2, @null)); var validRefUri3 = RandomFileUri(); ArtifactReference? validRef3 = new MockModuleReference("validRef3", validRefUri3); - mock.Setup(m => m.TryParseArtifactReference(null, "module", "validRef3", validRefUri3)).Returns(ResultHelper.Create(validRef3, @null)); + mock.Setup(m => m.TryParseArtifactReference(null, ArtifactType.Module, "validRef3")).Returns(ResultHelper.Create(validRef3, @null)); var badRefUri = RandomFileUri(); ArtifactReference? nullRef = null; ErrorBuilderDelegate? badRefError = x => new ErrorDiagnostic(x.TextSpan, "BCPMock", "Bad ref error"); - mock.Setup(m => m.TryParseArtifactReference(null, "module", "badRef", badRefUri)).Returns(ResultHelper.Create(nullRef, badRefError)); + mock.Setup(m => m.TryParseArtifactReference(null, ArtifactType.Module, "badRef")).Returns(ResultHelper.Create(nullRef, badRefError)); mock.Setup(m => m.IsArtifactRestoreRequired(validRef)).Returns(true); mock.Setup(m => m.IsArtifactRestoreRequired(validRef2)).Returns(false); diff --git a/src/Bicep.Core/Diagnostics/DiagnosticBuilder.cs b/src/Bicep.Core/Diagnostics/DiagnosticBuilder.cs index bf935dba133..10d5a9962e6 100644 --- a/src/Bicep.Core/Diagnostics/DiagnosticBuilder.cs +++ b/src/Bicep.Core/Diagnostics/DiagnosticBuilder.cs @@ -991,7 +991,7 @@ public ErrorDiagnostic DirectAccessToCollectionNotSupported(IEnumerable? "BCP162", "Expected a loop item variable identifier or \"(\" at this location."); - public ErrorDiagnostic ScopeUnsupportedOnChildResource(string parentIdentifier) => new( + public ErrorDiagnostic ScopeUnsupportedOnChildResource() => new( TextSpan, "BCP164", $"A child resource's scope is computed based on the scope of its ancestor resource. This means that using the \"{LanguageConstants.ResourceScopePropertyName}\" property on a child resource is unsupported."); @@ -1761,7 +1761,7 @@ public ErrorDiagnostic IndexOutOfBounds(string typeName, long tupleLength, long var message = new StringBuilder("The provided index value of \"").Append(indexSought).Append("\" is not valid for type \"").Append(typeName).Append("\"."); if (tupleLength > 0) { - message.Append(" Indexes for this type must be between 0 and ").Append(tupleLength - 1).Append("."); + message.Append(" Indexes for this type must be between 0 and ").Append(tupleLength - 1).Append('.'); } return new(TextSpan, "BCP311", message.ToString()); diff --git a/src/Bicep.Core/Emit/ScopeHelper.cs b/src/Bicep.Core/Emit/ScopeHelper.cs index c27d0635b99..3418ff0120f 100644 --- a/src/Bicep.Core/Emit/ScopeHelper.cs +++ b/src/Bicep.Core/Emit/ScopeHelper.cs @@ -418,7 +418,7 @@ void logInvalidScopeDiagnostic(IPositionable positionable, ResourceScope supplie if (resource.TryGetScopeSyntax() is { } scopeSyntax) { // it doesn't make sense to have scope on a descendent resource; it should be inherited from the oldest ancestor. - diagnosticWriter.Write(scopeSyntax, x => x.ScopeUnsupportedOnChildResource(ancestors.Last().Resource.Symbol.Name)); + diagnosticWriter.Write(scopeSyntax, x => x.ScopeUnsupportedOnChildResource()); // TODO: format the ancestor name using the resource accessor (::) for nested resources scopeInfo[resource] = defaultScopeData; continue; diff --git a/src/Bicep.Core/Registry/ArtifactRegistry.cs b/src/Bicep.Core/Registry/ArtifactRegistry.cs index b606a6e079d..36a7704b3b3 100644 --- a/src/Bicep.Core/Registry/ArtifactRegistry.cs +++ b/src/Bicep.Core/Registry/ArtifactRegistry.cs @@ -29,7 +29,7 @@ public abstract class ArtifactRegistry : IArtifactRegistry where T : Artifact public abstract ResultWithDiagnostic TryGetLocalArtifactEntryPointUri(T reference); - public abstract ResultWithDiagnostic TryParseArtifactReference(string? aliasName, string artifactType, string reference, Uri parentModuleUri); + public abstract ResultWithDiagnostic TryParseArtifactReference(string? aliasName, ArtifactType artifactType, string reference); public abstract string? TryGetDocumentationUri(T reference); diff --git a/src/Bicep.Core/Registry/IArtifactReferenceFactory.cs b/src/Bicep.Core/Registry/IArtifactReferenceFactory.cs index 68fc9a21a8a..d0c6f227786 100644 --- a/src/Bicep.Core/Registry/IArtifactReferenceFactory.cs +++ b/src/Bicep.Core/Registry/IArtifactReferenceFactory.cs @@ -15,5 +15,7 @@ public interface IArtifactReferenceFactory ResultWithDiagnostic TryGetArtifactReference(string reference, string artifactType, Uri parentModuleUri); + ResultWithDiagnostic TryGetModuleReference(string reference, Uri parentModuleUri); + ResultWithDiagnostic TryGetArtifactReference(IArtifactReferenceSyntax artifactDeclaration, Uri parentModuleUri); } diff --git a/src/Bicep.Core/Registry/IArtifactRegistry.cs b/src/Bicep.Core/Registry/IArtifactRegistry.cs index e487ba2e728..3dd5ce5ba03 100644 --- a/src/Bicep.Core/Registry/IArtifactRegistry.cs +++ b/src/Bicep.Core/Registry/IArtifactRegistry.cs @@ -12,6 +12,11 @@ namespace Bicep.Core.Registry { + public enum ArtifactType + { + Module, + Provider, + } /// /// An implementation of a Bicep artifact registry. /// @@ -34,8 +39,7 @@ public interface IArtifactRegistry /// The alias name /// The unqualified artifact reference /// The artifact type. Either "module" or "provider" - /// The URI of the parent module - ResultWithDiagnostic TryParseArtifactReference(string? aliasName, string artifactType, string reference, Uri parentModuleUri); + ResultWithDiagnostic TryParseArtifactReference(string? aliasName, ArtifactType artifactType, string reference); /// /// Returns true if the specified artifact is already cached in the local cache. diff --git a/src/Bicep.Core/Registry/LocalModuleRegistry.cs b/src/Bicep.Core/Registry/LocalModuleRegistry.cs index ed7ac7ec101..c68e750e104 100644 --- a/src/Bicep.Core/Registry/LocalModuleRegistry.cs +++ b/src/Bicep.Core/Registry/LocalModuleRegistry.cs @@ -31,7 +31,7 @@ public LocalModuleRegistry(IFileResolver fileResolver, Uri parentModuleUri, Bice public override RegistryCapabilities GetCapabilities(LocalModuleReference reference) => RegistryCapabilities.Default; - public override ResultWithDiagnostic TryParseArtifactReference(string? alias, string artifactType, string reference, Uri parentModuleUri) + public override ResultWithDiagnostic TryParseArtifactReference(string? alias, ArtifactType artifactType, string reference) { if (!LocalModuleReference.TryParse(reference, parentModuleUri).IsSuccess(out var @ref, out var failureBuilder)) { diff --git a/src/Bicep.Core/Registry/ModuleDispatcher.cs b/src/Bicep.Core/Registry/ModuleDispatcher.cs index c32c795aab3..8ebd9f9fd4a 100644 --- a/src/Bicep.Core/Registry/ModuleDispatcher.cs +++ b/src/Bicep.Core/Registry/ModuleDispatcher.cs @@ -41,6 +41,10 @@ private ImmutableDictionary Registries(Uri parentModu public ImmutableArray AvailableSchemes(Uri parentModuleUri) => Registries(parentModuleUri).Keys.OrderBy(s => s).ToImmutableArray(); + public ResultWithDiagnostic TryGetModuleReference(string reference, Uri parentModuleUri) + => TryGetArtifactReference(reference, "module", parentModuleUri); + + public ResultWithDiagnostic TryGetArtifactReference(string reference, string artifactType, Uri parentModuleUri) { var registries = Registries(parentModuleUri); @@ -51,7 +55,7 @@ public ResultWithDiagnostic TryGetArtifactReference(string re // local path reference if (registries.TryGetValue(ModuleReferenceSchemes.Local, out var localRegistry)) { - return localRegistry.TryParseArtifactReference(null, "module", parts[0], parentModuleUri); + return localRegistry.TryParseArtifactReference(null, ArtifactType.Module, parts[0]); } return new(x => x.UnknownModuleReferenceScheme(ModuleReferenceSchemes.Local, this.AvailableSchemes(parentModuleUri))); @@ -73,7 +77,7 @@ public ResultWithDiagnostic TryGetArtifactReference(string re // the scheme is recognized var rawValue = parts[1]; - return registry.TryParseArtifactReference(aliasName, artifactType, rawValue, parentModuleUri); + return registry.TryParseArtifactReference(aliasName, ArtifactType.Module, rawValue); } // unknown scheme diff --git a/src/Bicep.Core/Registry/Oci/OciArtifactReference.cs b/src/Bicep.Core/Registry/Oci/OciArtifactReference.cs index 745091804d8..47084004363 100644 --- a/src/Bicep.Core/Registry/Oci/OciArtifactReference.cs +++ b/src/Bicep.Core/Registry/Oci/OciArtifactReference.cs @@ -38,7 +38,6 @@ public OciArtifactReference(string registry, string repository, string? tag, str this.Type = type; } - /// /// Gets the registry URI. /// diff --git a/src/Bicep.Core/Registry/OciArtifactRegistry.cs b/src/Bicep.Core/Registry/OciArtifactRegistry.cs index 73d4ea3a8d0..8dc2a5ef7c4 100644 --- a/src/Bicep.Core/Registry/OciArtifactRegistry.cs +++ b/src/Bicep.Core/Registry/OciArtifactRegistry.cs @@ -60,12 +60,12 @@ public override RegistryCapabilities GetCapabilities(OciArtifactReference refere return reference.Tag is null ? RegistryCapabilities.Default : RegistryCapabilities.Publish; } - public override ResultWithDiagnostic TryParseArtifactReference(string? aliasName, string artifactType, string reference, Uri parentModuleUri) + public override ResultWithDiagnostic TryParseArtifactReference(string? aliasName, ArtifactType artifactType, string reference) { var type = artifactType switch { - "module" => OciArtifactReferenceType.Module, - "provider" => OciArtifactReferenceType.Provider, + ArtifactType.Module => OciArtifactReferenceType.Module, + ArtifactType.Provider => OciArtifactReferenceType.Provider, _ => throw new ArgumentException($"Unexpected artifact type \"{artifactType}\"."), }; if (!OciArtifactReference.TryParse(type, aliasName, reference, configuration, parentModuleUri).IsSuccess(out var @ref, out var failureBuilder)) diff --git a/src/Bicep.Core/Registry/TemplateSpecModuleRegistry.cs b/src/Bicep.Core/Registry/TemplateSpecModuleRegistry.cs index 3cfad915b9d..91bee8f321c 100644 --- a/src/Bicep.Core/Registry/TemplateSpecModuleRegistry.cs +++ b/src/Bicep.Core/Registry/TemplateSpecModuleRegistry.cs @@ -41,7 +41,7 @@ public TemplateSpecModuleRegistry(IFileResolver fileResolver, ITemplateSpecRepos public override RegistryCapabilities GetCapabilities(TemplateSpecModuleReference reference) => RegistryCapabilities.Default; - public override ResultWithDiagnostic TryParseArtifactReference(string? aliasName, string artifactType, string reference, Uri parentModuleUri) + public override ResultWithDiagnostic TryParseArtifactReference(string? aliasName, ArtifactType artifactType, string reference) { if (!TemplateSpecModuleReference.TryParse(aliasName, reference, configuration, parentModuleUri).IsSuccess(out var @ref, out var failureBuilder)) { diff --git a/src/Bicep.LangServer.IntegrationTests/Registry/ModuleRestoreSchedulerTests.cs b/src/Bicep.LangServer.IntegrationTests/Registry/ModuleRestoreSchedulerTests.cs index e1d7736cd63..cba3374a7db 100644 --- a/src/Bicep.LangServer.IntegrationTests/Registry/ModuleRestoreSchedulerTests.cs +++ b/src/Bicep.LangServer.IntegrationTests/Registry/ModuleRestoreSchedulerTests.cs @@ -204,7 +204,7 @@ public ResultWithDiagnostic TryGetLocalArtifactEntryPointUri(ArtifactRefere public Task TryGetDescription(ArtifactReference reference) => Task.FromResult(null); - public ResultWithDiagnostic TryParseArtifactReference(string? aliasName,string artifactType, string reference, Uri parentModuleUri) + public ResultWithDiagnostic TryParseArtifactReference(string? _, ArtifactType artifactType, string reference) { return new(new MockModuleRef(reference, PathHelper.FilePathToFileUrl(Path.GetTempFileName()))); }