Aspire.Hosting.Tests runs apphosts with currrent directory set to bindir for the Test project
I hit this when running playground/keycloak/Keycloak.AppHost/Keycloak.AppHost.csproj with Aspire.Hosting.Testing like in https://github.com/dotnet/aspire/pull/5208/files#diff-5051cd0c129b32d78bdb46214c5ec1f3db3b004e77ea64fe95791a1afc0ed520R28-R39 .
The test loads the assembly for the apphost, and tries to run it. But it fails because ..
https://github.com/dotnet/aspire/blob/22f1e122d8220ac2d1b42556ced4e3d8f68287eb/playground/keycloak/Keycloak.AppHost/Program.cs#L3-L6
.. tries to load WithRealmImport("../realms") but the current directory is /Users/ankj/dev/aspire/artifacts/bin/Aspire.Playground.Tests/Release/net8.0 instead of /Users/ankj/dev/aspire/playground/keycloak/Keycloak.AppHost/.
Failed Aspire.Playground.Tests.AppHostTests.TestEndpointsReturnOk(testEndpoints: Keycloak.AppHost (1 resources)) [211 ms]
Error Message:
System.IO.DirectoryNotFoundException : The realm import directory ../realms does not exist.
Stack Trace:
at Aspire.Hosting.KeycloakResourceBuilderExtensions.WithRealmImport(IResourceBuilder`1 builder, String importDirectory, Boolean isReadOnly) in /Users/ankj/dev/aspire/src/Aspire.Hosting.Keycloak/KeycloakResourceBuilderExtensions.cs:line 143
at Program.<Main>$(String[] args) in /Users/ankj/dev/aspire/playground/keycloak/Keycloak.AppHost/Program.cs:line 3
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
--- End of stack trace from previous location ---
at Aspire.Hosting.Testing.DistributedApplicationEntryPointInvoker.EntryPointInvoker.InvokeAsync(CancellationToken cancellationToken) in /Users/ankj/dev/aspire/src/Aspire.Hosting.Testing/DistributedApplicationEntryPointInvoker.cs:line 132
at Aspire.Hosting.Testing.DistributedApplicationEntryPointInvoker.<>c__DisplayClass0_0.<<ResolveEntryPoint>b__0>d.MoveNext() in /Users/ankj/dev/aspire/src/Aspire.Hosting.Testing/DistributedApplicationEntryPointInvoker.cs:line 39
--- End of stack trace from previous location ---
at Aspire.Hosting.Testing.DistributedApplicationFactory.InvokeEntryPoint(Func`3 factory) in /Users/ankj/dev/aspire/src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs:line 274
at Aspire.Hosting.Testing.DistributedApplicationFactory.ResolveBuilderAsync(CancellationToken cancellationToken) in /Users/ankj/dev/aspire/src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs:line 45
at Aspire.Hosting.Testing.DistributedApplicationTestingBuilder.SuspendingDistributedApplicationFactory.CreateBuilderAsync(CancellationToken cancellationToken) in /Users/ankj/dev/aspire/src/Aspire.Hosting.Testing/DistributedApplicationTestingBuilder.cs:line 96
at Aspire.Hosting.Testing.DistributedApplicationTestingBuilder.CreateAsync(Type entryPoint, String[] args, Action`2 configureBuilder, CancellationToken cancellationToken) in /Users/ankj/dev/aspire/src/Aspire.Hosting.Testing/DistributedApplicationTestingBuilder.cs:line 86
at SamplesIntegrationTests.DistributedApplicationTestFactory.CreateAsync(String appHostAssemblyPath, ITestOutputHelper testOutput) in /Users/ankj/dev/aspire/tests/Aspire.Playground.Tests/Infrastructure/DistributedApplicationTestFactory.cs:line 26
at Aspire.Playground.Tests.AppHostTests.TestEndpointsReturnOk(TestEndpoints testEndpoints) in /Users/ankj/dev/aspire/tests/Aspire.Playground.Tests/AppHostTests.cs:line 49
cc @ReubenBond @eerhardt
I think the CWD should be set before starting the apphost, so it has the same-ish environment as when using dotnet run. Does this make sense - https://github.com/dotnet/aspire/pull/5220 ?
Not sure if we should be setting CWD, but we should set the apphost base directory.
We avoided changing the CWD for the process as it might cause issues for tests running in parallel. see this.
The issue was fixed in https://github.com/dotnet/aspire/pull/5220 .