Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
change log

change log
  • Loading branch information
linzhp committed Oct 15, 2023
1 parent 007c123 commit db35bdb
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ A brief description of the categories of changes:
* Make `//python/pip_install:pip_repository_bzl` `bzl_library` target internal
as all of the publicly available symbols (etc. `package_annotation`) are
re-exported via `//python:pip_bzl` `bzl_library`.
* Gazelle Python extension no longer have runtime dependencies.

### Fixed

Expand Down
2 changes: 0 additions & 2 deletions examples/build_file_generation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ load("@bazel_gazelle//:def.bzl", "gazelle")
load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python_gazelle_plugin//:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")

Expand Down Expand Up @@ -56,7 +55,6 @@ gazelle_python_manifest(
# See https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.rst#example
gazelle(
name = "gazelle",
data = GAZELLE_PYTHON_RUNTIME_DEPS,
gazelle = "@rules_python_gazelle_plugin//python:gazelle_binary",
)

Expand Down
2 changes: 0 additions & 2 deletions examples/bzlmod_build_file_generation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ load("@bazel_gazelle//:def.bzl", "gazelle")
load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python_gazelle_plugin//:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")

Expand Down Expand Up @@ -70,7 +69,6 @@ gazelle_python_manifest(
# See: https://github.com/bazelbuild/bazel-gazelle#fix-and-update
gazelle(
name = "gazelle",
data = GAZELLE_PYTHON_RUNTIME_DEPS,
gazelle = "@rules_python_gazelle_plugin//python:gazelle_binary",
)

Expand Down
2 changes: 0 additions & 2 deletions gazelle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ with the rules_python extension included. This typically goes in your root

```starlark
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@rules_python_gazelle_plugin//:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")

# Our gazelle target points to the python gazelle binary.
# This is the simple case where we only need one language supported.
Expand All @@ -134,7 +133,6 @@ load("@rules_python_gazelle_plugin//:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
# See https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.rst#example
gazelle(
name = "gazelle",
data = GAZELLE_PYTHON_RUNTIME_DEPS,
gazelle = "@rules_python_gazelle_plugin//python:gazelle_binary",
)
```
Expand Down
2 changes: 2 additions & 0 deletions gazelle/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ go_test(
srcs = ["python_test.go"],
data = [
":gazelle_binary",
":helper",
] + glob(["testdata/**"]),
deps = [
"@bazel_gazelle//testtools:go_default_library",
"@com_github_ghodss_yaml//:yaml",
"@io_bazel_rules_go//go/runfiles:go_default_library",
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
],
)
Expand Down
28 changes: 18 additions & 10 deletions gazelle/python/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,28 @@ import (

var (
//go:embed helper.zip
pythonZip []byte
pyzPath string
helperZip []byte
helperPath string
)

type LifeCycleManager struct {
language.BaseLifecycleManager
pyzFilePath string
}

func (l *LifeCycleManager) Before(ctx context.Context) {
pyzFile, err := os.CreateTemp("", "python_zip_")
if err != nil {
log.Fatalf("failed to write parser zip: %v", err)
}
pyzPath = pyzFile.Name()
if _, err := pyzFile.Write(pythonZip); err != nil {
log.Fatalf("cannot write %q: %v", pyzPath, err)
helperPath = os.Getenv("GAZELLE_PYTHON_HELPER")
if helperPath == "" {
pyzFile, err := os.CreateTemp("", "python_zip_")
if err != nil {
log.Fatalf("failed to write parser zip: %v", err)
}
defer pyzFile.Close()
helperPath = pyzFile.Name()
l.pyzFilePath = helperPath
if _, err := pyzFile.Write(helperZip); err != nil {
log.Fatalf("cannot write %q: %v", helperPath, err)
}
}
startParserProcess(ctx)
startStdModuleProcess(ctx)
Expand All @@ -51,5 +57,7 @@ func (l *LifeCycleManager) DoneGeneratingRules() {

func (l *LifeCycleManager) AfterResolvingDeps(ctx context.Context) {
shutdownStdModuleProcess()
os.Remove(pyzPath)
if l.pyzFilePath != "" {
os.Remove(l.pyzFilePath)
}
}
Empty file modified gazelle/python/parse.py
100755 → 100644
Empty file.
5 changes: 3 additions & 2 deletions gazelle/python/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ var (
)

func startParserProcess(ctx context.Context) {
cmd := exec.CommandContext(ctx, "python3", pyzPath, "parse")

// due to #691, we need a system interpreter to boostrap, part of which is
// to locate the hermetic interpreter.
cmd := exec.CommandContext(ctx, "python3", helperPath, "parse")
cmd.Stderr = os.Stderr

stdin, err := cmd.StdinPipe()
Expand Down
19 changes: 15 additions & 4 deletions gazelle/python/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ package python_test

import (
"bytes"
"context"
"errors"
"github.com/bazelbuild/bazel-gazelle/testtools"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/ghodss/yaml"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"

"github.com/bazelbuild/bazel-gazelle/testtools"
"github.com/bazelbuild/rules_go/go/runfiles"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/ghodss/yaml"
)

const (
Expand Down Expand Up @@ -149,11 +153,18 @@ func testPath(t *testing.T, name string, files []bazel.RunfileEntry) {

args := []string{"-build_file_name=BUILD,BUILD.bazel"}

cmd := exec.Command(gazellePath, args...)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
t.Cleanup(cancel)
cmd := exec.CommandContext(ctx, gazellePath, args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Dir = workspaceRoot
helperScript, err := runfiles.Rlocation("rules_python_gazelle_plugin/python/helper")
if err != nil {
t.Fatalf("failed to initialize Python heler: %v", err)
}
cmd.Env = append(os.Environ(), "GAZELLE_PYTHON_HELPER="+helperScript)
if err := cmd.Run(); err != nil {
var e *exec.ExitError
if !errors.As(err, &e) {
Expand Down
5 changes: 3 additions & 2 deletions gazelle/python/std_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ var (
func startStdModuleProcess(ctx context.Context) {
stdModulesSeen = make(map[string]struct{})

cmd := exec.CommandContext(ctx, "python3", pyzPath, "std_modules")

// due to #691, we need a system interpreter to boostrap, part of which is
// to locate the hermetic interpreter.
cmd := exec.CommandContext(ctx, "python3", helperPath, "std_modules")
cmd.Stderr = os.Stderr
// All userland site-packages should be ignored.
cmd.Env = []string{"PYTHONNOUSERSITE=1"}
Expand Down

0 comments on commit db35bdb

Please sign in to comment.