Skip to content

perf: lazily load gazelle manifest files #2746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ Unreleased changes template.
* (pypi) The PyPI extension will no longer write the lock file entries as the
extension has been marked reproducible.
Fixes [#2434](https://github.com/bazel-contrib/rules_python/issues/2434).
* (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
[PR #2746](https://github.com/bazel-contrib/rules_python/pull/2746).
* (rules) {attr}`py_binary.srcs` and {attr}`py_test.srcs` is no longer mandatory when
`main_module` is specified (for `--bootstrap_impl=script`)

Expand Down
24 changes: 1 addition & 23 deletions gazelle/python/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
Expand All @@ -27,7 +26,6 @@ import (
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/bmatcuk/doublestar/v4"

"github.com/bazel-contrib/rules_python/gazelle/manifest"
"github.com/bazel-contrib/rules_python/gazelle/pythonconfig"
)

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

gazelleManifestPath := filepath.Join(c.RepoRoot, rel, gazelleManifestFilename)
gazelleManifest, err := py.loadGazelleManifest(gazelleManifestPath)
if err != nil {
log.Fatal(err)
}
if gazelleManifest != nil {
config.SetGazelleManifest(gazelleManifest)
}
}

func (py *Configurer) loadGazelleManifest(gazelleManifestPath string) (*manifest.Manifest, error) {
if _, err := os.Stat(gazelleManifestPath); err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("failed to load Gazelle manifest at %q: %w", gazelleManifestPath, err)
}
manifestFile := new(manifest.File)
if err := manifestFile.Decode(gazelleManifestPath); err != nil {
return nil, fmt.Errorf("failed to load Gazelle manifest at %q: %w", gazelleManifestPath, err)
}
return manifestFile.Manifest, nil
config.SetGazelleManifestPath(gazelleManifestPath)
}
40 changes: 36 additions & 4 deletions gazelle/pythonconfig/pythonconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package pythonconfig

import (
"fmt"
"log"
"os"
"path"
"regexp"
"strings"
Expand Down Expand Up @@ -153,10 +155,11 @@ func (c Configs) ParentForPackage(pkg string) *Config {
type Config struct {
parent *Config

extensionEnabled bool
repoRoot string
pythonProjectRoot string
gazelleManifest *manifest.Manifest
extensionEnabled bool
repoRoot string
pythonProjectRoot string
gazelleManifestPath string
gazelleManifest *manifest.Manifest

excludedPatterns *singlylinkedlist.List
ignoreFiles map[string]struct{}
Expand Down Expand Up @@ -281,11 +284,26 @@ func (c *Config) SetGazelleManifest(gazelleManifest *manifest.Manifest) {
c.gazelleManifest = gazelleManifest
}

// SetGazelleManifestPath sets the path to the gazelle_python.yaml file
// for the current configuration.
func (c *Config) SetGazelleManifestPath(gazelleManifestPath string) {
c.gazelleManifestPath = gazelleManifestPath
}

// FindThirdPartyDependency scans the gazelle manifests for the current config
// and the parent configs up to the root finding if it can resolve the module
// name.
func (c *Config) FindThirdPartyDependency(modName string) (string, string, bool) {
for currentCfg := c; currentCfg != nil; currentCfg = currentCfg.parent {
// Attempt to load the manifest if needed.
if currentCfg.gazelleManifestPath != "" && currentCfg.gazelleManifest == nil {
currentCfgManifest, err := loadGazelleManifest(currentCfg.gazelleManifestPath)
if err != nil {
log.Fatal(err)
}
currentCfg.SetGazelleManifest(currentCfgManifest)
}

if currentCfg.gazelleManifest != nil {
gazelleManifest := currentCfg.gazelleManifest
if distributionName, ok := gazelleManifest.ModulesMapping[modName]; ok {
Expand Down Expand Up @@ -526,3 +544,17 @@ func (c *Config) FormatThirdPartyDependency(repositoryName string, distributionN

return label.New(repositoryName, normConventionalDistributionName, normConventionalDistributionName)
}

func loadGazelleManifest(gazelleManifestPath string) (*manifest.Manifest, error) {
if _, err := os.Stat(gazelleManifestPath); err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("failed to load Gazelle manifest at %q: %w", gazelleManifestPath, err)
}
manifestFile := new(manifest.File)
if err := manifestFile.Decode(gazelleManifestPath); err != nil {
return nil, fmt.Errorf("failed to load Gazelle manifest at %q: %w", gazelleManifestPath, err)
}
return manifestFile.Manifest, nil
}
Loading