Skip to content

Commit 6db0d9b

Browse files
authored
refactor(cli,cli-lib-alpha): restrict relative package imports (#492)
Enabled the linter rule `import/no-relative-packages` to disallow relative imports from other monorepo packages. While relative imports are generally possible, they require special handling w.r.t. to bundling of the JS code **and** type declarations. Hence we discourage them by default. There are currently two packages that require relative imports: `aws-cdk` (the CLI) and `cli-lib-alpha`. Both depend on functionality that is not publicly available in the respective monorepo packages, so we need to import relatively. `cli-lib-alpha` is about to be deprecated, and `aws-cdk` should eventually move to only use public features from `toolkit-lib`. Both packages have their relative imports updated to optimize for the number of files allowed to still import relatively. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent ca6953e commit 6db0d9b

File tree

92 files changed

+131
-165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+131
-165
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projenrc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,6 @@ const cli = configureProject(
10241024
nodeBundle,
10251025
yargsGen,
10261026
cliPluginContract,
1027-
toolkitLib,
10281027
'@octokit/rest',
10291028
'@types/archiver',
10301029
'@types/fs-extra@^9',
@@ -1051,6 +1050,7 @@ const cli = configureProject(
10511050
cloudAssemblySchema.customizeReference({ versionType: 'minimal' }),
10521051
cloudFormationDiff.customizeReference({ versionType: 'exact' }),
10531052
cxApi,
1053+
toolkitLib,
10541054
'archiver',
10551055
`@aws-sdk/client-appsync@${CLI_SDK_V3_RANGE}`,
10561056
`@aws-sdk/client-cloudformation@${CLI_SDK_V3_RANGE}`,

packages/@aws-cdk-testing/cli-integ/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/cdk-cli-wrapper/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/cli-lib-alpha/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* eslint-disable import/no-extraneous-dependencies */
2+
/* eslint-disable import/no-relative-packages */
3+
export { prepareContext, prepareDefaultEnvironment } from '../../../aws-cdk/lib/api/cloud-assembly';
4+
export { createAssembly } from '../../../aws-cdk/lib/cxapp/exec';
5+
export { exec } from '../../../aws-cdk';
6+
export { debug } from '../../../aws-cdk/lib/logging';

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1+
import { exec as runCli, debug, createAssembly, prepareContext, prepareDefaultEnvironment } from './aws-cdk';
12
import type { SharedOptions, DeployOptions, DestroyOptions, BootstrapOptions, SynthOptions, ListOptions } from './commands';
23
import { StackActivityProgress, HotswapMode } from './commands';
3-
import { exec as runCli } from '../../../aws-cdk/lib';
4-
import { prepareContext, prepareDefaultEnvironment } from '../../../aws-cdk/lib/api/cloud-assembly';
5-
import { createAssembly } from '../../../aws-cdk/lib/cxapp';
6-
import { debug } from '../../../aws-cdk/lib/legacy-exports';
74

85
const debugFn = async (msg: string) => void debug(msg);
96

packages/@aws-cdk/cli-lib-alpha/test/cli.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { join } from 'path';
22
import * as core from 'aws-cdk-lib/core';
3-
import * as cli from '../../../aws-cdk/lib';
43
import { AwsCdkCli } from '../lib';
4+
import * as cli from '../lib/aws-cdk';
55

66
// These tests synthesize an actual CDK app and take a bit longer
77
jest.setTimeout(60_000);
88

9-
jest.mock('../../../aws-cdk/lib', () => {
10-
const original = jest.requireActual('../../../aws-cdk/lib');
9+
jest.mock('../lib/aws-cdk', () => {
10+
const original = jest.requireActual('../lib/aws-cdk');
1111
return {
1212
...original,
1313
exec: jest.fn(original.exec),

packages/@aws-cdk/cli-lib-alpha/test/commands.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import * as cli from 'aws-cdk';
12
import * as core from 'aws-cdk-lib/core';
2-
import * as cli from '../../../aws-cdk/lib';
33
import { AwsCdkCli } from '../lib';
44
import { HotswapMode, RequireApproval, StackActivityProgress } from '../lib/commands';
55

packages/@aws-cdk/cli-plugin-contract/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/cloud-assembly-schema/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/cloudformation-diff/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/integ-runner/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/node-bundle/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/toolkit-lib/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/@aws-cdk/user-input-gen/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/yarn-cling/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk/.projen/deps.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk/.projen/tasks.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk/lib/api-private.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
export * from '../../@aws-cdk/toolkit-lib/lib/api/private';
1+
/* eslint-disable import/no-relative-packages */
22
export { deployStack, DeployStackOptions as DeployStackApiOptions } from '../../@aws-cdk/toolkit-lib/lib/api/deployments/deploy-stack';
33
export * as cfnApi from '../../@aws-cdk/toolkit-lib/lib/api/deployments/cfn-api';
4+
export * from '../../@aws-cdk/toolkit-lib/lib/api/io/private';
5+
export * from '../../@aws-cdk/toolkit-lib/lib/private/activity-printer';
6+
export * from '../../@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/borrowed-assembly';
7+

packages/aws-cdk/lib/api/aws-auth.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from '../../../@aws-cdk/toolkit-lib/lib/api/aws-auth';
23
export * from '../../../@aws-cdk/toolkit-lib/lib/api/aws-auth/private';
4+
export * from '../../../@aws-cdk/toolkit-lib/lib/api/aws-auth/awscli-compatible';
5+

packages/aws-cdk/lib/api/bootstrap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from '../../../@aws-cdk/toolkit-lib/lib/api/bootstrap';

packages/aws-cdk/lib/api/cloud-assembly.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from '../../../@aws-cdk/toolkit-lib/lib/api/cloud-assembly';
23
export * from '../../../@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private';
34
export * from '../../../@aws-cdk/toolkit-lib/lib/api/cloud-assembly/environment';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from '../../../@aws-cdk/toolkit-lib/lib/api/cloudformation';

packages/aws-cdk/lib/api/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from '../../../@aws-cdk/toolkit-lib/lib/api/context';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from '../../../@aws-cdk/toolkit-lib/lib/api/deployments';

packages/aws-cdk/lib/api/environment.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/aws-cdk/lib/api/garbage-collection.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/aws-cdk/lib/api/hotswap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from '../../../@aws-cdk/toolkit-lib/lib/api/hotswap';

packages/aws-cdk/lib/api/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from './bootstrap';
2-
export * from './garbage-collection';
3+
export * from './cloudformation';
4+
export * from './cloud-assembly';
35
export * from './deployments';
4-
export * from './toolkit-info';
56
export * from './aws-auth';
6-
export * from './rwlock';
7-
export * from './tree';
8-
9-
export * from '../../../@aws-cdk/toolkit-lib/lib/toolkit/toolkit-error';
107
export * from './cloud-assembly';
11-
export * from './plugin';
8+
export * from './notices';
9+
10+
export * from '../../../@aws-cdk/toolkit-lib/lib/api/diff';
11+
export * from '../../../@aws-cdk/toolkit-lib/lib/api/io';
12+
export * from '../../../@aws-cdk/toolkit-lib/lib/api/logs-monitor';
13+
export * from '../../../@aws-cdk/toolkit-lib/lib/api/resource-import';
14+
export { RWLock, IReadLock } from '../../../@aws-cdk/toolkit-lib/lib/api/rwlock';
15+
export * from '../../../@aws-cdk/toolkit-lib/lib/api/toolkit-info';
16+
export { loadTree, some } from '../../../@aws-cdk/toolkit-lib/lib/api/tree';
17+
export * from '../../../@aws-cdk/toolkit-lib/lib/api/work-graph';
18+
export * from '../../../@aws-cdk/toolkit-lib/lib/api/garbage-collection';

packages/aws-cdk/lib/api/logs-monitor.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from '../../../@aws-cdk/toolkit-lib/lib/api/notices';

packages/aws-cdk/lib/api/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from '../../../@aws-cdk/toolkit-lib/lib/api/plugin';

packages/aws-cdk/lib/api/resource-import.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/aws-cdk/lib/api/rwlock.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/aws-cdk/lib/api/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from '../../../@aws-cdk/toolkit-lib/lib/api/settings';

packages/aws-cdk/lib/api/stack-events.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/aws-cdk/lib/api/tags.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/* eslint-disable import/no-relative-packages */
12
export * from '../../../@aws-cdk/toolkit-lib/lib/api/tags';

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/aws-cdk/lib/api/work-graph.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as path from 'path';
22
import { format } from 'util';
3+
import { RequireApproval } from '@aws-cdk/cloud-assembly-schema';
34
import * as cxapi from '@aws-cdk/cx-api';
5+
import { StackSelectionStrategy, ToolkitError, PermissionChangeType, Toolkit } from '@aws-cdk/toolkit-lib';
6+
import type { ToolkitAction, ToolkitOptions } from '@aws-cdk/toolkit-lib';
47
import * as chalk from 'chalk';
58
import * as chokidar from 'chokidar';
69
import * as fs from 'fs-extra';
@@ -9,28 +12,17 @@ import * as uuid from 'uuid';
912
import { CliIoHost } from './io-host';
1013
import type { Configuration } from './user-configuration';
1114
import { PROJECT_CONFIG } from './user-configuration';
12-
import type { ToolkitAction } from '../../../@aws-cdk/toolkit-lib';
13-
import { StackSelectionStrategy, ToolkitError } from '../../../@aws-cdk/toolkit-lib';
14-
import { asIoHelper } from '../../../@aws-cdk/toolkit-lib/lib/api/io/private';
15-
import { PermissionChangeType } from '../../../@aws-cdk/toolkit-lib/lib/payloads';
16-
import type { ToolkitOptions } from '../../../@aws-cdk/toolkit-lib/lib/toolkit';
17-
import { Toolkit } from '../../../@aws-cdk/toolkit-lib/lib/toolkit';
18-
import { DEFAULT_TOOLKIT_STACK_NAME } from '../api';
15+
import { asIoHelper, cfnApi } from '../../lib/api-private';
16+
import type { AssetBuildNode, AssetPublishNode, Concurrency, StackNode, WorkGraph } from '../api';
17+
import { DEFAULT_TOOLKIT_STACK_NAME, DiffFormatter, WorkGraphBuilder, removeNonImportResources, ResourceImporter, ResourceMigrator, GarbageCollector, CloudWatchLogEventMonitor, findCloudWatchLogGroups } from '../api';
1918
import type { SdkProvider } from '../api/aws-auth';
2019
import type { BootstrapEnvironmentOptions } from '../api/bootstrap';
2120
import { Bootstrapper } from '../api/bootstrap';
2221
import { ExtendedStackSelection, StackCollection } from '../api/cloud-assembly';
2322
import type { DeploymentMethod, Deployments, SuccessfulDeployStackResult } from '../api/deployments';
24-
import { GarbageCollector } from '../api/garbage-collection';
2523
import { EcsHotswapProperties, HotswapMode, HotswapPropertyOverrides } from '../api/hotswap';
26-
import { CloudWatchLogEventMonitor, findCloudWatchLogGroups } from '../api/logs-monitor';
27-
import { removeNonImportResources, ResourceImporter, ResourceMigrator } from '../api/resource-import';
2824
import { type Tag, tagsForStack } from '../api/tags';
29-
import type { AssetBuildNode, AssetPublishNode, Concurrency, StackNode, WorkGraph } from '../api/work-graph';
30-
import { WorkGraphBuilder } from '../api/work-graph';
31-
import { cfnApi } from '../api-private';
3225
import { StackActivityProgress } from '../commands/deploy';
33-
import { DiffFormatter, RequireApproval } from '../commands/diff';
3426
import { listStacks } from '../commands/list-stacks';
3527
import type { FromScan, GenerateTemplateOutput } from '../commands/migrate';
3628
import {
@@ -2032,6 +2024,6 @@ function stackMetadataLogger(verbose?: boolean): (level: 'info' | 'error' | 'war
20322024
* - RequireApproval.BROADENING and the changes are indeed broadening permissions
20332025
*/
20342026
function requiresApproval(requireApproval: RequireApproval, permissionChangeType: PermissionChangeType) {
2035-
return requireApproval === RequireApproval.ANY_CHANGE ||
2027+
return requireApproval === RequireApproval.ANYCHANGE ||
20362028
requireApproval === RequireApproval.BROADENING && permissionChangeType === PermissionChangeType.BROADENING;
20372029
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { RequireApproval } from '@aws-cdk/cloud-assembly-schema';
12
// eslint-disable-next-line import/no-extraneous-dependencies
23
import { CliHelpers, type CliConfig } from '@aws-cdk/user-input-gen';
34
import * as cdk_from_cfn from 'cdk-from-cfn';
45
import { StackActivityProgress } from '../commands/deploy';
5-
import { RequireApproval } from '../commands/diff';
66
import { availableInitLanguages } from '../commands/init';
77

88
export const YARGS_HELPERS = new CliHelpers('./util/yargs-helpers');
@@ -118,7 +118,7 @@ export async function makeConfig(): Promise<CliConfig> {
118118
'all': { type: 'boolean', desc: 'Deploy all available stacks', default: false },
119119
'build-exclude': { type: 'array', alias: 'E', desc: 'Do not rebuild asset with the given ID. Can be specified multiple times', default: [] },
120120
'exclusively': { type: 'boolean', alias: 'e', desc: 'Only deploy requested stacks, don\'t include dependencies' },
121-
'require-approval': { type: 'string', choices: [RequireApproval.NEVER, RequireApproval.ANY_CHANGE, RequireApproval.BROADENING], desc: 'What security-sensitive changes need manual approval' },
121+
'require-approval': { type: 'string', choices: [RequireApproval.NEVER, RequireApproval.ANYCHANGE, RequireApproval.BROADENING], desc: 'What security-sensitive changes need manual approval' },
122122
'notification-arns': { type: 'array', desc: 'ARNs of SNS topics that CloudFormation will notify with stack related events. These will be added to ARNs specified with the \'notificationArns\' stack property.' },
123123
// @deprecated(v2) -- tags are part of the Cloud Assembly and tags specified here will be overwritten on the next deployment
124124
'tags': { type: 'array', alias: 't', desc: 'Tags to add to the stack (KEY=VALUE), overrides tags from Cloud Assembly (deprecated)' },

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
1+
/* eslint-disable @typescript-eslint/no-shadow */ // yargs
12
import * as cxapi from '@aws-cdk/cx-api';
3+
import { ToolkitError } from '@aws-cdk/toolkit-lib';
24
import * as chalk from 'chalk';
35
import { CdkToolkit, AssetBuildTime } from './cdk-toolkit';
46
import type { IoMessageLevel } from './io-host';
57
import { CliIoHost } from './io-host';
68
import { parseCommandLineArguments } from './parse-command-line-arguments';
79
import { checkForPlatformWarnings } from './platform-warnings';
810
import { prettyPrintError } from './pretty-print-error';
11+
import { GLOBAL_PLUGIN_HOST } from './singleton-plugin-host';
912
import type { Command } from './user-configuration';
1013
import { Configuration } from './user-configuration';
1114
import * as version from './version';
12-
import { ToolkitError } from '../../../@aws-cdk/toolkit-lib';
13-
import { asIoHelper, IO } from '../../../@aws-cdk/toolkit-lib/lib/api/io/private';
14-
import { SdkProvider, IoHostSdkLogger, setSdkTracing } from '../api/aws-auth';
15+
import { asIoHelper, IO } from '../../lib/api-private';
16+
import type { IReadLock } from '../api';
17+
import { ToolkitInfo, Notices } from '../api';
18+
import { SdkProvider, IoHostSdkLogger, setSdkTracing, makeRequestHandler } from '../api/aws-auth';
1519
import type { BootstrapSource } from '../api/bootstrap';
1620
import { Bootstrapper } from '../api/bootstrap';
1721
import type { DeploymentMethod } from '../api/deployments';
1822
import { Deployments } from '../api/deployments';
1923
import { HotswapMode } from '../api/hotswap';
20-
import { Notices } from '../api/notices';
21-
import type { IReadLock } from '../api/rwlock';
2224
import type { Settings } from '../api/settings';
23-
import { ToolkitInfo } from '../api/toolkit-info';
2425
import { contextHandler as context } from '../commands/context';
2526
import { docs } from '../commands/docs';
2627
import { doctor } from '../commands/doctor';
2728
import { cliInit, printAvailableTemplates } from '../commands/init';
2829
import { getMigrateScanType } from '../commands/migrate';
2930
import { execProgram, CloudExecutable } from '../cxapp';
3031
import type { StackSelector, Synthesizer } from '../cxapp';
31-
import { GLOBAL_PLUGIN_HOST } from './singleton-plugin-host';
32-
import { makeRequestHandler } from '../../../@aws-cdk/toolkit-lib/lib/api/shared-private';
33-
/* eslint-disable max-len */
34-
/* eslint-disable @typescript-eslint/no-shadow */ // yargs
3532

3633
if (!process.stdout.isTTY) {
3734
// Disable chalk color highlighting

0 commit comments

Comments
 (0)