Skip to content

Commit 2dc5a56

Browse files
mrgraingithub-actions
and
github-actions
authored
refactor(cli): tree api with injectable logging (#276)
Updates the `tree.ts` APIs to use an injectable trace function, so that it can easily be used from old and new code paths. At some point in the future this should just take an `IoHelper`, but not now. --- 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 a477c66 commit 2dc5a56

File tree

10 files changed

+26
-21
lines changed

10 files changed

+26
-21
lines changed

packages/@aws-cdk/cli-plugin-contract/lib/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export interface CredentialProviderSource {
107107
/**
108108
* Construct a credential provider for the given account and the given access mode
109109
*
110-
* Guaranteed to be called only if canProvideCredentails() returned true at some point.
110+
* Guaranteed to be called only if canProvideCredentials() returned true at some point.
111111
*
112112
* While it is possible for the plugin to return a static set of credentials, it is
113113
* recommended to return a provider.

packages/@aws-cdk/tmp-toolkit-helpers/src/api/io/private/messages.ts

+5
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ export const IO = {
375375
}),
376376

377377
// Assembly codes
378+
DEFAULT_ASSEMBLY_TRACE: make.trace({
379+
code: 'CDK_ASSEMBLY_I0000',
380+
description: 'Default trace messages emitted from Cloud Assembly operations',
381+
}),
382+
378383
CDK_ASSEMBLY_I0010: make.debug({
379384
code: 'CDK_ASSEMBLY_I0010',
380385
description: 'Generic environment preparation debug messages',

packages/@aws-cdk/toolkit-lib/docs/message-registry.md

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ group: Documents
7373
| `CDK_TOOLKIT_W0101` | A notice that is marked as a warning | `warn` | n/a |
7474
| `CDK_TOOLKIT_E0101` | A notice that is marked as an error | `error` | n/a |
7575
| `CDK_TOOLKIT_I0101` | A notice that is marked as informational | `info` | n/a |
76+
| `CDK_ASSEMBLY_I0000` | Default trace messages emitted from Cloud Assembly operations | `trace` | n/a |
7677
| `CDK_ASSEMBLY_I0010` | Generic environment preparation debug messages | `debug` | n/a |
7778
| `CDK_ASSEMBLY_W0010` | Emitted if the found framework version does not support context overflow | `warn` | n/a |
7879
| `CDK_ASSEMBLY_I0042` | Writing updated context | `debug` | {@link UpdatedContext} |

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export { ResourceMigrator } from '../../../../aws-cdk/lib/api/resource-import';
1111
export { CloudWatchLogEventMonitor, findCloudWatchLogGroups } from '../../../../aws-cdk/lib/api/logs';
1212
export { type WorkGraph, WorkGraphBuilder, AssetBuildNode, AssetPublishNode, StackNode, Concurrency } from '../../../../aws-cdk/lib/api/work-graph';
1313
export { Bootstrapper } from '../../../../aws-cdk/lib/api/bootstrap';
14+
export { loadTree, some } from '../../../../aws-cdk/lib/api/tree';
1415

1516
// Context Providers
1617
export * as contextproviders from '../../../../aws-cdk/lib/context-providers';
@@ -20,9 +21,6 @@ export { HotswapMode } from '../../../../aws-cdk/lib/api/hotswap/common';
2021
export { HotswapPropertyOverrides, EcsHotswapProperties } from '../../../../aws-cdk/lib/api/hotswap/common';
2122
export { RWLock, type ILock } from '../../../../aws-cdk/lib/api/util/rwlock';
2223

23-
// @todo Not yet API probably should be
24-
export { loadTree, some } from '../../../../aws-cdk/lib/tree';
25-
2624
// @todo Cloud Assembly and Executable - this is a messy API right now
2725
export { CloudAssembly, sanitizePatterns, StackCollection, ExtendedStackSelection } from '../../../../aws-cdk/lib/api/cxapp/cloud-assembly';
2826
export { prepareDefaultEnvironment, prepareContext, spaceAvailableForContext } from '../../../../aws-cdk/lib/api/cxapp/exec';

packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/prepare-source.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ export async function withContext<T>(
130130
*
131131
* @param assembly the assembly to check
132132
*/
133-
export async function checkContextOverflowSupport(assembly: cxapi.CloudAssembly, ioHost: IoHelper): Promise<void> {
134-
const tree = loadTree(assembly);
133+
export async function checkContextOverflowSupport(assembly: cxapi.CloudAssembly, ioHelper: IoHelper): Promise<void> {
134+
const tree = loadTree(assembly, (msg: string) => void ioHelper.notify(IO.DEFAULT_ASSEMBLY_TRACE.msg(msg)));
135135
const frameworkDoesNotSupportContextOverflow = some(tree, node => {
136136
const fqn = node.constructInfo?.fqn;
137137
const version = node.constructInfo?.version;
@@ -142,7 +142,7 @@ export async function checkContextOverflowSupport(assembly: cxapi.CloudAssembly,
142142
// We're dealing with an old version of the framework here. It is unaware of the temporary
143143
// file, which means that it will ignore the context overflow.
144144
if (frameworkDoesNotSupportContextOverflow) {
145-
await ioHost.notify(IO.CDK_ASSEMBLY_W0010.msg('Part of the context could not be sent to the application. Please update the AWS CDK library to the latest version.'));
145+
await ioHelper.notify(IO.CDK_ASSEMBLY_W0010.msg('Part of the context could not be sent to the application. Please update the AWS CDK library to the latest version.'));
146146
}
147147
}
148148

packages/aws-cdk/lib/api/cxapp/exec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import * as cxapi from '@aws-cdk/cx-api';
66
import * as fs from 'fs-extra';
77
import * as semver from 'semver';
88
import { ToolkitError } from '../../../../@aws-cdk/tmp-toolkit-helpers/src/api';
9+
import { loadTree, some } from '../../api/tree';
910
import type { Configuration } from '../../cli/user-configuration';
1011
import { PROJECT_CONFIG, USER_DEFAULTS } from '../../cli/user-configuration';
1112
import { versionNumber } from '../../cli/version';
12-
import { debug, warning } from '../../logging';
13-
import { loadTree, some } from '../../tree';
13+
import { debug, trace, warning } from '../../logging';
1414
import { splitBySize } from '../../util';
1515
import type { SdkProvider } from '../aws-auth';
1616
import type { Settings } from '../settings';
@@ -293,7 +293,7 @@ function contextOverflowCleanup(location: string | undefined, assembly: cxapi.Cl
293293
if (location) {
294294
fs.removeSync(path.dirname(location));
295295

296-
const tree = loadTree(assembly);
296+
const tree = loadTree(assembly, trace);
297297
const frameworkDoesNotSupportContextOverflow = some(tree, node => {
298298
const fqn = node.constructInfo?.fqn;
299299
const version = node.constructInfo?.version;

packages/aws-cdk/lib/tree.ts renamed to packages/aws-cdk/lib/api/tree.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import * as path from 'path';
1+
import * as path from 'node:path';
22
import type { CloudAssembly } from '@aws-cdk/cx-api';
33
import * as fs from 'fs-extra';
4-
import { trace } from './logging';
54

65
/**
76
* Source information on a construct (class fqn and version)
@@ -37,7 +36,7 @@ export function some(node: ConstructTreeNode | undefined, predicate: (n: Constru
3736
}
3837
}
3938

40-
export function loadTree(assembly: CloudAssembly): ConstructTreeNode | undefined {
39+
export function loadTree(assembly: CloudAssembly, trace: (msg: string) => void): ConstructTreeNode | undefined {
4140
try {
4241
const outdir = assembly.directory;
4342
const fileName = assembly.tree()?.file;
@@ -48,7 +47,7 @@ export function loadTree(assembly: CloudAssembly): ConstructTreeNode | undefined
4847
}
4948
}
5049

51-
export function loadTreeFromDir(outdir: string): ConstructTreeNode | undefined {
50+
export function loadTreeFromDir(outdir: string, trace: (msg: string) => void): ConstructTreeNode | undefined {
5251
try {
5352
return fs.readJSONSync(path.join(outdir, 'tree.json')).tree;
5453
} catch (e) {

packages/aws-cdk/lib/notices.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import * as semver from 'semver';
88
import type { SdkHttpOptions } from './api';
99
import { AwsCliCompatible } from './api/aws-auth/awscli-compatible';
1010
import type { Context } from './api/context';
11+
import type { ConstructTreeNode } from './api/tree';
12+
import { loadTreeFromDir } from './api/tree';
1113
import type { IIoHost } from './cli/io-host';
1214
import { versionNumber } from './cli/version';
13-
import type { ConstructTreeNode } from './tree';
14-
import { loadTreeFromDir } from './tree';
1515
import { cdkCacheDir, formatErrorMessage } from './util';
1616
import { ToolkitError } from '../../@aws-cdk/tmp-toolkit-helpers/src/api';
1717
import { IO, asIoHelper, IoDefaultMessages } from '../../@aws-cdk/tmp-toolkit-helpers/src/api/io/private';
@@ -211,7 +211,7 @@ export class NoticesFilter {
211211
* Load the construct tree from the given directory and return its components
212212
*/
213213
private constructTreeComponents(manifestDir: string): ActualComponent[] {
214-
const tree = loadTreeFromDir(manifestDir);
214+
const tree = loadTreeFromDir(manifestDir, (msg: string) => void this.ioMessages.notify(IO.DEFAULT_ASSEMBLY_TRACE.msg(msg)));
215215
if (!tree) {
216216
return [];
217217
}

packages/aws-cdk/test/tree.test.ts renamed to packages/aws-cdk/test/api/tree.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable import/order */
22
import * as path from 'path';
3-
import { ConstructTreeNode, loadTreeFromDir, some } from '../lib/tree';
3+
import { ConstructTreeNode, loadTreeFromDir, some } from '../../lib/api/tree';
44

55
describe('some', () => {
66
const tree: ConstructTreeNode = {
@@ -104,12 +104,12 @@ describe('some', () => {
104104

105105
describe('loadTreeFromDir', () => {
106106
test('can find tree', () => {
107-
const tree = loadTreeFromDir(path.join(__dirname, 'cloud-assembly-trees', 'built-with-1_144_0'));
107+
const tree = loadTreeFromDir(path.join(__dirname, '..', 'cloud-assembly-trees', 'built-with-1_144_0'), () => {});
108108
expect(tree?.id).toEqual('App');
109109
});
110110

111111
test('cannot find tree', () => {
112-
const tree = loadTreeFromDir(path.join(__dirname, 'cloud-assembly-trees', 'foo'));
112+
const tree = loadTreeFromDir(path.join(__dirname, '..', 'cloud-assembly-trees', 'foo'), () => {});
113113
expect(tree).toEqual(undefined);
114114
});
115115
});

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,9 @@ describe(Notices, () => {
786786

787787
test('nothing when there are no notices', async () => {
788788
Notices.create({ ioHost, context: new Context() }).display();
789-
expect(ioHost.messages).toEqual([]);
789+
// expect a single trace that the tree.json was not found, but nothing else
790+
expect(ioHost.messages.length).toBe(1);
791+
ioHost.expectMessage({ level: 'trace', containing: 'Failed to get tree.json file' });
790792
});
791793

792794
test('total count when show total is true', async () => {

0 commit comments

Comments
 (0)