From ef172137386d8519fa062bed76c8b9ff350d8eed Mon Sep 17 00:00:00 2001 From: Michael Collins Date: Sun, 15 Jun 2025 22:22:00 -0700 Subject: [PATCH 1/2] Add Go Build Tags Support I updated GolangAppHostingExtension to support specifying Go build tags to be used when building and running Go applications. Go build tags can be used to include or exclude files from the build process based On specific conditions. For example, when using Aspire for development, a Go developer may use a build tag to enable special code for OpenTelemetry instrumentation or enable debugging features. Closes #730 --- .../GolangAppHostingExtension.cs | 19 ++++++++-- .../ResourceCreationTests.cs | 37 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs b/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs index 4541f9f0..e6f6d1cf 100644 --- a/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs +++ b/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs @@ -15,16 +15,27 @@ public static class GolangAppHostingExtension /// The name of the resource. /// The working directory to use for the command. If null, the working directory of the current process is used. /// The optinal arguments to be passed to the executable when it is started. + /// The optional build tags to be used when building the Golang application. /// A reference to the . - public static IResourceBuilder AddGolangApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string[]? args = null) + public static IResourceBuilder AddGolangApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string[]? args = null, string[]? buildTags = null) { ArgumentNullException.ThrowIfNull(builder, nameof(builder)); ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name)); ArgumentException.ThrowIfNullOrWhiteSpace(workingDirectory, nameof(workingDirectory)); - string[] allArgs = args is { Length: > 0 } - ? ["run", ".", .. args] - : ["run", ".",]; + string[] allArgs; + if (buildTags is { Length: > 0 }) + { + allArgs = args is { Length: > 0 } + ? ["run", "-tags", string.Join(",", buildTags), ".", .. args ?? []] + : ["run", "-tags", string.Join(",", buildTags), "."]; + } + else + { + allArgs = args is { Length: > 0 } + ? ["run", ".", .. args] + : ["run", ".",]; + } workingDirectory = Path.Combine(builder.AppHostDirectory, workingDirectory).NormalizePathForCurrentPlatform(); var resource = new GolangAppExecutableResource(name, workingDirectory); diff --git a/tests/CommunityToolkit.Aspire.Hosting.Golang.Tests/ResourceCreationTests.cs b/tests/CommunityToolkit.Aspire.Hosting.Golang.Tests/ResourceCreationTests.cs index c6e0ec1a..b0a0aff3 100644 --- a/tests/CommunityToolkit.Aspire.Hosting.Golang.Tests/ResourceCreationTests.cs +++ b/tests/CommunityToolkit.Aspire.Hosting.Golang.Tests/ResourceCreationTests.cs @@ -21,4 +21,41 @@ public void DefaultGolangApp() Assert.Equal("go", resource.Command); } + + [Fact] + public async Task GolangAppWithBuildTagsAsync() + { + var builder = DistributedApplication.CreateBuilder(); + + builder.AddGolangApp("golang", "../../examples/golang/gin-api", buildTags: ["dev"]); + + using var app = builder.Build(); + + var appModel = app.Services.GetRequiredService(); + + var resource = appModel.Resources.OfType().SingleOrDefault(); + + Assert.NotNull(resource); + + var args = await resource.GetArgumentValuesAsync(); + Assert.Collection( + args, + arg => + { + Assert.Equal("run", arg); + }, + arg => + { + Assert.Equal("-tags", arg); + }, + arg => + { + Assert.Equal("dev", arg); + }, + arg => + { + Assert.Equal(".", arg); + } + ); + } } \ No newline at end of file From 7135af08945b1155d92375779d43a497f615a651 Mon Sep 17 00:00:00 2001 From: Michael Collins Date: Mon, 16 Jun 2025 08:30:26 -0700 Subject: [PATCH 2/2] Revise Command-Line Building I revised the way that I was building the command line for the `go run` command based on review recommendations. The new approach should improve readability of the code. --- .../GolangAppHostingExtension.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs b/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs index e6f6d1cf..23f3b453 100644 --- a/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs +++ b/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs @@ -23,18 +23,19 @@ public static IResourceBuilder AddGolangApp(this ID ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name)); ArgumentException.ThrowIfNullOrWhiteSpace(workingDirectory, nameof(workingDirectory)); - string[] allArgs; + var allArgs = new List { "run" }; + if (buildTags is { Length: > 0 }) { - allArgs = args is { Length: > 0 } - ? ["run", "-tags", string.Join(",", buildTags), ".", .. args ?? []] - : ["run", "-tags", string.Join(",", buildTags), "."]; + allArgs.Add("-tags"); + allArgs.Add(string.Join(",", buildTags)); } - else + + allArgs.Add("."); + + if (args is { Length: > 0 }) { - allArgs = args is { Length: > 0 } - ? ["run", ".", .. args] - : ["run", ".",]; + allArgs.AddRange(args); } workingDirectory = Path.Combine(builder.AppHostDirectory, workingDirectory).NormalizePathForCurrentPlatform(); @@ -42,7 +43,7 @@ public static IResourceBuilder AddGolangApp(this ID return builder.AddResource(resource) .WithGolangDefaults() - .WithArgs(allArgs); + .WithArgs(allArgs.ToArray()); } private static IResourceBuilder WithGolangDefaults(