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

Improve root module discovery error handling #244

Merged
merged 1 commit into from
Jul 22, 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
9 changes: 1 addition & 8 deletions internal/terraform/rootmodule/root_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,15 +447,8 @@ func (rm *rootModule) UpdateSchemaCache(ctx context.Context, lockFile File) erro

rm.pluginLockFile = lockFile

err := rm.schemaStorage.ObtainSchemasForModule(ctx,
return rm.schemaStorage.ObtainSchemasForModule(ctx,
rm.tfExec, rootModuleDirFromFilePath(lockFile.Path()))
if err != nil {
// We fail silently here to still allow tracking the module
// The schema can be loaded later via watcher
rm.logger.Printf("failed to update plugin cache for %s: %s", rm.Path(), err.Error())
}

return nil
}

func (rm *rootModule) PathsToWatch() []string {
Expand Down
20 changes: 11 additions & 9 deletions langserver/handlers/did_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,31 @@ func (lh *logHandler) TextDocumentDidOpen(ctx context.Context, params lsp.DidOpe
}

rootDir, _ := lsctx.RootDirectory(ctx)
dir := relativePath(rootDir, f.Dir())

candidates := cf.RootModuleCandidatesByPath(f.Dir())

if walker.IsWalking() {
// avoid raising false warnings if walker hasn't finished yet
lh.logger.Printf("walker has not finished walking yet, data may be inaccurate for %s", f.FullPath())
} else if len(candidates) == 0 {
// TODO: Only notify once per f.Dir() per session
msg := fmt.Sprintf("No root module found for %s."+
" Functionality may be limited."+
// Unfortunately we can't be any more specific wrt where
// because we don't gather "init-able folders" in any way
" You may need to run terraform init", f.Filename())
" You may need to run terraform init"+
" and reload your editor.", dir)
return jrpc2.ServerPush(ctx, "window/showMessage", lsp.ShowMessageParams{
Type: lsp.MTWarning,
Message: msg,
})
}
if len(candidates) > 1 {
// TODO: Suggest specifying explicit root modules?

msg := fmt.Sprintf("Alternative root modules found for %s (%s), picked: %s",
f.Filename(), candidatePaths(rootDir, candidates[1:]),
renderCandidatePath(rootDir, candidates[0]))
msg := fmt.Sprintf("Alternative root modules found for %s (%s), picked: %s."+
" You can try setting paths to root modules explicitly in settings.",
dir, candidatePaths(rootDir, candidates[1:]),
relativePath(rootDir, candidates[0].Path()))
return jrpc2.ServerPush(ctx, "window/showMessage", lsp.ShowMessageParams{
Type: lsp.MTWarning,
Message: msg,
Expand All @@ -72,14 +74,14 @@ func candidatePaths(rootDir string, candidates []rootmodule.RootModule) string {
paths := make([]string, len(candidates))
for i, rm := range candidates {
// This helps displaying shorter, but still relevant paths
paths[i] = renderCandidatePath(rootDir, rm)
paths[i] = relativePath(rootDir, rm.Path())
}
return strings.Join(paths, ", ")
}

func renderCandidatePath(rootDir string, candidate rootmodule.RootModule) string {
func relativePath(rootDir string, path string) string {
trimmed := strings.TrimPrefix(
strings.TrimPrefix(candidate.Path(), rootDir), string(os.PathSeparator))
strings.TrimPrefix(path, rootDir), string(os.PathSeparator))
if trimmed == "" {
return "."
}
Expand Down