Skip to content

Commit 00a0ea6

Browse files
authored
feat: ignore error (#131)
* feat: remove go mod tidy for go parser * feat: add log * feat: change comment
1 parent e3d823d commit 00a0ea6

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

lang/golang/parser/file.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ import (
2929
func (p *GoParser) parseFile(ctx *fileContext, f *ast.File) error {
3030
cont := true
3131
ast.Inspect(f, func(node ast.Node) bool {
32-
defer func() {
33-
if r := recover(); r != nil {
34-
fmt.Fprintf(os.Stderr, "panic: %v in %s:%d\n", r, ctx.filePath, ctx.fset.Position(node.Pos()).Line)
35-
cont = false
36-
return
37-
}
38-
}()
3932
if funcDecl, ok := node.(*ast.FuncDecl); ok {
4033
// parse funcs
4134
_, ct := p.parseFunc(ctx, funcDecl)
@@ -152,7 +145,7 @@ func (p *GoParser) parseVar(ctx *fileContext, vspec *ast.ValueSpec, isConst bool
152145
v.Type = &ti.Id
153146
v.IsPointer = ti.IsPointer
154147
for _, dep := range ti.Deps {
155-
v.Dependencies = InsertDependency(v.Dependencies, NewDependency(dep, ctx.FileLine(vspec.Type)))
148+
v.Dependencies = InsertDependency(v.Dependencies, NewDependency(dep, ctx.FileLine(*val)))
156149
}
157150
} else {
158151
v.Type = typ

lang/golang/parser/parser.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"regexp"
3131
"strings"
3232

33+
"github.com/cloudwego/abcoder/lang/log"
3334
. "github.com/cloudwego/abcoder/lang/uniast"
3435
)
3536

@@ -148,25 +149,21 @@ type dep struct {
148149
}
149150

150151
func getDeps(dir string) (map[string]string, error) {
151-
// run go mod tidy first to ensure all dependencies are resolved
152-
cmd := exec.Command("go", "mod", "tidy")
152+
cmd := exec.Command("go", "list", "-e", "-json", "all")
153153
cmd.Dir = dir
154154
output, err := cmd.CombinedOutput()
155155
if err != nil {
156-
return nil, fmt.Errorf("failed to execute 'go mod tidy', err: %v, output: %s", err, string(output))
156+
return nil, fmt.Errorf("failed to execute 'go list -json all', err: %v, output: %s", err, string(output))
157157
}
158-
159-
if hasNoDeps(filepath.Join(dir, "go.mod")) {
160-
return map[string]string{}, nil
158+
// ignore content until first open
159+
index := strings.Index(string(output), "{")
160+
if index == -1 {
161+
return nil, fmt.Errorf("failed to find '{' in output, output: %s", string(output))
161162
}
162-
163-
cmd = exec.Command("go", "list", "-json", "all")
164-
cmd.Dir = dir
165-
output, err = cmd.Output()
166-
if err != nil {
167-
return nil, fmt.Errorf("failed to execute 'go list -json all', err: %v, output: %s", err, string(output))
163+
if index > 0 {
164+
log.Info("go list skip prefix, output: %s", string(output[:index]))
165+
output = output[index:]
168166
}
169-
170167
deps := make(map[string]string)
171168
decoder := json.NewDecoder(bytes.NewReader(output))
172169
for {
@@ -175,7 +172,7 @@ func getDeps(dir string) (map[string]string, error) {
175172
if err.Error() == "EOF" {
176173
break
177174
}
178-
return nil, fmt.Errorf("failed to decode json: %w, output: %s", err, string(output))
175+
return nil, fmt.Errorf("failed to decode json: %v, output: %s", err, string(output))
179176
}
180177
module := mod.Module
181178
// golang internal package, ignore it.

lang/golang/parser/pkg.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ func (p *GoParser) parseImports(fset *token.FileSet, file []byte, mod *Module, i
3636
ret := &importInfo{}
3737
for _, imp := range impts {
3838
importPath, _ := strconv.Unquote(imp.Path.Value) // remove the quotes
39+
// skip CGO import path
40+
if importPath == "C" {
41+
continue
42+
}
43+
3944
importAlias := ""
4045
// Check if user has defined an alias for current import
4146
if imp.Name != nil {
@@ -54,7 +59,7 @@ func (p *GoParser) parseImports(fset *token.FileSet, file []byte, mod *Module, i
5459
match, path := matchMod(importPath, mod.Dependencies)
5560
if match == "" {
5661
if !strings.HasPrefix(importPath, mod.Name) {
57-
return nil, fmt.Errorf("package %s not found mod", importPath)
62+
fmt.Fprintf(os.Stderr, "package %s not found mod", importPath)
5863
}
5964
projectImports[importAlias] = importPath
6065
} else {
@@ -192,19 +197,23 @@ func (p *GoParser) loadPackages(mod *Module, dir string, pkgPath PkgPath) (err e
192197
if pp, ok := mod.Packages[pkg.ID]; ok && pp != nil {
193198
continue
194199
}
195-
next_file:
196200
for idx, file := range pkg.Syntax {
197201
if idx >= len(pkg.GoFiles) {
198202
fmt.Fprintf(os.Stderr, "skip file %s by loader\n", file.Name)
199203
continue
200204
}
201205
filePath := pkg.GoFiles[idx]
206+
var skip bool
202207
for _, exclude := range p.exclues {
203208
if exclude.MatchString(filePath) {
204209
fmt.Fprintf(os.Stderr, "skip file %s\n", filePath)
205-
continue next_file
210+
skip = true
211+
break
206212
}
207213
}
214+
if skip {
215+
continue
216+
}
208217
bs := p.getFileBytes(filePath)
209218
ctx := &fileContext{
210219
repoDir: p.homePageDir,
@@ -252,6 +261,7 @@ func (p *GoParser) loadPackages(mod *Module, dir string, pkgPath PkgPath) (err e
252261
delete(mod.Packages, obj.PkgPath)
253262
}
254263
}
264+
mod.LoadErrors = append(mod.LoadErrors, pkg.Errors...)
255265
}
256266
return
257267
}

lang/uniast/ast.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"path/filepath"
2323
"strconv"
2424
"strings"
25+
26+
"golang.org/x/tools/go/packages"
2527
)
2628

2729
type Language string
@@ -196,6 +198,7 @@ type Module struct {
196198
Packages map[PkgPath]*Package // pkage import path => Package
197199
Dependencies map[string]string `json:",omitempty"` // module name => module_path@version
198200
Files map[string]*File `json:",omitempty"` // relative path => file info
201+
LoadErrors []packages.Error `json:"load_errors,omitempty"` // packages.Load error
199202
CompressData *string `json:"compress_data,omitempty"` // module compress info
200203
}
201204

0 commit comments

Comments
 (0)