From dbfa9562f764b0dfa0218de2cbac006289c3180e Mon Sep 17 00:00:00 2001 From: Waldek Mastykarz Date: Fri, 19 Jun 2026 08:50:56 +0200 Subject: [PATCH] fix: skip proxy-only plugin initialization for CLI subcommands Add IsProxyCommand property to InitArgs so plugins can conditionally skip initialization that is only relevant for proxy operation (e.g., opening browsers, starting telemetry listeners) when running CLI subcommands like 'devproxy jwt create'. Update DevToolsPlugin and OpenAITelemetryPlugin to check this flag before performing their proxy-only initialization. Closes dotnet/dev-proxy#1722 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- DevProxy.Abstractions/Proxy/ProxyEvents.cs | 7 +++++++ DevProxy.Plugins/Inspection/DevToolsPlugin.cs | 7 +++++++ DevProxy.Plugins/Inspection/OpenAITelemetryPlugin.cs | 5 +++++ DevProxy/Plugins/PluginServiceExtensions.cs | 4 ++-- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/DevProxy.Abstractions/Proxy/ProxyEvents.cs b/DevProxy.Abstractions/Proxy/ProxyEvents.cs index a69c2834..249685cd 100644 --- a/DevProxy.Abstractions/Proxy/ProxyEvents.cs +++ b/DevProxy.Abstractions/Proxy/ProxyEvents.cs @@ -45,6 +45,13 @@ public class ProxyResponseArgs(SessionEventArgs session, ResponseState responseS public class InitArgs { public required IServiceProvider ServiceProvider { get; init; } + /// + /// Indicates whether the proxy command (root command) is being invoked. + /// When false, a CLI subcommand is being executed and plugins should + /// skip initialization that is only relevant for proxy operation + /// (e.g., opening browsers, starting listeners). + /// + public bool IsProxyCommand { get; init; } = true; } public class OptionsLoadedArgs(ParseResult parseResult) diff --git a/DevProxy.Plugins/Inspection/DevToolsPlugin.cs b/DevProxy.Plugins/Inspection/DevToolsPlugin.cs index 0cf6cd44..7e270648 100644 --- a/DevProxy.Plugins/Inspection/DevToolsPlugin.cs +++ b/DevProxy.Plugins/Inspection/DevToolsPlugin.cs @@ -78,10 +78,17 @@ public sealed class DevToolsPlugin( public override async Task InitializeAsync(InitArgs e, CancellationToken cancellationToken) { + ArgumentNullException.ThrowIfNull(e); + _cancellationToken = cancellationToken; await base.InitializeAsync(e, cancellationToken); + if (!e.IsProxyCommand) + { + return; + } + InitInspector(); } diff --git a/DevProxy.Plugins/Inspection/OpenAITelemetryPlugin.cs b/DevProxy.Plugins/Inspection/OpenAITelemetryPlugin.cs index c9eb1351..8e9ff527 100644 --- a/DevProxy.Plugins/Inspection/OpenAITelemetryPlugin.cs +++ b/DevProxy.Plugins/Inspection/OpenAITelemetryPlugin.cs @@ -76,6 +76,11 @@ public override async Task InitializeAsync(InitArgs e, CancellationToken cancell await base.InitializeAsync(e, cancellationToken); + if (!e.IsProxyCommand) + { + return; + } + if (Configuration.IncludeCosts) { Configuration.PricesFile = ProxyUtils.GetFullPath(Configuration.PricesFile, ProxyConfiguration.ConfigFile); diff --git a/DevProxy/Plugins/PluginServiceExtensions.cs b/DevProxy/Plugins/PluginServiceExtensions.cs index 12afa104..90f48b23 100644 --- a/DevProxy/Plugins/PluginServiceExtensions.cs +++ b/DevProxy/Plugins/PluginServiceExtensions.cs @@ -181,7 +181,7 @@ private static void RegisterPlugin( // stage is synchronous, but if the implementation changes // in the future then we won't need to change the interface #pragma warning disable VSTHRD002 - plugin.InitializeAsync(new() { ServiceProvider = sp }, CancellationToken.None).Wait(); + plugin.InitializeAsync(new() { ServiceProvider = sp, IsProxyCommand = DevProxyCommand.IsRootCommand }, CancellationToken.None).Wait(); #pragma warning restore VSTHRD002 return plugin; }); @@ -200,7 +200,7 @@ private static void RegisterPlugin( // stage is synchronous, but if the implementation changes // in the future then we won't need to change the interface #pragma warning disable VSTHRD002 - plugin.InitializeAsync(new() { ServiceProvider = sp }, CancellationToken.None).Wait(); + plugin.InitializeAsync(new() { ServiceProvider = sp, IsProxyCommand = DevProxyCommand.IsRootCommand }, CancellationToken.None).Wait(); #pragma warning restore VSTHRD002 return plugin; });