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

Make formatting work regardless of initialization state #178

Merged
merged 1 commit into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
radeksimko marked this conversation as resolved.
Show resolved Hide resolved
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