Skip to content
Open
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
21 changes: 20 additions & 1 deletion dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)

// Dir attempts to gather info for the requested directory.
Expand Down Expand Up @@ -76,10 +78,27 @@ func fromNonGoDir(dir string) (Info, error) {
return i, fmt.Errorf("here.nonGoDir: %s: %w", dir, err)
}

if err := json.Unmarshal(b, &i.Module); err != nil {
// When workspaces used, go list returns multiple objects separated by new line
// Solution is to treat is as an array
b = append(append([]byte{'['}, b...), ']')

rx := regexp.MustCompile("\\}(\r)?\n\\{")
if rx.Match(b) {
b = rx.ReplaceAll(b, []byte("},{"))
}

var Modules []Module
if err := json.Unmarshal(b, &Modules); err != nil {
return i, fmt.Errorf("here.nonGoDir: %s: %w", dir, err)
}

// Select current module by dir
for _, module := range Modules {
if strings.HasPrefix(dir, module.Dir) {
i.Module = module
}
}

if i.ImportPath == "" && i.Module.Path != "command-line-arguments" {
i.ImportPath = i.Module.Path
}
Expand Down