Skip to content

add option to specify Terraform working directory - #483

Merged
TriPSs merged 4 commits into
TriPSs:masterfrom
johnmckenna:master
Jul 8, 2026
Merged

add option to specify Terraform working directory#483
TriPSs merged 4 commits into
TriPSs:masterfrom
johnmckenna:master

Conversation

@johnmckenna

@johnmckenna johnmckenna commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

fixes #482

Summary by CodeRabbit

  • New Features

    • Added an optional root setting to run Terraform tasks from a custom working directory.
    • Supported this root override consistently across plan, apply, destroy, init, validate, test, fmt, workspace, and providers tasks.
  • Documentation

    • Updated the Terraform “Available Options” documentation to include root, including its intended behavior and supported command coverage.

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1197dc9b-f25c-4af1-bbaa-13c7db0bb192

📥 Commits

Reviewing files that changed from the base of the PR and between ff5a2fd and 54576b1.

📒 Files selected for processing (2)
  • packages/terraform/README.md
  • packages/terraform/src/utils/create-executor.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/terraform/README.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/terraform/src/utils/create-executor.ts

📝 Walkthrough

Walkthrough

This PR adds a configurable root option to Terraform executor schemas, updates executor working-directory resolution to prefer options.root, then project terraformRoot, then sourceRoot, and refreshes the README options table.

Changes

Terraform root override

Layer / File(s) Summary
Executor working directory resolution
packages/terraform/src/utils/create-executor.ts
ExecutorOptions gains optional root; the executor computes targetDirectory from options.root ?? terraformRoot ?? sourceRoot and uses it as execSync cwd.
Schema updates for root property
packages/terraform/src/executors/{apply,destroy,fmt,init,plan,providers,test,validate,workspace}/schema.json
Each executor schema adds a root string property describing the working-directory override.
Documentation of root option
packages/terraform/README.md
The options table is reformatted and a new root row is added.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • TriPSs/nx-extend#429: Also changes packages/terraform/src/utils/create-executor.ts and related executor behavior around command execution.

Suggested reviewers: TriPSs

Poem

A rabbit hopped where Terraform grew,
and found a root to guide it through.
From sourceRoot paths to custom ground,
the right working dir was finally found.
Hop, hop — the config knows what to do. 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding a configurable Terraform working directory option.
Linked Issues check ✅ Passed The PR adds configurable cwd support and preserves sourceRoot fallback, which satisfies issue #482's core requirement.
Out of Scope Changes check ✅ Passed The changes are limited to Terraform executor config, runtime directory handling, and related docs/schema updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

}

const projectConfig = context.projectsConfigurations?.projects?.[projectName]
const projectTerraformRoot = projectConfig && 'terraformRoot' in projectConfig

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const projectTerraformRoot = projectConfig && 'terraformRoot' in projectConfig
const projectTerraformRoot = projectConfig && 'root' in projectConfig

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i see it correctly you now support two different root options?

Also, context.projectName is I think something Nx itself sets, so the check should not be needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi There!

Type 'undefined' cannot be used as an index type. warning occurs if I leave it without the check.

Yes, for project.json you can use "terraformRoot": "pathto/terraform" - I thought it might be a bit better to not have it generic inside project.json

In the targets you can use:

"options": {
  "root": "pathto/terraform"
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll also apply coderabbit's requested changes here

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
packages/terraform/src/utils/create-executor.ts (1)

40-42: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider typing terraformRoot more safely.

The (projectConfig as Record<string, any>).terraformRoot cast works but bypasses type safety. A typed interface or Record<string, unknown> with a narrowing check would be cleaner.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/terraform/src/utils/create-executor.ts` around lines 40 - 42, The
`createExecutor` logic is bypassing type safety by casting `projectConfig` to
`Record<string, any>` to read `terraformRoot`. Update the `projectConfig`
handling in `create-executor.ts` to use a safer typed shape or `Record<string,
unknown>` with a proper narrowing check before accessing `terraformRoot`,
keeping the existing `projectTerraformRoot` assignment path intact.
packages/terraform/README.md (1)

133-133: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the fallback chain in the root option description.

The schema descriptions mention overriding terraformRoot, but the README only says "Path to the Terraform working directory". Adding the fallback priority (rootterraformRootsourceRoot) would help users understand the option's relationship to existing configuration.

📝 Suggested description update
-| **`root`**            | `string`  | -        | Path to the Terraform working directory                                                        | `all`                              |
+| **`root`**            | `string`  | -        | Path to the Terraform working directory. Overrides `terraformRoot` and `sourceRoot`            | `all`                              |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/terraform/README.md` at line 133, The README description for the
`root` option is missing the fallback chain, so update the `root` row in the
Terraform options table to mention the priority order used by the config,
referencing the `root`, `terraformRoot`, and `sourceRoot` option names. Keep the
description concise but explicit so users can see that `root` is preferred
first, then `terraformRoot`, then `sourceRoot` when resolving the Terraform
working directory.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/terraform/README.md`:
- Line 133: The README description for the `root` option is missing the fallback
chain, so update the `root` row in the Terraform options table to mention the
priority order used by the config, referencing the `root`, `terraformRoot`, and
`sourceRoot` option names. Keep the description concise but explicit so users
can see that `root` is preferred first, then `terraformRoot`, then `sourceRoot`
when resolving the Terraform working directory.

In `@packages/terraform/src/utils/create-executor.ts`:
- Around line 40-42: The `createExecutor` logic is bypassing type safety by
casting `projectConfig` to `Record<string, any>` to read `terraformRoot`. Update
the `projectConfig` handling in `create-executor.ts` to use a safer typed shape
or `Record<string, unknown>` with a proper narrowing check before accessing
`terraformRoot`, keeping the existing `projectTerraformRoot` assignment path
intact.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 648b3d5c-82eb-4a8d-bee6-673a764f1c7a

📥 Commits

Reviewing files that changed from the base of the PR and between 28fe5c8 and ff5a2fd.

📒 Files selected for processing (11)
  • packages/terraform/README.md
  • packages/terraform/src/executors/apply/schema.json
  • packages/terraform/src/executors/destroy/schema.json
  • packages/terraform/src/executors/fmt/schema.json
  • packages/terraform/src/executors/init/schema.json
  • packages/terraform/src/executors/plan/schema.json
  • packages/terraform/src/executors/providers/schema.json
  • packages/terraform/src/executors/test/schema.json
  • packages/terraform/src/executors/validate/schema.json
  • packages/terraform/src/executors/workspace/schema.json
  • packages/terraform/src/utils/create-executor.ts

@TriPSs

TriPSs commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Can you also checkout the nitpick comment from coderabbit about the readme?

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.

@nx-extend/terraform support configurable path to run the executor

2 participants