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

fix(init): do not require re-init after explicit init on clean directory #2949

Merged
merged 4 commits into from
Oct 7, 2024

Conversation

jaymecd
Copy link
Contributor

@jaymecd jaymecd commented Feb 19, 2024

Description

UPD: added file content and adjusted commands sequence

Running terragrunt plan --terragrunt-no-auto-init on clean directory just after terragrunt init, raises confusing warning:

WARN Detected that init is needed, but Auto-Init is disabled. Continuing with further actions, but subsequent terraform commands may fail.

On 1st clean init previousVersion is always empty, therefore no need to mark it as init required again and this warning is misleading.

How-to reproduce:

Files content:

### file: terragrunt.hcl

terraform {
  source = "."
}

### file: main.tf

resource "null_resource" "test" {
  triggers = {
    number = "test"
  }
}

output "null_resource_id" {
  value = null_resource.test.id
}

Commands sequence:

$ rm -rf .terragrunt-cache/

$ terragrunt init
16:49:08.179 INFO   Downloading Terraform configurations from . into ./.terragrunt-cache/xsGgncPlv531bgEjaMU89xHqM8w/7u8g8e1lJs7NSLAk1_AaGslrY_I
16:49:08.211 STDOUT terraform: Initializing the backend...
16:49:08.211 STDOUT terraform: Initializing provider plugins...
16:49:08.211 STDOUT terraform: - Reusing previous version of hashicorp/null from the dependency lock file
16:49:08.568 STDOUT terraform: - Installing hashicorp/null v3.2.3...
16:49:08.961 STDOUT terraform: - Installed hashicorp/null v3.2.3 (signed by HashiCorp)
16:49:08.961 STDOUT terraform: Terraform has been successfully initialized!


$ find . -type f -name .terragrunt-init-required
./.terragrunt-cache/xsGgncPlv531bgEjaMU89xHqM8w/7u8g8e1lJs7NSLAk1_AaGslrY_I/.terragrunt-init-required


$ terragrunt plan --terragrunt-no-auto-init
16:49:49.988 WARN   Detected that init is needed, but Auto-Init is disabled. Continuing with further actions, but subsequent terraform commands may fail.
16:49:50.501 STDOUT terraform: Terraform used the selected providers to generate the following execution
16:49:50.501 STDOUT terraform: plan. Resource actions are indicated with the following symbols:
16:49:50.501 STDOUT terraform:   + create
16:49:50.501 STDOUT terraform: Terraform will perform the following actions:
16:49:50.501 STDOUT terraform:   # null_resource.test will be created
16:49:50.501 STDOUT terraform:   + resource "null_resource" "test" {
...

Workaround:
drop .terragrunt-init-required file after init using after_hook:

terraform {
  after_hook "cleanup_init_required_file" {
    commands = ["init"]
    execute  = ["sh", "-ec", "find . -type f -name .terragrunt-init-required -delete"]
  }
}

TODOs

Read the Gruntwork contribution guidelines.

  • Run the relevant tests successfully, including pre-commit checks.
  • Include release notes. If this PR is backward incompatible, include a migration guide.

Release Notes (draft)

Added / Removed / Updated [X].

Removed warning when using no-auto-init flag after explicit init on clean directory.

@denis256
Copy link
Member

Noticed failing test:

    download_source_test.go:378: 
        	Error Trace:	/home/circleci/project/cli/commands/terraform/download_source_test.go:378
        	            				/home/circleci/project/cli/commands/terraform/download_source_test.go:238
        	Error:      	Should be true
        	Test:       	TestDownloadTerraformSourceIfNecessaryRemoteUrlOverrideSource
--- FAIL: TestDownloadTerraformSourceIfNecessaryRemoteUrlOverrideSource (2.72s)

@yhakbar
Copy link
Collaborator

yhakbar commented Aug 6, 2024

Hey @jaymecd

We're still seeing errors like the following in tests:

=== NAME  TestTerragruntPrintAwsErrors
    integration_test.go:6156: 
                Error Trace:    /home/circleci/project/test/integration_test.go:6156
                Error:          An error is expected but got nil.
                Test:           TestTerragruntPrintAwsErrors
--- FAIL: TestTerragruntPrintAwsErrors (5.99s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x1a6f170]

Are you able to run the tests locally?

@jaymecd
Copy link
Contributor Author

jaymecd commented Aug 8, 2024

will take a look shortly

@yhakbar
Copy link
Collaborator

yhakbar commented Aug 27, 2024

Hey @jaymecd ,

Moving this PR to WIP. Please mark ready for review when it is.

@yhakbar yhakbar marked this pull request as draft August 27, 2024 13:08
@yhakbar yhakbar changed the title fix(init): do not require re-init after explicit init on clean directory WIP - fix(init): do not require re-init after explicit init on clean directory Aug 27, 2024
@jaymecd
Copy link
Contributor Author

jaymecd commented Sep 25, 2024

@yhakbar fixed.

also I've updated description with more detailed context how to reproduce it.

@jaymecd jaymecd changed the title WIP - fix(init): do not require re-init after explicit init on clean directory fix(init): do not require re-init after explicit init on clean directory Sep 25, 2024
@jaymecd jaymecd marked this pull request as ready for review September 25, 2024 15:45
@yhakbar
Copy link
Collaborator

yhakbar commented Sep 25, 2024

@jaymecd

Please address the linting errors in this PR:

$ make install-lint
$ make run-lint
...
cli/commands/terraform/download_source.go:135:3: assignments should only be cuddled with other assignments (wsl)
                initFile := util.JoinPath(terraformSource.WorkingDir, ModuleInitRequiredFile)
                ^
cli/commands/terraform/download_source.go:137:3: only one cuddle assignment allowed before if statement (wsl)
                if createErr != nil {
                ^

@jaymecd
Copy link
Contributor Author

jaymecd commented Sep 26, 2024

@yhakbar fixed

@yhakbar yhakbar merged commit e7ddb35 into gruntwork-io:main Oct 7, 2024
4 of 5 checks passed
@jaymecd jaymecd deleted the reinit_on_version_change branch October 7, 2024 17:10
@yhakbar
Copy link
Collaborator

yhakbar commented Oct 7, 2024

Fix pre-released in v0.68.0-beta2024100701. Will be included in the next minor release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants