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

pkg_zip doesn't seem to be working as one would expect it to #126

Closed
kwestb opened this issue Jan 14, 2020 · 2 comments
Closed

pkg_zip doesn't seem to be working as one would expect it to #126

kwestb opened this issue Jan 14, 2020 · 2 comments

Comments

@kwestb
Copy link

kwestb commented Jan 14, 2020

I'm trying to use pkg_zip but it doesn't seem to be working.

helloworld.txt:

Hello, World!

BUILD:

load("@rules_pkg//pkg:pkg.bzl", "pkg_zip")

pkg_zip(
    name = "helloworld",
    srcs = ["helloworld.txt"],
)

WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_pkg",
    sha256 = "5516557c11a0f9a7fa1eda94188c12029ce5ba851451d4481eda43ff2745c0f9",
    strip_prefix = "rules_pkg-0.2.4",
    urls = ["https://github.com/bazelbuild/rules_pkg/archive/0.2.4.zip"],
)

load("@rules_pkg//pkg:deps.bzl", "rules_pkg_dependencies", "rules_pkg_register_toolchains")
rules_pkg_dependencies()
rules_pkg_register_toolchains()

When I try to run bazel build //:helloworld, I get an error:

$ bazel build //:helloworld 
ERROR: /home/kwestb/pkg_zip_test/BUILD:3:1: every rule of type pkg_zip_impl implicitly depends upon the target '@rules_pkg//:build_zip', but this target could not be found because of: error loading package '@rules_pkg//': Unable to find package for @io_bazel_rules_go//go:def.bzl: The repository '@io_bazel_rules_go' could not be resolved.
ERROR: Analysis of target '//:helloworld' failed; build aborted: error loading package '@rules_pkg//': Unable to find package for @io_bazel_rules_go//go:def.bzl: The repository '@io_bazel_rules_go' could not be resolved.
INFO: Elapsed time: 0.557s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (5 packages loaded, 5 targets configured)

At first I thought this was simply an undeclared dependency on rules_go, so I added it to my WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "593a63be80bbee7313e361e91821a567b4b419b5f65473a95d39724f26d4503a",
    strip_prefix = "rules_go-0.21.0",
    urls = ["https://github.com/bazelbuild/rules_go/archive/v0.21.0.zip"],
)

http_archive(
    name = "rules_pkg",
    sha256 = "5516557c11a0f9a7fa1eda94188c12029ce5ba851451d4481eda43ff2745c0f9",
    strip_prefix = "rules_pkg-0.2.4",
    urls = ["https://github.com/bazelbuild/rules_pkg/archive/0.2.4.zip"],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()

load("@rules_pkg//pkg:deps.bzl", "rules_pkg_dependencies", "rules_pkg_register_toolchains")
rules_pkg_dependencies()
rules_pkg_register_toolchains()

But still no joy, this time with a different error:

$ bazel build //:helloworld 
ERROR: /home/kwestb/pkg_zip_test/BUILD:3:1: every rule of type pkg_zip_impl implicitly depends upon the target '@rules_pkg//:build_zip', but this target could not be found because of: error loading package '@rules_pkg//': Label '@rules_pkg//tools/update_deb_packages:update_deb_packages.bzl' is invalid because 'tools/update_deb_packages' is not a package; perhaps you meant to put the colon here: '@rules_pkg//:tools/update_deb_packages/update_deb_packages.bzl'?
ERROR: Analysis of target '//:helloworld' failed; build aborted: error loading package '@rules_pkg//': Label '@rules_pkg//tools/update_deb_packages:update_deb_packages.bzl' is invalid because 'tools/update_deb_packages' is not a package; perhaps you meant to put the colon here: '@rules_pkg//:tools/update_deb_packages/update_deb_packages.bzl'?
INFO: Elapsed time: 0.551s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (5 packages loaded, 5 targets configured)

Surely I must be missing something fundamental - can anyone point out the problem why I can't get this toy example to work with pkg_zip?

FYI, i'm running Bazel 2.0.0:

$ bazel info release
release 2.0.0
@nacl
Copy link
Collaborator

nacl commented Jan 14, 2020

Thanks for the detailed report!

Without having run your examples, the likely cause of your problem is the strip_prefix in the rules_pkg http_archive repo declaration: the pkg directory needs to stripped too, as that's where the WORKSPACE is.

I believe your issue can be resolved by tweaking your WORKSPACE snippet to look something like this:

http_archive(
    name = "rules_pkg",
    sha256 = "5516557c11a0f9a7fa1eda94188c12029ce5ba851451d4481eda43ff2745c0f9",
    strip_prefix = "rules_pkg-0.2.4/pkg",
    urls = ["https://github.com/bazelbuild/rules_pkg/archive/0.2.4.zip"],
)

load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies", "rules_pkg_register_toolchains")
rules_pkg_dependencies()
rules_pkg_register_toolchains()

Changes are in the strip_prefix attribute and the load call.

Honestly, rules_pkg's layout isn't following best practices, and we should change it (#111).

@kwestb
Copy link
Author

kwestb commented Jan 15, 2020

Thanks @nacl - your suggestion works. I had to make three edits, two in the WORKSPACE flle and one in the BUILD file:

diff --git a/BUILD b/BUILD
index c5adf22..8cb1722 100644
--- a/BUILD
+++ b/BUILD
@@ -1,4 +1,4 @@
-load("@rules_pkg//pkg:pkg.bzl", "pkg_zip")
+load("@rules_pkg//:pkg.bzl", "pkg_zip")
 
 pkg_zip(
     name = "helloworld",
diff --git a/WORKSPACE b/WORKSPACE
index 9aa5c56..1a5b27e 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -10,7 +10,7 @@ http_archive(
 http_archive(
     name = "rules_pkg",
     sha256 = "5516557c11a0f9a7fa1eda94188c12029ce5ba851451d4481eda43ff2745c0f9",
-    strip_prefix = "rules_pkg-0.2.4",
+    strip_prefix = "rules_pkg-0.2.4/pkg",
     urls = ["https://github.com/bazelbuild/rules_pkg/archive/0.2.4.zip"],
 )
 
@@ -18,6 +18,6 @@ load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_to
 go_rules_dependencies()
 go_register_toolchains()
 
-load("@rules_pkg//pkg:deps.bzl", "rules_pkg_dependencies", "rules_pkg_register_toolchains")
+load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies", "rules_pkg_register_toolchains")
 rules_pkg_dependencies()
 rules_pkg_register_toolchains()

Your suggestion to move the WORKSPACE file to the top-level is a good one; I would also suggest moving the http_archive call for io_bazel_rules_go to rules_pkg_dependencies() as well.

@kwestb kwestb closed this as completed Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants