Skip to content

Commit

Permalink
fix(gazelle): runfiles discovery (#1429)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aignas committed Sep 26, 2023
1 parent 237ab71 commit 44093e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 8 additions & 1 deletion python/parser.go
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions python/std_modules.go
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 44093e7

Please sign in to comment.