Skip to content

Commit

Permalink
ResourceIdentifier => IOResourceIdentifier (#15746)
Browse files Browse the repository at this point in the history
This PR incorporates feedback from @anthony-c-martin in my previous PR
regarding the potential ambiguity of the term ResourceIdentifier within
the Bicep context. I renamed the type to `IOUri` rather than the
initially suggested `FileUri`, as this choice accommodates future
scenarios where we may need to handle storage blobs with URI schemes
such as `https`, rather than being limited to `file`.
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/15746)
  • Loading branch information
shenglol authored Dec 3, 2024
1 parent 8e81fc8 commit 2b8c6f9
Show file tree
Hide file tree
Showing 89 changed files with 366 additions and 368 deletions.
2 changes: 1 addition & 1 deletion src/Bicep.Cli.IntegrationTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected static async Task<IEnumerable<string>> GetAllDiagnostics(string bicepF
{
var (line, character) = TextCoordinateConverter.GetPosition(bicepFile.LineStarts, diagnostic.Span.Position);
var codeDescription = diagnostic.Uri == null ? string.Empty : $" [{diagnostic.Uri.AbsoluteUri}]";
output.Add($"{bicepFile.Identifier.LocalPath}({line + 1},{character + 1}) : {diagnostic.Level} {diagnostic.Code}: {diagnostic.Message}{codeDescription}");
output.Add($"{bicepFile.Uri.LocalPath}({line + 1},{character + 1}) : {diagnostic.Level} {diagnostic.Code}: {diagnostic.Message}{codeDescription}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class ExperimentalFeatureWarningProvider
public static string? TryGetEnabledExperimentalFeatureWarningMessage(SourceFileGrouping sourceFileGrouping, IFeatureProviderFactory featureProviderFactory)
{
var experimentalFeaturesEnabled = sourceFileGrouping.SourceFiles
.Select(file => featureProviderFactory.GetFeatureProvider(file.Identifier))
.Select(file => featureProviderFactory.GetFeatureProvider(file.Uri))
.SelectMany(static features => features.EnabledFeatureMetadata.Where(f => f.impactsCompilation).Select(f => f.name))
.Distinct()
.ToImmutableArray();
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Cli/Helpers/ParamsFileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ public static BicepParamFile ApplyParameterOverrides(BicepParamFile sourceFile,
return sourceFile;
}

return SourceFileFactory.CreateBicepParamFile(sourceFile.Identifier, newProgramSyntax.ToString());
return SourceFileFactory.CreateBicepParamFile(sourceFile.Uri, newProgramSyntax.ToString());
}
}
4 changes: 2 additions & 2 deletions src/Bicep.Cli/Logging/DiagnosticLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static void LogDefaultDiagnostics(ILogger logger, ImmutableDictionary<Bi
// build a a code description link if the Uri is assigned
var codeDescription = diagnostic.Uri == null ? string.Empty : $" [{diagnostic.Uri.AbsoluteUri}]";

var message = $"{bicepFile.Identifier.LocalPath}({line + 1},{character + 1}) : {diagnostic.Level} {diagnostic.Code}: {diagnostic.Message}{codeDescription}";
var message = $"{bicepFile.Uri.LocalPath}({line + 1},{character + 1}) : {diagnostic.Level} {diagnostic.Code}: {diagnostic.Message}{codeDescription}";

logger.Log(ToLogLevel(diagnostic.Level), message);
}
Expand Down Expand Up @@ -136,7 +136,7 @@ private static Result GetSarifDiagnostic(BicepSourceFile sourceFile, IDiagnostic
{
ArtifactLocation = new ArtifactLocation
{
Uri = sourceFile.Identifier,
Uri = sourceFile.Uri,
},
Region = new Region
{
Expand Down
8 changes: 4 additions & 4 deletions src/Bicep.Cli/Rpc/CliJsonRpcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task<CompileParamsResponse> CompileParams(CompileParamsRequest requ

var workspace = new Workspace();
workspace.UpsertSourceFile(paramFile);
compilation = await compiler.CreateCompilation(paramFile.Identifier, workspace);
compilation = await compiler.CreateCompilation(paramFile.Uri, workspace);
var paramsResult = compilation.Emitter.Parameters();

return new(
Expand All @@ -96,9 +96,9 @@ public async Task<GetFileReferencesResponse> GetFileReferences(GetFileReferences
var fileUris = new HashSet<Uri>();
foreach (var otherModel in compilation.GetAllBicepModels())
{
fileUris.Add(otherModel.SourceFile.Identifier);
fileUris.Add(otherModel.SourceFile.Uri);
fileUris.UnionWith(otherModel.GetAuxiliaryFileReferences());
if (otherModel.Configuration.ConfigFileIdentifier is { } configFileIdentifier)
if (otherModel.Configuration.ConfigFileUri is { } configFileIdentifier)
{
var uri = new UriBuilder
{
Expand Down Expand Up @@ -235,7 +235,7 @@ private static IEnumerable<DiagnosticDefinition> GetDiagnostics(Compilation comp
{
foreach (var diagnostic in diagnostics)
{
yield return new(bicepFile.Identifier.LocalPath, GetRange(bicepFile, diagnostic), diagnostic.Level.ToString(), diagnostic.Code, diagnostic.Message);
yield return new(bicepFile.Uri.LocalPath, GetRange(bicepFile, diagnostic), diagnostic.Level.ToString(), diagnostic.Code, diagnostic.Message);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core.IntegrationTests/DecoratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ param inputb string
};

var compilation = Services.BuildCompilation(files, mainUri);
var diagnosticsByFile = compilation.GetAllDiagnosticsByBicepFile().ToDictionary(kvp => kvp.Key.Identifier, kvp => kvp.Value);
var diagnosticsByFile = compilation.GetAllDiagnosticsByBicepFile().ToDictionary(kvp => kvp.Key.Uri, kvp => kvp.Value);
var success = diagnosticsByFile.Values.SelectMany(x => x).All(d => !d.IsError());

using (new AssertionScope())
Expand Down Expand Up @@ -215,7 +215,7 @@ param inputb string
};

var compilation = Services.BuildCompilation(files, mainUri);
var diagnosticsByFile = compilation.GetAllDiagnosticsByBicepFile().ToDictionary(kvp => kvp.Key.Identifier, kvp => kvp.Value);
var diagnosticsByFile = compilation.GetAllDiagnosticsByBicepFile().ToDictionary(kvp => kvp.Key.Uri, kvp => kvp.Value);
var success = diagnosticsByFile.Values.SelectMany(x => x).All(d => !d.IsError());

using (new AssertionScope())
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core.IntegrationTests/ExamplesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private async Task RunExampleTest(EmbeddedFile embeddedBicep, FeatureProviderOve
diagnostics.Where(d => !IsPermittedMissingTypeDiagnostic(d)),
diagnostics =>
{
diagnostics.Should().BeEmpty("{0} should not have warnings or errors", file.Identifier.LocalPath);
diagnostics.Should().BeEmpty("{0} should not have warnings or errors", file.Uri.LocalPath);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core.IntegrationTests/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ param p resource 'Some.Fake/Type@2019-06-01'
output storage resource 'Another.Fake/Type@2019-06-01' = fake
"));
var diagnosticsMap = result.Compilation.GetAllDiagnosticsByBicepFile().ToDictionary(kvp => kvp.Key.Identifier, kvp => kvp.Value);
var diagnosticsMap = result.Compilation.GetAllDiagnosticsByBicepFile().ToDictionary(kvp => kvp.Key.Uri, kvp => kvp.Value);
using (new AssertionScope())
{
diagnosticsMap[InMemoryFileResolver.GetFileUri("/path/to/module.bicep")].Should().HaveDiagnostics(new[]
Expand Down Expand Up @@ -769,7 +769,7 @@ private static string GetTemplate(Compilation compilation)

private static (bool success, IDictionary<Uri, ImmutableArray<IDiagnostic>> diagnosticsByFile) GetSuccessAndDiagnosticsByFile(Compilation compilation)
{
var diagnosticsByFile = compilation.GetAllDiagnosticsByBicepFile().ToDictionary(kvp => kvp.Key.Identifier, kvp => kvp.Value);
var diagnosticsByFile = compilation.GetAllDiagnosticsByBicepFile().ToDictionary(kvp => kvp.Key.Uri, kvp => kvp.Value);
var success = diagnosticsByFile.Values.SelectMany(x => x).All(d => !d.IsError());

return (success, diagnosticsByFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@ public void CompileWithLocalJsonModule_ValidTemplate_Succeeds()
}

private static ImmutableDictionary<string, ImmutableArray<IDiagnostic>> GetDiagnosticsByFileName(Compilation compilation) =>
compilation.GetAllDiagnosticsByBicepFile().ToImmutableDictionary(kvp => Path.GetFileName(kvp.Key.Identifier.LocalPath), kvp => kvp.Value);
compilation.GetAllDiagnosticsByBicepFile().ToImmutableDictionary(kvp => Path.GetFileName(kvp.Key.Uri.LocalPath), kvp => kvp.Value);
}
}
2 changes: 1 addition & 1 deletion src/Bicep.Core.UnitTests/BicepTestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static RootConfiguration CreateMockConfiguration(Dictionary<string, objec
element = element.SetPropertyByPath(path, value);
}

ResourceIdentifier? configFileIdentifier = configFilePath is not null ? new ResourceIdentifier("file", "", configFilePath) : null;
IOUri? configFileIdentifier = configFilePath is not null ? new IOUri("file", "", configFilePath) : null;

return RootConfiguration.Bind(element, configFileIdentifier);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void RootConfiguration_LeadingTildeInCacheRootDirectory_ExpandPath(string
cacheRootDirectory,
BicepTestConstants.BuiltInConfiguration.ExperimentalFeaturesEnabled,
BicepTestConstants.BuiltInConfiguration.Formatting,
BicepTestConstants.BuiltInConfiguration.ConfigFileIdentifier,
BicepTestConstants.BuiltInConfiguration.ConfigFileUri,
BicepTestConstants.BuiltInConfiguration.Diagnostics);

configuration.CacheRootDirectory.Should().Be(expectedExpandedDirectory);
Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core.UnitTests/Diagnostics/ErrorBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ private static object CreateMockParameter(ParameterInfo parameter, int index)
return ArtifactType.Module;
}

if (parameter.ParameterType == typeof(ResourceIdentifier) || parameter.ParameterType == typeof(ResourceIdentifier?))
if (parameter.ParameterType == typeof(IOUri) || parameter.ParameterType == typeof(IOUri?))
{
return new ResourceIdentifier("file", "", "/foo");
return new IOUri("file", "", "/foo");
}

throw new AssertFailedException($"Unable to generate mock parameter value of type '{parameter.ParameterType}' for the diagnostic builder method.");
Expand Down
10 changes: 5 additions & 5 deletions src/Bicep.Core.UnitTests/SourceCode/SourceArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void CanPackAndUnpackSourceFiles()


using var stream = SourceArchive.PackSourcesIntoStream(
mainBicep.SourceFile.Identifier,
mainBicep.SourceFile.Uri,
CacheRoot, mainBicep, mainJson, standaloneJson, templateSpecMainJson, localModuleJson, templateSpecMainJson2, externalModuleJson);
stream.Length.Should().BeGreaterThan(0);

Expand Down Expand Up @@ -252,7 +252,7 @@ public void CanPackAndUnpackDocumentLinks()
}
},
};
using var stream = SourceArchive.PackSourcesIntoStream(mainBicep.SourceFile.Identifier, CacheRoot, linksInput, mainBicep, mainJson, standaloneJson, templateSpecMainJson, localModuleJson, localModuleBicep, externalModuleJson);
using var stream = SourceArchive.PackSourcesIntoStream(mainBicep.SourceFile.Uri, CacheRoot, linksInput, mainBicep, mainJson, standaloneJson, templateSpecMainJson, localModuleJson, localModuleBicep, externalModuleJson);
stream.Length.Should().BeGreaterThan(0);

SourceArchive? sourceArchive = SourceArchive.UnpackFromStream(stream).TryUnwrap();
Expand Down Expand Up @@ -433,7 +433,7 @@ string[] expectedArchivePaths
}
var files = inputPaths.Select(path => CreateSourceFile(fs, path, SourceArchive.SourceKind.Bicep, $"// {path}")).ToArray();

using var stream = SourceArchive.PackSourcesIntoStream(files[0].SourceFile.Identifier, CacheRoot, files);
using var stream = SourceArchive.PackSourcesIntoStream(files[0].SourceFile.Uri, CacheRoot, files);
SourceArchive sourceArchive = SourceArchive.UnpackFromStream(stream).UnwrapOrThrow();

sourceArchive.EntrypointRelativePath.Should().Be(expectedPaths[0], "entrypoint path should be correct");
Expand Down Expand Up @@ -495,8 +495,8 @@ public void DuplicateNamesAfterMunging_ShouldHaveSeparateEntries(
var sutFile3 = inputBicepPath3 is null ? null : CreateSourceFile(fs, rootBicepFolder, inputBicepPath3, SourceArchive.SourceKind.Bicep, SecondaryDotBicepSource);

using var stream = sutFile3 is null ?
SourceArchive.PackSourcesIntoStream(entrypointFile.SourceFile.Identifier, CacheRoot, entrypointFile, sutFile1, sutFile2) :
SourceArchive.PackSourcesIntoStream(entrypointFile.SourceFile.Identifier, CacheRoot, entrypointFile, sutFile1, sutFile2, sutFile3);
SourceArchive.PackSourcesIntoStream(entrypointFile.SourceFile.Uri, CacheRoot, entrypointFile, sutFile1, sutFile2) :
SourceArchive.PackSourcesIntoStream(entrypointFile.SourceFile.Uri, CacheRoot, entrypointFile, sutFile1, sutFile2, sutFile3);

SourceArchive sourceArchive = SourceArchive.UnpackFromStream(stream).UnwrapOrThrow();

Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core.UnitTests/Utils/RegistryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static async Task PublishModuleToRegistryAsync(
throw new InvalidOperationException($"Module {moduleName} failed to produce a template.");
}

var features = featureProviderFactory.GetFeatureProvider(result.BicepFile.Identifier);
var features = featureProviderFactory.GetFeatureProvider(result.BicepFile.Uri);
BinaryData? sourcesStream = publishSource ? BinaryData.FromStream(SourceArchive.PackSourcesIntoStream(dispatcher, result.Compilation.SourceFileGrouping, features.CacheRootDirectory)) : null;
await dispatcher.PublishModule(targetReference, BinaryData.FromString(result.Template.ToString()), sourcesStream, documentationUri);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core.UnitTests/Utils/SourceArchiveBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Stream BuildStream()
SourceFiles[0].Should().BeOfType<BicepFile>("Entrypoint should be a bicep file");

return SourceArchive.PackSourcesIntoStream(
EntrypointFile.Identifier,
EntrypointFile.Uri,
cacheRoot,
SourceFiles.Select(x => new SourceFileWithArtifactReference(x, null)).ToArray());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core/Analyzers/Linter/LinterAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public IEnumerable<IDiagnostic> Analyze(SemanticModel semanticModel)
DiagnosticLevel.Info,
DiagnosticSource.CoreLinter,
"Linter Disabled",
string.Format(CoreResources.LinterDisabledFormatMessage, semanticModel.Configuration.ConfigFileIdentifier?.ToString() ?? IConfigurationManager.BuiltInConfigurationResourceName)));
string.Format(CoreResources.LinterDisabledFormatMessage, semanticModel.Configuration.ConfigFileUri?.ToString() ?? IConfigurationManager.BuiltInConfigurationResourceName)));
}
}

Expand All @@ -90,7 +90,7 @@ private IDiagnostic GetConfigurationDiagnostic(SemanticModel model)
{
var configMessage = model.Configuration.IsBuiltIn
? CoreResources.BicepConfigNoCustomSettingsMessage
: string.Format(CoreResources.BicepConfigCustomSettingsFoundFormatMessage, model.Configuration.ConfigFileIdentifier?.ToString());
: string.Format(CoreResources.BicepConfigCustomSettingsFoundFormatMessage, model.Configuration.ConfigFileUri?.ToString());

return new Diagnostic(
TextSpan.TextDocumentStart,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static RootConfiguration WithAnalyzersConfiguration(this RootConfiguratio
current.CacheRootDirectory,
current.ExperimentalFeaturesEnabled,
current.Formatting,
current.ConfigFileIdentifier,
current.ConfigFileUri,
current.Diagnostics);

public static RootConfiguration WithAllAnalyzersDisabled(this RootConfiguration current) =>
Expand Down
18 changes: 9 additions & 9 deletions src/Bicep.Core/Configuration/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public RootConfiguration GetConfiguration(Uri sourceFileUri)
return GetDefaultConfiguration();
}

var sourceFileIdentifier = sourceFileUri.ToFileIdentifier();
var sourceDirectory = this.fileExplorer.GetFile(sourceFileIdentifier).GetParent();
var sourceFileIOUri = sourceFileUri.ToIOUri();
var sourceDirectory = this.fileExplorer.GetFile(sourceFileIOUri).GetParent();

if (!configFileLookupCache.GetOrAdd(sourceDirectory, LookupConfigurationFile).IsSuccess(out var configFileHandle, out var lookupDiagnostic))
{
Expand Down Expand Up @@ -64,7 +64,7 @@ public void PurgeCache()

public void PurgeLookupCache() => configFileLookupCache.Clear();

public (RootConfiguration prevConfiguration, RootConfiguration newConfiguration)? RefreshConfigCacheEntry(ResourceIdentifier configFileIdentifier)
public (RootConfiguration prevConfiguration, RootConfiguration newConfiguration)? RefreshConfigCacheEntry(IOUri configFileIdentifier)
{
(RootConfiguration, RootConfiguration)? returnVal = null;
var configFileHandle = this.fileExplorer.GetFile(configFileIdentifier);
Expand All @@ -81,7 +81,7 @@ public void PurgeCache()
return returnVal;
}

public void RemoveConfigCacheEntry(ResourceIdentifier identifier)
public void RemoveConfigCacheEntry(IOUri identifier)
{
var configFileHandle = this.fileExplorer.GetFile(identifier);
if (loadedConfigCache.TryRemove(configFileHandle, out _))
Expand All @@ -100,19 +100,19 @@ private static ResultWithDiagnostic<RootConfiguration> LoadConfiguration(IFileHa
using var stream = configFileHandle.OpenRead();
var element = IConfigurationManager.BuiltInConfigurationElement.Merge(JsonElementFactory.CreateElementFromStream(stream));

return RootConfiguration.Bind(element, configFileHandle.Identifier);
return RootConfiguration.Bind(element, configFileHandle.Uri);
}
catch (ConfigurationException exception)
{
return new(ConfigDiagnosticBuilder.InvalidBicepConfigFile(configFileHandle.Identifier, exception.Message));
return new(ConfigDiagnosticBuilder.InvalidBicepConfigFile(configFileHandle.Uri, exception.Message));
}
catch (JsonException exception)
{
return new(ConfigDiagnosticBuilder.UnparsableBicepConfigFile(configFileHandle.Identifier, exception.Message));
return new(ConfigDiagnosticBuilder.UnparsableBicepConfigFile(configFileHandle.Uri, exception.Message));
}
catch (Exception exception)
{
return new(ConfigDiagnosticBuilder.UnloadableBicepConfigFile(configFileHandle.Identifier, exception.Message));
return new(ConfigDiagnosticBuilder.UnloadableBicepConfigFile(configFileHandle.Uri, exception.Message));
}
}

Expand All @@ -134,7 +134,7 @@ private static ResultWithDiagnostic<RootConfiguration> LoadConfiguration(IFileHa
}
catch (IOException exception)
{
return new(ConfigDiagnosticBuilder.PotentialConfigDirectoryCouldNotBeScanned(directoryToLookup?.Identifier, exception.Message));
return new(ConfigDiagnosticBuilder.PotentialConfigDirectoryCouldNotBeScanned(directoryToLookup?.Uri, exception.Message));
}

return new((IFileHandle?)null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static RootConfiguration WithExperimentalFeaturesConfiguration(this RootC
current.CacheRootDirectory,
featuresEnabled,
current.Formatting,
current.ConfigFileIdentifier,
current.ConfigFileUri,
current.Diagnostics);

public static RootConfiguration WithExperimentalFeaturesEnabled(this RootConfiguration current, ExperimentalFeaturesEnabled configuration) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static RootConfiguration WithExtensions(this RootConfiguration rootConfig
rootConfiguration.CacheRootDirectory,
rootConfiguration.ExperimentalFeaturesEnabled,
rootConfiguration.Formatting,
rootConfiguration.ConfigFileIdentifier,
rootConfiguration.ConfigFileUri,
rootConfiguration.Diagnostics);
}

Expand All @@ -39,7 +39,7 @@ public static RootConfiguration WithImplicitExtensions(this RootConfiguration ro
rootConfiguration.CacheRootDirectory,
rootConfiguration.ExperimentalFeaturesEnabled,
rootConfiguration.Formatting,
rootConfiguration.ConfigFileIdentifier,
rootConfiguration.ConfigFileUri,
rootConfiguration.Diagnostics);
}
}
Loading

0 comments on commit 2b8c6f9

Please sign in to comment.