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

Add TF_HOME_DIR env variable support as alternative to ~/.terraform.d #31952

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions internal/command/cliconfig/config_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import (
)

func configFile() (string, error) {
dir, err := homeDir()
tfHomeDir, err := tfHomeDir()
if err != nil {
return filepath.Join(tfHomeDir, "terraformrc"), nil
}

dir, err := userHomeDir()
if err != nil {
return "", err
}
Expand All @@ -20,18 +25,30 @@ func configFile() (string, error) {
}

func configDir() (string, error) {
dir, err := homeDir()
tfHomeDir, err := tfHomeDir()
if err != nil {
return tfHomeDir, nil
}

dir, err := userHomeDir()
if err != nil {
return "", err
}

return filepath.Join(dir, ".terraform.d"), nil
}

func homeDir() (string, error) {
func tfHomeDir() (string, error) {
if tfHome := os.Getenv("TF_HOME_DIR"); tfHome != "" {
return tfHome, nil
}
return "", errors.New("TF_HOME_DIR is not set")
}

func userHomeDir() (string, error) {
// First prefer the HOME environmental variable
if home := os.Getenv("HOME"); home != "" {
// FIXME: homeDir gets called from globalPluginDirs during init, before
// FIXME: userHomeDir gets called from globalPluginDirs during init, before
// the logging is set up. We should move meta initializtion outside of
// init, but in the meantime we just need to silence this output.
//log.Printf("[DEBUG] Detected home directory from env var: %s", home)
Expand Down
6 changes: 3 additions & 3 deletions internal/command/cliconfig/config_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
const CSIDL_APPDATA = 26

func configFile() (string, error) {
dir, err := homeDir()
dir, err := userHomeDir()
if err != nil {
return "", err
}
Expand All @@ -26,15 +26,15 @@ func configFile() (string, error) {
}

func configDir() (string, error) {
dir, err := homeDir()
dir, err := userHomeDir()
if err != nil {
return "", err
}

return filepath.Join(dir, "terraform.d"), nil
}

func homeDir() (string, error) {
func userHomeDir() (string, error) {
b := make([]uint16, syscall.MAX_PATH)

// See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx
Expand Down
2 changes: 1 addition & 1 deletion plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// older versions where both satisfy the provider version constraints.
func globalPluginDirs() []string {
var ret []string
// Look in ~/.terraform.d/plugins/ , or its equivalent on non-UNIX
// Look in "${TF_HOME_DIR:-~/.terraform.d}/plugins/" , or its equivalent on non-UNIX
dir, err := cliconfig.ConfigDir()
if err != nil {
log.Printf("[ERROR] Error finding global config directory: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion provider_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func implicitProviderSource(services *disco.Disco) getproviders.Source {
// way to include them in bundles uploaded to Terraform Cloud, where
// there has historically otherwise been no way to use custom providers.
// - The "plugins" subdirectory of the CLI config search directory.
// (thats ~/.terraform.d/plugins on Unix systems, equivalents elsewhere)
// (thats "${TF_HOME_DIR:-~/.terraform.d}/plugins" on Unix systems, equivalents elsewhere)
// - The "plugins" subdirectory of any platform-specific search paths,
// following e.g. the XDG base directory specification on Unix systems,
// Apple's guidelines on OS X, and "known folders" on Windows.
Expand Down
10 changes: 5 additions & 5 deletions website/docs/cli/config/config-file.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ general syntax; see the following section for information on the meaning
of each of these settings:

```hcl
plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"
plugin_cache_dir = "${TF_HOME_DIR:-$HOME/.terraform.d}/plugin-cache"
disable_checkpoint = true
```

Expand Down Expand Up @@ -298,10 +298,10 @@ The set of directories Terraform can select as filesystem mirrors depends on
the operating system where you are running Terraform:

* **Windows:** `%APPDATA%/terraform.d/plugins` and `%APPDATA%/HashiCorp/Terraform/plugins`
* **Mac OS X:** `$HOME/.terraform.d/plugins`,
* **Mac OS X:** `${TF_HOME_DIR:-$HOME/.terraform.d}/plugins`,
`~/Library/Application Support/io.terraform/plugins`, and
`/Library/Application Support/io.terraform/plugins`
* **Linux and other Unix-like systems**:`$HOME/.terraform.d/plugins` and
* **Linux and other Unix-like systems**:`${TF_HOME_DIR:-$HOME/.terraform.d}/plugins` and
`terraform/plugins` located within a valid
[XDG Base Directory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
data directory such as `$XDG_DATA_HOME/terraform/plugins`.
Expand Down Expand Up @@ -343,7 +343,7 @@ To enable the plugin cache, use the `plugin_cache_dir` setting in
the CLI configuration file. For example:

```hcl
plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"
plugin_cache_dir = "${TF_HOME_DIR:-$HOME/.terraform.d}/plugin-cache"
```

This directory must already exist before Terraform will cache plugins;
Expand All @@ -359,7 +359,7 @@ variable can be used to enable caching or to override an existing cache
directory within a particular shell session:

```bash
export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"
export TF_PLUGIN_CACHE_DIR="${TF_HOME_DIR:-$HOME/.terraform.d}/plugin-cache"
```

When a plugin cache directory is enabled, the `terraform init` command will
Expand Down
2 changes: 1 addition & 1 deletion website/docs/language/resources/provisioners/syntax.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ provisioners must connect to the remote system using SSH or WinRM.
You must include [a `connection` block](/terraform/language/resources/provisioners/connection) so that Terraform knows how to communicate with the server.

Terraform includes several built-in provisioners. You can also use third-party provisioners as plugins, by placing them
in `%APPDATA%\terraform.d\plugins`, `~/.terraform.d/plugins`, or the same
in `%APPDATA%\terraform.d\plugins`, `${TF_HOME_DIR:-~/.terraform.d}/plugins`, or the same
directory where the Terraform binary is installed. However, we do not recommend
using any provisioners except the built-in `file`, `local-exec`, and
`remote-exec` provisioners.
Expand Down