Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileexists should not return an error when subject is a directory (should always return boolean) #25316

Closed
barneyparker opened this issue Jun 19, 2020 · 8 comments

Comments

@barneyparker
Copy link

Terraform Version

Terraform v0.12.26

Terraform Configuration Files

terraform console
> fileexists(".")

>  
Error: Error in function call

  on <console-input> line 1:
  (source code not available)

Call to function "fileexists" failed: . is not a regular file, but
"drwxrwxr-x".

Debug Output

Crash Output

Expected Behavior

terraform console
> fileexists(".")
true

Actual Behavior

fileexists returns an error

Steps to Reproduce

Additional Context

I am trying to determine if the node_modules directory exists to determine if npm install needs to be performed, e.g.

resource "null_resource" "lambda_dependencies" {
  provisioner "local-exec" {
    command = "cd ${path.module}/src && npm install"
  }

  triggers = {
    index = "${base64sha256(file("${path.module}/src/index.js"))}"
    package = "${base64sha256(file("${path.module}/src/package.json"))}"
    lock = "${base64sha256(file("${path.module}/src/package-lock.json"))}"
    node = fileexists("${path.module}/src/node_modules")
  }
}

There doesn't appear to be a useful case where you would want to error on a file being a directory (that I can think of) but in this case there also doesn't appear to be any other native way to discover if a directory resource exists (no direxists function for example)

References

Identical to issue #22260 but with a different use case

@GergelyKalmar
Copy link

I think that the suggestion in #22260 to introduce a new direxists function would be great – currently there seems to be no way in Terraform to check if a folder exists.

@willemm
Copy link

willemm commented Feb 27, 2023

Indeed, even try(fileexists("directory/file.txt"), false) errors out when the directory does not exist.

And also try(file("directory/file.txt"), "") gives an error instead of returning an empty string if that directory does not exist.

@austinvalle
Copy link
Member

Hey everyone 👋🏻 ,

The hashicorp/local provider has just released a new direxists function in v2.5, which should provide what you're looking for here.

You can test this out now with Terraform v1.8.0-beta1 or wait for the upcoming v1.8.0 release.

# Configuration using provider functions must include required_providers configuration.
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "2.5.1"
    }
  }
  # Provider functions require Terraform 1.8 and later.
  required_version = ">= 1.8.0"
}

output "exists" {
  value = provider::local::direxists("${path.module}/example-directory")
}

output "does_not_exist" {
  value = provider::local::direxists("${path.module}/fake-directory")
}
 $ tree          
.
├── example-directory
└── main.tf

2 directories, 1 file

 $ terraform plan

Changes to Outputs:
  + does_not_exist = false
  + exists         = true

It's designed similar to filexists for directories, so if you try to use it on a file it'll return an error.

If you run into any problems testing out the new function, please create an issue over in the local provider issue tracker, thanks!

@crw crw closed this as completed Mar 12, 2024
@GergelyKalmar
Copy link

This approach doesn't work in Terragrunt configuration files, which only have access to Terraform's built-in functions. Is there a particular reason why this functionality cannot be added inside Terraform itself? It is confusing and not consistent at all that checking files is built-in, but checking a folder needs a separate provider.

@crw
Copy link
Collaborator

crw commented Apr 4, 2024

A function such as fileexists will continue to be supported for reasons of 1.x compatibility. The path for new functions to be added to core Terraform will be through providers. New functions that are proven out through a provider function may in rare cases be brought into terraform as a built-in function, but I would not expect that to happen frequently.

@GergelyKalmar
Copy link

Understood, and in general it seems like a good approach. In this particular case I'd argue that moving it to be a built-in function would make sense though, given that this is a very natural pair for the existing fileexists function. Is it worth opening a separate issue for that, or should I not bother?

@crw
Copy link
Collaborator

crw commented Apr 5, 2024

Yes, go ahead an open a new issue. We can use it to both nudge this idea in the future and gather support for the request (move direxists from local to a built-in).

Copy link

github-actions bot commented May 6, 2024

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants