@@ -16,6 +16,8 @@ package pythonconfig
1616
1717import (
1818 "fmt"
19+ "log"
20+ "os"
1921 "path"
2022 "regexp"
2123 "strings"
@@ -153,10 +155,11 @@ func (c Configs) ParentForPackage(pkg string) *Config {
153155type 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.
287296func (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