Skip to content

Fix AddViteApp port configuration to use Aspire-assigned port #724

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

Merged
merged 15 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from 13 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
4 changes: 1 addition & 3 deletions examples/nodejs-ext/pnpm-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "run-script-os",
"dev:windows": "vite dev --port %PORT%",
"dev:default": "vite dev --port $PORT",
"dev": "vite dev",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
Expand Down
5 changes: 2 additions & 3 deletions examples/nodejs-ext/vite-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "run-script-os",
"dev:windows": "vite dev --port %PORT%",
"dev:default": "vite dev --port $PORT",
"dev": "vite dev",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
Expand All @@ -30,3 +28,4 @@
"vite": "^6.3.4"
}
}

5 changes: 2 additions & 3 deletions examples/nodejs-ext/yarn-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "run-script-os",
"dev:windows": "vite dev --port %PORT%",
"dev:default": "vite dev --port $PORT",
"dev": "vite dev",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
Expand All @@ -30,3 +28,4 @@
"vite": "^6.3.4"
}
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Lifecycle;
using CommunityToolkit.Aspire.Hosting.NodeJS.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using CommunityToolkit.Aspire.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Aspire.Hosting;

Expand Down Expand Up @@ -37,9 +37,21 @@ public static IResourceBuilder<NodeAppResource> AddViteApp(this IDistributedAppl
_ => builder.AddNpmApp(name, wd, "dev")
};

return useHttps
_ = useHttps
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Baking in WithExternalHttpEndpoints is probably a bad thing btw. We should revert that and let he user decide what to expose publicly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed .WithExternalHttpEndpoints() from both HTTP and HTTPS endpoint configurations in the AddViteApp method. Users now have full control over whether to expose endpoints externally. Updated the corresponding test to verify this behavior. (commit 91305ae)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree - the point of Vite is to create a website that can somehow be accessed, and when deployed that would need external HTTP(S) endpoints (in most scenarios - yes you could have them not public and have to handle IP allow listing/VPN/etc. so it can be accessed).

Aspire is full of opinionated defaults, and I think it's a pretty valid opinion that websites should have public endpoints.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imagine you were building a service with ingress proxy being your only public endpoint. We don’t make this assumption anywhere else because it’s something that the user should really do on their own. I do understand that it’s the least friction thing to do but baking in a security decision like this makes me super nervous.

? resource.WithHttpsEndpoint(env: "PORT").WithExternalHttpEndpoints()
: resource.WithHttpEndpoint(env: "PORT").WithExternalHttpEndpoints();

return resource.WithArgs(ctx =>
{
if (packageManager == "npm")
{
ctx.Args.Add("--");
}

var targetEndpoint = resource.Resource.GetEndpoint(useHttps ? "https" : "http");
ctx.Args.Add("--port");
ctx.Args.Add(targetEndpoint.Property(EndpointProperty.TargetPort));
});
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Aspire.Hosting;
using Microsoft.AspNetCore.Http;
using System.Diagnostics;

namespace CommunityToolkit.Aspire.Hosting.NodeJS.Extensions.Tests;

Expand Down Expand Up @@ -195,4 +193,36 @@ public void WithNpmPackageInstallationCanUseCICommand()
var resource = Assert.Single(appModel.Resources.OfType<NodeAppResource>());
Assert.Equal("npm", resource.Command);
}

[Fact]
public void ViteAppConfiguresPortFromEnvironment()
{
var builder = DistributedApplication.CreateBuilder();

builder.AddViteApp("vite");

using var app = builder.Build();

var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();

var resource = Assert.Single(appModel.Resources.OfType<NodeAppResource>());

// Verify that command line arguments callback is configured
Assert.True(resource.TryGetAnnotationsOfType<CommandLineArgsCallbackAnnotation>(out var argsCallbackAnnotations));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List<object> args = [];
var ctx = new CommandLineArgsCallbackContext(args);

foreach (var annotation in argsCallbackAnnotations)
{
annotation.Callback(ctx);
}

Assert.Collection(args,
arg => Assert.Equal("run", arg),
arg => Assert.Equal("dev", arg),
arg => Assert.Equal("--", arg),
arg => Assert.Equal("--port", arg),
arg => Assert.IsType<EndpointReferenceExpression>(arg)
);
}
}
Loading