Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Aspire.Hosting.Azure/AzureResourcePreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,10 @@ private async Task<HashSet<IAzureResource>> GetAzureReferences(IResource resourc

if (resource.TryGetAnnotationsOfType<CommandLineArgsCallbackAnnotation>(out var commandLineArgsCallbackAnnotations))
{
var context = new CommandLineArgsCallbackContext([], resource, cancellationToken: cancellationToken);
var context = new CommandLineArgsCallbackContext([], resource, cancellationToken: cancellationToken)
{
ExecutionContext = executionContext
};

foreach (var c in commandLineArgsCallbackAnnotations)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Aspire.Hosting/ResourceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2310,15 +2310,15 @@ public static IResourceBuilder<T> WithVSCodeDebugSupport<T>(this IResourceBuilde
ArgumentException.ThrowIfNullOrWhiteSpace(projectPath);
ArgumentException.ThrowIfNullOrWhiteSpace(debugAdapterId);

if (!builder.ApplicationBuilder.ExecutionContext.IsRunMode)
{
return builder;
}

if (builder is IResourceBuilder<IResourceWithArgs> resourceWithArgs)
{
resourceWithArgs.WithArgs(ctx =>
{
if (!ctx.ExecutionContext.IsRunMode)
{
return;
}

var config = ctx.ExecutionContext.ServiceProvider.GetRequiredService<IConfiguration>();
if (ExtensionUtils.IsExtensionHost(config) && argsCallback is not null)
{
Expand Down
27 changes: 27 additions & 0 deletions tests/Aspire.Hosting.Azure.Tests/AzureResourcePreparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,33 @@ public async Task NullCommandLineArgIsIgnored()
Assert.True(true);
}

[Fact]
public async Task CommandLineArgsCallbackContextHasCorrectExecutionContextDuringPublish()
{
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);
builder.AddAzureContainerAppEnvironment("env");

DistributedApplicationExecutionContext? capturedExecutionContext = null;

// Create a project with a WithArgs callback that captures the ExecutionContext
var api = builder.AddProject<Project>("api", launchProfileName: null)
.WithArgs(context =>
{
// Capture the ExecutionContext to verify it's set correctly
capturedExecutionContext = context.ExecutionContext;
});

using var app = builder.Build();

// This should not throw - the ExecutionContext should be set correctly
await ExecuteBeforeStartHooksAsync(app, default);

// Verify the ExecutionContext was captured and is in Publish mode
Assert.NotNull(capturedExecutionContext);
Assert.True(capturedExecutionContext.IsPublishMode);
Assert.False(capturedExecutionContext.IsRunMode);
}

private sealed class Project : IProjectMetadata
{
public string ProjectPath => "project";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#pragma warning disable ASPIREEXTENSION001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable IDE0005 // Using directive is unnecessary.

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Utils;

namespace Aspire.Hosting.Tests;
Expand Down Expand Up @@ -65,4 +70,29 @@ public void WithWorkingDirectoryAllowsEmptyString()
var annotation = executable.Resource.Annotations.OfType<ExecutableAnnotation>().Single();
Assert.Equal(builder.AppHostDirectory, annotation.WorkingDirectory);
}

[Fact]
public void WithVSCodeDebugSupportAddsAnnotationInRunMode()
{
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Run);
var executable = builder.AddExecutable("myexe", "command", "workingdirectory")
.WithVSCodeDebugSupport("project.py", "python", "ms-python.python");

var annotation = executable.Resource.Annotations.OfType<SupportsDebuggingAnnotation>().SingleOrDefault();
Assert.NotNull(annotation);
Assert.Equal("project.py", annotation.ProjectPath);
Assert.Equal("python", annotation.DebugAdapterId);
Assert.Equal("ms-python.python", annotation.RequiredExtensionId);
}

[Fact]
public void WithVSCodeDebugSupportDoesNotAddAnnotationInPublishMode()
{
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);
var executable = builder.AddExecutable("myexe", "command", "workingdirectory")
.WithVSCodeDebugSupport("project.py", "python", "ms-python.python");

var annotation = executable.Resource.Annotations.OfType<SupportsDebuggingAnnotation>().SingleOrDefault();
Assert.Null(annotation);
}
}