diff --git a/CHANGELOG.md b/CHANGELOG.md index d7403b4f1a..5a71e5f972 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,8 @@ END_UNRELEASED_TEMPLATE underlying runtime. * (performance) 90% reduction in py_binary/py_test analysis phase cost. ([#3381](https://github.com/bazel-contrib/rules_python/pull/3381)). +* (gazelle) Fix `gazelle_python_manifest.test` so that it accesses manifest files via `runfile` path handling rather than directly ([#3397](https://github.com/bazel-contrib/rules_python/issues/3397)). + {#v0-0-0-added} ### Added diff --git a/gazelle/manifest/defs.bzl b/gazelle/manifest/defs.bzl index 45fdb32e7d..b615c4efc1 100644 --- a/gazelle/manifest/defs.bzl +++ b/gazelle/manifest/defs.bzl @@ -117,9 +117,9 @@ def gazelle_python_manifest( if requirements: attrs = { "env": { - "_TEST_MANIFEST": "$(rootpath {})".format(manifest), + "_TEST_MANIFEST": "$(rlocationpath {})".format(manifest), "_TEST_MANIFEST_GENERATOR_HASH": "$(rlocationpath {})".format(manifest_generator_hash), - "_TEST_REQUIREMENTS": "$(rootpath {})".format(requirements), + "_TEST_REQUIREMENTS": "$(rlocationpath {})".format(requirements), }, "size": "small", } diff --git a/gazelle/manifest/test/test.go b/gazelle/manifest/test/test.go index 5804a7102e..77b354b495 100644 --- a/gazelle/manifest/test/test.go +++ b/gazelle/manifest/test/test.go @@ -26,23 +26,32 @@ import ( "path/filepath" "testing" - "github.com/bazelbuild/rules_go/go/runfiles" "github.com/bazel-contrib/rules_python/gazelle/manifest" + "github.com/bazelbuild/rules_go/go/runfiles" ) -func TestGazelleManifestIsUpdated(t *testing.T) { - requirementsPath := os.Getenv("_TEST_REQUIREMENTS") - if requirementsPath == "" { - t.Fatal("_TEST_REQUIREMENTS must be set") +// getResolvedRunfile resolves an environment variable to a runfiles path. +// It handles getting the env var, checking it's set, and resolving it through +// the runfiles mechanism, providing detailed error messages if anything fails. +func getResolvedRunfile(t *testing.T, envVar string) string { + t.Helper() + path := os.Getenv(envVar) + if path == "" { + t.Fatalf("%s must be set", envVar) } - - manifestPath := os.Getenv("_TEST_MANIFEST") - if manifestPath == "" { - t.Fatal("_TEST_MANIFEST must be set") + resolvedPath, err := runfiles.Rlocation(path) + if err != nil { + t.Fatalf("failed to resolve runfiles path for %s (%q): %v", envVar, path, err) } + return resolvedPath +} + +func TestGazelleManifestIsUpdated(t *testing.T) { + requirementsPathResolved := getResolvedRunfile(t, "_TEST_REQUIREMENTS") + manifestPathResolved := getResolvedRunfile(t, "_TEST_MANIFEST") manifestFile := new(manifest.File) - if err := manifestFile.Decode(manifestPath); err != nil { + if err := manifestFile.Decode(manifestPathResolved); err != nil { t.Fatalf("decoding manifest file: %v", err) } @@ -50,11 +59,7 @@ func TestGazelleManifestIsUpdated(t *testing.T) { t.Fatal("failed to find the Gazelle manifest file integrity") } - manifestGeneratorHashPath, err := runfiles.Rlocation( - os.Getenv("_TEST_MANIFEST_GENERATOR_HASH")) - if err != nil { - t.Fatalf("failed to resolve runfiles path of manifest: %v", err) - } + manifestGeneratorHashPath := getResolvedRunfile(t, "_TEST_MANIFEST_GENERATOR_HASH") manifestGeneratorHash, err := os.Open(manifestGeneratorHashPath) if err != nil { @@ -62,9 +67,9 @@ func TestGazelleManifestIsUpdated(t *testing.T) { } defer manifestGeneratorHash.Close() - requirements, err := os.Open(requirementsPath) + requirements, err := os.Open(requirementsPathResolved) if err != nil { - t.Fatalf("opening %q: %v", requirementsPath, err) + t.Fatalf("opening %q: %v", requirementsPathResolved, err) } defer requirements.Close() @@ -73,9 +78,9 @@ func TestGazelleManifestIsUpdated(t *testing.T) { t.Fatalf("verifying integrity: %v", err) } if !valid { - manifestRealpath, err := filepath.EvalSymlinks(manifestPath) + manifestRealpath, err := filepath.EvalSymlinks(manifestPathResolved) if err != nil { - t.Fatalf("evaluating symlink %q: %v", manifestPath, err) + t.Fatalf("evaluating symlink %q: %v", manifestPathResolved, err) } t.Errorf( "%q is out-of-date. Follow the update instructions in that file to resolve this",