cdktf synth fails when outdir is set in AppOptions instead of the CLI flag
Explains why passing outdir to the CDKTF App constructor raises a synth error and the three supported ways to set the output directory (the -o/--output flag, the CDKTF_OUTDIR environment variable, or the output property in cdktf.json). Use when synth fails after setting outdir in code. Not for general synth crashes or missing stacks.
TL;DR
Do not pass outdir to the App constructor for real runs; it is only for testing. Set the output directory with cdktf synth -o [dir], the CDKTF_OUTDIR env var, or the output property in cdktf.json.
The error
Setting outdir in AppOptions (e.g. new App({ outdir: "my-dir" })) and running cdktf synth raises a synth error instead of writing output to that directory.
Fix it
- Remove
outdirfrom theAppconstructor in your code; keepnew App()plain (or with only testing-related options). Pick one of the supported ways to set the output directory:
- CLI flag:
cdktf synth -o my-dir(or--output my-dir) - Environment variable:
export CDKTF_OUTDIR=my-dir - Config file: set
"output": "my-dir"incdktf.json
- CLI flag:
- Re-run
cdktf synthand confirm output lands in the chosen directory.
Expected result: synth completes and cdk.tf.json appears under the configured output directory.
When to use this
- You set
outdirinApp/AppOptionsand synth errors out - You want the synth output in a non-default directory
When NOT to use this
- Synth fails for other reasons (missing provider, bad import) with outdir untouched
- You are writing unit tests for stacks (the in-code outdir is intended for that)
Root cause
The CDKTF CLI spawns your app as a separate process and there is no channel for the app to tell the CLI which outdir it chose, so the CLI cannot find the generated Terraform config afterward. By design the outdir must come from the CLI side (flag, env var, or cdktf.json). The in-code option exists only so tests can synthesize to a temp directory without a CLI.
Edge cases
cdktf.jsonoutputandCDKTF_OUTDIRcan disagree; the CLI flag wins when several are set.- Relative outdir paths resolve against the project root, not the stack file location.