Skip to content

Commit

Permalink
Fix support for project name attribute (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmarti committed May 30, 2024
1 parent 85499b3 commit 3913f55
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Notable changes.

## May 2024

### [0.62.0]
- Fix support for project name attribute. (https://github.com/devcontainers/cli/issues/831)

### [0.61.0]
- Use --depth 1 to make dotfiles install process faster (https://github.com/devcontainers/cli/pull/830)
- Enable --cache-to and --cache-from in devcontainer up (https://github.com/devcontainers/cli/pull/813)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@devcontainers/cli",
"description": "Dev Containers CLI",
"version": "0.61.0",
"version": "0.62.0",
"bin": {
"devcontainer": "devcontainer.js"
},
Expand Down
14 changes: 12 additions & 2 deletions src/spec-node/dockerCompose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,18 @@ export async function getProjectName(params: DockerCLIParameters | DockerResolve
throw err;
}
}
if (composeConfig?.name) {
return toProjectName(composeConfig.name, newProjectName);
for (let i = composeFiles.length - 1; i >= 0; i--) {
try {
const fragment = yaml.load((await cliHost.readFile(composeFiles[i])).toString()) || {} as any;
if (fragment.name) {
return toProjectName(fragment.name, newProjectName);
}
} catch (error) {
// fallback when parsing fails due to custom yaml tags (e.g., !reset)
if (composeConfig?.name && composeConfig.name !== 'devcontainer') {
return toProjectName(composeConfig.name, newProjectName);
}
}
}
const configDir = workspace.configFolderPath;
const workingDir = composeFiles[0] ? cliHost.path.dirname(composeFiles[0]) : cliHost.cwd; // From https://github.com/docker/compose/blob/79557e3d3ab67c3697641d9af91866d7e400cfeb/compose/config/config.py#L290
Expand Down
28 changes: 27 additions & 1 deletion src/test/cli.up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('Dev Containers CLI', function () {
assert.equal(upResult!.outcome, 'success');
});
});
describe('for docker-compose with image without features with custom project name', () => {
describe('for minimal docker-compose with custom project name', () => {
let upResult: UpResult | null = null;
const testFolder = `${__dirname}/configs/compose-with-name`;
before(async () => {
Expand All @@ -105,6 +105,32 @@ describe('Dev Containers CLI', function () {
assert.equal(upResult!.composeProjectName, 'custom-project-name');
});
});
describe('for minimal docker-compose with custom project name and custom yaml', () => {
let upResult: UpResult | null = null;
const testFolder = `${__dirname}/configs/compose-with-name-and-custom-yaml`;
before(async () => {
// build and start the container
upResult = await devContainerUp(cli, testFolder, { 'logLevel': 'trace', extraArgs: `--docker-compose-path trigger-compose-v2` });
});
after(async () => await devContainerDown({ composeProjectName: upResult?.composeProjectName }));
it('should succeed', () => {
assert.equal(upResult!.outcome, 'success');
assert.equal(upResult!.composeProjectName, 'custom-project-name-custom-yaml');
});
});
describe('for minimal docker-compose without custom project name', () => {
let upResult: UpResult | null = null;
const testFolder = `${__dirname}/configs/compose-without-name`;
before(async () => {
// build and start the container
upResult = await devContainerUp(cli, testFolder, { 'logLevel': 'trace', extraArgs: `--docker-compose-path trigger-compose-v2` });
});
after(async () => await devContainerDown({ composeProjectName: upResult?.composeProjectName }));
it('should succeed', () => {
assert.equal(upResult!.outcome, 'success');
assert.equal(upResult!.composeProjectName, 'compose-without-name_devcontainer');
});
});

// Additional tests to verify the handling of persisted files
describe('for docker-compose with Dockerfile with features', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.8'

name: custom-project-name-custom-yaml

services:
app:
image: ubuntu:latest
ports: !reset []
volumes:
- ..:/workspace:cached
command: sleep infinity
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3.8'

services:
app:
image: ubuntu:latest
volumes:
- ..:/workspace:cached
command: sleep infinity

0 comments on commit 3913f55

Please sign in to comment.