Skip to content

Commit cbd414a

Browse files
committed
fix: workaround to avoid high memory consumption
1 parent 4a13fc7 commit cbd414a

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

pkg/goanalysis/runner.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"go/token"
1616
"runtime"
1717
"sort"
18-
"strings"
1918
"sync"
2019

2120
"golang.org/x/exp/maps"
@@ -59,12 +58,11 @@ type runner struct {
5958
passToPkg map[*analysis.Pass]*packages.Package
6059
passToPkgGuard sync.Mutex
6160
sw *timeutils.Stopwatch
62-
goVersion string // TODO(ldez) temporary workaround
6361
}
6462

65-
func newRunner(prefix string, logger logutils.Log,
66-
pkgCache *pkgcache.Cache, loadGuard *load.Guard, loadMode LoadMode,
67-
sw *timeutils.Stopwatch, goVersion string) *runner {
63+
func newRunner(prefix string, logger logutils.Log, pkgCache *pkgcache.Cache, loadGuard *load.Guard,
64+
loadMode LoadMode, sw *timeutils.Stopwatch,
65+
) *runner {
6866
return &runner{
6967
prefix: prefix,
7068
log: logger,
@@ -73,7 +71,6 @@ func newRunner(prefix string, logger logutils.Log,
7371
loadMode: loadMode,
7472
passToPkg: map[*analysis.Pass]*packages.Package{},
7573
sw: sw,
76-
goVersion: goVersion,
7774
}
7875
}
7976

@@ -250,7 +247,6 @@ func (r *runner) analyze(pkgs []*packages.Package, analyzers []*analysis.Analyze
250247

251248
loadingPackages[pkg] = &loadingPackage{
252249
pkg: pkg,
253-
goVersion: "go" + strings.TrimPrefix(r.goVersion, "go"),
254250
imports: imports,
255251
isInitial: initialPkgs[pkg],
256252
log: r.log,

pkg/goanalysis/runner_loadingpackage.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"go/types"
1010
"os"
1111
"reflect"
12+
"runtime"
13+
"strings"
1214
"sync"
1315
"sync/atomic"
1416

@@ -23,7 +25,6 @@ const unsafePkgName = "unsafe"
2325

2426
type loadingPackage struct {
2527
pkg *packages.Package
26-
goVersion string // TODO(ldez) temporary workaround
2728
imports map[string]*loadingPackage
2829
isInitial bool
2930
log logutils.Log
@@ -151,13 +152,15 @@ func (lp *loadingPackage) loadFromSource(loadMode LoadMode) error {
151152
}
152153
return imp.Types, nil
153154
}
155+
154156
tc := &types.Config{
155157
Importer: importerFunc(importer),
156158
Error: func(err error) {
157159
pkg.Errors = append(pkg.Errors, lp.convertError(err)...)
158160
},
159-
GoVersion: lp.goVersion, // TODO(ldez) temporary workaround
161+
GoVersion: getGoVersion(),
160162
}
163+
161164
_ = types.NewChecker(tc, pkg.Fset, pkg.Types, pkg.TypesInfo).Files(pkg.Syntax)
162165
// Don't handle error here: errors are adding by tc.Error function.
163166

@@ -499,3 +502,14 @@ func sizeOfReflectValueTreeBytes(rv reflect.Value, visitedPtrs map[uintptr]struc
499502
panic("unknown rv of type " + rv.String())
500503
}
501504
}
505+
506+
// TODO(ldez) temporary workaround
507+
func getGoVersion() string {
508+
goVersion := runtime.Version()
509+
if fields := strings.Fields(goVersion); len(fields) > 0 {
510+
// When using GOEXPERIMENT, the version returned might look something like "go1.23.0 X:boringcrypto".
511+
return fields[0]
512+
}
513+
514+
return goVersion
515+
}

pkg/goanalysis/runners.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
3636
const stagesToPrint = 10
3737
defer sw.PrintTopStages(stagesToPrint)
3838

39-
var goVersion string
40-
if lintCtx.Cfg != nil {
41-
goVersion = lintCtx.Cfg.Run.Go
42-
}
43-
44-
runner := newRunner(cfg.getName(), log, lintCtx.PkgCache, lintCtx.LoadGuard, cfg.getLoadMode(), sw, goVersion)
39+
runner := newRunner(cfg.getName(), log, lintCtx.PkgCache, lintCtx.LoadGuard, cfg.getLoadMode(), sw)
4540

4641
pkgs := lintCtx.Packages
4742
if cfg.useOriginalPackages() {

0 commit comments

Comments
 (0)