From e7ddb357e5d987ea4c501b9504ad75384c34d5d1 Mon Sep 17 00:00:00 2001 From: Nikolai Zujev <824109+jaymecd@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:16:34 +0200 Subject: [PATCH] fix(init): do not require re-init after explicit init on clean directory (#2949) * fix(init): avoid re-init warning after explicit init * fix failed test * fix linter issue --- cli/commands/terraform/download_source.go | 4 +++- cli/commands/terraform/download_source_test.go | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cli/commands/terraform/download_source.go b/cli/commands/terraform/download_source.go index eb866f170..174fe66b0 100644 --- a/cli/commands/terraform/download_source.go +++ b/cli/commands/terraform/download_source.go @@ -130,7 +130,9 @@ func DownloadTerraformSourceIfNecessary(ctx context.Context, terraformSource *te currentVersion, err := terraformSource.EncodeSourceVersion() // if source versions are different or calculating version failed, create file to run init // https://github.com/gruntwork-io/terragrunt/issues/1921 - if previousVersion != currentVersion || err != nil { + if (previousVersion != "" && previousVersion != currentVersion) || err != nil { + terragruntOptions.Logger.Debugf("Requesting re-init, source version has changed from %s to %s recently.", previousVersion, currentVersion) + initFile := util.JoinPath(terraformSource.WorkingDir, ModuleInitRequiredFile) f, createErr := os.Create(initFile) diff --git a/cli/commands/terraform/download_source_test.go b/cli/commands/terraform/download_source_test.go index 497f87623..d1f842221 100644 --- a/cli/commands/terraform/download_source_test.go +++ b/cli/commands/terraform/download_source_test.go @@ -168,7 +168,7 @@ func TestDownloadTerraformSourceIfNecessaryLocalDirToEmptyDir(t *testing.T) { downloadDir := tmpDir(t) defer os.Remove(downloadDir) - testDownloadTerraformSourceIfNecessary(t, canonicalURL, downloadDir, false, "# Hello, World", true) + testDownloadTerraformSourceIfNecessary(t, canonicalURL, downloadDir, false, "# Hello, World", false) } func TestDownloadTerraformSourceIfNecessaryLocalDirToAlreadyDownloadedDir(t *testing.T) { @@ -190,7 +190,7 @@ func TestDownloadTerraformSourceIfNecessaryRemoteUrlToEmptyDir(t *testing.T) { downloadDir := tmpDir(t) defer os.Remove(downloadDir) - testDownloadTerraformSourceIfNecessary(t, canonicalURL, downloadDir, false, "# Hello, World", true) + testDownloadTerraformSourceIfNecessary(t, canonicalURL, downloadDir, false, "# Hello, World", false) } func TestDownloadTerraformSourceIfNecessaryRemoteUrlToAlreadyDownloadedDir(t *testing.T) { @@ -238,7 +238,7 @@ func TestDownloadTerraformSourceIfNecessaryRemoteUrlOverrideSource(t *testing.T) copyFolder(t, "../../../test/fixtures/download-source/hello-world-version-remote", downloadDir) - testDownloadTerraformSourceIfNecessary(t, canonicalURL, downloadDir, true, "# Hello, World", true) + testDownloadTerraformSourceIfNecessary(t, canonicalURL, downloadDir, true, "# Hello, World", false) } func TestDownloadTerraformSourceIfNecessaryInvalidTerraformSource(t *testing.T) {