From 769f626a0eb66404f38e843ac68763deb1e69fe3 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Fri, 30 Aug 2019 19:41:47 -0400 Subject: [PATCH] lang/funcs: Remove homedir.Expand() and refactor path trimming with filepath.Rel() in fileset() function Reference: https://github.com/hashicorp/terraform/pull/22621#pullrequestreview-282259385 --- lang/funcs/filesystem.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lang/funcs/filesystem.go b/lang/funcs/filesystem.go index 48185fa5f6..001accc4f7 100644 --- a/lang/funcs/filesystem.go +++ b/lang/funcs/filesystem.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "os" "path/filepath" - "strings" "unicode/utf8" "github.com/hashicorp/hcl2/hcl" @@ -227,11 +226,6 @@ func MakeFileSetFunc(baseDir string) function.Function { path := args[0].AsString() pattern := args[1].AsString() - path, err := homedir.Expand(path) - if err != nil { - return cty.UnknownVal(cty.Set(cty.String)), fmt.Errorf("failed to expand ~: %s", err) - } - if !filepath.IsAbs(path) { path = filepath.Join(baseDir, path) } @@ -259,7 +253,11 @@ func MakeFileSetFunc(baseDir string) function.Function { } // Remove the path and file separator from matches. - match = strings.TrimPrefix(match, path+string(filepath.Separator)) + match, err = filepath.Rel(path, match) + + if err != nil { + return cty.UnknownVal(cty.Set(cty.String)), fmt.Errorf("failed to trim path of match (%s): %s", match, err) + } // Replace any remaining file separators with forward slash (/) // separators for cross-system compatibility.