Skip to content

fix(cli-internal): avoid duplicate Construct import for double-quoted sources#14927

Draft
sarayev wants to merge 2 commits into
devfrom
fix/gen2-construct-import-dedup
Draft

fix(cli-internal): avoid duplicate Construct import for double-quoted sources#14927
sarayev wants to merge 2 commits into
devfrom
fix/gen2-construct-import-dedup

Conversation

@sarayev

@sarayev sarayev commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Problem

transformResource() injected import { Construct } from 'constructs'; guarded by a single-quote-only substring check (content.includes(\"from 'constructs'\")). A custom resource whose cdk-stack.ts already imported Construct with double quotes slipped past the guard, yielding two Construct imports — TS2300.

Fix

Replace the guard with a quote-agnostic, binding-specific regex that detects an existing Construct named import in either quote style, while still injecting when only a different binding (e.g. IConstruct) is imported from constructs.

Test

custom.generator.construct-import-dedup.test.ts drives CustomResourceGenerator with a double-quote Construct fixture and asserts exactly one constructs import.

Risk

Low. One-line guard change; existing custom-resources tests unchanged.

… sources

transformResource() injected 'import { Construct } from "constructs";'
guarded by a single-quote-only substring check
(content.includes("from 'constructs'")). A custom resource whose
cdk-stack.ts already imported Construct with double quotes slipped past
the guard, yielding two Construct imports and TS2300 duplicate identifier.

Replace the guard with a quote-agnostic, binding-specific regex that
detects an existing Construct named import in either quote style, while
still injecting when only a different binding (e.g. IConstruct) is
imported from 'constructs'.
…dup test

The construct-import dedup test used real temp dirs and relied on a file-reading
mock, which fails under the package's default jest config (fs-extra is
auto-mocked and JSONUtilities is real), causing 'project-config.json does not
exist'. Rewrite it to mirror the sibling custom.generator.test.ts pattern:
mock node:fs/promises and JSONUtilities, drive the generator with a
double-quoted Construct import, and assert exactly one constructs import in the
written content.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant