Skip to content

Commit 77de275

Browse files
committed
For Exec Defaults workspace folder to current directory if not specified
1 parent ac2d5b7 commit 77de275

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/spec-node/devContainersSpecCLI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ function execOptions(y: Argv) {
12091209
'docker-compose-path': { type: 'string', description: 'Docker Compose CLI path.' },
12101210
'container-data-folder': { type: 'string', description: 'Container data folder where user data inside the container will be stored.' },
12111211
'container-system-data-folder': { type: 'string', description: 'Container system data folder where system data inside the container will be stored.' },
1212-
'workspace-folder': { type: 'string', description: 'Workspace folder path. The devcontainer.json will be looked up relative to this path.' },
1212+
'workspace-folder': { type: 'string', description: 'Workspace folder path. Defaults to current directory if not specified. The devcontainer.json will be looked up relative to this path.' },
12131213
'mount-workspace-git-root': { type: 'boolean', default: true, description: 'Mount the workspace using its Git root.' },
12141214
'container-id': { type: 'string', description: 'Id of the container to run the user commands for.' },
12151215
'id-label': { type: 'string', description: 'Id label(s) of the format name=value. If no --container-id is given the id labels will be used to look up the container. If no --id-label is given, one will be inferred from the --workspace-folder path.' },
@@ -1243,7 +1243,7 @@ function execOptions(y: Argv) {
12431243
throw new Error('Unmatched argument format: remote-env must match <name>=<value>');
12441244
}
12451245
if (!argv['container-id'] && !idLabels?.length && !argv['workspace-folder']) {
1246-
throw new Error('Missing required argument: One of --container-id, --id-label or --workspace-folder is required.');
1246+
argv['workspace-folder'] = process.cwd();
12471247
}
12481248
return true;
12491249
});

src/test/cli.exec.base.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ export function describeTests1({ text, options }: BuildKitOption) {
8282
assert.strictEqual(env.FOO, 'BAR');
8383
assert.strictEqual(env.BAZ, '');
8484
});
85+
it('should exec with default workspace folder (current directory)', async () => {
86+
const originalCwd = process.cwd();
87+
const absoluteTmpPath = path.resolve(__dirname, 'tmp');
88+
const absoluteCli = `npx --prefix ${absoluteTmpPath} devcontainer`;
89+
process.chdir(testFolder);
90+
91+
try {
92+
// Exec without --workspace-folder should use current directory as default
93+
const execRes = await shellExec(`${absoluteCli} exec echo "default workspace test"`);
94+
assert.strictEqual(execRes.error, null);
95+
assert.match(execRes.stdout, /default workspace test/);
96+
} finally {
97+
// Restore original directory
98+
process.chdir(originalCwd);
99+
}
100+
});
85101
});
86102
describe(`with valid (image) config containing features [${text}]`, () => {
87103
let containerId: string | null = null;

0 commit comments

Comments
 (0)