Skip to content

Commit c6f3d47

Browse files
authored
fix: always write devcontainer.metadata label as JSON array (#1199)
* fix: always write devcontainer.metadata label as JSON array When there is only one metadata entry (e.g. docker-compose devcontainer with no features), `getDevcontainerMetadataLabel` wrote a bare JSON object instead of an array. This violates the spec which states the label "can contain an array of json snippets" and causes tools like Zed that expect an array to fail when attaching to an existing container. Always wrap the metadata in an array regardless of the number of entries. Spec reference: https://containers.dev/implementors/json_reference/ Fixes #1054 * cleanup: remove unnecessary spaces * test: add test for single metadata entry always being an array
1 parent 997a2db commit c6f3d47

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/spec-node/imageMetadata.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,9 @@ export function getDevcontainerMetadataLabel(devContainerMetadata: SubstitutedCo
497497
if (!metadata.length) {
498498
return '';
499499
}
500-
const imageMetadataLabelValue = metadata.length !== 1
501-
? `[${metadata
502-
.map(feature => ` \\\n${toLabelString(feature)}`)
503-
.join(',')} \\\n]`
504-
: toLabelString(metadata[0]);
500+
const imageMetadataLabelValue = `[${metadata
501+
.map(feature => ` \\\n${toLabelString(feature)}`)
502+
.join(',')} \\\n]`;
505503
return `LABEL ${imageMetadataLabel}="${imageMetadataLabelValue}"`;
506504
}
507505

src/test/imageMetadata.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,22 @@ describe('Image Metadata', function () {
432432
assert.strictEqual(label.replace(/ \\\n/g, ''), `LABEL devcontainer.metadata="${JSON.stringify(expected).replace(/"/g, '\\"')}"`);
433433
});
434434

435+
it('should create array label for single metadata entry (docker-compose with Dockerfile, no features)', () => {
436+
// When there is only one metadata entry, the label should still be a JSON array.
437+
// Regression test for https://github.com/devcontainers/cli/issues/1054
438+
const label = getDevcontainerMetadataLabel(configWithRaw([
439+
{
440+
remoteUser: 'testUser',
441+
}
442+
]));
443+
const expected = [
444+
{
445+
remoteUser: 'testUser',
446+
}
447+
];
448+
assert.strictEqual(label.replace(/ \\\n/g, ''), `LABEL devcontainer.metadata="${JSON.stringify(expected).replace(/"/g, '\\"')}"`);
449+
});
450+
435451
it('should merge metadata from devcontainer.json and features', () => {
436452
const merged = mergeConfiguration({
437453
configFilePath: URI.parse('file:///devcontainer.json'),

0 commit comments

Comments
 (0)