diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Internal/Telemetry/Activity/Providers/TelemetrySolutionInfoEnricher.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Internal/Telemetry/Activity/Providers/TelemetrySolutionInfoEnricher.cs index 49b4c2edbd..7ecfd6b647 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Internal/Telemetry/Activity/Providers/TelemetrySolutionInfoEnricher.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Internal/Telemetry/Activity/Providers/TelemetrySolutionInfoEnricher.cs @@ -113,13 +113,32 @@ internal sealed class TelemetrySolutionInfoEnricher : TelemetryActivityEventEnri continue; } - ExtractModuleImportsInfo(modulePath, modules); + ExtractModuleImportsInfo(modulePath, modules, new HashSet(StringComparer.Ordinal)); } context.Current[ActivityPropertyNames.InstalledModules] = modules; } - private static void ExtractModuleImportsInfo(string modulePath, List> modules) + private static void ExtractModuleImportsInfo(string modulePath, List> modules, HashSet importChain) + { + // Modules can import each other, so skip a module that is already in the current import chain. + var fullModulePath = Path.GetFullPath(modulePath); + if (!importChain.Add(fullModulePath)) + { + return; + } + + try + { + ExtractModuleImportsInfoCore(modulePath, modules, importChain); + } + finally + { + importChain.Remove(fullModulePath); + } + } + + private static void ExtractModuleImportsInfoCore(string modulePath, List> modules, HashSet importChain) { var moduleJsonFileContent = File.ReadAllText(modulePath); using var moduleDoc = JsonDocument.Parse(moduleJsonFileContent, new JsonDocumentOptions @@ -138,7 +157,7 @@ internal sealed class TelemetrySolutionInfoEnricher : TelemetryActivityEventEnri if (!importPath.IsNullOrEmpty()) { - ExtractModuleImportsInfo(importPath, modules); + ExtractModuleImportsInfo(importPath, modules, importChain); continue; } diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Telemetry/TelemetrySolutionInfoEnricher_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Telemetry/TelemetrySolutionInfoEnricher_Tests.cs new file mode 100644 index 0000000000..57e3aad372 --- /dev/null +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Telemetry/TelemetrySolutionInfoEnricher_Tests.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Shouldly; +using Volo.Abp.Internal.Telemetry.Activity; +using Volo.Abp.Internal.Telemetry.Activity.Contracts; +using Volo.Abp.Internal.Telemetry.Activity.Providers; +using Volo.Abp.Internal.Telemetry.Constants; +using Xunit; + +namespace Volo.Abp.Telemetry; + +public class TelemetrySolutionInfoEnricher_Tests : IDisposable +{ + private readonly string _solutionDirectory; + + public TelemetrySolutionInfoEnricher_Tests() + { + _solutionDirectory = Path.Combine(Path.GetTempPath(), "abp-telemetry-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(Path.Combine(_solutionDirectory, "catalog")); + Directory.CreateDirectory(Path.Combine(_solutionDirectory, "ordering")); + } + + [Fact] + public async Task Should_Handle_Modules_That_Import_Each_Other() + { + File.WriteAllText(Path.Combine(_solutionDirectory, "MySolution.abpsln"), """ + { + "modules": { + "Catalog": { "path": "catalog/Catalog.abpmdl" }, + "Ordering": { "path": "ordering/Ordering.abpmdl" } + } + } + """); + File.WriteAllText(Path.Combine(_solutionDirectory, "catalog", "Catalog.abpmdl"), """ + { + "imports": { + "Volo.Abp.SettingManagement": { "version": "10.7.0" }, + "Ordering": { "path": "../ordering/Ordering.abpmdl" } + } + } + """); + File.WriteAllText(Path.Combine(_solutionDirectory, "ordering", "Ordering.abpmdl"), """ + { + "imports": { + "Volo.Abp.FeatureManagement": { "version": "10.7.0" }, + "Catalog": { "path": "../catalog/Catalog.abpmdl" } + } + } + """); + + var storage = Substitute.For(); + storage.ShouldAddSolutionInformation(Arg.Any()).Returns(true); + var enricher = new TelemetrySolutionInfoEnricher(storage, new ServiceCollection().BuildServiceProvider()); + + var context = ActivityContext.Create("Test"); + context.Current[ActivityPropertyNames.SolutionId] = Guid.NewGuid(); + context.ExtraProperties[ActivityPropertyNames.SolutionPath] = Path.Combine(_solutionDirectory, "MySolution.abpsln"); + + await enricher.EnrichAsync(context); + + context.Current[ActivityPropertyNames.HasSolutionInfo].ShouldBe(true); + var moduleNames = ((List>)context.Current[ActivityPropertyNames.InstalledModules]!) + .Select(x => x[ActivityPropertyNames.ModuleName]) + .ToList(); + moduleNames.ShouldContain("Volo.Abp.SettingManagement"); + moduleNames.ShouldContain("Volo.Abp.FeatureManagement"); + } + + public void Dispose() + { + if (Directory.Exists(_solutionDirectory)) + { + Directory.Delete(_solutionDirectory, true); + } + } +}