Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Bazel deps helper scripts #19

Merged

Conversation

greggdonovan
Copy link
Member

Add scripts to auto-generate the thirdparty/jvm BUILD files from the maven_deps.yaml.

These scripts handle:

  • checking out and updating bazel-deps
  • running the buildifier on the generated BUILD files
  • making any manual edits to the generated files via buildozer rules
  • a check to ensure the generated files are in sync with the yaml

The PR also runs these scripts to regenerate the thirdparty/jvm files that have been buildifier formatted.

This allows us to more easily add new dependencies to maven_deps.yaml. For example, for Scala support.

…maven_deps.yaml.

These scripts handle:
- checking out and updating bazel-deps
- running the buildifier on the generated BUILD files
- making any manual edits to the generated files via buildozer rules
- a check to ensure the generated files are in sync with the yaml
@bazel-io
Copy link
Member

Can one of the admins verify this patch?

@petroseskinder
Copy link
Member

@greggdonovan can you add a line checking if buildifier is installed (and installing it otherwise)? I ran into this issue when running generate.sh on a machine without buildifier.

@greggdonovan
Copy link
Member Author

@petroseskinder Good idea, thanks. I should do the same with buildozer, as well, I think.

I'll update the PR.

@cgrushko
Copy link
Contributor

ok to test

Copy link
Contributor

@cgrushko cgrushko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
I hope that we can one day drop these scripts and upstream all of its goodies :)

@@ -43,3 +43,5 @@ hs_err_pid*

# Ignore mac attributes files
*.DS_STORE
# Ignore bazel-deps resolverCache. See: https://github.com/johnynek/bazel-deps#options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you have experience with bazel_output_base from that link?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use bazel_output_base internally. @kevingessner added it here. I like it and I think it fits in with Bazel conventions nicely (e.g. removed with bazel clean --expunge). I can switch it in the PR if you like.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm... actually let's leave it like that, in case it turns out that it's useful to only have Bazel stuff in bazel-out.

# shellcheck disable=SC1090
. "$SCRIPT_DIR/setup.sh"

"${BAZEL_DEPS_DIR}/gen_maven_deps.sh" add-dep --deps "${ROOT_DIR}/maven_deps.yaml" --lang "$lang" "${coords[@]}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wat. It already supports consuming maven dependencies?

I gather the reason for using this script instead of calling bazel-deps directly is because it formats and handles AutoValue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Plus making sure you have a checked out version bazel-deps.

+1 to the goal of removing the scripts and using bazel-deps via a WORKSPACE rule of some sort.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gen_maven_deps.sh comes with bazel-deps and supports converting maven coords into dependencies.yaml. This wrapper script makes sure that bazel-deps is installed (setup.sh), updates dependencies.yaml, and then generates the new third_party code. It makes dep additions and updates a one-step process instead of 2 or 3. Switching to a workspace rule would take care of the first, but I still think it's a good workflow to have changing dependencies.yaml and regenerating the thrid_party files be a single command; maybe we could add that to bazel-deps.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sounds reasonable to ask bazel-deps to also regenerate the BUILD files.

then
echo
echo "* * *"
echo "Generated dependency files are not up to date. Run \`dev-scripts/dependencies/generate.sh\` and commit the changes."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this something that you found useful in general with bazel-deps?
If so, let's open an Issue in bazel-deps and see what Oscar thinks.
If he's open to the idea, let's put a # TODO here to delete the file once the upstream issue is resolved?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, its occasionally useful. We have check-deps.sh run in our CI so that someone doesn't update maven_deps.yaml without running the generation step and checking in the thirdparty BUILD files. It's saved us a couple of times so far.

Are you thinking something like "${BAZEL_DEPS_DIR}/gen_maven_deps.sh check -r ${SCRIPT_DIR} -s thirdparty/workspace.bzl -d maven_deps.yaml?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, something like that.

buildozer 'add deps //external:jar/com/google/auto/value/auto_value' //thirdparty/jvm/com/google/auto/value:auto_value_plugin

# The generated BUILD files are not well-formatted. Run the buildifier on them.
find "${ROOT_DIR}/thirdparty/jvm" -name "BUILD" -exec "buildifier" -showlog -mode=fix {} +
Copy link
Contributor

@cgrushko cgrushko Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a
# TODO(https://github.com/johnynek/bazel-deps/issues/73): Drop once issue is fixed.


"$BAZEL_DEPS_DIR/gen_maven_deps.sh" generate --repo-root "$ROOT_DIR" --sha-file "thirdparty/workspace.bzl" --deps maven_deps.yaml

# Manually add the AutoValue plugin. Otherwise, everything can be auto-generated from the YAML.
Copy link
Contributor

@cgrushko cgrushko Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a
# TODO(https://github.com/johnynek/bazel-deps/issues/62): Drop once issue is fixed.


SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
ROOT_DIR=$(cd "${SCRIPT_DIR}/../.." && pwd)
BAZEL_DEPS_DIR="$ROOT_DIR/../bazel-deps"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that'll land the new repo outside of the current tree?
feels awkward, though landing it inside the tree is also awkward. So I don't know :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Managing external repos outside of bazel feels really awkward. I don't have a great solution.

Do you or @kevingessner or @johnynek have a link to the issue or issues preventing us from using bazel-deps as a repository rule? It'd be nice to track them. I see bazel #1673 but perhaps there's more to it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1673 is the only one I know of. Even if it's in the workspace, it'll be a little bit of wrangling to get it running correctly -- IIRC bazel-deps comes with the gen_maven_deps.sh wrapper script because it doesn't work with bazel run. So we'll need to get the various paths right. Doable but may be a little tricky.

@@ -1,4 +1,16 @@
def maven_dependencies(callback):
def declare_maven(hash):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you delete load.bzl and the reference to it in WORKSPACE, now that it's not needed anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Updated the PR.

@greggdonovan
Copy link
Member Author

Re: The buildozer and buildifier dependencies. How should we be having folks install them? For OSX there is a homebrew package for buildifier. We can add one for buildozer. I'm not sure if we hope to have these tools in standard yum/deb repos for linux.

Are we better off exiting with the proper instructions for installing them or better off doing something like:
command -v buildozer >/dev/null 2>&1 || { echo >&2 "buildozer is missing; installing via go get."; go get github.com/bazelbuild/buildtools/buildozer; } ?

The devtools script handles this via having buildifier as a submodule and building from source if missing, but maybe that's not the right approach for an end-user oriented tool?

@cgrushko
Copy link
Contributor

I filed bazelbuild/bazel#3895 after chatting with @damienmg . There's a tentative agreement, but I'm told it's low priority.

BFG uses Buildozer to create the BUILD files, so it's actually a prerequisite. I'll update README.md with instructions.

So, I think it's ok to exit with instructions (pointing at README.md should be fine)

We can drop our workaround when annotation processor support and buildifier formatting are integrated into bazel-deps.
…o is not installed.

TODO: Remove this once bazelbuild/bazel#3895 is resolved and we can provide a better method for installing these tools.
Copy link
Contributor

@cgrushko cgrushko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's awesome, thanks!

@cgrushko
Copy link
Contributor

Feel free to merge (you should have Write permissions).

@greggdonovan greggdonovan merged commit ce08bf1 into bazelbuild:master Oct 13, 2017
@cgrushko
Copy link
Contributor

Ah, I ran into some trouble with setup.sh - I tried using it in another repo, and it clashes with BFG. Might be worth to change BAZEL_DEPS_DIR to point inside the repo
(i.e. BAZEL_DEPS_DIR="$ROOT_DIR/bazel-deps")

@greggdonovan
Copy link
Member Author

@cgrushko That's a good point -- I'll update in a PR. I can also add in removals of the workaround to bazel-deps#73 and bazel-deps#85.

I should be back to the meat of the Scala work this week, too. Sorry -- had to finish up some other work.

@kevingessner
Copy link

I'm hoping to work on bazelbuild/bazel#1673 this week, which should make setup.sh unnecessary. I'll let you know if I can make progress!

@greggdonovan
Copy link
Member Author

@cgrushko Cloning bazel-deps inside of the BFG repo means that its rules get run on bazel test //... and there doesn't appear to be a good solution for excluding them.

How about BAZEL_DEPS_DIR="$ROOT_DIR/../BUILD_file_generator-bazel-deps" for now to avoid conflicts until @kevingessner solves this for real with bazelbuild/bazel#1673?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants