Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ 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.
Comment thread
dougthor42 marked this conversation as resolved.
Outdated

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

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
}