diff --git a/CHANGELOG.md b/CHANGELOG.md index be69f5e8d0..1629e1e708 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/examples/build_file_generation/BUILD.bazel b/examples/build_file_generation/BUILD.bazel index 79f62519df..a03af54a1a 100644 --- a/examples/build_file_generation/BUILD.bazel +++ b/examples/build_file_generation/BUILD.bazel @@ -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") @@ -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", ) diff --git a/examples/bzlmod_build_file_generation/BUILD.bazel b/examples/bzlmod_build_file_generation/BUILD.bazel index 9b2e5bdce4..67288d6f43 100644 --- a/examples/bzlmod_build_file_generation/BUILD.bazel +++ b/examples/bzlmod_build_file_generation/BUILD.bazel @@ -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") @@ -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", ) diff --git a/gazelle/README.md b/gazelle/README.md index b8be32ff44..fe275e97c9 100644 --- a/gazelle/README.md +++ b/gazelle/README.md @@ -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. @@ -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", ) ``` diff --git a/gazelle/python/BUILD.bazel b/gazelle/python/BUILD.bazel index ca3944e658..507d69e9d7 100644 --- a/gazelle/python/BUILD.bazel +++ b/gazelle/python/BUILD.bazel @@ -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", ], ) diff --git a/gazelle/python/lifecycle.go b/gazelle/python/lifecycle.go index 910763c8dc..6d628e9137 100644 --- a/gazelle/python/lifecycle.go +++ b/gazelle/python/lifecycle.go @@ -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) @@ -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) + } } diff --git a/gazelle/python/parse.py b/gazelle/python/parse.py old mode 100755 new mode 100644 diff --git a/gazelle/python/parser.go b/gazelle/python/parser.go index 91c5ea928a..ad55e03a01 100644 --- a/gazelle/python/parser.go +++ b/gazelle/python/parser.go @@ -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() diff --git a/gazelle/python/python_test.go b/gazelle/python/python_test.go index 23225c15a7..74bd85bce6 100644 --- a/gazelle/python/python_test.go +++ b/gazelle/python/python_test.go @@ -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 ( @@ -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) { diff --git a/gazelle/python/std_modules.go b/gazelle/python/std_modules.go index 5aa2f62222..dd59cd8832 100644 --- a/gazelle/python/std_modules.go +++ b/gazelle/python/std_modules.go @@ -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"}