@@ -16,6 +16,8 @@ package pythonconfig
16
16
17
17
import (
18
18
"fmt"
19
+ "log"
20
+ "os"
19
21
"path"
20
22
"regexp"
21
23
"strings"
@@ -153,10 +155,11 @@ func (c Configs) ParentForPackage(pkg string) *Config {
153
155
type Config struct {
154
156
parent * Config
155
157
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
160
163
161
164
excludedPatterns * singlylinkedlist.List
162
165
ignoreFiles map [string ]struct {}
@@ -281,11 +284,26 @@ func (c *Config) SetGazelleManifest(gazelleManifest *manifest.Manifest) {
281
284
c .gazelleManifest = gazelleManifest
282
285
}
283
286
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
+
284
293
// FindThirdPartyDependency scans the gazelle manifests for the current config
285
294
// and the parent configs up to the root finding if it can resolve the module
286
295
// name.
287
296
func (c * Config ) FindThirdPartyDependency (modName string ) (string , string , bool ) {
288
297
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
+
289
307
if currentCfg .gazelleManifest != nil {
290
308
gazelleManifest := currentCfg .gazelleManifest
291
309
if distributionName , ok := gazelleManifest .ModulesMapping [modName ]; ok {
@@ -526,3 +544,17 @@ func (c *Config) FormatThirdPartyDependency(repositoryName string, distributionN
526
544
527
545
return label .New (repositoryName , normConventionalDistributionName , normConventionalDistributionName )
528
546
}
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