aws-cdk: link-all.sh not working as expected

Describe the issue

I’m following the contribution guide:

  • I cloned the repository
  • ran the npx lerna run build --skip-nx-cache
  • created a fresh CDK TS-based app
  • and ran link-all.sh script

Even though I see the linking script to do symlinks, I still see:

$ npx cdk --version
2.1006.0 (build a3b9762)

instead of 0.0.0. When I tried to do:

$ rm -rf node_modules
$ npx cdk --version
Need to install the following packages:
cdk@2.1017.1
Ok to proceed? (y)

It seems like it’s not seeing the locally built CDK version. I even went through my system, cleaned up stale aws-cdk versions but it didn’t help. Anything obvious I am missing here?

Links

https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md#linking

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 19 (17 by maintainers)

Commits related to this issue

Most upvoted comments

@mrgrain

Hi! I’m running into a similar issue, where after the link-all.sh call, I am no longer able to run npx cdk deploy

The response is like so:

/workplace/hungshou/cdk/cdkApp/node_modules/ts-node/src/index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
../aws-cdk/packages/aws-cdk-lib/aws-iam/lib/principals.ts:228:43 - error TS2729: Property 'wrapped' is used before its initialization.

228   public readonly assumeRoleAction = this.wrapped.assumeRoleAction;
...

I was trying to develop a local feature by building it myself, then linking it to the app, so that my app calls my modified version.

I have called npx lerna run build in the main cdk directory to ensure that everything was built before I tried to run npx cdk deploy. I can confirm that npx cdk deploy was working if I install a fresh cdk via npm install aws-cdk@latest.

Not sure if I’m missing anything here?

Edit: I tried removing the flag --prefer-ts-exts from cdk.json, as suggested above.

Now everything works. Thanks a lot!

✅ I confirm that adding resolveJsonModule: true into my (prod) app works.

Nice.


Fundamentally, this is a choice you are making (but potentially being unaware of it) because of the app command you are using (which again, is probably because that’s the default from cdk init and it makes a lot of sense for end-users). As a reminder, the app command is:

npx ts-node --prefer-ts-exts bin/cdk.ts

So the short answer to all of your questions is: This is how ts-node works.

This is fine, but assuming resolution via adding resolveJsonModule I’m wondering why would I need to modify my code instead of the compiler using the settings from respective modules that are there?

ts-node and therefore TypeScript runs in the context of your app. Unless your app is using Project References, TypeScript doesn’t use any other config files. That’s even if they seem to be more appropriate or are located closer to a source.

Furthermore, why would it need to re-compile the sources if the build was already done via lerna?

--prefer-ts-exts instructs ts-node to prefer a TS file over a js file. This also applies to imported files. From TS perspective, files inside node_modules aren’t special. There’s another part of tsc that handles the “translation” from package name aws-cdk-lib to physical import path. But after that conversion it’s just the same.

And yet even more, why would it try to compile anything in node_modules while it is excluded via "exclude": ["node_modules", "cdk.out"] in tsconfig.json.

Because that’s not how exclude works: https://www.typescriptlang.org/tsconfig/#exclude

Important: exclude only changes which files are included as a result of the [include](https://www.typescriptlang.org/tsconfig/#include) setting. A file specified by exclude can still become part of your codebase due to an import statement in your code, a types inclusion, a /// <reference directive, or being specified in the [files](https://www.typescriptlang.org/tsconfig/#files) list.

@mrgrain no, I don’t want to change the CLI. I want to be able to test a very simple change in the aws-cdk-lib constructs (see this PR) and the logic that you both explain doesn’t seem to work as you describe it.

Right, thanks for the clarification.

TS compile errors

Can you share these errors? They seem to be the core of the issue. Can you also confirm if at this point npx cdk version works and what it returns?

Edit: Apologies, I see you’ve already posted the errors. Bear with me.

$ npm install aws-cdk@latest // node_modules again using upstream. cdk diff works fine again.

You certainly don’t need this step. In fact - and like you’ve discovered - it will undo the linking. It’s not needed because link-all.sh should not change anything with regards to the CLI.