Skip to content

Commit

Permalink
Support environment variables defined in containerEnv
Browse files Browse the repository at this point in the history
envbuilder only looked at remoteEnv, but it should also define variables
from containerEnv. containerEnv is recommended over remoteEnv by the
spec:

> We recommend using containerEnv (over remoteEnv) as much as possible
> since it allows all processes to see the variable and isn’t
> client-specific.
  • Loading branch information
aaronlehmann committed Mar 2, 2024
1 parent eb069e4 commit 472990e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions devcontainer/devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Spec struct {
Build BuildSpec `json:"build"`
RemoteUser string `json:"remoteUser"`
ContainerUser string `json:"containerUser"`
ContainerEnv map[string]string `json:"containerEnv"`
RemoteEnv map[string]string `json:"remoteEnv"`
// Features is a map of feature names to feature configurations.
Features map[string]any `json:"features"`
Expand Down
4 changes: 2 additions & 2 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,8 @@ func Run(ctx context.Context, options Options) error {

configFile.Config.User = container.RemoteUser
}
if container.RemoteEnv != nil {
for key, value := range container.RemoteEnv {
for _, env := range []map[string]string{container.ContainerEnv, container.RemoteEnv} {
for key, value := range env {
os.Setenv(key, value)
exportEnv(key, value)
}
Expand Down
8 changes: 6 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,11 @@ func TestExportEnvFile(t *testing.T) {
"build": {
"dockerfile": "Dockerfile"
},
"containerEnv": {
"FROM_CONTAINER_ENV": "bar"
}
"remoteEnv": {
"FROM_DEVCONTAINER_JSON": "bar"
"FROM_REMOTE_ENV": "baz"
}
}`,
".devcontainer/Dockerfile": "FROM alpine:latest\nENV FROM_DOCKERFILE=foo",
Expand All @@ -425,7 +428,8 @@ func TestExportEnvFile(t *testing.T) {
output := execContainer(t, ctr, "cat /env")
require.Contains(t, strings.TrimSpace(output),
`FROM_DOCKERFILE=foo
FROM_DEVCONTAINER_JSON=bar`)
FROM_CONTAINER_ENV=bar
FROM_REMOTE_ENV=baz`)
}

func TestLifecycleScripts(t *testing.T) {
Expand Down

0 comments on commit 472990e

Please sign in to comment.