From 896257a1bdb9a4b29bc19d07c0819e6f3f98a3d5 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Fri, 22 Sep 2023 12:30:27 +0900 Subject: [PATCH 1/2] fix(gazelle): fix runfiles discovery --- CHANGELOG.md | 2 ++ gazelle/python/parser.go | 9 ++++++++- gazelle/python/std_modules.go | 11 +++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e319d28fb5..17d284ab92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,8 @@ A brief description of the categories of changes: dependencies improving initial build times involving external dependency fetching. +* (gazelle) Improve runfiles lookup hermeticity. + ## [0.25.0] - 2023-08-22 ### Changed diff --git a/gazelle/python/parser.go b/gazelle/python/parser.go index c45aef139a..2dfcfa35e1 100644 --- a/gazelle/python/parser.go +++ b/gazelle/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 = rfiles.Env() cmd.Stderr = os.Stderr diff --git a/gazelle/python/std_modules.go b/gazelle/python/std_modules.go index 15ef766ff2..a87deec366 100644 --- a/gazelle/python/std_modules.go +++ b/gazelle/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) From b485d1bd79c8e4da086aa062042200f5f4fc75e8 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Mon, 25 Sep 2023 11:13:46 +0900 Subject: [PATCH 2/2] comment: do not drop existing env vars in parser.go --- gazelle/python/parser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gazelle/python/parser.go b/gazelle/python/parser.go index 2dfcfa35e1..60a3c24269 100644 --- a/gazelle/python/parser.go +++ b/gazelle/python/parser.go @@ -51,7 +51,7 @@ func startParserProcess(ctx context.Context) { } cmd := exec.CommandContext(ctx, parseScriptRunfile) - cmd.Env = rfiles.Env() + cmd.Env = append(os.Environ(), rfiles.Env()...) cmd.Stderr = os.Stderr