Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow variable substitution default value containing a colon #882

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/spec-common/variableSubstitution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ function evaluateSingleVariable(replace: Replace, match: string, variable: strin
// try to separate variable arguments from variable name
let args: string[] = [];
const parts = variable.split(':');
if (parts.length > 1) {
variable = parts[0];
args = parts.slice(1);
const limitedParts = [
...parts.slice(0, 2),
parts.slice(2).join(':')
];
const parts = variable.split(':', 3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would prevent us from introducing additional arguments in the future because we would break existing configs.

Also: split() with the limit argument does not include the remaining part of the text after the limit was hit. This would need a closer look.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we would need additional arguments in the future after the current ones it would be nice to be able to escape the : in the default value. Would this be an approach to go for?

something like "WEBSITE_URL": "${localEnv:WEBSITE_URL:https\://example.com"

if (limitedParts.length > 1) {
variable = limitedParts[0];
args = limitedParts.slice(1);
}

return replace(match, variable, args);
Expand Down Expand Up @@ -168,4 +173,4 @@ function devcontainerIdForLabels(idLabels: Record<string, string>): string {
.toString(32)
.padStart(52, '0');
return uniqueId;
}
}
15 changes: 15 additions & 0 deletions src/test/variableSubstitution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ describe('Variable substitution', function () {
assert.strictEqual(result.foo, 'bardefaultbar');
});

it(`environment variables with default value containing colon if they do not exist`, async () => {
const raw = {
foo: 'bar${localEnv:baz:de:fault}bar'
};
const result = substitute({
platform: 'linux',
localWorkspaceFolder: '/foo/bar',
containerWorkspaceFolder: '/baz/blue',
configFile: URI.file('/foo/bar/baz.json'),
env: {
},
}, raw);
assert.strictEqual(result.foo, 'barde:faultbar');
});

it(`environment variables without default value if they do not exist`, async () => {
const raw = {
foo: 'bar${localEnv:baz}bar'
Expand Down