ayltai/terraform-plugin

Problems Using the Plugin if the Source is not Located in src/main/terraform

Closed this issue · 5 comments

Describe the bug
My source code is not located in a src/main/terraform directory. I have set the source variable to the location of where my Terraform files are. I cannot seem to use init or plan (and I am assuming apply). It gives me an error that says "Terraform initialized in an empty directory!" when I run the init.

To Reproduce
Steps to reproduce the behavior:

  1. Put your Terraform files in a directory that does not follow the src/main/terraform convention.
  2. Put your build.gradle file in that directory.
  3. Set the source variable in the terraform extension to use that directory.
  4. Run './gradlew tfInit', assuming that you are using the wrapper.
  5. That should reproduce the error

Expected behavior
I would expect the tfInit, tfPlan, and tfApply to complete successfully.

Desktop (please complete the following information):

  • OS: macOS
  • Version 10.15.5
  • Gradle version: 6.5.1

Looking through the source code of TerraformPlugin.java, I see calls to set the source in most of the Register*Task methods, but I do not see the call in the registerInitTask, registerPlanTask, registerApplyTask, or registerDestroyTask methods.

The tfValidate task does work fine with the source directory set in my terraform extension in my build.gradle file.

I just tested the plugin again with the source folder being $projectRoot/terraform and it worked fine. The source path is set in TerraformPlugin.configureStatefulTask() method.

Sample build.gradle config:

terraform {
    toolVersion = '0.12.29'
    backend = 'app.terraform.io'
    apiToken = 'xxxxxx'
    workspace = 'main'
    source = 'terraform'
}

Could you share a minimal repo that reproduces this issue?

BTW, I've updated the release to 0.2.13, which supports selecting a workspace in tfInit (but this should be independent to this issue). Use it in case you need it.

I can share my gradle build files and a screenshot of the project structure. I'm using a multi-project build to try and run it for each subproject. I am including the root gradle file and the build.gradle file from one of the subprojects (it has 605604 in its name). I almost wonder if it is gradle issue because if I run the tfInit with debug logging, I see the PWD is the root project directory, which does have no terraform files in it.

Screen Shot 2020-09-18 at 9 26 41 AM

[build_files.zip](https://github.com/ayltai/terraform-plugin/files/5245833/build_files.zip)

I figured it out. I had to change the line in the subproject build gradle from:
source = "."
to:
source = "./${project.name}"

I see. If you are using Gradle wrapper (like most of us do) gradlew in the root project directory, the base working directory will be the project root, so in case the Terraform source code is in a sub-project, source should be relative to that path.