aws-cdk: cli: synth fails if cdk.out does not exist

šŸ› Bug Report

What is the problem?

cdk synth returns ENOENT: no such file or directory, open 'cdk.out/manifest.json' when the cdk.out directory is not present.

However, this results in the creation of a default manifest.json, and subsequent calls to synth succeed

Reproduction Steps

from cdk project directory:

  1. mvn clean
  2. rm -rf cdk.out
  3. mvn compile
  4. cdk synth

Verbose Log

xxxxxxxxx:cdk xxxxxxxx$ ls
cdk.json        cdk.out         pom.xml         src             target
xxxxxxxxx:cdk xxxxxxxx$ mvn clean
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.xxx.xxx:project >--------------------
[INFO] Building project 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project ---
[INFO] Deleting /Users/xxxxxxxx/workspace/xxxxxxxx/cdk/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.303 s
[INFO] Finished at: 2019-08-19T16:27:07-07:00
[INFO] ------------------------------------------------------------------------
xxxxxxxxx:cdk xxxxxxxx$ rm -rf cdk.out
xxxxxxxxx:cdk xxxxxxxx$ mvn compile
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.xxx.xxx:project >--------------------
[INFO] Building project 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/xxxxxxxx/workspace/xxxxxxxx/cdk/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/xxxxxxxx/workspace/xxxxxxxx/cdk/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.835 s
[INFO] Finished at: 2019-08-19T16:27:24-07:00
[INFO] ------------------------------------------------------------------------
xxxxxxxxx:cdk xxxxxxxx$ cdk synth
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.xxx.xxx:project >--------------------
[INFO] Building project 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ project ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.574 s
[INFO] Finished at: 2019-08-19T16:27:31-07:00
[INFO] ------------------------------------------------------------------------
ENOENT: no such file or directory, open 'cdk.out/manifest.json'

Environment

  • CDK CLI Version: 1.4.0 (build 175471f)
  • Module Version:
  • OS: OSX Mojave
  • Language: Java

Other information

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 13
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks to @ivoanjo who also had this problem, I’m able to reproduce this in Java.

I was able to reproduce this using our standard sample-app package from cdk init -l=java and modify the code to drop app.synth(). Running cdk synth on this when there’s no cdk.out/ folder gets this error.

There seems to be some race condition between the cdk.out/ folder being generated and when the CLI expects it to be present for parsing. This specifically happens when the app does not call app.synth().

For those encountering this issue, the workaround is for your app to call app.synth() at the end of the program.

For me the issue was that I had missed the difference between the file in bin and the file in lib, so I was missing:

const app = new cdk.App();
new MyStack(app, 'MyStack');

As soon as I added that back in I stopped getting the error about the missing file. It’d be great if we could improve the error message in this case.

I was getting the same error.

  • OpenJDK 14
  • macOS Catalina 10.15.7
  • cdk 1.102.0 (build a75d52f)
  • node v14.5.0
  • npm 6.14.5

Code:

import software.amazon.awscdk.core.App;
...
public static void main(final String... args) {
    App app = new App();
    new MyStack(...);
}
$ rm -r cdk.out
$ mvn clean compile
...
$ cdk synth
ENOENT: no such file or directory, open 'cdk.out/manifest.json'

However, after adding app.synth(); to the end of my main method:

import software.amazon.awscdk.core.App;

...

public static void main(final String... args) {
    App app = new App();
    new MyStack(...);

    app.synth();     // NEW LINE
}

I then got the expected output when running cdk synth:

$ rm -r cdk.out
$ mvn clean compile
...
$ cdk synth
Resources:
...

So check that you have app.synth() after your resource definitions. I’m not sure why this worked for me, I haven’t done a deeper dive than this (because it now works for me) but wanted to share.

Hope this is helpful in some way.

Here’s the offending block in the aws-cdk pacakge: https://github.com/aws/aws-cdk/blob/5a6fa7fa17a5dce5e429eed4ebfe2dbbac3d6d07/packages/aws-cdk/lib/api/cxapp/exec.ts#L68-L92

The code looks for output on the command line and disregards the AppProps.outdir passed in code. That means in order to resolve this, you have to set both outdir in the AppProps during instantiation of the App, as well as use --outdir set to the same exact value, or completely rely on --output. Any way you go, --output is required. This is very redundant, and makes using dynamic output directories a massive pain. Not remotely ideal.

@rix0rrr @shivlaks @SomayaB There’s the triage. Can we get a fix or some documentation around this?

I don’t think setting AppProps.outdir can even work, as the CLI won’t know where to look for the Cloud Assembly.

Now that you mention it, I’m not sure why we have that property in the first place. I guess it is for unit tests.

@eladb you added this property in https://github.com/aws/aws-cdk/pull/2636… do you happen to remember why it is configurable from inside the CDK app?

I am having the same problem. However, the cause of the problem seems to differ in that in my case, it occurs whenever I specify an outdir other than cdk.out, whether or not I call app.synth().

I created a separate issue for this (#5815), since I think the cause is different. But the relevant details from that issue are copied below.

If I synthesize without a modified outdir (producing a cdk.out/mainfest.json), and then synthesize with a modified outdir, it successfully outputs the expected files to the modified outdir. So it seems that at some point during the process it needs to read from cdk.out/manifest.json, whether or not the outdir property is set to cdk.out, and then it proceeds to behave as expected.

It also appears that a cdk.out folder is created when running cdk synth with a modified outdir.

Additionally, it seems that in at least one project where I am having this issue, running ā€˜cdk deploy’ deploys the templates in cdk.out, not the specified outdir. So it seems like currently the outdir is just not really useful.

Is it possible that ā€˜cdk.out’ is hard-coded somewhere?