Skip to content

fix(cli-internal): unify custom-resource dependency detection#14928

Draft
sarayev wants to merge 4 commits into
fix/gen2-construct-import-dedupfrom
fix/gen2-unify-dependency-detection
Draft

fix(cli-internal): unify custom-resource dependency detection#14928
sarayev wants to merge 4 commits into
fix/gen2-construct-import-dedupfrom
fix/gen2-unify-dependency-detection

Conversation

@sarayev

@sarayev sarayev commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Problem

Custom resource generation used two independent dependency detectors that could disagree: a regex (extractDependencies) decided whether the resource.ts wrapper passed a backend argument, while the AST transformer decided whether the construct constructor received a backend parameter. When the regex missed a dependency the AST caught (e.g. addResourceDependency with a non-literal category), the wrapper emitted new Construct(stack, id, backend) against a 2-parameter constructor — TS2554.

Fix

AmplifyHelperTransformer.transform now returns { sourceFile, addedBackendParam }. Both the construct constructor parameter and the wrapper argument are driven from that single signal, so they always agree. The import type { Backend } decision is keyed off it too. The now-redundant regex extractDependencies is removed.

Test

  • amplify-helper-transformer.test.ts updated to the new return shape, with added addedBackendParam true/false assertions.
  • New custom.generator.dependency-consistency.test.ts drives CustomResourceGenerator with a variable-category dependency (which the old regex missed) and asserts constructor param count equals wrapper call arg count.

Note

Stacked on #14927 (shares custom.generator.ts); review/merge that first, then rebase this onto dev.

Risk

Medium. Unifies on the AST detector (authoritative — it rewrites the references). Guarantees arg/param agreement; the residual TS2663 'undefined dependencies' case depends on customer source and is out of scope here.

sarayev added 4 commits June 22, 2026 10:40
…arg-count mismatch

Custom resource generation used two independent dependency detectors that
could disagree: a regex (extractDependencies) decided whether the resource.ts
wrapper passed a backend argument, while the AST transformer decided whether
the construct constructor received a backend parameter. When the regex missed
a dependency the AST caught (e.g. addResourceDependency with a non-literal
category), the wrapper emitted new Construct(stack, id, backend) against a
2-parameter constructor -> TS2554.

Make AmplifyHelperTransformer.transform return { sourceFile, addedBackendParam }
and drive both the construct constructor parameter and the wrapper argument
from that single signal, guaranteeing they always agree. The now-redundant
regex extractDependencies is removed. The Backend type import decision is also
keyed off addedBackendParam.
…ncy test

Rewrite the dependency-consistency test to mirror the sibling custom.generator
test mocking pattern (mock node:fs/promises and JSONUtilities) instead of real
temp dirs, which failed under the package's default jest config. The test still
asserts that, for a custom resource with a cross-category dependency, the
generated construct constructor parameter count equals the resource.ts
constructor-call argument count.
…igration

The gen2-migration custom-resource transformer only recognized the
property-access form `AmplifyHelpers.addResourceDependency(...)`. The
bare named-import form used by customer repos (import { addResourceDependency }
from '@aws-amplify/cli-extensibility-helper' then
const dependencies: AmplifyDependentResourcesAttributes = addResourceDependency(this, ...))
was never detected, so the variable was not registered in dependencyVariables
and hasDependencies stayed false. The Fn.ref rewrite never ran, leaving
undeclared dependencies.* references and causing TS2663.

- Detect bare Identifier addResourceDependency(...) calls in addition to the
  existing PropertyAccess form, registering the assigned variable and setting
  hasDependencies.
- In the : AmplifyDependentResourcesAttributes typed-declaration branch,
  register the variable and set hasDependencies before deleting it, so the
  backend ctor param/arg are emitted and dependencies.* refs are rewritten to
  backend.*.

Adds unit tests for the bare-import pattern and an integration test in the
dependency-consistency suite exercising the real generator path.
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