diff --git a/command/meta.go b/command/meta.go index 53c59831bc94..7658520e6287 100644 --- a/command/meta.go +++ b/command/meta.go @@ -266,6 +266,10 @@ func (m *Meta) StdinPiped() bool { // operation itself is unsuccessful. Use the "Result" field of the // returned operation object to recognize operation-level failure. func (m *Meta) RunOperation(b backend.Enhanced, opReq *backend.Operation) (*backend.RunningOperation, error) { + if opReq.ConfigDir != "" { + opReq.ConfigDir = m.normalizePath(opReq.ConfigDir) + } + op, err := b.Operation(context.Background(), opReq) if err != nil { return nil, fmt.Errorf("error starting operation: %s", err) diff --git a/terraform/evaluate.go b/terraform/evaluate.go index 8e503c3d13aa..38fe8767346d 100644 --- a/terraform/evaluate.go +++ b/terraform/evaluate.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "path/filepath" "strconv" "sync" @@ -423,7 +424,7 @@ func (d *evaluationStateData) GetPathAttr(addr addrs.PathAttr, rng tfdiags.Sourc }) return cty.DynamicVal, diags } - return cty.StringVal(wd), diags + return cty.StringVal(filepath.ToSlash(wd)), diags case "module": moduleConfig := d.Evaluator.Config.DescendentForInstance(d.ModulePath) @@ -433,11 +434,11 @@ func (d *evaluationStateData) GetPathAttr(addr addrs.PathAttr, rng tfdiags.Sourc panic(fmt.Sprintf("module.path read from module %s, which has no configuration", d.ModulePath)) } sourceDir := moduleConfig.Module.SourceDir - return cty.StringVal(sourceDir), diags + return cty.StringVal(filepath.ToSlash(sourceDir)), diags case "root": sourceDir := d.Evaluator.Config.Module.SourceDir - return cty.StringVal(sourceDir), diags + return cty.StringVal(filepath.ToSlash(sourceDir)), diags default: suggestion := nameSuggestion(addr.Name, []string{"cwd", "module", "root"}) diff --git a/terraform/evaluate_test.go b/terraform/evaluate_test.go index d3f27ec4da03..18b2d65f7d03 100644 --- a/terraform/evaluate_test.go +++ b/terraform/evaluate_test.go @@ -7,6 +7,7 @@ import ( "github.com/zclconf/go-cty/cty" "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/tfdiags" ) @@ -34,3 +35,46 @@ func TestEvaluatorGetTerraformAttr(t *testing.T) { } }) } + +func TestEvaluatorGetPathAttr(t *testing.T) { + evaluator := &Evaluator{ + Meta: &ContextMeta{ + Env: "foo", + }, + Config: &configs.Config{ + Module: &configs.Module{ + SourceDir: "bar/baz", + }, + }, + } + data := &evaluationStateData{ + Evaluator: evaluator, + } + scope := evaluator.Scope(data, nil) + + t.Run("module", func(t *testing.T) { + want := cty.StringVal("bar/baz") + got, diags := scope.Data.GetPathAttr(addrs.PathAttr{ + Name: "module", + }, tfdiags.SourceRange{}) + if len(diags) != 0 { + t.Errorf("unexpected diagnostics %s", spew.Sdump(diags)) + } + if !got.RawEquals(want) { + t.Errorf("wrong result %#v; want %#v", got, want) + } + }) + + t.Run("root", func(t *testing.T) { + want := cty.StringVal("bar/baz") + got, diags := scope.Data.GetPathAttr(addrs.PathAttr{ + Name: "root", + }, tfdiags.SourceRange{}) + if len(diags) != 0 { + t.Errorf("unexpected diagnostics %s", spew.Sdump(diags)) + } + if !got.RawEquals(want) { + t.Errorf("wrong result %#v; want %#v", got, want) + } + }) +}