Skip to content

Commit dad66a7

Browse files
aledbfclaude
andcommitted
fix(features): keep host variables raw in the metadata label
A Feature's mounts were resolved on the build host before the image's devcontainer.metadata label was written, so the image carried that host's paths (${localEnv:HOME} became /home/<user>) and resolved them the same way on every other host. The metadata substitution also wrote its result back through mounts and maps it shared with the label's entries, so in compose it rewrote base-image mounts in the label too. As the reference CLI does, the label now keeps each Feature's values as the Feature declares them, and up resolves them on the host it runs on, from a copy: for single-container and compose, where Feature lifecycle commands with host variables used to run unresolved, and in read-configuration's merged configuration, which printed them raw. Only the Dockerfile ENV line for a Feature's containerEnv stays resolved at build time (upstream #308). Three runtime parity cases compare the label, the compose labels and the merged configuration with the reference. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent af38ca7 commit dad66a7

11 files changed

Lines changed: 146 additions & 24 deletions

‎docs/parity/parity-matrix.yaml‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,48 @@ initial_cases:
10931093
current_status: match
10941094
notes: "The compose service's Features Dockerfile (appended to `FROM <image> AS dev_container_auto_added_stage_label`) has the same structure as the image build's: builtin env, consecutiveId folders, install wrapper, the Feature's containerEnv and, like TS getFeaturesBuildOptions, the config's containerEnv as ENV lines (CFG_VAR). Go used the _dev_container_feature_<i> layout and left the config's containerEnv out of the compose image."
10951095

1096+
- id: build.feature-host-variables-raw-label-success
1097+
lane: runtime
1098+
command: build
1099+
priority: p1
1100+
docker_required: true
1101+
setup_cmd: "WORKSPACE=/tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}; rm -rf $WORKSPACE; mkdir -p $WORKSPACE/.devcontainer/f; printf '%s' '{\"id\":\"f\",\"version\":\"1.0.0\",\"mounts\":[{\"source\":\"${localEnv:HOME}\",\"target\":\"/parity-home\",\"type\":\"bind\"}],\"postCreateCommand\":\"echo ${localEnv:HOME}\"}' > $WORKSPACE/.devcontainer/f/devcontainer-feature.json; printf '#!/bin/sh\\ntrue\\n' > $WORKSPACE/.devcontainer/f/install.sh; chmod +x $WORKSPACE/.devcontainer/f/install.sh; printf '%s' '{\"image\":\"alpine:3\",\"features\":{\"./f\":{}}}' > $WORKSPACE/.devcontainer/devcontainer.json; echo WORKSPACE=$WORKSPACE"
1102+
ts_cmd: "build --workspace-folder ${WORKSPACE} --image-name parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
1103+
verify_cmd: "docker image inspect -f '{{index .Config.Labels \"devcontainer.metadata\"}}' ${IMAGE_NAME}"
1104+
cleanup_cmd: "docker rmi -f parity-${PARITY_CASE_ID}-${PARITY_SIDE} >/dev/null 2>&1; rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}; true"
1105+
asserts: [exit_code, stdout_normalized]
1106+
class: build-runtime
1107+
current_status: match
1108+
notes: "TS getDevcontainerMetadata writes the Feature's raw values to the devcontainer.metadata label (imageMetadata.ts uses .raw), so a Feature mount and lifecycle command keep ${localEnv:HOME} and are resolved on the host that runs `up`. Go resolved the Feature's mounts before building the label, baking the build host's HOME into the image."
1109+
1110+
- id: up.compose-feature-host-variables-raw-label-success
1111+
lane: runtime
1112+
command: up
1113+
priority: p1
1114+
docker_required: true
1115+
setup_cmd: "WORKSPACE=/tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}; rm -rf $WORKSPACE; mkdir -p $WORKSPACE/.devcontainer/f; printf '%s' '{\"id\":\"f\",\"version\":\"1.0.0\",\"mounts\":[{\"source\":\"${localEnv:HOME}\",\"target\":\"/parity-home\",\"type\":\"bind\"}],\"postCreateCommand\":\"echo ${localEnv:HOME}\"}' > $WORKSPACE/.devcontainer/f/devcontainer-feature.json; printf '#!/bin/sh\\ntrue\\n' > $WORKSPACE/.devcontainer/f/install.sh; chmod +x $WORKSPACE/.devcontainer/f/install.sh; printf 'services:\\n app:\\n image: alpine:3\\n command: sleep infinity\\n' > $WORKSPACE/.devcontainer/docker-compose.yml; printf '%s' '{\"dockerComposeFile\":\"docker-compose.yml\",\"service\":\"app\",\"workspaceFolder\":\"/\",\"features\":{\"./f\":{}}}' > $WORKSPACE/.devcontainer/devcontainer.json; echo WORKSPACE=$WORKSPACE"
1116+
ts_cmd: "up --workspace-folder ${WORKSPACE} --skip-post-create --update-remote-user-uid-default never --id-label parity.case=${PARITY_CASE_ID}-${PARITY_SIDE}"
1117+
verify_cmd: "docker inspect -f '{{index .Config.Labels \"devcontainer.metadata\"}}' ${CONTAINER_ID}; docker image inspect -f '{{index .Config.Labels \"devcontainer.metadata\"}}' $(docker inspect -f '{{.Image}}' ${CONTAINER_ID}); docker inspect -f '{{range .Mounts}}{{.Source}}->{{.Destination}} {{end}}' ${CONTAINER_ID}"
1118+
cleanup_cmd: "docker compose --project-name ${COMPOSE_PROJECT_NAME} -f ${WORKSPACE}/.devcontainer/docker-compose.yml down -t 0 >/dev/null 2>&1; rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}; true"
1119+
asserts: [exit_code, stdout_normalized]
1120+
class: up-runtime
1121+
current_status: match
1122+
notes: "The compose service's image and container labels keep the Feature's ${localEnv:HOME} raw, as TS writes them; the bind mount the container gets is resolved on this host (TS getDevcontainerMetadata's featureRaw.map(substitute)). Go resolved the Feature's mounts before building the label."
1123+
1124+
- id: read-configuration.feature-host-variables-merged-success
1125+
lane: runtime
1126+
command: read-configuration
1127+
priority: p1
1128+
docker_required: true
1129+
setup_cmd: "WORKSPACE=/tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}; rm -rf $WORKSPACE; mkdir -p $WORKSPACE/.devcontainer/f; printf '%s' '{\"id\":\"f\",\"version\":\"1.0.0\",\"mounts\":[{\"source\":\"${localEnv:HOME}\",\"target\":\"/parity-home\",\"type\":\"bind\"}],\"postCreateCommand\":\"echo ${localEnv:HOME}\"}' > $WORKSPACE/.devcontainer/f/devcontainer-feature.json; printf '#!/bin/sh\\ntrue\\n' > $WORKSPACE/.devcontainer/f/install.sh; chmod +x $WORKSPACE/.devcontainer/f/install.sh; printf '%s' '{\"image\":\"alpine:3\",\"features\":{\"./f\":{}}}' > $WORKSPACE/.devcontainer/devcontainer.json; echo WORKSPACE=$WORKSPACE"
1130+
ts_cmd: "read-configuration --workspace-folder ${WORKSPACE} --include-merged-configuration"
1131+
verify_cmd: "true"
1132+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
1133+
asserts: [exit_code, stdout_normalized]
1134+
class: merged-configuration
1135+
current_status: match
1136+
notes: "Without a container, TS getDevcontainerMetadata resolves the Features' raw entries with the configuration's host substitution, so mergedConfiguration lists the Feature's mount and postCreateCommand with ${localEnv:HOME} resolved. Go listed them raw."
1137+
10961138
- id: read-configuration.container-id-missing-error
10971139
lane: runtime
10981140
command: read-configuration

‎internal/cli/compose_up.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,11 @@ func (r *upRunner) buildAndExtendCompose(ctx context.Context, s composeStart, pe
399399
featureEntries = append(featureEntries, featureMetadataEntry(fs, s.skipPersistCustoms))
400400
}
401401
labelMetadata := slices.Concat(baseEntries, featureEntries, []imagemeta.Entry{r.configLabel})
402-
metadata := slices.Clone(baseEntries)
402+
metadata := slices.Concat(baseEntries, featureEntries)
403403
if err := r.substitute(&metadata); err != nil {
404404
return nil, fmt.Errorf("substitute the image's metadata: %w", err)
405405
}
406-
metadata = slices.Concat(metadata, featureEntries, []imagemeta.Entry{configMetadataEntry(cfg, false)})
406+
metadata = append(metadata, configMetadataEntry(cfg, false))
407407

408408
result := &composeExtendResult{imageMetadata: metadata}
409409
userSyntax := ""

‎internal/cli/compose_up_held_test.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,3 +1278,32 @@ func TestBuildAndExtendComposeForTheBuildCommand(t *testing.T) {
12781278
}
12791279
}
12801280
}
1281+
1282+
func TestBuildAndExtendComposeKeepsFeatureHostVariablesRawInTheLabel(t *testing.T) {
1283+
h := newComposeHarness(t)
1284+
h.write("vars/devcontainer-feature.json", `{"id":"vars","version":"1.0.0","containerEnv":{"FE":"${localEnv:SRC}/env"},"mounts":[{"source":"${localEnv:SRC}","target":"/m","type":"bind"}],"postCreateCommand":"echo ${localEnv:SRC}"}`)
1285+
h.write("vars/install.sh", "#!/bin/sh\n")
1286+
s := h.start(&config.DevContainer{Features: map[string]any{"./vars": map[string]any{}}}, map[string]any{"image": "alpine:3"})
1287+
s.loadResult.HostSub = config.HostSubContext{Env: map[string]string{"SRC": "/host/src"}}
1288+
h.r.substitute = hostMetadataSubstitution(s.loadResult.HostSub, s.idLabels)
1289+
res, err := h.r.buildAndExtendCompose(context.Background(), s, h.persisted, "", true)
1290+
if err != nil {
1291+
t.Fatal(err)
1292+
}
1293+
content := readFile(t, overrideDockerfile(t, readFile(t, res.overrideFiles[0])))
1294+
for _, want := range []string{`\"source\":\"\${localEnv:SRC}\"`, `\"postCreateCommand\":\"echo \${localEnv:SRC}\"`, `ENV FE="/host/src/env"`} {
1295+
if !strings.Contains(content, want) {
1296+
t.Errorf("Dockerfile lacks %s:\n%s", want, content)
1297+
}
1298+
}
1299+
if strings.Contains(content, `\"source\":\"/host/src\"`) {
1300+
t.Errorf("the label carries the build host's mount source:\n%s", content)
1301+
}
1302+
feature := res.imageMetadata[1]
1303+
if got := feature.Mounts[0].(map[string]any)["source"]; got != "/host/src" {
1304+
t.Errorf("the container's Feature mount source = %v, want /host/src", got)
1305+
}
1306+
if feature.PostCreateCommand != "echo /host/src" {
1307+
t.Errorf("the container's Feature postCreateCommand = %v, want echo /host/src", feature.PostCreateCommand)
1308+
}
1309+
}

‎internal/cli/e2e_fixes_test.go‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ func TestE2E_FeatureUserHomeAndLocalEnv(t *testing.T) {
4444
t.Skip("Docker not available")
4545
}
4646
t.Setenv("E2E_LOCALENV_MARKER", "resolved-localenv-1234")
47+
mountSrc := t.TempDir()
48+
if err := os.WriteFile(filepath.Join(mountSrc, "marker"), []byte("mounted-from-localenv"), 0o644); err != nil {
49+
t.Fatal(err)
50+
}
51+
t.Setenv("E2E_LOCALENV_MOUNT", mountSrc)
4752

4853
ws := writeFixture(t, map[string]string{
4954
".devcontainer/devcontainer.json": `{
@@ -55,7 +60,8 @@ func TestE2E_FeatureUserHomeAndLocalEnv(t *testing.T) {
5560
"id": "localfeat",
5661
"version": "1.0.0",
5762
"name": "localfeat",
58-
"containerEnv": { "MY_LOCAL_ENV": "${localEnv:E2E_LOCALENV_MARKER}" }
63+
"containerEnv": { "MY_LOCAL_ENV": "${localEnv:E2E_LOCALENV_MARKER}" },
64+
"mounts": [{ "source": "${localEnv:E2E_LOCALENV_MOUNT}", "target": "/e2e-localenv-mount", "type": "bind" }]
5965
}`,
6066
".devcontainer/localfeat/install.sh": "#!/bin/sh\nset -e\n" +
6167
`echo "REMOTE_USER_HOME=[$_REMOTE_USER_HOME]" > /e2e-feature-marker` + "\n",
@@ -75,6 +81,16 @@ func TestE2E_FeatureUserHomeAndLocalEnv(t *testing.T) {
7581
if got := dockerExec(t, id, "printenv MY_LOCAL_ENV"); got != "resolved-localenv-1234" {
7682
t.Errorf("#308 Feature containerEnv ${localEnv} = %q, want resolved-localenv-1234", got)
7783
}
84+
if got := dockerExec(t, id, "cat /e2e-localenv-mount/marker"); got != "mounted-from-localenv" {
85+
t.Errorf("Feature mount ${localEnv} not resolved on the host running up: %q", got)
86+
}
87+
out, err := exec.Command("sh", "-c", `docker image inspect -f '{{index .Config.Labels "devcontainer.metadata"}}' "$(docker inspect -f '{{.Image}}' `+id+`)"`).CombinedOutput()
88+
if err != nil {
89+
t.Fatalf("inspect the container's image: %v\n%s", err, out)
90+
}
91+
if label := string(out); !strings.Contains(label, `"source":"${localEnv:E2E_LOCALENV_MOUNT}"`) || strings.Contains(label, mountSrc) {
92+
t.Errorf("image label = %s, want the Feature's mount source raw", label)
93+
}
7894
}
7995

8096
func TestE2E_RemoveExistingBeforeInitialize(t *testing.T) {

‎internal/cli/feature_hostsub_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestSubstituteFeatureHostVars(t *testing.T) {
3131
if got := f.ContainerEnv["WS"]; got != "/home/me/proj/x" {
3232
t.Errorf("containerEnv WS = %q, want /home/me/proj/x", got)
3333
}
34-
if got := f.Mounts[0].(string); got != "source=/home/me/.ssh,target=/root/.ssh,type=bind" {
35-
t.Errorf("mount = %q, want resolved localEnv:HOME", got)
34+
if got := f.Mounts[0].(string); got != "source=${localEnv:HOME}/.ssh,target=/root/.ssh,type=bind" {
35+
t.Errorf("mount = %q, want it raw", got)
3636
}
3737
}

‎internal/cli/feature_install.go‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -548,10 +548,8 @@ func engineAtLeast23(ctx context.Context, engine *docker.EngineClient) bool {
548548
}
549549

550550
// substituteFeatureHostVars resolves host-side variables (${localEnv:…},
551-
// ${localWorkspaceFolder}, …) in each Feature's containerEnv values and mounts,
552-
// so a Feature mount like "${localEnv:HOME}/.cfg" reaches Docker resolved —
553-
// matching how the config's own containerEnv/mounts are substituted (upstream
554-
// #308, which only substituted devcontainer.json's own values).
551+
// ${localWorkspaceFolder}, …) in each Feature's containerEnv, which the
552+
// Features Dockerfile writes as ENV lines (upstream #308).
555553
func substituteFeatureHostVars(sets []*features.Set, hs config.HostSubContext) error {
556554
resolver := config.NewVariableResolver()
557555
ctx := config.SubstitutionContext{HostSubContext: hs}
@@ -567,15 +565,6 @@ func substituteFeatureHostVars(sets []*features.Set, hs config.HostSubContext) e
567565
f.ContainerEnv[k] = s
568566
}
569567
}
570-
if len(f.Mounts) > 0 {
571-
resolved, err := resolver.Resolve(ctx, config.PhaseHost, f.Mounts)
572-
if err != nil {
573-
return fmt.Errorf("resolve Feature %s mounts: %w", f.ID, err)
574-
}
575-
if m, ok := resolved.([]any); ok {
576-
f.Mounts = m
577-
}
578-
}
579568
}
580569
}
581570
return nil

‎internal/cli/feature_install_nodocker_held_test.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestExtendImageWithFeaturesResolvesFeatureHostVars(t *testing.T) {
2121
if err := os.MkdirAll(dir, 0o755); err != nil {
2222
t.Fatal(err)
2323
}
24-
if err := os.WriteFile(filepath.Join(dir, "devcontainer-feature.json"), []byte(`{"id":"envy","version":"1.0.0","containerEnv":{"FROM_HOST":"${localEnv:HOST_VALUE}"}}`), 0o644); err != nil {
24+
if err := os.WriteFile(filepath.Join(dir, "devcontainer-feature.json"), []byte(`{"id":"envy","version":"1.0.0","containerEnv":{"FROM_HOST":"${localEnv:HOST_VALUE}"},"mounts":[{"source":"${localEnv:HOST_VALUE}","target":"/m","type":"bind"}]}`), 0o644); err != nil {
2525
t.Fatal(err)
2626
}
2727
if err := os.WriteFile(filepath.Join(dir, "install.sh"), []byte("#!/bin/sh\n"), 0o755); err != nil {
@@ -53,9 +53,12 @@ func TestExtendImageWithFeaturesResolvesFeatureHostVars(t *testing.T) {
5353
if err != nil {
5454
t.Fatalf("extended Dockerfile: %v (calls %v)", err, r.calls)
5555
}
56-
if !strings.Contains(string(data), "resolved-on-host") || strings.Contains(string(data), "localEnv") {
56+
if !strings.Contains(string(data), `ENV FROM_HOST="resolved-on-host"`) {
5757
t.Errorf("extended Dockerfile = %s, want FROM_HOST resolved from the host", data)
5858
}
59+
if !strings.Contains(string(data), `\"source\":\"\${localEnv:HOST_VALUE}\"`) || strings.Contains(string(data), `\"source\":\"resolved-on-host\"`) {
60+
t.Errorf("extended Dockerfile = %s, want the label's Feature mount raw", data)
61+
}
5962
}
6063

6164
func TestExtendImageWithFeaturesBuildContext(t *testing.T) {

‎internal/cli/metadata_label.go‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,30 @@ type metadataSubstitution func(entries *[]imagemeta.Entry) error
5353

5454
func hostMetadataSubstitution(hostSub config.HostSubContext, idLabels []string) metadataSubstitution {
5555
return func(entries *[]imagemeta.Entry) error {
56+
// Resolve a copy: decoding into *entries would write through the
57+
// mounts, commands and maps they share with the raw entries that
58+
// become the devcontainer.metadata label.
59+
data, err := json.Marshal(*entries)
60+
if err != nil {
61+
return fmt.Errorf("copy metadata entries: %w", err)
62+
}
63+
var resolved []imagemeta.Entry
64+
if err := json.Unmarshal(data, &resolved); err != nil {
65+
return fmt.Errorf("copy metadata entries: %w", err)
66+
}
5667
resolver := config.NewVariableResolver()
5768
ctx := config.SubstitutionContext{HostSubContext: hostSub}
5869
if idLabels == nil {
59-
return resolver.ResolveInto(ctx, config.PhaseHost, entries)
70+
err = resolver.ResolveInto(ctx, config.PhaseHost, &resolved)
71+
} else {
72+
ctx.DevContainerID = config.ComputeDevContainerID(config.EnvMap(idLabels))
73+
err = resolver.BeforeContainerInto(ctx, &resolved)
74+
}
75+
if err != nil {
76+
return err
6077
}
61-
ctx.DevContainerID = config.ComputeDevContainerID(config.EnvMap(idLabels))
62-
return resolver.BeforeContainerInto(ctx, entries)
78+
*entries = resolved
79+
return nil
6380
}
6481
}
6582

‎internal/cli/metadata_label_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"reflect"
7+
"slices"
78
"testing"
89

910
"github.com/devcontainers/cli/internal/config"
@@ -141,3 +142,25 @@ func TestHostMetadataSubstitution(t *testing.T) {
141142
t.Errorf("with id labels: ID = %q, want %q", entries[0].ContainerEnv["ID"], want)
142143
}
143144
}
145+
146+
func TestHostMetadataSubstitutionLeavesTheRawEntriesRaw(t *testing.T) {
147+
raw := []imagemeta.Entry{{
148+
ID: "f",
149+
Mounts: []any{map[string]any{"source": "${localEnv:SRC}", "target": "/m", "type": "bind"}},
150+
PostCreateCommand: []any{"echo", "${localEnv:SRC}"},
151+
Customizations: map[string]any{"x": "${localEnv:SRC}"},
152+
}}
153+
label := imagemeta.GenerateMetadataLabel(raw)
154+
resolved := slices.Clone(raw)
155+
for _, idLabels := range [][]string{nil, {"devcontainer.local_folder=/ws"}} {
156+
if err := hostMetadataSubstitution(config.HostSubContext{Env: map[string]string{"SRC": "/host/src"}}, idLabels)(&resolved); err != nil {
157+
t.Fatal(err)
158+
}
159+
if got := resolved[0].Mounts[0].(map[string]any)["source"]; got != "/host/src" {
160+
t.Errorf("idLabels %v: resolved mount source = %v, want /host/src", idLabels, got)
161+
}
162+
if got := imagemeta.GenerateMetadataLabel(raw); got != label {
163+
t.Errorf("idLabels %v: the raw entries changed:\n got %s\nwant %s", idLabels, got, label)
164+
}
165+
}
166+
}

‎internal/cli/read_configuration.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ func runReadConfiguration(ctx context.Context, out Output, opts *readConfigOpts)
210210
for _, fs := range featResult.FeatureSets {
211211
featureEntries = append(featureEntries, featureMetadataEntry(fs, false))
212212
}
213+
if err := hostMetadataSubstitution(result.HostSub, nil)(&featureEntries); err != nil {
214+
return readConfigurationFailure(lgr, fmt.Errorf("substitute the Features' metadata: %w", err))
215+
}
213216
}
214217
entries, entriesErr := readConfigurationMetadata(append(base, featureEntries...), featureEntries, containerCfg, result, opts.idLabels, workspaceFolder, lgr)
215218
if entriesErr != nil {

0 commit comments

Comments
 (0)