-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(aws-cdk-lib): prevent deep imports (#17707)
Sometimes, IDEs like VSCode will autocomplete deep imports into the CDK library. For example, they may generate the following: ```ts import { Bucket } from 'aws-cdk-lib/aws-s3/lib/bucket'; ``` Whereas the correct import should have been: ```ts import { Bucket } from 'aws-cdk-lib/aws-s3'; ``` If we allow people to write the former, they will be broken every time we change the internal file layout of our module (or conversely, we will not be allowed to change the file layout at all). Use the `package.json` `"exports"` mechanism to advertise the select paths that users are allowed to import from, and disallow the rest. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
6 changed files
with
546 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Check that the imports from 'aws-cdk-lib' we expect to work, work, and those we have shielded off don't work. | ||
*/ | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs-extra'; | ||
|
||
async function main() { | ||
// First make a tempdir and symlink `aws-cdk-lib` into it so we can refer to it | ||
// as if it was an installed module. | ||
await withTemporaryDirectory(async (tmpDir) => { | ||
await fs.mkdirp(path.join(tmpDir, 'node_modules')); | ||
await fs.symlink(path.resolve(__dirname, '..'), path.join(tmpDir, 'node_modules', 'aws-cdk-lib')); | ||
|
||
assertImportSucceeds('aws-cdk-lib'); | ||
assertImportFails('aws-cdk-lib/LICENSE', 'ERR_PACKAGE_PATH_NOT_EXPORTED'); | ||
assertImportFails('aws-cdk-lib/aws-s3/lib/bucket', 'ERR_PACKAGE_PATH_NOT_EXPORTED'); | ||
assertImportSucceeds('aws-cdk-lib/aws-s3'); | ||
|
||
function assertImportSucceeds(name: string) { | ||
require.resolve(name, { paths: [tmpDir] }); | ||
} | ||
|
||
function assertImportFails(name: string, code: string) { | ||
try { | ||
require.resolve(name, { paths: [tmpDir] }); | ||
|
||
// eslint-disable-next-line no-console | ||
console.error(`Import of '${name}' should have produced an error, but didn't.`); | ||
process.exitCode = 1; | ||
} catch (e) { | ||
if ((e as any).code !== code) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Import of '${name}' should have produced error ${code}, but got ${(e as any).code}.`); | ||
process.exitCode = 1; | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
|
||
export async function withTemporaryDirectory<T>(callback: (dir: string) => Promise<T>): Promise<T> { | ||
const tmpdir = await fs.mkdtemp(path.join(os.tmpdir(), path.basename(__filename))); | ||
try { | ||
return await callback(tmpdir); | ||
} finally { | ||
await fs.remove(tmpdir); | ||
} | ||
} | ||
|
||
|
||
main().catch((e) => { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
process.exitCode = 1; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.