Skip to content

Commit 1ab5a09

Browse files
iankhougithub-actions
andauthored
fix: notices key in cdk.json is not respected (#821)
Fixes #724 ## Description Setting `"notices": false` in cdk.json doesn't suppress notices. The root cause is that we handle logic to determine whether we should print notices at *yargs*. Yargs had a default value determined by a helper function (`YARGS_HELPERS.shouldDisplayNotices()`) that returned true when not in CI, or when in a safe CI. This meant that any setting in our configuration would be overwritten by the output of this helper, because it looks like it's coming from the command line! ## Solution Centralize logic for notices in `cli.ts`. Made the notices option in yargs `undefined` by default. Moved the logic from `YARGS_HELPERS.shouldDisplayNotices()` into the `cli.ts`. This has several implications. - Setting `notices` in config now functions as expected; it will still not override the command line setting - If a CDK app is running in a CI environment where it would not be safe to print notices - NEW BEHAVIOR: Notices WILL be printed if `notices: true` or `notices: [truthy value]` is set in config - Notices WILL be printed if flag `--notices` is passed, and NOT printed if `--no-notices` / `--notices=false` is passed at the command line (was already the case) **Caveat: `"notices": "false"` in the config will suppress notices, but other "truthy" values will cause them to be printed.** ### Testing Added unit tests to verify the hierarchy of behavior: 1. Flags in CLI (`--notices` or `--no-notices`) 2. Setting in configuration (`"notices": false` in cdk.json) 3. Print notices if not in CI or in CI where it is safe --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license --------- Signed-off-by: github-actions <[email protected]> Co-authored-by: github-actions <[email protected]>
1 parent e7ea9d5 commit 1ab5a09

File tree

9 files changed

+370
-55
lines changed

9 files changed

+370
-55
lines changed

packages/@aws-cdk-testing/cli-integ/tests/cli-integ-tests/synth/cdk-synth-telemetry-with-errors.integtest.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ integTest(
3232
json: false,
3333
debug: false,
3434
staging: true,
35-
notices: true,
3635
['no-color']: false,
3736
ci: expect.anything(), // changes based on where this is called
3837
validation: true,
@@ -81,7 +80,6 @@ integTest(
8180
json: false,
8281
debug: false,
8382
staging: true,
84-
notices: true,
8583
['no-color']: false,
8684
ci: expect.anything(), // changes based on where this is called
8785
validation: true,

packages/@aws-cdk-testing/cli-integ/tests/cli-integ-tests/synth/cdk-synth-telemetry.integtest.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ integTest(
2424
json: false,
2525
debug: false,
2626
staging: true,
27-
notices: true,
2827
['no-color']: false,
2928
ci: expect.anything(), // changes based on where this is called
3029
validation: true,
@@ -74,7 +73,6 @@ integTest(
7473
json: false,
7574
debug: false,
7675
staging: true,
77-
notices: true,
7876
['no-color']: false,
7977
ci: expect.anything(), // changes based on where this is called
8078
validation: true,

packages/@aws-cdk/toolkit-lib/lib/api/notices/notices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class Notices {
150150
* Refresh the list of notices this instance is aware of.
151151
*
152152
* This method throws an error if the data source fails to fetch notices.
153-
* When using, consider if execution should halt immdiately or if catching the rror and continuing is more appropriate
153+
* When using, consider if execution should halt immdiately or if catching the error and continuing is more appropriate
154154
*
155155
* @throws on failure to refresh the data source
156156
*/

packages/aws-cdk/lib/cli/cli-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export async function makeConfig(): Promise<CliConfig> {
3939
'role-arn': { type: 'string', alias: 'r', desc: 'ARN of Role to use when invoking CloudFormation', default: undefined, requiresArg: true },
4040
'staging': { type: 'boolean', desc: 'Copy assets to the output directory (use --no-staging to disable the copy of assets which allows local debugging via the SAM CLI to reference the original source files)', default: true },
4141
'output': { type: 'string', alias: 'o', desc: 'Emits the synthesized cloud assembly into a directory (default: cdk.out)', requiresArg: true },
42-
'notices': { type: 'boolean', desc: 'Show relevant notices', default: YARGS_HELPERS.shouldDisplayNotices() },
42+
'notices': { type: 'boolean', desc: 'Show relevant notices' },
4343
'no-color': { type: 'boolean', desc: 'Removes colors and other style from console output', default: false },
4444
'ci': { type: 'boolean', desc: 'Force CI detection. If CI=true then logs will be sent to stdout instead of stderr', default: YARGS_HELPERS.isCI() },
4545
'unstable': { type: 'array', desc: 'Opt in to unstable features. The flag indicates that the scope and API of a feature might still change. Otherwise the feature is generally production ready and fully supported. Can be specified multiple times.', default: [] },

packages/aws-cdk/lib/cli/cli.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ChangeSetDeployment, DeploymentMethod, DirectDeployment } from '@a
44
import { ToolkitError, Toolkit } from '@aws-cdk/toolkit-lib';
55
import * as chalk from 'chalk';
66
import { CdkToolkit, AssetBuildTime } from './cdk-toolkit';
7+
import { ciSystemIsStdErrSafe } from './ci-systems';
78
import { displayVersionMessage } from './display-version';
89
import type { IoMessageLevel } from './io-host';
910
import { CliIoHost } from './io-host';
@@ -33,6 +34,7 @@ import type { StackSelector, Synthesizer } from '../cxapp';
3334
import { ProxyAgentProvider } from './proxy-agent';
3435
import { cdkCliErrorName } from './telemetry/error';
3536
import type { ErrorDetails } from './telemetry/schema';
37+
import { isCI } from './util/ci';
3638
import { isDeveloperBuildVersion, versionWithBuild, versionNumber } from './version';
3739
import { getLanguageFromAlias } from '../commands/language';
3840

@@ -112,7 +114,34 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
112114
await ioHost.asIoHelper().defaults.trace(`Telemetry instantiation failed: ${e.message}`);
113115
}
114116

115-
const shouldDisplayNotices = configuration.settings.get(['notices']);
117+
/**
118+
* The default value for displaying (and refreshing) notices on all commands.
119+
*
120+
* If the user didn't supply either `--notices` or `--no-notices`, we do
121+
* autodetection. The autodetection currently is: do write notices if we are
122+
* not on CI, or are on a CI system where we know that writing to stderr is
123+
* safe. We fail "closed"; that is, we decide to NOT print for unknown CI
124+
* systems, even though technically we maybe could.
125+
*/
126+
const isSafeToWriteNotices = !isCI() || Boolean(ciSystemIsStdErrSafe());
127+
128+
// Determine if notices should be displayed based on CLI args and configuration
129+
let shouldDisplayNotices: boolean;
130+
if (argv.notices !== undefined) {
131+
// CLI argument takes precedence
132+
shouldDisplayNotices = argv.notices;
133+
} else {
134+
// Fall back to configuration file setting, then autodetection
135+
const configNotices = configuration.settings.get(['notices']);
136+
if (configNotices !== undefined) {
137+
// Consider string "false" to be falsy in this context
138+
shouldDisplayNotices = configNotices !== 'false' && Boolean(configNotices);
139+
} else {
140+
// Default autodetection behavior
141+
shouldDisplayNotices = isSafeToWriteNotices;
142+
}
143+
}
144+
116145
// Notices either go to stderr, or nowhere
117146
ioHost.noticesDestination = shouldDisplayNotices ? 'stderr' : 'drop';
118147
const notices = Notices.create({
@@ -196,7 +225,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
196225
includeAcknowledged: !argv.unacknowledged,
197226
showTotal: argv.unacknowledged,
198227
});
199-
} else if (cmd !== 'version') {
228+
} else if (shouldDisplayNotices && cmd !== 'version') {
200229
await notices.display();
201230
}
202231
}

packages/aws-cdk/lib/cli/parse-command-line-arguments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export function parseCommandLineArguments(args: Array<string>): any {
135135
requiresArg: true,
136136
})
137137
.option('notices', {
138-
default: helpers.shouldDisplayNotices(),
138+
default: undefined,
139139
type: 'boolean',
140140
desc: 'Show relevant notices',
141141
})

packages/aws-cdk/lib/cli/util/yargs-helpers.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { ciSystemIsStdErrSafe } from '../ci-systems';
2-
import { isCI } from '../util/ci';
31
import { versionWithBuild } from '../version';
42

53
export { isCI } from '../util/ci';
@@ -48,16 +46,3 @@ export function browserForPlatform(): string {
4846
return 'xdg-open %u';
4947
}
5048
}
51-
52-
/**
53-
* The default value for displaying (and refreshing) notices on all commands.
54-
*
55-
* If the user didn't supply either `--notices` or `--no-notices`, we do
56-
* autodetection. The autodetection currently is: do write notices if we are
57-
* not on CI, or are on a CI system where we know that writing to stderr is
58-
* safe. We fail "closed"; that is, we decide to NOT print for unknown CI
59-
* systems, even though technically we maybe could.
60-
*/
61-
export function shouldDisplayNotices(): boolean {
62-
return !isCI() || Boolean(ciSystemIsStdErrSafe());
63-
}

packages/aws-cdk/test/cli/cli-arguments.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('yargs', () => {
3333
lookups: true,
3434
trace: undefined,
3535
unstable: [],
36-
notices: expect.any(Boolean),
36+
notices: undefined,
3737
output: undefined,
3838
},
3939
deploy: {

0 commit comments

Comments
 (0)