Skip to content

Fix GetCurrentExecutableInfo to use CreateBuilderAsync arguments instead of Environment.GetCommandLineArgs #6180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 25, 2025

The CurrentTestApplicationModuleInfo.GetCurrentExecutableInfo() method was always using Environment.GetCommandLineArgs() to retrieve command line arguments, ignoring any custom arguments passed to TestApplication.CreateBuilderAsync(). This caused issues with extensions like retry that depend on seeing the actual arguments used to configure the test application.

Problem

When users write custom code to modify arguments before calling CreateBuilderAsync():

// User's custom Main method
if (args.Length == 0)
{
    args = ["--retry-failed-tests", "1"];
}

var builder = await TestApplication.CreateBuilderAsync(args);
using var app = await builder.BuildAsync();
return await app.RunAsync();

The retry extension would fail because GetCurrentExecutableInfo() returned the original empty command line arguments from Environment.GetCommandLineArgs(), not the modified args ["--retry-failed-tests", "1"] passed to CreateBuilderAsync().

Solution

This PR implements a service-based approach to capture and provide access to the original arguments passed to CreateBuilderAsync():

  1. Created ICommandLineArgumentsProvider interface and implementation to store the original arguments
  2. Modified the builder chain to pass arguments through TestApplicationTestApplicationBuilderTestHostBuilder
  3. Updated GetCurrentExecutableInfo() to intelligently combine custom arguments with environment arguments
  4. Maintained backward compatibility by falling back to environment arguments when no custom arguments are provided

Key Features

Smart Argument Handling: The implementation correctly handles different execution scenarios:

  • Executable scenarios: Uses custom args while preserving executable name from environment
  • Dotnet scenarios: Combines assembly path from environment with custom args to ensure proper command reconstruction
  • Backward compatibility: Falls back to original behavior when no custom args are provided

Example transformations:

// Dotnet with custom args:
// Environment: ["dotnet", "MyTest.dll"] 
// Custom: ["--retry-failed-tests", "1"]
// Result: ["exec", "dotnet", "MyTest.dll", "--retry-failed-tests", "1"]

Testing

Added comprehensive unit tests covering:

  • Executable scenarios with custom arguments
  • Dotnet scenarios with custom arguments
  • Backward compatibility when no custom arguments are provided

The fix ensures that retry and other extensions now correctly see and use the arguments that were actually passed to CreateBuilderAsync(), while maintaining full compatibility with dotnet execution scenarios.

Fixes #5239.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits July 25, 2025 20:55
…arguments

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
…os and add tests

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [MTP] GetCurrentExecutableInfo shouldn't calculate command line arguments via Environment Fix GetCurrentExecutableInfo to use CreateBuilderAsync arguments instead of Environment.GetCommandLineArgs Jul 25, 2025
@Copilot Copilot AI requested a review from Evangelink July 25, 2025 21:05
Copilot finished work on behalf of Evangelink July 25, 2025 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[MTP] GetCurrentExecutableInfo shouldn't calculate command line arguments via Environment
2 participants