Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gazelle): Remove integrity field from Gazelle manifest #1666

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ A brief description of the categories of changes:
target for each file with `if __name__ == "__main__"` instead of just one
`py_binary` for the whole module.

* (gazelle) the Gazelle manifest integrity field is now optional. If the
`requirements` argument to `gazelle_python_manifest` is unset, no integrity
field will be generated.

### Fixed

* (gazelle) The gazelle plugin helper was not working with Python toolchains 3.11
Expand Down
2 changes: 1 addition & 1 deletion examples/bzlmod_build_file_generation/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.0.0
6.4.0
6 changes: 2 additions & 4 deletions examples/bzlmod_build_file_generation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ modules_mapping(
exclude_patterns = [
"^_|(\\._)+", # This is the default.
"(\\.tests)+", # Add a custom one to get rid of the psutil tests.
"^colorama", # Get rid of colorama on Windows.
"^lazy_object_proxy\\.cext$", # Get rid of this on Linux because it isn't included on Windows.
],
wheels = all_whl_requirements,
)
Expand All @@ -47,10 +49,6 @@ gazelle_python_manifest(
name = "gazelle_python_manifest",
modules_mapping = ":modules_map",
pip_repository_name = "pip",
requirements = [
"//:requirements_lock.txt",
"//:requirements_windows.txt",
],
tags = ["exclusive"],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,3 @@ manifest:
yamllint.rules.truthy: yamllint
pip_repository:
name: pip
integrity: cd25503dc6b3d9e1c5f46715ba2d0499ecc8b3d654ebcbf9f4e52f2074290e0a
1 change: 1 addition & 0 deletions gazelle/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "rules_python", version = "0.18.0")
bazel_dep(name = "rules_go", version = "0.41.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "gazelle", version = "0.33.0", repo_name = "bazel_gazelle")
Expand Down
4 changes: 4 additions & 0 deletions gazelle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ gazelle_python_manifest(
pip_repository_name = "pip",
# This should point to wherever we declare our python dependencies
# (the same as what we passed to the modules_mapping rule in WORKSPACE)
# This argument is optional. If provided, the `.test` target is very
# fast because it just has to check an integrity field. If not provided,
# the integrity field is not added to the manifest which can help avoid
# merge conflicts in large repos.
requirements = "//:requirements_lock.txt",
)
```
Expand Down
5 changes: 5 additions & 0 deletions gazelle/manifest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

exports_files([
# This gets wrapped up into a py_binary with args inside of the gazelle_python_manifest macro.
"copy_to_source.py",
])

go_library(
name = "manifest",
srcs = ["manifest.go"],
Expand Down
36 changes: 36 additions & 0 deletions gazelle/manifest/copy_to_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Copy a generated file to the source tree.

Run like:
copy_to_source path/to/generated_file path/to/source_file_to_overwrite
"""

import os
import shutil
import stat
import sys
from pathlib import Path


def copy_to_source(generated_relative_path: Path, target_relative_path: Path) -> None:
"""Copy the generated file to the target file path.

Expands the relative paths by looking at Bazel env vars to figure out which absolute paths to use.
"""
# This script normally gets executed from the runfiles dir, so find the absolute path to the generated file based on that.
generated_absolute_path = Path.cwd() / generated_relative_path

# Similarly, the target is relative to the source directory.
target_absolute_path = os.getenv("BUILD_WORKSPACE_DIRECTORY") / target_relative_path

print(f"Copying {generated_absolute_path} to {target_absolute_path}")
target_absolute_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(generated_absolute_path, target_absolute_path)

target_absolute_path.chmod(0O664)


if __name__ == "__main__":
if len(sys.argv) != 3:
sys.exit("Usage: copy_to_source <generated_file> <target_file>")

copy_to_source(Path(sys.argv[1]), Path(sys.argv[2]))
119 changes: 72 additions & 47 deletions gazelle/manifest/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
for updating and testing the Gazelle manifest file.
"""

load("@io_bazel_rules_go//go:def.bzl", "GoSource", "go_binary", "go_test")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@io_bazel_rules_go//go:def.bzl", "GoSource", "go_test")
load("@rules_python//python:defs.bzl", "py_binary")

def gazelle_python_manifest(
name,
requirements,
modules_mapping,
requirements = [],
pip_repository_name = "",
pip_deps_repository_name = "",
manifest = ":gazelle_python.yaml",
Expand All @@ -30,15 +32,18 @@ def gazelle_python_manifest(

Args:
name: the name used as a base for the targets.
modules_mapping: the target for the generated modules_mapping.json file.
requirements: the target for the requirements.txt file or a list of
requirements files that will be concatenated before passing on to
the manifest generator.
the manifest generator. If unset, no integrity field is added to the
manifest, meaning testing it is just as expensive as generating it,
but modifying it is much less likely to result in a merge conflict.
pip_repository_name: the name of the pip_install or pip_repository target.
pip_deps_repository_name: deprecated - the old pip_install target name.
modules_mapping: the target for the generated modules_mapping.json file.
manifest: the target for the Gazelle manifest file.
**kwargs: other bazel attributes passed to the target target generated by
this macro.
manifest: the Gazelle manifest file.
defaults to the same value as manifest.
**kwargs: other bazel attributes passed to the generate and test targets
generated by this macro.
"""
if pip_deps_repository_name != "":
# buildifier: disable=print
Expand All @@ -52,12 +57,17 @@ def gazelle_python_manifest(
# This is a temporary check while pip_deps_repository_name exists as deprecated.
fail("pip_repository_name must be set in //{}:{}".format(native.package_name(), name))

test_target = "{}.test".format(name)
update_target = "{}.update".format(name)
update_target_label = "//{}:{}".format(native.package_name(), update_target)

manifest_genrule = name + ".genrule"
generated_manifest = name + ".generated_manifest"
manifest_generator = Label("//manifest/generate:generate")
manifest_generator_hash = Label("//manifest/generate:generate_lib_sources_hash")

if type(requirements) == "list":
if requirements and type(requirements) == "list":
# This runs if requirements is a list or is unset (default value is empty list)
native.genrule(
name = name + "_requirements_gen",
srcs = sorted(requirements),
Expand All @@ -68,56 +78,71 @@ def gazelle_python_manifest(
requirements = name + "_requirements_gen"

update_args = [
"--manifest-generator-hash",
"$(rootpath {})".format(manifest_generator_hash),
"--requirements",
"$(rootpath {})".format(requirements),
"--pip-repository-name",
pip_repository_name,
"--modules-mapping",
"$(rootpath {})".format(modules_mapping),
"--output",
"$(rootpath {})".format(manifest),
"--update-target",
update_target_label,
"--manifest-generator-hash=$(execpath {})".format(manifest_generator_hash),
"--requirements=$(rootpath {})".format(requirements) if requirements else "--requirements=",
"--pip-repository-name={}".format(pip_repository_name),
"--modules-mapping=$(execpath {})".format(modules_mapping),
f0rmiga marked this conversation as resolved.
Show resolved Hide resolved
"--output=$(execpath {})".format(generated_manifest),
f0rmiga marked this conversation as resolved.
Show resolved Hide resolved
"--update-target={}".format(update_target_label),
]

go_binary(
name = update_target,
embed = [Label("//manifest/generate:generate_lib")],
data = [
manifest,
native.genrule(
name = manifest_genrule,
outs = [generated_manifest],
cmd = "$(execpath {}) {}".format(manifest_generator, " ".join(update_args)),
tools = [manifest_generator],
srcs = [
modules_mapping,
requirements,
manifest_generator_hash,
],
args = update_args,
visibility = ["//visibility:private"],
tags = ["manual"],
] + ([requirements] if requirements else []),
)

attrs = {
"env": {
"_TEST_MANIFEST": "$(rootpath {})".format(manifest),
"_TEST_MANIFEST_GENERATOR_HASH": "$(rootpath {})".format(manifest_generator_hash),
"_TEST_REQUIREMENTS": "$(rootpath {})".format(requirements),
},
"size": "small",
}
go_test(
name = "{}.test".format(name),
srcs = [Label("//manifest/test:test.go")],
py_binary(
name = update_target,
srcs = [Label("//manifest:copy_to_source.py")],
main = Label("//manifest:copy_to_source.py"),
args = [
"$(rootpath {})".format(generated_manifest),
"$(rootpath {})".format(manifest),
],
data = [
generated_manifest,
manifest,
requirements,
manifest_generator_hash,
],
rundir = ".",
deps = [Label("//manifest")],
# kwargs could contain test-specific attributes like size or timeout
**dict(attrs, **kwargs)
**kwargs
)

if requirements:
attrs = {
"env": {
"_TEST_MANIFEST": "$(rootpath {})".format(manifest),
"_TEST_MANIFEST_GENERATOR_HASH": "$(rootpath {})".format(manifest_generator_hash),
"_TEST_REQUIREMENTS": "$(rootpath {})".format(requirements),
},
"size": "small",
}
go_test(
name = test_target,
srcs = [Label("//manifest/test:test.go")],
data = [
manifest,
requirements,
manifest_generator_hash,
],
rundir = ".",
deps = [Label("//manifest")],
# kwargs could contain test-specific attributes like size or timeout
**dict(attrs, **kwargs)
)
else:
diff_test(
name = test_target,
file1 = generated_manifest,
file2 = manifest,
failure_message = "Gazelle manifest is out of date. Run 'bazel run {}' to update it.".format(native.package_relative_label(update_target)),
**kwargs
)

native.filegroup(
name = name,
srcs = [manifest],
Expand Down
55 changes: 23 additions & 32 deletions gazelle/manifest/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ import (
"github.com/bazelbuild/rules_python/gazelle/manifest"
)

func init() {
if os.Getenv("BUILD_WORKSPACE_DIRECTORY") == "" {
log.Fatalln("ERROR: this program must run under Bazel")
}
}

func main() {
var (
manifestGeneratorHashPath string
Expand Down Expand Up @@ -79,10 +73,6 @@ func main() {
"The Bazel target to update the YAML manifest file.")
flag.Parse()

if requirementsPath == "" {
log.Fatalln("ERROR: --requirements must be set")
}

if modulesMappingPath == "" {
log.Fatalln("ERROR: --modules-mapping must be set")
}
Expand All @@ -102,12 +92,12 @@ func main() {

header := generateHeader(updateTarget)
repository := manifest.PipRepository{
Name: pipRepositoryName,
Name: pipRepositoryName,
}

manifestFile := manifest.NewFile(&manifest.Manifest{
ModulesMapping: modulesMapping,
PipRepository: &repository,
PipRepository: &repository,
})
if err := writeOutput(
outputPath,
Expand Down Expand Up @@ -155,12 +145,7 @@ func writeOutput(
manifestGeneratorHashPath string,
requirementsPath string,
) error {
stat, err := os.Stat(outputPath)
if err != nil {
return fmt.Errorf("failed to write output: %w", err)
}

outputFile, err := os.OpenFile(outputPath, os.O_WRONLY|os.O_TRUNC, stat.Mode())
outputFile, err := os.OpenFile(outputPath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
Expand All @@ -170,20 +155,26 @@ func writeOutput(
return fmt.Errorf("failed to write output: %w", err)
}

manifestGeneratorHash, err := os.Open(manifestGeneratorHashPath)
if err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
defer manifestGeneratorHash.Close()

requirements, err := os.Open(requirementsPath)
if err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
defer requirements.Close()

if err := manifestFile.Encode(outputFile, manifestGeneratorHash, requirements); err != nil {
return fmt.Errorf("failed to write output: %w", err)
if requirementsPath != "" {
manifestGeneratorHash, err := os.Open(manifestGeneratorHashPath)
if err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
defer manifestGeneratorHash.Close()

requirements, err := os.Open(requirementsPath)
if err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
defer requirements.Close()

if err := manifestFile.EncodeWithIntegrity(outputFile, manifestGeneratorHash, requirements); err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
} else {
if err := manifestFile.EncodeWithoutIntegrity(outputFile); err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
}

return nil
Expand Down
Loading
Loading