Skip to content

Commit

Permalink
rootmodule: find executor for unmatched root dir (hashicorp#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Jun 24, 2020
1 parent e4e3480 commit a3071bc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
8 changes: 8 additions & 0 deletions internal/terraform/rootmodule/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ func (e *RootModuleNotFoundErr) Error() string {
}
return "root module not found"
}

func IsRootModuleNotFound(err error) bool {
if err == nil {
return false
}
_, ok := err.(*RootModuleNotFoundErr)
return ok
}
33 changes: 30 additions & 3 deletions internal/terraform/rootmodule/root_module_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,42 @@ func (rmm *rootModuleManager) ParserForDir(path string) (lang.Parser, error) {
return rm.Parser(), nil
}

func (rmm *rootModuleManager) TerraformExecutorForDir(path string) (*exec.Executor, error) {
func (rmm *rootModuleManager) TerraformExecutorForDir(ctx context.Context, path string) (*exec.Executor, error) {
rm, err := rmm.RootModuleByPath(path)
if err != nil {
return nil, err
if err != nil && IsRootModuleNotFound(err) {
return rmm.terraformExecutorForDir(ctx, path)
}

return rm.TerraformExecutor(), nil
}

func (rmm *rootModuleManager) terraformExecutorForDir(ctx context.Context, dir string) (*exec.Executor, error) {
tfPath := rmm.tfExecPath
if tfPath == "" {
var err error
d := &discovery.Discovery{}
tfPath, err = d.LookPath()
if err != nil {
return nil, err
}
}

tf := exec.NewExecutor(ctx, tfPath)

tf.SetWorkdir(dir)
tf.SetLogger(rmm.logger)

if rmm.tfExecLogPath != "" {
tf.SetExecLogPath(rmm.tfExecLogPath)
}

if rmm.tfExecTimeout != 0 {
tf.SetTimeout(rmm.tfExecTimeout)
}

return tf, nil
}

// rootModuleDirFromPath strips known lock file paths and filenames
// to get the directory path of the relevant rootModule
func rootModuleDirFromFilePath(filePath string) string {
Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/rootmodule/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ParserFinder interface {
}

type TerraformExecFinder interface {
TerraformExecutorForDir(path string) (*exec.Executor, error)
TerraformExecutorForDir(ctx context.Context, path string) (*exec.Executor, error)
}

type RootModuleCandidateFinder interface {
Expand Down
2 changes: 1 addition & 1 deletion langserver/handlers/formatting.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (h *logHandler) TextDocumentFormatting(ctx context.Context, params lsp.Docu
return edits, err
}

tf, err := tff.TerraformExecutorForDir(fh.Dir())
tf, err := tff.TerraformExecutorForDir(ctx, fh.Dir())
if err != nil {
// TODO: detect no root module found error
// -> find OS-wide executor instead
Expand Down

0 comments on commit a3071bc

Please sign in to comment.