Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(cli): TypeError: localAspects is not iterable #32470

Closed
1 task done
github2023spring opened this issue Dec 11, 2024 · 17 comments · Fixed by #32647
Closed
1 task done

(cli): TypeError: localAspects is not iterable #32470

github2023spring opened this issue Dec 11, 2024 · 17 comments · Fixed by #32647
Labels
bug This issue is a bug. management/tracking Issues that track a subject or multiple issues p0 package/tools Related to AWS CDK Tools or CLI potential-regression Marking this issue as a potential regression to be checked by team member

Comments

@github2023spring
Copy link

github2023spring commented Dec 11, 2024

Please add your +1 👍 to let us know you have encountered this

Status: IN-PROGRESS

Overview:

Users who run into this issue have multiple copies of aws-cdk-lib in your dependency tree at different versions. For example using a construct library that directly depends on a different aws-cdk-lib version, or from local symlinking multiple workspace directories together (npm link), or a messy npm install history.

In those cases, if you get an Aspects object from an older aws-cdk-lib version, then aspects.applied would return undefined which definitely is not iterable!

Complete Error Message:

^
TypeError: localAspects is not iterable
at sortAspectsByPriority (/local/p4clients/pkgbuild-gX619/workspace/src/MarsCompatibilityMessageCDK/node_modules/aws-cdk-lib/core/lib/private/synthesis.js:2:4349)
at recurse (/local/p4clients/pkgbuild-gX619/workspace/src/MarsCompatibilityMessageCDK/node_modules/aws-cdk-lib/core/lib/private/synthesis.js:2:3271)
at recurse (/local/p4clients/pkgbuild-gX619/workspace/src/MarsCompatibilityMessageCDK/node_modules/aws-cdk-lib/core/lib/private/synthesis.js:2:4167)
at recurse (/local/p4clients/pkgbuild-gX619/workspace/src/MarsCompatibilityMessageCDK/node_modules/aws-cdk-lib/core/lib/private/synthesis.js:2:4167)
at recurse (/local/p4clients/pkgbuild-gX619/workspace/src/MarsCompatibilityMessageCDK/nod

Workaround:

Downgrade to CDK v2.171.1, Rolling forward to CDK v2.173.3 when released, or fixing the dependency issue in your CDK app.

Solution:

#32647

Related Issues:


Original Issue:

Describe the bug

After updating to 2.172.0, the cdk synth command produce the following error message.

^
TypeError: localAspects is not iterable
at sortAspectsByPriority (/local/p4clients/pkgbuild-gX619/workspace/src/MarsCompatibilityMessageCDK/node_modules/aws-cdk-lib/core/lib/private/synthesis.js:2:4349)
at recurse (/local/p4clients/pkgbuild-gX619/workspace/src/MarsCompatibilityMessageCDK/node_modules/aws-cdk-lib/core/lib/private/synthesis.js:2:3271)
at recurse (/local/p4clients/pkgbuild-gX619/workspace/src/MarsCompatibilityMessageCDK/node_modules/aws-cdk-lib/core/lib/private/synthesis.js:2:4167)
at recurse (/local/p4clients/pkgbuild-gX619/workspace/src/MarsCompatibilityMessageCDK/node_modules/aws-cdk-lib/core/lib/private/synthesis.js:2:4167)
at recurse (/local/p4clients/pkgbuild-gX619/workspace/src/MarsCompatibilityMessageCDK/nod

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

2.171.1

Expected Behavior

cdk synth should produce cloudformation template

Current Behavior

cdk synth failed

Reproduction Steps

Updating to 2.172.0, and run cdk synth.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.172.0

Framework Version

No response

Node.js Version

v20.18.1

OS

MacOs

Language

TypeScript

Language Version

5.7.2

Other information

No response

@github2023spring github2023spring added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 11, 2024
@github-actions github-actions bot added package/tools Related to AWS CDK Tools or CLI potential-regression Marking this issue as a potential regression to be checked by team member labels Dec 11, 2024
@nmussy
Copy link
Contributor

nmussy commented Dec 11, 2024

This seems likely to have been caused by #32097 @sumupitchayan

@ashishdhingra
Copy link
Contributor

ashishdhingra commented Dec 11, 2024

Most likely caused here which was implemented as part of PR #32097 as @nmussy mentioned in #32470 (comment).

Below sample CDK code using Aspects doesn't reproduce the issue (used CDK version 2.172.0 (build 0f666c5)):

class BucketVersioningChecker implements cdk.IAspect {
  public visit(node: IConstruct): void {
    // See that we're dealing with a CfnBucket
    if (node instanceof s3.CfnBucket) {

      // Check for versioning property, exclude the case where the property
      // can be a token (IResolvable).
      if (!node.versioningConfiguration
        || (!cdk.Tokenization.isResolvable(node.versioningConfiguration)
            && node.versioningConfiguration.status !== 'Enabled')) {
        cdk.Annotations.of(node).addError('Bucket versioning is not enabled');
      }
    }
  }
}

// Later, apply to the stack
cdk.Aspects.of(stack).add(new BucketVersioningChecker());

Taking example from Applying Aspects with Priority also works fine:

import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';

const app = new cdk.App();

const stack = new CdktestStackNew(app, 'CdktestStackNew', {
  env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
});

class CdktestStackNew extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new s3.Bucket(this, 'TestBucket')
  }
}

class MyAspect implements cdk.IAspect {
  visit(node: IConstruct) {
    // Modifies a resource in some way
    if (node instanceof s3.CfnBucket) {
      node.versioningConfiguration = {
        status: 'Enabled'
      };
    }
  }
}

class ValidationAspect implements cdk.IAspect {
  visit(node: IConstruct) {
    // Perform some readonly validation on the construct tree
    if (node instanceof s3.CfnBucket) {

      // Check for versioning property, exclude the case where the property
      // can be a token (IResolvable).
      if (!node.versioningConfiguration
        || (!cdk.Tokenization.isResolvable(node.versioningConfiguration)
            && node.versioningConfiguration.status !== 'Enabled')) {
        cdk.Annotations.of(node).addError('Bucket versioning is not enabled');
      }
    }
  }
}

// Later, apply to the stack
cdk.Aspects.of(stack).add(new MyAspect(), { priority: cdk.AspectPriority.MUTATING } );  // Run first (mutating aspects)
cdk.Aspects.of(stack).add(new ValidationAspect(), { priority: cdk.AspectPriority.READONLY } );  // Run later (readonly aspects)

@github2023spring Good afternoon. Could you please share minimal reproducible CDK code to reproduce the issue?

@ashishdhingra ashishdhingra added p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Dec 11, 2024
@ashishdhingra ashishdhingra self-assigned this Dec 11, 2024
@ashishdhingra ashishdhingra added p1 needs-review and removed p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Dec 13, 2024
@rosmcmahon
Copy link

i haven't been able to reproduce on a small scale, but i've got the same issue...

@rosmcmahon
Copy link

could you give any more information on what i might be looking for? afaik i don't use these "Aspects" in my code, it's all under-the-hood for me.

@github2023spring
Copy link
Author

Same, I am not sure how to reproduce it without sharing the actual production code.

@rosmcmahon
Copy link

i do some kinda weird stuff with stacks here: https://github.com/shepherd-media-classifier/shepherd/blob/master/app.ts

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 16, 2024

@rosmcmahon thanks your GitHub repo is very helpful. Is the main branch in a state that reproduces the error?

@rosmcmahon
Copy link

@rosmcmahon thanks your GitHub repo is very helpful. Is the main branch in a state that reproduces the error?

master branch has been fixed at 2.171.1, if you upgrade it will give the error

@ashishdhingra ashishdhingra removed their assignment Dec 16, 2024
@sumupitchayan
Copy link
Contributor

i do some kinda weird stuff with stacks here: https://github.com/shepherd-media-classifier/shepherd/blob/master/app.ts

@rosmcmahon what command do you run on your repo to reproduce this error?

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 17, 2024

master branch has been fixed at 2.171.1, if you upgrade it will give the error

I can't even get it to synth ☹️

$ npx cdk -a 'npx tsx app.ts config.example.ts' synth
reading params from infra stack...
INFO: using prod authkey for tailscale
/Users/huijbers/Temp/shepherd/node_modules/@aws-sdk/client-ssm/dist-cjs/index.js:8176
  const exception = new ParameterNotFound({
                    ^

ParameterNotFound: UnknownError
    at de_ParameterNotFoundRes (/Users/huijbers/Temp/shepherd/node_modules/@aws-sdk/client-ssm/dist-cjs/index.js:8176:21)
    at de_CommandError (/Users/huijbers/Temp/shepherd/node_modules/@aws-sdk/client-ssm/dist-cjs/index.js:7005:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async /Users/huijbers/Temp/shepherd/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20
    at async /Users/huijbers/Temp/shepherd/node_modules/@smithy/core/dist-cjs/index.js:168:18
    at async /Users/huijbers/Temp/shepherd/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38
    at async /Users/huijbers/Temp/shepherd/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:34:22
    at async globalParam (/Users/huijbers/Temp/shepherd/infra/lib/tailscale-ec2-router.ts:5:62)
    at async <anonymous> (/Users/huijbers/Temp/shepherd/infra/lib/tailscale-ec2-router.ts:17:15) {
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 400,
    requestId: '757d9efb-f2e0-4c5c-8f97-69a25b99c665',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  __type: 'ParameterNotFound'
}

Node.js v22.11.0
Subprocess exited with error 1

Looks like this repo needs a just-so setup.

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 17, 2024

I ripped out all the synth-time SSM lookups and upgraded to 2.172.0. Doesn't repro for me:

$ npx cdk -a 'npx tsx app.ts example' synth
reading params from infra stack...
Bundling asset InfraStack/fnSlackInputAgeAlarm/Code/Stage...

...

⚡ Done in 1ms

I wonder if the node version has anything to do with it?

$ node --version
v22.11.0

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 17, 2024

I don't understand how this can happen. The relevant bits of code look like this (from unminified JS to make sure it's not transpilation gone awry):

class Aspects {
    get applied() {
        return [...this._appliedAspects]
    }
}

/////////////////////////////

        let aspects = aspect_1().Aspects.of(construct);
        let localAspects = aspects.applied;
        const allAspectsHere = sortAspectsByPriority(inheritedAspects, localAspects),

///////////////////////////

function sortAspectsByPriority(inheritedAspects, localAspects) {
    return [...inheritedAspects, ...localAspects].sort(...)    // <-- boom here
}

I don't understand how this can in any way lead to localAspects not being iterable. The only reason I can think of is that aspects.applied returns the actual function instead of evaluating the get applied() getter. But that would be silly too...

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 17, 2024

Oooooooooohhhhhhhhhh I think you might have multiple copies of aws-cdk-lib in your dependency tree at different versions. For example from local symlinking multiple workspace directories together (npm link), or a messy npm install history.

In those cases, if you get an Aspects object from an older aws-cdk-lib version, then aspects.applied would return undefined which definitely is not iterable!

@rohang98
Copy link

I'm encountering the same error and all the versions of aws-cdk-lib are at the same version:

npm list aws-cdk-lib
[email protected] /path/to/project
├─┬ [email protected]
│ └── [email protected] deduped
├─┬ [email protected]
│ └── [email protected] deduped
└── [email protected]

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 20, 2024

Any locally symlinked packages on your machine? npm link or yarn link or otherwise?

@rohang98
Copy link

No, everything seems fine on that front.

@mergify mergify bot closed this as completed in #32647 Dec 23, 2024
mergify bot pushed a commit that referenced this issue Dec 23, 2024
Closes #32470 

### Reason for this change

Some customers have reported seeing the error `TypeError: localAspects is not iterable` upon upgrading to CDK v2.172.0 (this is when the Priority-ordered aspects feature was released). This is likely caused by customers having dependencies on third-party constructs/libraries which are using outdated versions (< 2.172.0) of CDK. The problem more specifically is that the `Aspects.applied` function was added in v2.172.0, and the new `invokeAspects` function calls this function on all nodes in the tree.

### Description of changes

Created a workaround for customers. Added the `getAspectApplications` function in `synthesis.ts` - this function creates `AspectApplication` objects from `Aspects.all` if `Aspects.applied` does not exist.

### Describe any new or updated permissions being added

None.

### Description of how you validated changes

New unit test in `aspect.test.ts` with a monkey patched `Aspects.applied` function.

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 23, 2024
@kaizencc kaizencc added p0 management/tracking Issues that track a subject or multiple issues and removed p1 needs-review labels Dec 26, 2024
kaizencc pushed a commit that referenced this issue Dec 26, 2024
Closes #32470 

### Reason for this change

Some customers have reported seeing the error `TypeError: localAspects is not iterable` upon upgrading to CDK v2.172.0 (this is when the Priority-ordered aspects feature was released). This is likely caused by customers having dependencies on third-party constructs/libraries which are using outdated versions (< 2.172.0) of CDK. The problem more specifically is that the `Aspects.applied` function was added in v2.172.0, and the new `invokeAspects` function calls this function on all nodes in the tree.

### Description of changes

Created a workaround for customers. Added the `getAspectApplications` function in `synthesis.ts` - this function creates `AspectApplication` objects from `Aspects.all` if `Aspects.applied` does not exist.

### Describe any new or updated permissions being added

None.

### Description of how you validated changes

New unit test in `aspect.test.ts` with a monkey patched `Aspects.applied` function.

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@kaizencc kaizencc changed the title (aws-cdk): localAspects is not iterable (cli): TypeError: localAspects is not iterable Dec 26, 2024
@kaizencc kaizencc pinned this issue Dec 26, 2024
@moelasmar moelasmar unpinned this issue Jan 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. management/tracking Issues that track a subject or multiple issues p0 package/tools Related to AWS CDK Tools or CLI potential-regression Marking this issue as a potential regression to be checked by team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants