From 44093e7fc63b0abd508946503a39303c8948803b Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 26 Sep 2023 11:21:15 +0900 Subject: [PATCH] fix(gazelle): runfiles discovery (#1429) Pass the environment generated by the `runfiles` helper so that the Python launcher can find the runfiles correctly as implemented in bazelbuild/bazel#14740. Fixes #1426 --- python/parser.go | 9 ++++++++- python/std_modules.go | 11 +++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/python/parser.go b/python/parser.go index c45aef1..60a3c24 100644 --- a/python/parser.go +++ b/python/parser.go @@ -38,13 +38,20 @@ var ( ) func startParserProcess(ctx context.Context) { - parseScriptRunfile, err := runfiles.Rlocation("rules_python_gazelle_plugin/python/parse") + rfiles, err := runfiles.New() + if err != nil { + log.Printf("failed to create a runfiles object: %v\n", err) + os.Exit(1) + } + + parseScriptRunfile, err := rfiles.Rlocation("rules_python_gazelle_plugin/python/parse") if err != nil { log.Printf("failed to initialize parser: %v\n", err) os.Exit(1) } cmd := exec.CommandContext(ctx, parseScriptRunfile) + cmd.Env = append(os.Environ(), rfiles.Env()...) cmd.Stderr = os.Stderr diff --git a/python/std_modules.go b/python/std_modules.go index 15ef766..a87deec 100644 --- a/python/std_modules.go +++ b/python/std_modules.go @@ -39,7 +39,13 @@ var ( func startStdModuleProcess(ctx context.Context) { stdModulesSeen = make(map[string]struct{}) - stdModulesScriptRunfile, err := runfiles.Rlocation("rules_python_gazelle_plugin/python/std_modules") + rfiles, err := runfiles.New() + if err != nil { + log.Printf("failed to create a runfiles object: %v\n", err) + os.Exit(1) + } + + stdModulesScriptRunfile, err := rfiles.Rlocation("rules_python_gazelle_plugin/python/std_modules") if err != nil { log.Printf("failed to initialize std_modules: %v\n", err) os.Exit(1) @@ -49,7 +55,8 @@ func startStdModuleProcess(ctx context.Context) { cmd.Stderr = os.Stderr // All userland site-packages should be ignored. - cmd.Env = []string{"PYTHONNOUSERSITE=1"} + cmd.Env = append([]string{"PYTHONNOUSERSITE=1"}, rfiles.Env()...) + stdin, err := cmd.StdinPipe() if err != nil { log.Printf("failed to initialize std_modules: %v\n", err)