Skip to content

Commit a394e28

Browse files
Address PR feedback: add install parameter and use IsRunMode instead of IsPublishMode
Co-authored-by: tommasodotNET <12819039+tommasodotNET@users.noreply.github.com>
1 parent d3756c9 commit a394e28

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,18 @@ private static string GetDefaultGoBaseImage(string workingDirectory, IServicePro
239239
/// Ensures Go module dependencies are tidied before the application starts using <c>go mod tidy</c>.
240240
/// </summary>
241241
/// <param name="builder">The Golang app resource builder.</param>
242+
/// <param name="install">When true (default), automatically runs go mod tidy before the application starts. When false, this method does nothing.</param>
242243
/// <param name="configureInstaller">Optional action to configure the installer resource.</param>
243244
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
244245
public static IResourceBuilder<GolangAppExecutableResource> WithGoModTidy(
245246
this IResourceBuilder<GolangAppExecutableResource> builder,
247+
bool install = true,
246248
Action<IResourceBuilder<GoModInstallerResource>>? configureInstaller = null)
247249
{
248250
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
249251

250-
// Only install packages during development, not in publish mode
251-
if (!builder.ApplicationBuilder.ExecutionContext.IsPublishMode)
252+
// Only install packages if in run mode and install is true
253+
if (builder.ApplicationBuilder.ExecutionContext.IsRunMode && install)
252254
{
253255
var installerName = $"{builder.Resource.Name}-go-mod-tidy";
254256
var installer = new GoModInstallerResource(installerName, builder.Resource.WorkingDirectory);
@@ -271,16 +273,18 @@ public static IResourceBuilder<GolangAppExecutableResource> WithGoModTidy(
271273
/// Ensures Go module dependencies are downloaded before the application starts using <c>go mod download</c>.
272274
/// </summary>
273275
/// <param name="builder">The Golang app resource builder.</param>
276+
/// <param name="install">When true (default), automatically runs go mod download before the application starts. When false, this method does nothing.</param>
274277
/// <param name="configureInstaller">Optional action to configure the installer resource.</param>
275278
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
276279
public static IResourceBuilder<GolangAppExecutableResource> WithGoModDownload(
277280
this IResourceBuilder<GolangAppExecutableResource> builder,
281+
bool install = true,
278282
Action<IResourceBuilder<GoModInstallerResource>>? configureInstaller = null)
279283
{
280284
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
281285

282-
// Only install packages during development, not in publish mode
283-
if (!builder.ApplicationBuilder.ExecutionContext.IsPublishMode)
286+
// Only install packages if in run mode and install is true
287+
if (builder.ApplicationBuilder.ExecutionContext.IsRunMode && install)
284288
{
285289
var installerName = $"{builder.Resource.Name}-go-mod-download";
286290
var installer = new GoModInstallerResource(installerName, builder.Resource.WorkingDirectory);

src/CommunityToolkit.Aspire.Hosting.Golang/README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ var golang = builder.AddGolangApp("golang", "../gin-api")
4343
.WithHttpEndpoint(env: "PORT");
4444
```
4545

46+
By default, `WithGoModTidy()` runs `go mod tidy` before the application starts (equivalent to `install: true`). You can disable this behavior by setting `install: false`:
47+
48+
```csharp
49+
var golang = builder.AddGolangApp("golang", "../gin-api")
50+
.WithGoModTidy(install: false) // Does not run go mod tidy
51+
.WithHttpEndpoint(env: "PORT");
52+
```
53+
4654
### Using `go mod download`
4755

4856
To run `go mod download` before your application starts (to download dependencies without verification):
@@ -53,7 +61,15 @@ var golang = builder.AddGolangApp("golang", "../gin-api")
5361
.WithHttpEndpoint(env: "PORT");
5462
```
5563

56-
Both methods create an installer resource that runs before your application starts, ensuring dependencies are available. The installer resource appears as a child resource in the Aspire dashboard.
64+
Similarly, you can control whether the download runs:
65+
66+
```csharp
67+
var golang = builder.AddGolangApp("golang", "../gin-api")
68+
.WithGoModDownload(install: false) // Does not run go mod download
69+
.WithHttpEndpoint(env: "PORT");
70+
```
71+
72+
Both methods create an installer resource that runs before your application starts when `install` is `true`, ensuring dependencies are available. The installer resource appears as a child resource in the Aspire dashboard.
5773

5874
You can also customize the installer resource using the optional `configureInstaller` parameter:
5975

@@ -66,7 +82,7 @@ var golang = builder.AddGolangApp("golang", "../gin-api")
6682
.WithHttpEndpoint(env: "PORT");
6783
```
6884

69-
> **Note:** The `WithGoModTidy` and `WithGoModDownload` methods only run during development. When publishing, the generated Dockerfile handles dependency management automatically.
85+
> **Note:** The `WithGoModTidy` and `WithGoModDownload` methods only run in run mode (when the application is started locally). They do not run when publishing, as the generated Dockerfile handles dependency management automatically.
7086
7187
## Publishing
7288

tests/CommunityToolkit.Aspire.Hosting.Golang.Tests/ResourceCreationTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,34 @@ public void WithGoModDownloadNullBuilderThrows()
182182

183183
Assert.Throws<ArgumentNullException>(() => builder.WithGoModDownload());
184184
}
185+
186+
[Fact]
187+
public void GolangAppWithGoModTidyInstallFalseDoesNotCreateInstaller()
188+
{
189+
var builder = DistributedApplication.CreateBuilder();
190+
191+
builder.AddGolangApp("golang", "../../examples/golang/gin-api").WithGoModTidy(install: false);
192+
193+
using var app = builder.Build();
194+
195+
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
196+
197+
var golangResource = Assert.Single(appModel.Resources.OfType<GolangAppExecutableResource>());
198+
Assert.Empty(appModel.Resources.OfType<GoModInstallerResource>());
199+
}
200+
201+
[Fact]
202+
public void GolangAppWithGoModDownloadInstallFalseDoesNotCreateInstaller()
203+
{
204+
var builder = DistributedApplication.CreateBuilder();
205+
206+
builder.AddGolangApp("golang", "../../examples/golang/gin-api").WithGoModDownload(install: false);
207+
208+
using var app = builder.Build();
209+
210+
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
211+
212+
var golangResource = Assert.Single(appModel.Resources.OfType<GolangAppExecutableResource>());
213+
Assert.Empty(appModel.Resources.OfType<GoModInstallerResource>());
214+
}
185215
}

0 commit comments

Comments
 (0)