Skip to content

Commit 512cf95

Browse files
authored
fix(cli): remove source maps (#32317)
Remove the source maps from the bundled CLI. The source maps are not really useful for customers anyway, and have the following downsides: - They are 30+MB, which we vend to customers for no benefit. - They tend to slow down Node as it loads and processes them. We have reports that on some Node versions this even leads to socket timeouts as the Node process was too busy loading source maps (#19930). There are 2 steps to producing a CLI build: - First compile with TypeScript -> JavaScript. Produces sourcemaps that are still being loaded. - Then bundle JavaScript -> bundle. This removes sourcemaps. Developers running a local (non-bundled) build will benefit from the source maps generated by TypeScript. Two other changes in this PR that came up around this: * Clarify what the `--debug` flag is for (debugging the CDK app) and what it's not for (debugging the CLI) * Only print the stack trace in a CLI error if we're on a developer build; due to the minification printing the stack trace on a bundled copy prints a 1000-character minified line which is not useful to anyone. Closes #19930. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 62729ed commit 512cf95

File tree

9 files changed

+42
-9
lines changed

9 files changed

+42
-9
lines changed

packages/aws-cdk/CONTRIBUTING.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,20 @@ will change, and needs to be regenerated. For you, this means that:
257257
2. When you build the CLI locally, you must ensure your dependencies are up to date by running `yarn install` inside the CLI package.
258258
Otherwise, you might get an error like so: `aws-cdk: - [bundle/outdated-attributions] THIRD_PARTY_LICENSES is outdated (fixable)`.
259259

260+
## Source Maps
261+
262+
The source map handling is not entirely intuitive, so it bears some description here.
263+
264+
There are 2 steps to producing a CLI build:
265+
266+
- First we compile TypeScript to JavaScript. This step is configured to produce inline sourcemaps.
267+
- Then we bundle JavaScript -> bundled JavaScript. This removes the inline
268+
sourcemaps, and also is configured to *not* emit a fresh sourcemap file.
269+
270+
The upshot is that we don't vend a 30+MB sourcemap to customers that they have no use for,
271+
and that we don't slow down Node loading those sourcemaps, while if we are locally developing
272+
and testing the sourcemaps are still present and can be used.
273+
274+
During the CLI initialization, we always enable source map support: if we are developing
275+
then source maps are present and can be used, while in a production build there will be no
276+
source maps so there's nothing to load anyway.

packages/aws-cdk/bin/cdk

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env node
22

33
// source maps must be enabled before importing files
4-
if (process.argv.includes('--debug')) {
5-
process.setSourceMapsEnabled(true);
6-
}
4+
process.setSourceMapsEnabled(true);
5+
76
require('./cdk.js');

packages/aws-cdk/lib/cli.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ function determineHotswapMode(hotswap?: boolean, hotswapFallback?: boolean, watc
559559
return hotswapMode;
560560
}
561561

562+
/* istanbul ignore next: we never call this in unit tests */
562563
export function cli(args: string[] = process.argv.slice(2)) {
563564
exec(args)
564565
.then(async (value) => {
@@ -568,7 +569,10 @@ export function cli(args: string[] = process.argv.slice(2)) {
568569
})
569570
.catch((err) => {
570571
error(err.message);
571-
if (err.stack) {
572+
573+
// Log the stack trace if we're on a developer workstation. Otherwise this will be into a minified
574+
// file and the printed code line and stack trace are huge and useless.
575+
if (err.stack && version.isDeveloperBuild()) {
572576
debug(err.stack);
573577
}
574578
process.exitCode = 1;

packages/aws-cdk/lib/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function makeConfig(): CliConfig {
2121
'ignore-errors': { type: 'boolean', default: false, desc: 'Ignores synthesis errors, which will likely produce an invalid output' },
2222
'json': { type: 'boolean', alias: 'j', desc: 'Use JSON output instead of YAML when templates are printed to STDOUT', default: false },
2323
'verbose': { type: 'boolean', alias: 'v', desc: 'Show debug logs (specify multiple times to increase verbosity)', default: false, count: true },
24-
'debug': { type: 'boolean', desc: 'Enable emission of additional debugging information, such as creation stack traces of tokens', default: false },
24+
'debug': { type: 'boolean', desc: 'Debug the CDK app. Log additional information during synthesis, such as creation stack traces of tokens (sets CDK_DEBUG, will slow down synthesis)', default: false },
2525
'profile': { type: 'string', desc: 'Use the indicated AWS profile as the default environment', requiresArg: true },
2626
'proxy': { type: 'string', desc: 'Use the indicated proxy. Will read from HTTPS_PROXY environment variable if not specified', requiresArg: true },
2727
'ca-bundle-path': { type: 'string', desc: 'Path to CA certificate to use when validating HTTPS requests. Will read from AWS_CA_BUNDLE environment variable if not specified', requiresArg: true },

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function parseCommandLineArguments(
7373
})
7474
.option('debug', {
7575
type: 'boolean',
76-
desc: 'Enable emission of additional debugging information, such as creation stack traces of tokens',
76+
desc: 'Debug the CDK app. Log additional information during synthesis, such as creation stack traces of tokens (sets CDK_DEBUG, will slow down synthesis)',
7777
default: false,
7878
})
7979
.option('profile', {

packages/aws-cdk/lib/version.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const UPGRADE_DOCUMENTATION_LINKS: Record<number, string> = {
1515

1616
export const DISPLAY_VERSION = `${versionNumber()} (build ${commit()})`;
1717

18+
export function isDeveloperBuild(): boolean {
19+
return versionNumber() === '0.0.0';
20+
}
21+
1822
export function versionNumber(): string {
1923
// eslint-disable-next-line @typescript-eslint/no-require-imports
2024
return require(path.join(rootDir(), 'package.json')).version.replace(/\+[0-9a-f]+$/, '');

packages/aws-cdk/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"entryPoints": [
5858
"lib/index.js"
5959
],
60-
"sourcemap": "linked",
6160
"minifyWhitespace": true
6261
}
6362
},

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as os from 'os';
77
import * as sinon from 'sinon';
88
import * as logging from '../lib/logging';
99
import * as npm from '../lib/util/npm';
10-
import { latestVersionIfHigher, VersionCheckTTL, displayVersionMessage } from '../lib/version';
10+
import { latestVersionIfHigher, VersionCheckTTL, displayVersionMessage, isDeveloperBuild } from '../lib/version';
1111

1212
jest.setTimeout(10_000);
1313

@@ -141,3 +141,13 @@ describe('version message', () => {
141141
expect(printSpy).not.toHaveBeenCalledWith(expect.stringContaining('Information about upgrading from 99.x to 100.x'));
142142
});
143143
});
144+
145+
test('isDeveloperBuild call does not throw an error', () => {
146+
// To be frank: this is just to shut CodeCov up. It don't want to make an assertion
147+
// that the value is `true` when running tests, because I won't want to make too
148+
// many assumptions for no good reason.
149+
150+
isDeveloperBuild();
151+
152+
// THEN: should not explode
153+
});

tools/@aws-cdk/node-bundle/src/api/bundle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ export class Bundle {
443443
bundle: true,
444444
target: 'node14',
445445
platform: 'node',
446-
sourcemap: this.sourcemap ?? 'inline',
446+
sourcemap: this.sourcemap,
447447
metafile: true,
448448
minify: this.minify,
449449
minifyWhitespace: this.minifyWhitespace,

0 commit comments

Comments
 (0)