Skip to content

Commit b7eeda6

Browse files
authored
fix(cli): equals sign in a tag value is dropped (#27130)
When overriding tags from the CLI the tag key-value string is split by an equal sign and the left and right parts are used for key and value respectively. The documentation for the [String.prototype.split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) states that the second parameter sets a limit of the returned values after execution: > If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all. So basically if we have `foo=bar=test` and we apply a split with a limit of 2 (`"foo=bar=test".split("=", 2)`) we will get the following array `["foo", "bar"]` instead of the expected one`["foo", "bar=test"]`. There has been the same problem with the context values and since it is the same problem the solution is also the same: #5773 With this fix the `context` and `tags` get the same behaviour when parsing the passed values. Closes #21003 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent e1d4187 commit b7eeda6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/aws-cdk/lib/settings.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export class Settings {
336336
const tags: Tag[] = [];
337337

338338
for (const assignment of nonEmptyTags) {
339-
const parts = assignment.split('=', 2);
339+
const parts = assignment.split(/=(.*)/, 2);
340340
if (parts.length === 2) {
341341
debug('CLI argument tags: %s=%s', parts[0], parts[1]);
342342
tags.push({

packages/aws-cdk/test/settings.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable import/order */
22
import { Command, Context, Settings } from '../lib/settings';
3+
import { Tag } from '../lib/cdk-toolkit';
34

45
test('can delete values from Context object', () => {
56
// GIVEN
@@ -81,6 +82,26 @@ test('can parse string context from command line arguments with equals sign in v
8182
expect(settings2.get(['context']).foo).toEqual( 'bar=');
8283
});
8384

85+
test('can parse tag values from command line arguments', () => {
86+
// GIVEN
87+
const settings1 = Settings.fromCommandLineArguments({ tags: ['foo=bar'], _: [Command.DEPLOY] });
88+
const settings2 = Settings.fromCommandLineArguments({ tags: ['foo='], _: [Command.DEPLOY] });
89+
90+
// THEN
91+
expect(settings1.get(['tags']).find((tag: Tag) => tag.Key === 'foo').Value).toEqual('bar');
92+
expect(settings2.get(['tags']).find((tag: Tag) => tag.Key === 'foo').Value).toEqual('');
93+
});
94+
95+
test('can parse tag values from command line arguments with equals sign in value', () => {
96+
// GIVEN
97+
const settings1 = Settings.fromCommandLineArguments({ tags: ['foo==bar='], _: [Command.DEPLOY] });
98+
const settings2 = Settings.fromCommandLineArguments({ tags: ['foo=bar='], _: [Command.DEPLOY] });
99+
100+
// THEN
101+
expect(settings1.get(['tags']).find((tag: Tag) => tag.Key === 'foo').Value).toEqual('=bar=');
102+
expect(settings2.get(['tags']).find((tag: Tag) => tag.Key === 'foo').Value).toEqual('bar=');
103+
});
104+
84105
test('bundling stacks defaults to an empty list', () => {
85106
// GIVEN
86107
const settings = Settings.fromCommandLineArguments({

0 commit comments

Comments
 (0)