Skip to content

Commit 225f9cc

Browse files
committed
perf: lazily load gazelle manifest files
1 parent da0e52f commit 225f9cc

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Unreleased changes template.
7676
* (pypi) The PyPI extension will no longer write the lock file entries as the
7777
extension has been marked reproducible.
7878
Fixes [#2434](https://github.com/bazel-contrib/rules_python/issues/2434).
79+
* (gazelle) Lazily load and parse manifest files when running Gazelle. This ensures no manifest files are loaded when Gazelle is run over a set of non-python directories.
7980

8081
[20250317]: https://github.com/astral-sh/python-build-standalone/releases/tag/20250317
8182

Diff for: gazelle/python/configure.go

+1-23
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"flag"
1919
"fmt"
2020
"log"
21-
"os"
2221
"path/filepath"
2322
"strconv"
2423
"strings"
@@ -27,7 +26,6 @@ import (
2726
"github.com/bazelbuild/bazel-gazelle/rule"
2827
"github.com/bmatcuk/doublestar/v4"
2928

30-
"github.com/bazel-contrib/rules_python/gazelle/manifest"
3129
"github.com/bazel-contrib/rules_python/gazelle/pythonconfig"
3230
)
3331

@@ -228,25 +226,5 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
228226
}
229227

230228
gazelleManifestPath := filepath.Join(c.RepoRoot, rel, gazelleManifestFilename)
231-
gazelleManifest, err := py.loadGazelleManifest(gazelleManifestPath)
232-
if err != nil {
233-
log.Fatal(err)
234-
}
235-
if gazelleManifest != nil {
236-
config.SetGazelleManifest(gazelleManifest)
237-
}
238-
}
239-
240-
func (py *Configurer) loadGazelleManifest(gazelleManifestPath string) (*manifest.Manifest, error) {
241-
if _, err := os.Stat(gazelleManifestPath); err != nil {
242-
if os.IsNotExist(err) {
243-
return nil, nil
244-
}
245-
return nil, fmt.Errorf("failed to load Gazelle manifest at %q: %w", gazelleManifestPath, err)
246-
}
247-
manifestFile := new(manifest.File)
248-
if err := manifestFile.Decode(gazelleManifestPath); err != nil {
249-
return nil, fmt.Errorf("failed to load Gazelle manifest at %q: %w", gazelleManifestPath, err)
250-
}
251-
return manifestFile.Manifest, nil
229+
config.SetGazelleManifestPath(gazelleManifestPath)
252230
}

Diff for: gazelle/pythonconfig/pythonconfig.go

+36-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package pythonconfig
1616

1717
import (
1818
"fmt"
19+
"log"
20+
"os"
1921
"path"
2022
"regexp"
2123
"strings"
@@ -153,10 +155,11 @@ func (c Configs) ParentForPackage(pkg string) *Config {
153155
type Config struct {
154156
parent *Config
155157

156-
extensionEnabled bool
157-
repoRoot string
158-
pythonProjectRoot string
159-
gazelleManifest *manifest.Manifest
158+
extensionEnabled bool
159+
repoRoot string
160+
pythonProjectRoot string
161+
gazelleManifestPath string
162+
gazelleManifest *manifest.Manifest
160163

161164
excludedPatterns *singlylinkedlist.List
162165
ignoreFiles map[string]struct{}
@@ -281,11 +284,26 @@ func (c *Config) SetGazelleManifest(gazelleManifest *manifest.Manifest) {
281284
c.gazelleManifest = gazelleManifest
282285
}
283286

287+
// SetGazelleManifestPath sets the path to the gazelle_python.yaml file
288+
// for the current configuration.
289+
func (c *Config) SetGazelleManifestPath(gazelleManifestPath string) {
290+
c.gazelleManifestPath = gazelleManifestPath
291+
}
292+
284293
// FindThirdPartyDependency scans the gazelle manifests for the current config
285294
// and the parent configs up to the root finding if it can resolve the module
286295
// name.
287296
func (c *Config) FindThirdPartyDependency(modName string) (string, string, bool) {
288297
for currentCfg := c; currentCfg != nil; currentCfg = currentCfg.parent {
298+
// Attempt to load the manifest if needed.
299+
if currentCfg.gazelleManifestPath != "" && currentCfg.gazelleManifest == nil {
300+
currentCfgManifest, err := loadGazelleManifest(currentCfg.gazelleManifestPath)
301+
if err != nil {
302+
log.Fatal(err)
303+
}
304+
currentCfg.SetGazelleManifest(currentCfgManifest)
305+
}
306+
289307
if currentCfg.gazelleManifest != nil {
290308
gazelleManifest := currentCfg.gazelleManifest
291309
if distributionName, ok := gazelleManifest.ModulesMapping[modName]; ok {
@@ -526,3 +544,17 @@ func (c *Config) FormatThirdPartyDependency(repositoryName string, distributionN
526544

527545
return label.New(repositoryName, normConventionalDistributionName, normConventionalDistributionName)
528546
}
547+
548+
func loadGazelleManifest(gazelleManifestPath string) (*manifest.Manifest, error) {
549+
if _, err := os.Stat(gazelleManifestPath); err != nil {
550+
if os.IsNotExist(err) {
551+
return nil, nil
552+
}
553+
return nil, fmt.Errorf("failed to load Gazelle manifest at %q: %w", gazelleManifestPath, err)
554+
}
555+
manifestFile := new(manifest.File)
556+
if err := manifestFile.Decode(gazelleManifestPath); err != nil {
557+
return nil, fmt.Errorf("failed to load Gazelle manifest at %q: %w", gazelleManifestPath, err)
558+
}
559+
return manifestFile.Manifest, nil
560+
}

0 commit comments

Comments
 (0)