-
Notifications
You must be signed in to change notification settings - Fork 144
Description
The current implementation of the GolangAppHostingExtension.AddGolangApp extension makes the assumption that the main.go file exists in the directory specified by the workingDirectory argument. For example:
builder.AddGolangApp("web", "../../web")This code block will execute the following command: go run . with the working directory relative to the execution directory for the Aspire host.
Following the Go Project Layout specification, executable programs should be placed in a subdirectory of the cmd subdirectory. While the workingDirectory argument could point to that directory, the program may have valid reasons to need to be executed in the context of a different working directory, such as the root directory of the Go project, to locate dependencies and resources needed at runtime.
I would like to propose creating a new override of AddGolangApp that takes a path to the package directory or source file to execute. The existing implementation of AddGolangApp can be moved to the new override, just replacing the name of the executable program in the arguments passed to the go run command. Since the executable name will not be null, this should not cause any breaking changes for existing consumers.
// Existing method
public static IResourceBuilder<GolangAppExecutableResource> AddGolangApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string[]? args = null, string[]? buildTags = null)
=> AddGolangApp(builder, name, workingDirectory, ".", args, buildTags);
// New overload
public static IResourceBuilder<GolangAppExecutableResource> AddGolangApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string executable, string[]? args = null, string[]? buildTags = null)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name));
ArgumentException.ThrowIfNullOrWhiteSpace(workingDirectory, nameof(workingDirectory));
var allArgs = new List<string> { "run" };
if (buildTags is { Length: > 0 })
{
allArgs.Add("-tags");
allArgs.Add(string.Join(",", buildTags));
}
allArgs.Add(executable); // <-- this changes to pass the executable argument
if (args is { Length: > 0 })
{
allArgs.AddRange(args);
}
workingDirectory = Path.Combine(builder.AppHostDirectory, workingDirectory).NormalizePathForCurrentPlatform();
var resource = new GolangAppExecutableResource(name, workingDirectory);
return builder.AddResource(resource)
.WithGolangDefaults()
.WithArgs([.. allArgs]);
}I can make this change and submit a PR if approved.