rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

crate_universe: `crate.spec` does not support git tags

Open uhthomas opened this issue 2 years ago • 3 comments

rules_rust: 0.2.1

Given

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains()

load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")

crate_universe_dependencies()

load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "crate")

crates_repository(
    name = "crate_index",
    lockfile = "//:Cargo.Bazel.lock",
    packages = {
        "regex": crate.spec(
            git = "https://github.com/rust-lang/regex",
            rev = "1.5.5",
        )
    },
)

load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()

The crate will fail to build.

❯ bazel build @crate_index//:regex
(18:01:03) INFO: Current date is 2022-04-14
(18:01:05) INFO: Repository crate_index__regex-1.5.5 instantiated at:
  /Users/thomas/code/<redacted>/WORKSPACE:166:19: in <toplevel>
  /private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/crate_index/defs.bzl:392:10: in crate_repositories
  /private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/utils.bzl:233:18: in maybe
Repository rule new_git_repository defined at:
  /private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git.bzl:186:37: in <toplevel>
(18:01:05) ERROR: An error occurred during the fetch of repository 'crate_index__regex-1.5.5':
   Traceback (most recent call last):
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git.bzl", line 174, column 30, in _new_git_repository_implementation
                update = _clone_or_update(ctx)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git.bzl", line 36, column 20, in _clone_or_update
                git_ = git_repo(ctx, directory)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 91, column 12, in git_repo
                _update(ctx, git_repo)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 104, column 10, in _update
                reset(ctx, git_repo)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 148, column 9, in reset
                _git(ctx, git_repo, "reset", "--hard", git_repo.reset_ref)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 169, column 15, in _git
                _error(ctx.name, start + list(args), st.stderr)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 190, column 9, in _error
                fail("error running '%s' while working with @%s:\n%s" % (command_text, name, stderr))
Error in fail: error running 'git reset --hard 1.5.5' while working with @crate_index__regex-1.5.5:
fatal: ambiguous argument '1.5.5': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
(18:01:05) ERROR: /Users/thomas/code/<redacted>/WORKSPACE:166:19: fetching new_git_repository rule //external:crate_index__regex-1.5.5: Traceback (most recent call last):
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git.bzl", line 174, column 30, in _new_git_repository_implementation
                update = _clone_or_update(ctx)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git.bzl", line 36, column 20, in _clone_or_update
                git_ = git_repo(ctx, directory)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 91, column 12, in git_repo
                _update(ctx, git_repo)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 104, column 10, in _update
                reset(ctx, git_repo)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 148, column 9, in reset
                _git(ctx, git_repo, "reset", "--hard", git_repo.reset_ref)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 169, column 15, in _git
                _error(ctx.name, start + list(args), st.stderr)
        File "/private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 190, column 9, in _error
                fail("error running '%s' while working with @%s:\n%s" % (command_text, name, stderr))
Error in fail: error running 'git reset --hard 1.5.5' while working with @crate_index__regex-1.5.5:
fatal: ambiguous argument '1.5.5': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
(18:01:05) ERROR: /private/var/tmp/_bazel_thomas/796dd52d4d981101108548a9446ddca6/external/crate_index/BUILD.bazel:28:6: @crate_index//:regex depends on @crate_index__regex-1.5.5//:regex in repository @crate_index__regex-1.5.5 which failed to fetch. no such package '@crate_index__regex-1.5.5//': error running 'git reset --hard 1.5.5' while working with @crate_index__regex-1.5.5:
fatal: ambiguous argument '1.5.5': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
(18:01:05) ERROR: Analysis of target '@crate_index//:regex' failed; build aborted: Analysis failed
(18:01:05) INFO: Elapsed time: 2.836s
(18:01:05) INFO: 0 processes.
(18:01:05) FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured\
)

This is available on crates.io, and works. I'm building from source as we need to use regex-capi.

uhthomas avatar Apr 14 '22 17:04 uhthomas

Thanks for filing this issue! The problem here is that you're passing a tag to rev and not a revision or commit. The following diff should fix this issue:

diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel
index 87af91f..badb996 100644
--- a/WORKSPACE.bazel
+++ b/WORKSPACE.bazel
@@ -25,8 +25,8 @@ crates_repository(
     lockfile = "//:Cargo.Bazel.lock",
     packages = {
         "regex": crate.spec(
-            git = "https://github.com/rust-lang/regex",
-            rev = "1.5.5",
+            git = "https://github.com/rust-lang/regex.git",
+            rev = "d130381b150756ba7e5940efdc6ebdf47f4febc0",
         )
     },
 )

I don't think it'd be terribly hard to add support for tag or branch if you wanted to take a stab at a PR? I'd be happy to review it.

UebelAndre avatar Apr 15 '22 13:04 UebelAndre

Ah, makes sense. Thank you! I also found that revisions for commits do work. I'll see about cutting a PR for working with tags too.

uhthomas avatar Apr 15 '22 15:04 uhthomas

Ah, makes sense. Thank you! I also found that revisions for commits do work. I'll see about cutting a PR for working with tags too.

Thanks! I'll leave this open as a feature request for other means of fetching crates via git.

UebelAndre avatar Apr 15 '22 16:04 UebelAndre