Skip to content

Commit 0317d31

Browse files
committed
fix(go): read modfile correct; use modfile.ModulePath to get module path
1 parent 3ab1456 commit 0317d31

1 file changed

Lines changed: 11 additions & 29 deletions

File tree

lang/golang/parser/utils.go

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515
package parser
1616

1717
import (
18-
"bufio"
19-
"bytes"
2018
"container/list"
2119
"fmt"
2220
"go/ast"
2321
"go/build"
2422
"go/types"
25-
"io"
2623
"os"
2724
"os/exec"
2825
"path"
@@ -154,41 +151,26 @@ func getPackageAlias(importPath string) string {
154151
}
155152

156153
func hasNoDeps(modFilePath string) bool {
157-
mod, err := modfile.Parse(modFilePath, nil, nil)
154+
content, err := os.ReadFile(modFilePath)
158155
if err != nil {
159156
return false
160157
}
161158

162-
return len(mod.Require) == 0
163-
}
164-
165-
func getModuleName(modFilePath string) (string, []byte, error) {
166-
file, err := os.Open(modFilePath)
167-
if err != nil {
168-
return "", nil, fmt.Errorf("failed to open file: %v", err)
169-
}
170-
defer file.Close()
171-
data, err := io.ReadAll(file)
159+
modf, err := modfile.Parse(modFilePath, content, nil)
172160
if err != nil {
173-
return "", nil, fmt.Errorf("failed to read file: %v", err)
174-
}
175-
scanner := bufio.NewScanner(bytes.NewBuffer(data))
176-
for scanner.Scan() {
177-
line := scanner.Text()
178-
if strings.HasPrefix(line, "module") {
179-
// Assuming 'module' keyword is followed by module name
180-
parts := strings.Split(line, " ")
181-
if len(parts) > 1 {
182-
return parts[1], data, nil
183-
}
184-
}
161+
return false
185162
}
186163

187-
if err := scanner.Err(); err != nil {
188-
return "", data, fmt.Errorf("failed to scan file: %v", err)
164+
return len(modf.Require) == 0
165+
}
166+
167+
func getModuleName(modFilePath string) (string, error) {
168+
content, err := os.ReadFile(modFilePath)
169+
if err != nil {
170+
return "", fmt.Errorf("failed to read file: %s", err.Error())
189171
}
190172

191-
return "", data, nil
173+
return modfile.ModulePath(content), nil
192174
}
193175

194176
func isGoBuiltins(name string) bool {

0 commit comments

Comments
 (0)