Passing variables from .bazelrc into WORKSPACE

644 views
Skip to first unread message

Robert Cauble

unread,
Sep 16, 2022, 12:42:04 PM9/16/22
to bazel-discuss
Hi,

Newbie question here. What's the easiest way to define a custom variable in .bazelrc and then reference within WORKSPACE?

For context, I'm experimenting with moving a project from maven to bazel and our maven repository is password-protected. I have something like this in my WORKSPACE:

maven_install(
  ...

  repositories = [
  ]
)

It works, but the problem is we need to externalize the user:pass portion of it as this is shared among users. Would like to be able to do something like this:

maven_install(
  ...
  repositories = [
  ]
)  

Thanks,
Rob

Alex Humesky

unread,
Sep 16, 2022, 3:48:25 PM9/16/22
to Robert Cauble, bazel-discuss
There isn't really a way to add variables from the .bazelrc files (or really, from command line flags, since .bazelrc files basically contain flags) to the WORKSPACE file (though there are ways to add new command line flags that can be read in BUILD / bzl files, and WORKSPACE / repository rules can read environment variables).

But it looks like the way to do this may be to use Coursier's properties files:

"Credentials for private repositories can also be specified using a property file or environment variables. See the Coursier documentation for more information."


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/b6205fde-9f06-4d33-a96f-76c49c91182cn%40googlegroups.com.

Robert Cauble

unread,
Sep 16, 2022, 4:43:17 PM9/16/22
to Alex Humesky, bazel-discuss
Thank you for the pointer. I am not having luck with getting coursier properties setup. I just get a generic unauthorized message and no extra debug info (I don't know if it's even picking up my properties file).

You mentioned there was a way to add new command line flags. Do you have a pointer to that? I can give that a try instead.

Alex Humesky

unread,
Sep 16, 2022, 5:24:10 PM9/16/22
to Robert Cauble, bazel-discuss
The discussion here seemed to say that the property file ought to work:

You may need to manually refetch the repository (bazel fetch, or do a bazel clean) if you didn't try that already (bazel probably doesn't know anything about this property file so it doesn't know to refetch the repo).

For getting more debug info see

You might also try the COURSIER_CREDENTIALS environment variable:

and fetch the repository again (since maven_install doesn't declare COURSIER_CREDENTIALS in the list of dependent environment variables:
maybe it ought to)

You might also try adding --repo_env=COURSIER_CREDENTIALS
though the docs make me think that that ought not be necessary.

Regarding command line flags, they aren't available in WORKSPACE files, only in BUILD files (via config_setting() and select()) and bzl files (in rule implementations), so they can't really help here.


Robert Cauble

unread,
Sep 16, 2022, 8:41:16 PM9/16/22
to Alex Humesky, bazel-discuss
Thanks, I tried all of those but still no luck. Even the verbose logging didn't show any additional logging. 

I found one thing that  works:

1) Add load("//:repository.bzl", "USERPASS") to WORKSPACE
2) Change the repositories declaration to "https://"+USERPASS+"@maven.pkg..."
3) Add repository.bzl to .gitignore
4) Each user when they first clone a workspace does
echo 'USERPASS = "user:password"' >  repository.bzl 

(Not too terrible).

Austin Schuh

unread,
Sep 17, 2022, 12:27:52 AM9/17/22
to Robert Cauble, Alex Humesky, bazel-discuss
Is there a way to get .netrc working?
https://docs.bazel.build/versions/main/repo/http.html#http_archive-netrc
suggests it might just work, or you can override it.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAM-R0_oBdNR_HN0y3cJzE0gJoRG_cau%3DKWMf4VHFw3bFmyUprw%40mail.gmail.com.

Robert Cauble

unread,
Sep 17, 2022, 7:33:23 PM9/17/22
to Austin Schuh, Alex Humesky, bazel-discuss
This is working for me now with the Coursier properties as documented. The issue was that I was using a very old version of maven_install (2.8) based on the example here:

https://docs.bazel.build/versions/5.1.0/migrate-maven.html

Jingwen Chen

unread,
Sep 19, 2022, 4:38:37 AM9/19/22
to Robert Cauble, Austin Schuh, Alex Humesky, bazel-discuss
Reply all
Reply to author
Forward
0 new messages