tofu show: -module=DIR mode, for showing just a single module

We previously added the -config mode for showing the entire assembled
configuration tree, including the content of any descendent modules, but
that mode requires first running "tofu init" to install all of the
provider and module dependencies of the configuration.

This new -module=DIR mode returns a subset of the same JSON representation
for only a single module that can be generated without first installing
any dependencies, making this mode more appropriate for situations like
generating documentation for a single module when importing it into the
OpenTofu Registry. The registry generation process does not want to endure
the overhead of installing other providers and modules when all it actually
needs is metadata about the top-level declarations in the module.

To minimize the risk to the already-working full-config JSON representation
while still reusing most of its code, the implementation details of package
jsonconfig are a little awkward here. Since this code changes relatively
infrequently and is implementing an external interface subject to
compatibility constraints, and since this new behavior is relatively
marginal and intended primarily for our own OpenTofu Registry purposes,
this is a pragmatic tradeoff that is hopefully compensated for well enough
by the code comments that aim to explain what's going on for the benefit
of future maintainers. If we _do_ find ourselves making substantial changes
to this code at a later date then we can consider a more significant
restructure of the code at that point; the weird stuff is intentionally
encapsulated inside package jsonconfig so it can change later without
changing any callers.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
This commit is contained in:
Martin Atkins
2025-07-08 14:08:23 -07:00
parent 868dc2f01b
commit 6a27c82bb4
12 changed files with 519 additions and 71 deletions

View File

@@ -28,7 +28,8 @@ to inspect:
- `-state`: Inspect the latest state snapshot, if any.
- `-plan=FILENAME`: Inspect the plan stored in the given saved plan file.
- `-config`: Inspect the current configuration (requires `-json`).
- `-config`: Inspect the current full configuration (requires `-json`).
- `-module=DIR`: Inspect the configuration of just a single module in the given directory, without requiring any dependencies to be installed (requires `-json`).
The `-state` option is the default if none of these options are used. The
target-selection options are mutually-exclusive.
@@ -43,11 +44,11 @@ This command also accepts the following additional options:
used in module source addresses or backend settings in the
current configuration.
This command relies on schema information from provider plugins to fully
understand the provider-specific data structures in state, plan, and
configuration artifacts. If you are currently using different provider
versions than were used when creating the selected artifact then
you may need to use `tofu apply` (or similar) to allow OpenTofu to
Unless using the `-module=DIR` option, this command relies on schema information
from provider plugins to fully understand the provider-specific data structures
in state, plan, and configuration artifacts. If you are currently using
different provider versions than were used when creating the selected artifact
then you may need to use `tofu apply` (or similar) to allow OpenTofu to
upgrade the stored data to match the latest provider schemas.
## JSON Output
@@ -62,6 +63,14 @@ depends on the selected artifact type:
- `-config` returns [the JSON configuration representation](../../internals/json-format.mdx#configuration-representation),
providing exactly the same configuration-related information that the plan representation would include,
but without requiring a plan to be created first.
- `-module=DIR` returns a subset of [the JSON configuration representation](../../internals/json-format.mdx#configuration-representation), where:
- The `"module"` property of each module call is omitted.
- The `"schema_version"` property of each resource is omitted.
- All expression-related properties are omitted.
These omissions together allow this particular mode to work without first
executing `tofu init`, and thus without first installing the module's
dependencies.
## Legacy Usage