Skip to content

Commit

Permalink
Fixing converting absolute paths to relative in logs (#3433)
Browse files Browse the repository at this point in the history
* fix: convert paths abs to rel

* fix: lint

* fix: strict-lint

* fix: copyloopvar
  • Loading branch information
levkohimins authored Sep 24, 2024
1 parent 6f5e448 commit 9c55822
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .strict.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ output:
print-linter-name: true

linters-settings:
lll:
line-length: 140
staticcheck:
checks:
- all
Expand Down
13 changes: 12 additions & 1 deletion cli/commands/terraform/download_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const ModuleInitRequiredFile = ".terragrunt-init-required"

const tfLintConfig = ".tflint.hcl"

const fileURIScheme = "file://"

// 1. Download the given source URL, which should use Terraform's module source syntax, into a temporary folder
// 2. Check if module directory exists in temporary folder
// 3. Copy the contents of terragruntOptions.WorkingDir into the temporary folder.
Expand Down Expand Up @@ -227,7 +229,16 @@ func updateGetters(terragruntOptions *options.TerragruntOptions, terragruntConfi

// Download the code from the Canonical Source URL into the Download Folder using the go-getter library
func downloadSource(terraformSource *terraform.Source, terragruntOptions *options.TerragruntOptions, terragruntConfig *config.TerragruntConfig) error {
terragruntOptions.Logger.Infof("Downloading Terraform configurations from %s into %s", terraformSource.CanonicalSourceURL, terraformSource.DownloadDir)
canonicalSourceURL := terraformSource.CanonicalSourceURL.String()

// Since we convert abs paths to rel in logs, `file://../../path/to/dir` doesn't look good,
// so it's better to get rid of it.
canonicalSourceURL = strings.TrimPrefix(canonicalSourceURL, fileURIScheme)

terragruntOptions.Logger.Infof(
"Downloading Terraform configurations from %s into %s",
canonicalSourceURL,
terraformSource.DownloadDir)

if err := getter.GetAny(terraformSource.DownloadDir, terraformSource.CanonicalSourceURL.String(), updateGetters(terragruntOptions, terragruntConfig)); err != nil {
return errors.WithStackTrace(err)
Expand Down
7 changes: 5 additions & 2 deletions pkg/log/hooks/relative_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package hooks

import (
"fmt"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -51,7 +52,9 @@ func NewRelativePathHook(baseDir string) (*RelativePathHook, error) {

reversIndex--
relPaths[reversIndex] = relPath
absPathsReg[reversIndex] = regexp.MustCompile(regexp.QuoteMeta(absPath) + `([` + regexp.QuoteMeta(pathSeparator) + `"'\s]|$)`)

regStr := fmt.Sprintf(`(^|[^%[1]s\w])%[2]s([%[1]s"'\s]|$)`, regexp.QuoteMeta(pathSeparator), regexp.QuoteMeta(absPath))
absPathsReg[reversIndex] = regexp.MustCompile(regStr)
}

return &RelativePathHook{
Expand Down Expand Up @@ -91,7 +94,7 @@ func (hook *RelativePathHook) Fire(entry *logrus.Entry) error {

func (hook *RelativePathHook) replaceAbsPathsWithRel(text string) string {
for i, absPath := range hook.absPathsReg {
text = absPath.ReplaceAllString(text, hook.relPaths[i]+"$1")
text = absPath.ReplaceAllString(text, "$1"+hook.relPaths[i]+"$2")
}

return text
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dependency "databricks_workspace" {
config_path = "../workspace"
skip_outputs = true
}

terraform {
source = "../../../..//tf"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
source = "../../../..//tf"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "null_resource" "this" {
}

output "dummy" {
value = "dummy"
}
42 changes: 41 additions & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ const (
testFixtureInitOnce = "fixtures/init-once"
testFixtureInputs = "fixtures/inputs"
testFixtureInputsFromDependency = "fixtures/inputs-from-dependency"
testFixtureLogFormatter = "fixtures/log-formatter"
testFixtureLogFormatter = "fixtures/log/formatter"
testFixtureLogRelPaths = "fixtures/log/rel-paths"
testFixtureMissingDependence = "fixtures/missing-dependencies/main"
testFixtureModulePathError = "fixtures/module-path-in-error"
testFixtureNoColor = "fixtures/no-color"
Expand Down Expand Up @@ -149,6 +150,45 @@ func TestLogWithAbsPath(t *testing.T) {
}
}

func TestLogWithRelPath(t *testing.T) {
t.Parallel()

cleanupTerraformFolder(t, testFixtureLogRelPaths)
tmpEnvPath := copyEnvironment(t, testFixtureLogRelPaths)
rootPath := util.JoinPath(tmpEnvPath, testFixtureLogRelPaths)

testCases := []struct {
workingDir string
assertFn func(t *testing.T, stdout, stderr string)
}{
{
workingDir: "duplicate-dir-names/workspace/one/two/aaa", // dir `workspace` duplicated twice in path
assertFn: func(t *testing.T, _, stderr string) {
t.Helper()

assert.Contains(t, stderr, "Module ./bbb/ccc/workspace")
assert.Contains(t, stderr, "Module ./bbb/ccc/module-b")
assert.Contains(t, stderr, "Downloading Terraform configurations from .. into ./bbb/ccc/workspace/.terragrunt-cache")
assert.Contains(t, stderr, "[bbb/ccc/workspace]")
assert.Contains(t, stderr, "[bbb/ccc/module-b]")
},
},
}

for i, testCase := range testCases {
workingDir := filepath.Join(rootPath, testCase.workingDir)

t.Run(fmt.Sprintf("testCase-%d", i), func(t *testing.T) {
t.Parallel()

stdout, stderr, err := runTerragruntCommandWithOutput(t, "terragrunt run-all init --terragrunt-log-level debug --terragrunt-non-interactive --terragrunt-disable-log-formatting=false -no-color --terragrunt-no-color --terragrunt-working-dir "+workingDir)
require.NoError(t, err)

testCase.assertFn(t, stdout, stderr)
})
}
}

func TestLogFormatterPrettyOutput(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 9c55822

Please sign in to comment.