Skip to content

Commit 7d9ca48

Browse files
committed
Further changes to escape dollar and new line characters in the properties and the updated test script for the fix.
1 parent ee1627c commit 7d9ca48

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

src/spec-node/dockerCompose.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ while sleep 1 & wait $$!; do :; done", "-"${userEntrypoint.map(a => `, ${JSON.st
557557
init: true` : ''}${user ? `
558558
user: ${user}` : ''}${Object.keys(env).length ? `
559559
environment:${Object.keys(env).map(key => `
560-
- '${key}=${env[key].replace(/'/g, '\'\'')}'`).join('')}` : ''}${mergedConfig.privileged ? `
560+
- '${key}=${env[key].replace(/\n/g, '\\n').replace(/\$/g, '$$$$').replace(/'/g, '\'\'')}'`).join('')}` : ''}${mergedConfig.privileged ? `
561561
privileged: true` : ''}${capAdd.length ? `
562562
cap_add:${capAdd.map(cap => `
563563
- ${cap}`).join('')}` : ''}${securityOpts.length ? `

src/test/cli.up.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,40 @@ describe('Dev Containers CLI', function () {
266266
await shellExec(`docker rm -f ${containerId}`);
267267
});
268268

269+
it('should follow the correct merge logic for containerEnv using docker compose', async () => {
270+
const res = await shellExec(`${cli} up --workspace-folder ${__dirname}/configs/image-containerEnv-issue`);
271+
const response = JSON.parse(res.stdout);
272+
assert.equal(response.outcome, 'success');
273+
const containerId: string = response.containerId;
274+
assert.ok(containerId, 'Container id not found.');
275+
276+
const somePath = await shellExec(`docker exec ${containerId} bash -c 'echo -n $SOME_PATH'`);
277+
assert.equal('/tmp/path/doc-ver/loc', somePath.stdout);
278+
279+
const envWithSpaces = await shellExec(`docker exec ${containerId} bash -c 'echo -n $VAR_WITH_SPACES'`);
280+
assert.equal('value with spaces', envWithSpaces.stdout);
281+
282+
const evalEnvWithCommand = await shellExec(`docker exec ${containerId} bash -c 'eval $ENV_WITH_COMMAND'`);
283+
assert.equal('Hello, World!', evalEnvWithCommand.stdout);
284+
285+
const envWithTestMessage = await shellExec(`docker exec ${containerId} bash -c 'echo -n $Test_Message'`);
286+
assert.equal('H"\\n\\ne"\'\'\'llo M:;a/t?h&^iKa%#@!``ni,sk_a-', envWithTestMessage.stdout);
287+
288+
const envWithFormat = await shellExec(`docker exec ${containerId} bash -c 'echo -n $ROSCONSOLE_FORMAT'`);
289+
assert.equal('[$${severity}] [$${walltime:%Y-%m-%d %H:%M:%S}] [$${node}]: $${message}', envWithFormat.stdout);
290+
291+
const envWithDoubleQuote = await shellExec(`docker exec ${containerId} bash -c 'echo -n $VAR_WITH_QUOTES_WE_WANT_TO_KEEP'`);
292+
assert.equal('value with \"quotes\" we want to keep', envWithDoubleQuote.stdout);
293+
294+
const envWithDollar = await shellExec(`docker exec ${containerId} bash -c 'echo -n $VAR_WITH_DOLLAR_SIGN'`);
295+
assert.equal('value with $dollar sign', envWithDollar.stdout);
296+
297+
const envWithBackSlash = await shellExec(`docker exec ${containerId} bash -c 'echo -n $VAR_WITH_BACK_SLASH'`);
298+
assert.equal('value with \\back slash', envWithBackSlash.stdout);
299+
300+
await shellExec(`docker rm -f ${containerId}`);
301+
});
302+
269303
it('should run with config in subfolder', async () => {
270304
const upRes = await shellExec(`${cli} up --workspace-folder ${__dirname}/configs/dockerfile-without-features --config ${__dirname}/configs/dockerfile-without-features/.devcontainer/subfolder/devcontainer.json`);
271305
const response = JSON.parse(upRes.stdout);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"dockerComposeFile": "docker-compose.yml",
3+
"service": "devcontainerissues",
4+
"workspaceFolder": "/workspaces/cli",
5+
"containerEnv": {
6+
"SOME_PATH": "/tmp/path/doc-ver/loc",
7+
"Test_Message": "H\"\n\ne\"'''llo M:;a/t?h&^iKa%#@!``ni,sk_a-",
8+
"ROSCONSOLE_FORMAT": "[$${severity}] [$${walltime:%Y-%m-%d %H:%M:%S}] [$${node}]: $${message}",
9+
"VAR_WITH_SPACES": "value with spaces",
10+
"VAR_WITH_QUOTES_WE_WANT_TO_KEEP": "value with \"quotes\" we want to keep",
11+
"VAR_WITH_DOLLAR_SIGN": "value with $dollar sign",
12+
"VAR_WITH_BACK_SLASH": "value with \\back slash",
13+
"ENV_WITH_COMMAND": "bash -c 'echo -n \"Hello, World!\"'"
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
services:
2+
devcontainerissues:
3+
# set this to the premade image generated by running the 'original_container' vsc-original-container-c521b00ea40fee92e585b105d9e4f1699ad8216f18220c1b914451f2eafef3b4
4+
image: mcr.microsoft.com/devcontainers/javascript-node:1-22-bookworm
5+
volumes:
6+
- ../..:/workspaces:cached
7+
environment:
8+
FOO: b"\n\ta"r
9+
Test_Message: "Hello Max"
10+
ROSCONSOLE_FORMAT: "Ros from compose"
11+
command: sleep infinity
12+

0 commit comments

Comments
 (0)