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

Buildifier #456

Merged
merged 5 commits into from Dec 5, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions .circleci/config.yml
Expand Up @@ -56,24 +56,24 @@ jobs:
# Cannot be run on BuildKite (BazelCI), see
# https://github.com/bazelbuild/continuous-integration/issues/338
lint:
<<: *job_defaults
docker:
- image: circleci/node:10.12
steps:
- checkout:
<<: *post_checkout
- *setup_bazelrc

- run: yarn install

# Run the skylark linter to check our Bazel rules
# deprecated-api is disabled because we legacy providers for typescript
# which cannot be upgraded to modern providers without breaking compatability
- run: 'find . -type f -name "*.bzl" |
xargs java -jar /usr/local/bin/Skylint_deploy.jar --disable-checks=deprecated-api ||
(echo -e "\n.bzl files have lint errors. Please run ''yarn skylint''"; exit 1)'

# Enforce that BUILD files are formatted. Note that this uses the version of buildifier
# from the docker image above - take care that you use the same version when you run
# buildifier locally on your change.
- run: 'buildifier -mode=check $(find . -type f \( -name BUILD.bazel -or -name BUILD \)) ||
(echo "BUILD files not formatted. Please run ''yarn buildifier''" ; exit 1)'
# Note, this is not yet enforced, because
# - buildifier doesn't exit non-zero in the presence of lint warnings: https://github.com/bazelbuild/buildtools/issues/470
# - false positive for rule docstrings: https://github.com/bazelbuild/buildtools/issues/471
- run: 'yarn bazel:lint ||
(echo -e "\n.bzl files have lint errors. Please run ''yarn bazel:lint-fix''"; exit 1)'

# Enforce that BUILD files are formatted.
- run: 'yarn bazel:format -mode=check ||
(echo "BUILD files not formatted. Please run ''yarn bazel:format''" ; exit 1)'

# Overlaps with testing we do on BuildKite. This is still here because:
# - BuildKite doesn't have a public results UI; failures here let contributors fix their changes
Expand Down
28 changes: 19 additions & 9 deletions WORKSPACE
Expand Up @@ -13,9 +13,11 @@
# limitations under the License.

workspace(name = "build_bazel_rules_nodejs")

load("//:package.bzl", "rules_nodejs_dependencies", "rules_nodejs_dev_dependencies")

rules_nodejs_dependencies()

rules_nodejs_dev_dependencies()

load("//internal/common:check_bazel_version.bzl", "check_bazel_version")
Expand All @@ -37,7 +39,7 @@ local_repository(
path = "examples/packages",
)

load("//:defs.bzl", "node_repositories", "yarn_install", "npm_install")
load("//:defs.bzl", "node_repositories", "npm_install", "yarn_install")

# Install a hermetic version of node.
# After this is run, these labels will be available:
Expand Down Expand Up @@ -69,31 +71,39 @@ packages_example_setup_workspace()

# Dependencies to run skydoc
load("@io_bazel_rules_sass//sass:sass_repositories.bzl", "sass_repositories")

sass_repositories()

load("@io_bazel_skydoc//skylark:skylark.bzl", "skydoc_repositories")
skydoc_repositories()

# Dependencies to run buildifier and skylint
load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")

go_rules_dependencies()
go_register_toolchains()
skydoc_repositories()

#
# Install npm dependencies for tests
#

yarn_install(
name = "fine_grained_deps_yarn",
included_files = [
"",
".js",
".d.ts",
".json",
".proto",
],
package_json = "//internal/e2e/fine_grained_deps:package.json",
yarn_lock = "//internal/e2e/fine_grained_deps:yarn.lock",
included_files = ["", ".js", ".d.ts", ".json", ".proto"],
)

npm_install(
name = "fine_grained_deps_npm",
included_files = [
"",
".js",
".d.ts",
".json",
".proto",
],
package_json = "//internal/e2e/fine_grained_deps:package.json",
package_lock_json = "//internal/e2e/fine_grained_deps:package-lock.json",
included_files = ["", ".js", ".d.ts", ".json", ".proto"],
)
38 changes: 20 additions & 18 deletions defs.bzl
Expand Up @@ -18,10 +18,12 @@ Users should not load files under "/internal"
"""

load("//internal/common:check_bazel_version.bzl", _check_bazel_version = "check_bazel_version")
load("//internal/node:node.bzl",
_nodejs_binary = "nodejs_binary_macro",
_nodejs_test = "nodejs_test_macro")
load("//internal/node:node_repositories.bzl",_node_repositories = "node_repositories")
load(
"//internal/node:node.bzl",
_nodejs_binary = "nodejs_binary_macro",
_nodejs_test = "nodejs_test_macro",
)
load("//internal/node:node_repositories.bzl", _node_repositories = "node_repositories")
load("//internal/jasmine_node_test:jasmine_node_test.bzl", _jasmine_node_test = "jasmine_node_test")
load("//internal/npm_install:npm_install.bzl", _npm_install = "npm_install", _yarn_install = "yarn_install")
load("//internal/rollup:rollup_bundle.bzl", _rollup_bundle = "rollup_bundle")
Expand All @@ -45,17 +47,17 @@ http_server = _http_server

check_rules_nodejs_version = _check_rules_nodejs_version

def node_modules_filegroup(packages, patterns=[], **kwargs):
native.filegroup(
srcs = native.glob(["/".join([
"node_modules",
pkg,
"**",
ext,
]) for pkg in packages for ext in [
"*.js",
"*.json",
"*.d.ts",
]] + patterns),
**kwargs
)
def node_modules_filegroup(packages, patterns = [], **kwargs):
native.filegroup(
srcs = native.glob(["/".join([
"node_modules",
pkg,
"**",
ext,
]) for pkg in packages for ext in [
"*.js",
"*.json",
"*.d.ts",
]] + patterns),
**kwargs
)
4 changes: 4 additions & 0 deletions examples/bazel_managed_deps/WORKSPACE
@@ -1,13 +1,17 @@
# BEGIN BORING BOILERPLATE
workspace(name = "bazel_managed_deps_example")

local_repository(
name = "build_bazel_rules_nodejs",
path = "../..",
)

load("@build_bazel_rules_nodejs//:package.bzl", "rules_nodejs_dependencies")

rules_nodejs_dependencies()

load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "yarn_install")

node_repositories()
# END BORING BOILERPLATE

Expand Down
2 changes: 2 additions & 0 deletions examples/packages/WORKSPACE
Expand Up @@ -4,7 +4,9 @@ local_repository(
name = "build_bazel_rules_nodejs",
path = "../..",
)

load("@build_bazel_rules_nodejs//:package.bzl", "check_rules_nodejs_version", "rules_nodejs_dependencies")

rules_nodejs_dependencies()

# Test that check_rules_nodejs_version works as expected
Expand Down
68 changes: 34 additions & 34 deletions examples/packages/setup_workspace.bzl
Expand Up @@ -15,43 +15,43 @@
"""Helper function to setup @package_example workspace.
"""

load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install", "npm_install")
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_install", "yarn_install")

def packages_example_setup_workspace():
"""Node repositories for @packages_examples
"""
npm_install(
name = "packages_example_npm_install",
package_json = "@packages_example//:package.json",
package_lock_json = "@packages_example//:package-lock.json",
data = ["@packages_example//:postinstall.js"],
# Just here as a smoke test for this attribute
prod_only = True,
)
"""Node repositories for @packages_examples
"""
npm_install(
name = "packages_example_npm_install",
package_json = "@packages_example//:package.json",
package_lock_json = "@packages_example//:package-lock.json",
data = ["@packages_example//:postinstall.js"],
# Just here as a smoke test for this attribute
prod_only = True,
)

npm_install(
name = "packages_example_npm_install_duplicate_for_determinism_testing",
package_json = "@packages_example//:package.json",
package_lock_json = "@packages_example//:package-lock.json",
data = ["@packages_example//:postinstall.js"],
)
npm_install(
name = "packages_example_npm_install_duplicate_for_determinism_testing",
package_json = "@packages_example//:package.json",
package_lock_json = "@packages_example//:package-lock.json",
data = ["@packages_example//:postinstall.js"],
)

npm_install(
name = "packages_example_npm_no_lockfile",
package_json = "@packages_example//:package.json",
data = ["@packages_example//:postinstall.js"],
)
npm_install(
name = "packages_example_npm_no_lockfile",
package_json = "@packages_example//:package.json",
data = ["@packages_example//:postinstall.js"],
)

yarn_install(
name = "packages_example_yarn_install",
package_json = "@packages_example//:package.json",
yarn_lock = "@packages_example//:yarn.lock",
data = ["@packages_example//:postinstall.js"],
)
yarn_install(
name = "packages_example_yarn_install",
package_json = "@packages_example//:package.json",
yarn_lock = "@packages_example//:yarn.lock",
data = ["@packages_example//:postinstall.js"],
)

yarn_install(
name = "packages_example_yarn_install_duplicate_for_determinism_testing",
package_json = "@packages_example//:package.json",
yarn_lock = "@packages_example//:yarn.lock",
data = ["@packages_example//:postinstall.js"],
)
yarn_install(
name = "packages_example_yarn_install_duplicate_for_determinism_testing",
package_json = "@packages_example//:package.json",
yarn_lock = "@packages_example//:yarn.lock",
data = ["@packages_example//:postinstall.js"],
)
6 changes: 5 additions & 1 deletion examples/program/WORKSPACE
Expand Up @@ -19,7 +19,11 @@ load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories")
# @nodejs//:npm
# - The yarn package manager:
# @nodejs//:yarn
node_repositories(package_json = ["//:package.json"], node_version = "8.11.1", yarn_version = "1.5.1")
node_repositories(
node_version = "8.11.1",
package_json = ["//:package.json"],
yarn_version = "1.5.1",
)

# Now the user must run either
# bazel run @nodejs//:yarn
Expand Down
27 changes: 14 additions & 13 deletions examples/rollup/rollup.bzl
Expand Up @@ -19,17 +19,17 @@ other rules in this repo.
"""

def _rollup(ctx):
args = ["--input", ctx.attr.entry_point]
args += ["--output.file", ctx.outputs.bundle.path]
args += ["--output.format", "es"]
args = ["--input", ctx.attr.entry_point]
args += ["--output.file", ctx.outputs.bundle.path]
args += ["--output.format", "es"]

ctx.actions.run(
inputs = ctx.files.srcs,
executable = ctx.executable.rollup,
outputs = [ctx.outputs.bundle],
arguments = args,
)
return [DefaultInfo()]
ctx.actions.run(
inputs = ctx.files.srcs,
executable = ctx.executable.rollup,
outputs = [ctx.outputs.bundle],
arguments = args,
)
return [DefaultInfo()]

rollup = rule(
implementation = _rollup,
Expand All @@ -39,9 +39,10 @@ rollup = rule(
"rollup": attr.label(
default = Label("//examples/rollup"),
executable = True,
cfg = "host"),
cfg = "host",
),
},
outputs = {
"bundle": "%{name}.js"
"bundle": "%{name}.js",
},
)
)
6 changes: 3 additions & 3 deletions examples/vendored_node/WORKSPACE
Expand Up @@ -9,11 +9,11 @@ load("@build_bazel_rules_nodejs//:package.bzl", "rules_nodejs_dependencies")

rules_nodejs_dependencies()

load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "yarn_install", "npm_install")
load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "npm_install", "yarn_install")

node_repositories(
vendored_node = "@vendored_node_example//:node-v10.12.0-linux-x64",
vendored_yarn = "@vendored_node_example//:yarn-v1.10.0",
vendored_node = "@vendored_node_example//:node-v10.12.0-linux-x64",
vendored_yarn = "@vendored_node_example//:yarn-v1.10.0",
)

yarn_install(
Expand Down
56 changes: 29 additions & 27 deletions internal/common/check_bazel_version.bzl
Expand Up @@ -26,30 +26,32 @@ load(":check_version.bzl", "check_version")
# Check that a specific bazel version is being used.
# Args: minimum_bazel_version in the form "<major>.<minor>.<patch>"
def check_bazel_version(minimum_bazel_version, message = ""):
"""
Verify the users Bazel version is at least the given one.

This should be called from the `WORKSPACE` file so that the build fails as
early as possible. For example:

```
# in WORKSPACE:
load("@build_bazel_rules_nodejs//:defs.bzl", "check_bazel_version")
check_bazel_version("0.11.0")
```

Args:
minimum_bazel_version: a string indicating the minimum version
message: optional string to print to your users, could be used to help them update
"""
if "bazel_version" not in dir(native):
fail("\nCurrent Bazel version is lower than 0.2.1, expected at least %s\n" %
minimum_bazel_version)
elif not native.bazel_version:
print("\nCurrent Bazel is not a release version, cannot check for " +
"compatibility.")
print("Make sure that you are running at least Bazel %s.\n" % minimum_bazel_version)
else:
if not check_version(native.bazel_version, minimum_bazel_version):
fail("\nCurrent Bazel version is {}, expected at least {}\n{}".format(
native.bazel_version, minimum_bazel_version, message))
"""
Verify the users Bazel version is at least the given one.

This should be called from the `WORKSPACE` file so that the build fails as
early as possible. For example:

```
# in WORKSPACE:
load("@build_bazel_rules_nodejs//:defs.bzl", "check_bazel_version")
check_bazel_version("0.11.0")
```

Args:
minimum_bazel_version: a string indicating the minimum version
message: optional string to print to your users, could be used to help them update
"""
if "bazel_version" not in dir(native):
fail("\nCurrent Bazel version is lower than 0.2.1, expected at least %s\n" %
minimum_bazel_version)
elif not native.bazel_version:
print("\nCurrent Bazel is not a release version, cannot check for " +
"compatibility.")
print("Make sure that you are running at least Bazel %s.\n" % minimum_bazel_version)
elif not check_version(native.bazel_version, minimum_bazel_version):
fail("\nCurrent Bazel version is {}, expected at least {}\n{}".format(
native.bazel_version,
minimum_bazel_version,
message,
))