microsoft / microsoft/aspire

.env files being resorted by Docker Compose publisher

Open
#14,577 1 comment 0 reactions 0 assignees View on GitHub
area-app-model
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

Partially related to #12703
We have some additional environment variables that depend on each other:
```
DOCKER_REGISTRY_HOSTNAME=registry.myhost.com
MIRRORS_DOCKER_REPOSITORY=${DOCKER_REGISTRY_HOSTNAME}/devops/container-images/mirrors/
ASPIRE_DOCKER_REPOSITORY=${MIRRORS_DOCKER_REPOSITORY}aspire-dashboard
```
Due to [EnvFile](https://github.com/dotnet/aspire/blob/64bf57f06f71a4bca3f3f3ab2c1212eed3226227/src/Aspire.Hosting.Docker/EnvFile.cs#L15) using `SortedDictionary`, the loaded lines get out of order. `ASPIRE_DOCKER_REPOSITORY` will be on top before `MIRRORS_DOCKER_REPOSITORY` is declared, leading to an error on startup.

### Expected Behavior

At least when already loading the existing file (`publish-compose`), make sure to not mess up the existing file. On `prepare-compose`, it's not loaded and just freshly written either way, which sadly blocks us from using it in the first place due to not being able to get sorting in there. But this would at least unblock `publish-compse` for us. Right now, we manually merge the changes to be able to at least manually start our environment.

Sadly `DockerComposeEnvironmentResource.AddEnvironmentVariable` is only reachable through `DockerComposeServiceExtensions.AsEnvironmentPlaceholder`, so it's not possible to just add our variables to the environment. If `DockerComposeEnvironmentResource` would offer to add self-defined environment variables directly AND have the `SortedDictionary` being replaced by a `Dictionary`, `prepare-compose` would also start working.

This sadly makes usage of most `aspire do` commands unusable. We COULD avoid using composed values, but this would make the generated docker-compose file less flexible for quick manipulation on customer systems.

### Steps To Reproduce

1. Create an Aspire project using environment variables
2. Add the above mentioned variables
3. Call `aspire do publish-compose`
4. Check the sorting in `.env`

### Exceptions (if any)

_No response_

### .NET Version info

.NET SDK:
Version: 10.0.103
Commit: c2435c3e0f
Workload version: 10.0.100-manifests.a62d7899
MSBuild version: 18.0.11+c2435c3e0

Runtime Environment:
OS Name: Mac OS X
OS Version: 26.3
OS Platform: Darwin
RID: osx-arm64
Base Path: /usr/local/share/dotnet/sdk/10.0.103/

.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.

Host:
Version: 10.0.3
Architecture: arm64
Commit: c2435c3e0f

.NET SDKs installed:
9.0.311 [/usr/local/share/dotnet/sdk]
10.0.103 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 9.0.13 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 9.0.13 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
None

Environment variables:
DOTNET_INSTALL_DIR [/usr/local/share/dotnet/]

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

### Anything else?

Aspire version: 13.1.1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.