Skip to content

Commit 2e3e5cc

Browse files
aledbfclaude
andcommitted
fix(cli): put the metadata label where the reference does
TS records devcontainer.metadata as getDevcontainerMetadata's raw entries: the base image's, the Features' and pick(config.raw, pickConfigProperties), variables unresolved and runArgs left out. Only a container from an image with no Features gets the label (JSON of the entries, "[]" when none); a Dockerfile or Features build, and every image `build` makes, puts it on the image with the configuration's entry. Go always labelled the container (repeating the config entry for Dockerfile builds), left the config entry out of up's image label and the base image's entries out of build's, wrote substituted values and runArgs, and wrote "" for no entries. Since a CLI-created container's metadata now records the configuration, up, exec, run-user-commands and read-configuration read it as TS getImageMetadataFromContainer does: the label's entries with their variables substituted, then only the updateable properties (remoteUser, userEnvProbe, remoteEnv) for a container with every id label, else the configuration's entry. The merged configuration keeps the substituted entries. build of a compose configuration now goes through the compose port of buildAndExtendDockerCompose, as TS does: `docker compose build` with the label build override, also for an image-only service without Features, so the image carries compose's project and service labels and the project name follows TS getProjectName. --secrets-file does not apply there (documented); --label keeps applying through a FROM build. Every differentialRuntimeKnown entry about labels is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c27793b commit 2e3e5cc

18 files changed

Lines changed: 727 additions & 383 deletions

‎docs/go-only-features.md‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ Flags: `--workspace-folder`, `--config`, `--id-label`, `--container-id`,
135135

136136
Boot the container from an already-built image (features baked in), **skipping the image
137137
build and feature installation entirely**. The merged configuration is recovered from the
138-
image's `devcontainer.metadata` label — exactly as an image-based config would — while
139-
`remoteUser`, mounts and lifecycle hooks still come from `devcontainer.json`.
138+
image's `devcontainer.metadata` label, which a `devcontainer build` of the configuration
139+
writes with the configuration's entry (its mounts, lifecycle hooks, …), as for any
140+
container the CLI creates; `remoteUser`, `userEnvProbe` and `remoteEnv` come from the
141+
current `devcontainer.json`. An image without that label is used as an image-based
142+
config's image is: the whole configuration comes from `devcontainer.json`.
140143

141144
```sh
142145
devcontainer up --workspace-folder . \
@@ -184,6 +187,10 @@ Each `KEY` becomes `--secret id=KEY,env=KEY`; the value is delivered through the
184187
subprocess environment, never as an argument. Requires buildx (ignored with a warning on
185188
the legacy builder).
186189

190+
A Docker Compose configuration is built by `docker compose build`, as the reference CLI
191+
builds it, which takes no build secrets from the command line: there the flag is ignored
192+
with a warning.
193+
187194
## Automatic credential bridge
188195

189196
Not a flag — a behavior. The upstream CLI's credential chain applies only to its *own*

‎internal/cli/build.go‎

Lines changed: 135 additions & 117 deletions
Large diffs are not rendered by default.

‎internal/cli/build_compose_path_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestBuildComposePathWiring(t *testing.T) {
5151
DockerComposeFile: config.StringOrStrings{"docker-compose.yml"},
5252
}
5353

54-
_, err := r.buildCompose(t.Context(), cfg, false)
54+
_, err := r.buildCompose(t.Context(), cfg, &config.LoadResult{}, false)
5555
if err == nil {
5656
t.Fatal("expected compose-client detection error, got nil")
5757
}

‎internal/cli/build_held_test.go‎

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,13 @@ func TestDockerfileImageMetadata(t *testing.T) {
145145
}
146146

147147
cfg := &config.DevContainer{RemoteUser: "dev"}
148-
if got, want := ids(dockerfileImageMetadata(cfg, base, stage)), []string{"base/", "stage/", "/dev"}; !slices.Equal(got, want) {
149-
t.Errorf("without features: %v, want %v", got, want)
148+
configLabel := imagemeta.Entry{RemoteUser: "${localEnv:USER}"}
149+
if got, want := ids(dockerfileImageMetadata(cfg, base, stage, configLabel)), []string{"base/", "stage/", "/${localEnv:USER}"}; !slices.Equal(got, want) {
150+
t.Errorf("without features: %v, want %v (the config's entry as written)", got, want)
150151
}
151152

152153
cfg.Features = map[string]any{"ghcr.io/devcontainers/features/go:1": true}
153-
if got, want := ids(dockerfileImageMetadata(cfg, base, stage)), []string{"base/", "stage/"}; !slices.Equal(got, want) {
154+
if got, want := ids(dockerfileImageMetadata(cfg, base, stage, configLabel)), []string{"base/", "stage/"}; !slices.Equal(got, want) {
154155
t.Errorf("with features (the Feature build adds the config entry): %v, want %v", got, want)
155156
}
156157
}
@@ -185,49 +186,6 @@ func TestBuildxSecrets(t *testing.T) {
185186
}
186187
}
187188

188-
func TestComposeBaseImageName(t *testing.T) {
189-
for _, tc := range []struct {
190-
name string
191-
service map[string]any
192-
want string
193-
}{
194-
{"service image", map[string]any{"image": "alpine:3"}, "alpine:3"},
195-
{"empty image", map[string]any{"image": ""}, "proj-app"},
196-
{"build only", map[string]any{"build": map[string]any{}}, "proj-app"},
197-
{"no service config", nil, "proj-app"},
198-
} {
199-
t.Run(tc.name, func(t *testing.T) {
200-
if got := composeBaseImageName("proj", "app", tc.service); got != tc.want {
201-
t.Errorf("composeBaseImageName = %q, want %q", got, tc.want)
202-
}
203-
})
204-
}
205-
}
206-
207-
func TestComposeRetag(t *testing.T) {
208-
cfg := &config.DevContainer{RemoteUser: "dev"}
209-
isMetadata := func(l string) bool { return strings.HasPrefix(l, imagemeta.MetadataLabel+"=") }
210-
211-
if _, _, ok := composeRetag(cfg, &buildOpts{}, "proj-app"); ok {
212-
t.Error("no --label or --image-name: want no re-tag")
213-
}
214-
215-
tags, labels, ok := composeRetag(cfg, &buildOpts{labels: []string{"a=1"}}, "proj-app")
216-
if !ok || !slices.Equal(tags, []string{"proj-app"}) || !slices.Equal(labels, []string{"a=1"}) {
217-
t.Errorf("--label only: (%v, %v, %v), want the service image re-tagged with the user's label", tags, labels, ok)
218-
}
219-
220-
tags, labels, ok = composeRetag(cfg, &buildOpts{imageNames: []string{"out:1"}}, "proj-app")
221-
if !ok || !slices.Equal(tags, []string{"out:1"}) || len(labels) != 1 || !isMetadata(labels[0]) || !strings.Contains(labels[0], `"remoteUser":"dev"`) {
222-
t.Errorf("--image-name only: (%v, %v, %v), want the name with the config's metadata label", tags, labels, ok)
223-
}
224-
225-
tags, labels, ok = composeRetag(cfg, &buildOpts{imageNames: []string{"out:1", "out:2"}, labels: []string{"a=1"}}, "proj-app")
226-
if !ok || !slices.Equal(tags, []string{"out:1", "out:2"}) || len(labels) != 2 || labels[0] != "a=1" || !isMetadata(labels[1]) {
227-
t.Errorf("both: (%v, %v, %v), want the names, the user's label then the metadata label", tags, labels, ok)
228-
}
229-
}
230-
231189
// --buildkit never skips BuildKit detection, so the build runs as if buildx
232190
// were absent even when the Docker CLI has it. The fake --docker-path reports
233191
// buildx, and both cases stop at the flag check before any Docker build.

‎internal/cli/cli_test.go‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,24 +161,6 @@ func TestReadConfiguration_AllFixtures(t *testing.T) {
161161
t.Logf("read-configuration: %d passed, %d skipped", passed, skipped)
162162
}
163163

164-
func TestComposeServiceConfig(t *testing.T) {
165-
cfg := map[string]any{
166-
"services": map[string]any{
167-
"app": map[string]any{
168-
"image": "example/app:latest",
169-
},
170-
},
171-
}
172-
173-
got := composeServiceConfig(cfg, "app")
174-
if got == nil {
175-
t.Fatal("composeServiceConfig() = nil")
176-
}
177-
if got["image"] != "example/app:latest" {
178-
t.Fatalf("image = %v", got["image"])
179-
}
180-
}
181-
182164
func TestConfigToMetadataEntry_IncludesMountsAndUIDSetting(t *testing.T) {
183165
trueValue := true
184166
cfg := &config.DevContainer{

‎internal/cli/compose_up.go‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ type composeStart struct {
4242
client *docker.ComposeClient
4343
// container is the existing (stopped) service container, nil for none.
4444
container *container.InspectResponse
45+
// The build command's settings (zero for up): metadataOnImage puts the
46+
// metadata label on an image-only service's image even without Features (TS
47+
// canAddLabelsToContainer false), buildServices are the services `compose
48+
// build` builds (nil for TS up's runServices filter), skipPersistCustoms
49+
// leaves the Features' customizations out of the metadata.
50+
metadataOnImage bool
51+
buildServices []string
52+
skipPersistCustoms bool
4553
}
4654

4755
// persistedFolder is TS common.persistedFolder: --user-data-folder, or
@@ -418,12 +426,19 @@ func (r *upRunner) buildAndExtendCompose(ctx context.Context, s composeStart, pe
418426
return nil, err
419427
}
420428

421-
configEntry := configMetadataEntry(cfg, opts.omitConfigRemoteEnvFromMeta)
422-
metadata := slices.Clone(baseEntries)
429+
// TS getDevcontainerMetadata: the label records the image's entries, the
430+
// Features' and the configuration's as written (labelMetadata); the merged
431+
// configuration reads them with their variables substituted (metadata).
432+
var featureEntries []imagemeta.Entry
423433
for _, fs := range featureSets {
424-
metadata = append(metadata, featureMetadataEntry(fs, false))
434+
featureEntries = append(featureEntries, featureMetadataEntry(fs, s.skipPersistCustoms))
435+
}
436+
labelMetadata := slices.Concat(baseEntries, featureEntries, []imagemeta.Entry{r.configLabel})
437+
metadata := slices.Clone(baseEntries)
438+
if err := r.substitute(&metadata); err != nil {
439+
return nil, fmt.Errorf("substitute the image's metadata: %w", err)
425440
}
426-
metadata = append(metadata, configEntry)
441+
metadata = slices.Concat(metadata, featureEntries, []imagemeta.Entry{configMetadataEntry(cfg, false)})
427442

428443
result := &composeExtendResult{imageMetadata: metadata}
429444
userSyntax := ""
@@ -434,10 +449,10 @@ func (r *upRunner) buildAndExtendCompose(ctx context.Context, s composeStart, pe
434449
var info *imagemeta.ExtendImageBuildInfo
435450
dstFolder := featuresFolder
436451
switch {
437-
case len(featureSets) == 0 && !svcBuild.HasBuild:
452+
case len(featureSets) == 0 && !svcBuild.HasBuild && !s.metadataOnImage:
438453
// TS getExtendImageBuildInfo: no Features and an image (no Dockerfile) →
439454
// only the metadata label, added to the container.
440-
result.labels = []string{imagemeta.MetadataLabel + "=" + imagemeta.GenerateMetadataLabel(metadata)}
455+
result.labels = []string{imagemeta.MetadataLabel + "=" + containerMetadataLabel(labelMetadata)}
441456
case len(featureSets) == 0:
442457
// TS getImageBuildOptions: extend the Dockerfile with the metadata label.
443458
syntax := userSyntax
@@ -448,7 +463,7 @@ func (r *upRunner) buildAndExtendCompose(ctx context.Context, s composeStart, pe
448463
if err := os.MkdirAll(dstFolder, 0o755); err != nil {
449464
return nil, fmt.Errorf("create features folder: %w", err)
450465
}
451-
info = imagemeta.GenerateComposeLabelBuild(baseName, metadata, syntax)
466+
info = imagemeta.GenerateComposeLabelBuild(baseName, labelMetadata, syntax)
452467
default:
453468
// TS findContainerUsers over the image + Features + config metadata.
454469
containerUser, remoteUser := findContainerUsers(metadata, svcUser, imageUser)
@@ -471,7 +486,7 @@ func (r *upRunner) buildAndExtendCompose(ctx context.Context, s composeStart, pe
471486
info = imagemeta.GenerateFeaturesBuildOptions(imagemeta.FeaturesBuildParams{
472487
BaseName: baseName,
473488
FeatureSets: featureSets,
474-
Metadata: metadata,
489+
Metadata: labelMetadata,
475490
ContainerUser: containerUser,
476491
RemoteUser: remoteUser,
477492
ImageUser: imageUser,
@@ -548,7 +563,11 @@ func (r *upRunner) buildAndExtendCompose(ctx context.Context, s composeStart, pe
548563

549564
if !noBuild {
550565
files := append(slices.Clone(s.composeFiles), result.overrideFiles...)
551-
if err := s.client.Build(ctx, files, "", []string{"--project-name", s.projectName}, composeServicesFilter(cfg), opts.buildNoCache); err != nil {
566+
services := s.buildServices
567+
if services == nil {
568+
services = composeServicesFilter(cfg)
569+
}
570+
if err := s.client.Build(ctx, files, "", []string{"--project-name", s.projectName}, services, opts.buildNoCache); err != nil {
552571
return nil, fmt.Errorf("An error occurred building the Docker Compose images: %w", err)
553572
}
554573
}

‎internal/cli/compose_up_held_test.go‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ func newComposeHarness(t *testing.T) *composeHarness {
295295
docker: &docker.Client{DockerPath: "docker", Log: logger, Runner: run},
296296
engine: &docker.EngineClient{API: engine, Log: logger},
297297
opts: &upOpts{userDataFolder: persisted, updateRemoteUserUIDDefault: "off", gpuAvailability: "none"},
298+
// The fixtures' metadata names no variables.
299+
substitute: func(*[]imagemeta.Entry) error { return nil },
298300
},
299301
}
300302
}
@@ -1228,3 +1230,48 @@ func TestStartComposeContainerImageServiceWithFeatures(t *testing.T) {
12281230
t.Errorf("compose up %q, want the compose file and both overrides", ups)
12291231
}
12301232
}
1233+
1234+
// For the build command (TS canAddLabelsToContainer false) an image-only
1235+
// service without Features is still built, with the metadata label on its
1236+
// image: the image's entries and the configuration's as written, while the
1237+
// merged metadata has it substituted; compose builds the dev container
1238+
// service alone.
1239+
func TestBuildAndExtendComposeForTheBuildCommand(t *testing.T) {
1240+
h := newComposeHarness(t)
1241+
h.r.configLabel = imagemeta.Entry{RemoteUser: "${localEnv:ME}"}
1242+
s := h.start(&config.DevContainer{RemoteUser: "me", RunServices: []string{"db"}}, map[string]any{"image": "alpine:3"})
1243+
s.metadataOnImage, s.buildServices = true, []string{"app"}
1244+
res, err := h.r.buildAndExtendCompose(context.Background(), s, h.persisted, "", false)
1245+
if err != nil {
1246+
t.Fatal(err)
1247+
}
1248+
if res.labels != nil || res.overrideImageName != folderImageName(h.ws) {
1249+
t.Errorf("labels %q, override image %q; want no container label and the folder image", res.labels, res.overrideImageName)
1250+
}
1251+
content := readFile(t, overrideDockerfile(t, readFile(t, res.overrideFiles[0])))
1252+
if !strings.Contains(content, `LABEL devcontainer.metadata="[`) || !strings.Contains(content, `\"remoteUser\":\"\${localEnv:ME}\"`) || !strings.Contains(content, `\"remoteUser\":\"node\"`) {
1253+
t.Errorf("final Dockerfile lacks the label with the image's and the raw config's entries:\n%s", content)
1254+
}
1255+
if last := res.imageMetadata[len(res.imageMetadata)-1]; last.RemoteUser != "me" {
1256+
t.Errorf("merged metadata ends with %+v, want the substituted config entry", last)
1257+
}
1258+
if got, want := h.run.compose("build"), [][]string{{"-f", h.compose, "-f", res.overrideFiles[0], "--project-name", "proj", "build", "app"}}; !slices.EqualFunc(got, want, slices.Equal) {
1259+
t.Errorf("compose build runs %q, want %q", got, want)
1260+
}
1261+
1262+
// --skip-persisting-customizations-from-features leaves a Feature's
1263+
// customizations out of the metadata.
1264+
h.write("custom/devcontainer-feature.json", `{"id":"custom","version":"1.0.0","customizations":{"vscode":{"extensions":["x"]}}}`)
1265+
h.write("custom/install.sh", "#!/bin/sh\n")
1266+
for _, skip := range []bool{false, true} {
1267+
s := h.start(&config.DevContainer{Features: map[string]any{"./custom": map[string]any{}}}, map[string]any{"image": "alpine:3"})
1268+
s.skipPersistCustoms = skip
1269+
res, err := h.r.buildAndExtendCompose(context.Background(), s, h.persisted, "", true)
1270+
if err != nil {
1271+
t.Fatal(err)
1272+
}
1273+
if got := res.imageMetadata[1].Customizations != nil; got == skip {
1274+
t.Errorf("skip=%t: Feature entry %+v", skip, res.imageMetadata[1])
1275+
}
1276+
}
1277+
}

‎internal/cli/differential_runtime_test.go‎

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,18 @@ var differentialRuntimeKnown = map[string]map[string]string{
2323
// docs/DIVERGENCES.md: Features backed by GitHub Releases (v1, owner/repo/id)
2424
// are not implemented; the configuration fails instead of resolving them.
2525
"up": {
26-
"dockerfile-with-syntax": "legacy GitHub Release Feature (accepted limitation)",
27-
"dockerfile-with-target": "legacy GitHub Release Feature (accepted limitation)",
28-
"compose-Dockerfile-with-features": "legacy GitHub Release Feature (accepted limitation)",
29-
"compose-Dockerfile-with-target": "legacy GitHub Release Feature (accepted limitation)",
30-
"compose-image-with-features": "legacy GitHub Release Feature (accepted limitation)",
31-
"compose-with-name": "the container label is empty where TS writes [] for no metadata",
32-
"compose-with-name-and-custom-yaml": "the container label is empty where TS writes [] for no metadata",
33-
"compose-without-name": "the container label is empty where TS writes [] for no metadata",
34-
"dockerfile-with-features": "the image label leaves out the configuration's entry",
35-
"dockerfile-with-parallel-commands": "the image label leaves out the configuration's entry",
36-
"dockerfile-without-features": "the image label leaves out the configuration's entry",
37-
"image": "the container label holds the substituted configuration and runArgs; TS writes the raw one, without runArgs",
38-
"image-metadata-containerEnv": "the image label leaves out the configuration's entry",
39-
"image-with-features": "the image label leaves out the configuration's entry",
40-
"image-with-git-feature": "the image label leaves out the configuration's entry",
41-
"updateUID": "a container from a Dockerfile gets a container label repeating the image label; TS adds none",
42-
"updateUIDOnly": "a container from a Dockerfile gets a container label repeating the image label; TS adds none",
43-
"updateUIDamd64": "a container from a Dockerfile gets a container label repeating the image label; TS adds none",
44-
"updateUIDamd64-platform-option": "a container from a Dockerfile gets a container label repeating the image label, with runArgs; TS adds none",
26+
"dockerfile-with-syntax": "legacy GitHub Release Feature (accepted limitation)",
27+
"dockerfile-with-target": "legacy GitHub Release Feature (accepted limitation)",
28+
"compose-Dockerfile-with-features": "legacy GitHub Release Feature (accepted limitation)",
29+
"compose-Dockerfile-with-target": "legacy GitHub Release Feature (accepted limitation)",
30+
"compose-image-with-features": "legacy GitHub Release Feature (accepted limitation)",
4531
},
4632
"build": {
47-
"dockerfile-with-syntax": "legacy GitHub Release Feature (accepted limitation)",
48-
"dockerfile-with-target": "legacy GitHub Release Feature (accepted limitation)",
49-
"compose-Dockerfile-with-features": "legacy GitHub Release Feature (accepted limitation)",
50-
"compose-Dockerfile-with-target": "legacy GitHub Release Feature (accepted limitation)",
51-
"compose-image-with-features": "legacy GitHub Release Feature (accepted limitation)",
52-
"compose-Dockerfile-without-features": "the image label leaves out the base image's metadata",
53-
"compose-image-with-mounts": "the image label leaves out the base image's metadata, and compose project and service labels",
54-
"compose-image-without-features": "the image label leaves out the base image's metadata, and compose project and service labels",
55-
"compose-image-without-features-minimal": "the image lacks compose's project and service labels",
56-
"compose-with-name": "the image lacks compose's project and service labels and has an empty metadata label",
57-
"compose-with-name-and-custom-yaml": "the image lacks compose's project and service labels and has an empty metadata label",
58-
"compose-without-name": "the image lacks compose's project and service labels and has an empty metadata label",
59-
"dockerfile-with-features": "the image label leaves out the base image's metadata",
60-
"dockerfile-without-features": "the image label leaves out the base image's metadata",
61-
"image": "the image label holds the substituted configuration and runArgs; TS writes the raw one, without runArgs",
62-
"image-containerEnv-issue": "the image label leaves out the base image's metadata, and compose project and service labels",
63-
"image-metadata-containerEnv": "the image label leaves out the configuration's entry",
64-
"image-with-features": "the image label leaves out the configuration's entry",
65-
"image-with-git-feature": "the image label leaves out the configuration's entry",
66-
"image-with-mounts": "the image label leaves out the base image's metadata",
67-
"image-with-parallel-initialize-command": "the image label leaves out the base image's metadata",
68-
"poetry-example": "the image label leaves out the configuration's entry",
69-
"updateUIDamd64-platform-option": "the image label records runArgs, which TS leaves out",
33+
"dockerfile-with-syntax": "legacy GitHub Release Feature (accepted limitation)",
34+
"dockerfile-with-target": "legacy GitHub Release Feature (accepted limitation)",
35+
"compose-Dockerfile-with-features": "legacy GitHub Release Feature (accepted limitation)",
36+
"compose-Dockerfile-with-target": "legacy GitHub Release Feature (accepted limitation)",
37+
"compose-image-with-features": "legacy GitHub Release Feature (accepted limitation)",
7038
},
7139
}
7240

0 commit comments

Comments
 (0)