Skip to content

Consider local_directory Data Source #216

@bflad

Description

@bflad

Terraform CLI and Provider Versions

N/A

Use Cases or Problem Statement

The Terraform configuration language has the fileexists() function which returns successfully if a file exists at a given path, however it does not currently handle directories. Even if it was adjusted to support directories (or created as a separate function), as with most configuration language functions, the result is evaluated before the rest of the graph. This can cause issues for practitioners wishing to check/manage directories generated/updated during the graph for purposes of clearer error messaging or other operations which might need a listing of all files within a directory.

While today we generally rely on the function documentation to warn practitioners about this potential issue, it may make sense to provide a graph-aware solution for the use case.

Proposal

Create a new local_directory data source which attempts to read a local filesystem directory. It should error if the directory does not exist otherwise return a set of file paths, for potential usage elsewhere in a Terraform configuration.

As a design sketch of potential usage:

# Ensure example directory exists, potentially due to
# another graph node with side effects creating the directory
# or as a precondition for other graph nodes
data "local_directory" "example" {
  path = "${path.module}/example"
}

# Another way to check directory exists and has files, using new check block functionality to only raise warning
check {
  data "local_directory" "node_modules" {
    path = "${path.module}/node_modules"
  }

  assert {
    condition = length(data.local_directory.node_modules.file_paths) > 0
    error_message = "${data.local_directory.node_modules.path} does not contain any files"
  }
}

Proposed data source configurable attributes:

  • path - String, required, the path to the directory

Proposed data source computed attributes:

  • file_paths - Set of strings, the paths to all files within directory

How much impact is this issue causing?

Medium

Additional Information

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions