use_default_shell_env and env in ctx.actions.run_shell

Skip to first unread message

sbag...@grailbio.com

unread,
Oct 13, 2017, 3:44:58 AM10/13/17
to bazel-discuss
Hello,

Are use_default_shell_env and env exclusive?
https://docs.bazel.build/versions/master/skylark/lib/actions.html#run_shell

My dict in env is discarded completely if I set use_default_shell_env to be True.

From the documentation, I thought that one can use both, and some sort of a union of the two environments will be defined for the action.

Thanks.

Sid

László Csomor

unread,
Oct 13, 2017, 10:00:14 AM10/13/17
to sbag...@grailbio.com, bazel-discuss
Hi Sid,

Yes, your observation is correct: use_default_shell_env=True makes the action ignore the `env` you provided and use a standardized environment instead.

Cheers,
Laszlo

--
László Csomor | Software Engineer | laszlo...@google.com

Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado


--
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-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/01f05a75-2c71-4501-849d-61738559242b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sbag...@grailbio.com

unread,
Oct 13, 2017, 10:05:54 AM10/13/17
to bazel-discuss
Is there a way to combine the two? I want to get PATH from the system but also define some more environment variables.

If not, I can also print out my extra environment variables in my script, but was hoping for a cleaner solution.

On Friday, October 13, 2017 at 7:00:14 AM UTC-7, László Csomor wrote:
> Hi Sid,
>
>
> Yes, your observation is correct: use_default_shell_env=True makes the action ignore the `env` you provided and use a standardized environment instead.
> https://github.com/bazelbuild/bazel/blob/80a34dc97799961201e6dce20fd58dd08022c032/src/main/java/com/google/devtools/build/lib/analysis/actions/SpawnAction.java#L725-L729
>
>
> Cheers,
> Laszlo
>
>
>
>
>
> --
> László Csomor | Software Engineer | laszlo...@google.com
>
> Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
> Registergericht und -nummer: Hamburg, HRB 86891
> Sitz der Gesellschaft: Hamburg
> Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
>
>
> On Fri, Oct 13, 2017 at 9:44 AM, <sbag...@grailbio.com> wrote:
> Hello,
>
>
>
> Are use_default_shell_env and env exclusive?
>
> https://docs.bazel.build/versions/master/skylark/lib/actions.html#run_shell
>
>
>
> My dict in env is discarded completely if I set use_default_shell_env to be True.
>
>
>
> From the documentation, I thought that one can use both, and some sort of a union of the two environments will be defined for the action.
>
>
>
> Thanks.
>
>
>
> Sid
>
>
>
> --
>
> 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.

László Csomor

unread,
Oct 13, 2017, 10:19:51 AM10/13/17
to sbag...@grailbio.com, bazel-discuss
Why do you need PATH inside the action?

If you intend to use a tool in the action, the tool should be an input of the action, otherwise Bazel doesn't know about the dependency and cannot guarantee build correctness.



--
László Csomor | Software Engineer | laszlo...@google.com

Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/6bba24a5-7cf1-4089-a7a9-d830d9a4b9eb%40googlegroups.com.

sbag...@grailbio.com

unread,
Oct 13, 2017, 10:33:26 AM10/13/17
to bazel-discuss
You are right, my builds are not exactly hermetic because I have not defined my own toolchain yet.

genrule exposes the PATH through use_default_shell_env so I thought maybe I could also do it in my rules.

László Csomor

unread,
Oct 13, 2017, 10:43:28 AM10/13/17
to sbag...@grailbio.com, bazel-discuss
For testing purposes or as a quick hack, you can use_default_shell_env=True and pass --client_env=FOO or --client_env=BAR=baz flags to Bazel. This will copy your $FOO envvar's value and set $BAR=baz in the action's environment. See https://github.com/bazelbuild/bazel/blob/80a34dc97799961201e6dce20fd58dd08022c032/src/main/java/com/google/devtools/build/lib/analysis/actions/SpawnAction.java#L918-L948

The long term, right fix is to add to your WORKSPACE file a new_local_repository rule for the tool: https://docs.bazel.build/versions/master/be/workspace.html#new_local_repository
If new_local_repository doesn't suit your needs, consider writing a custom repository rule. Repo rules are allowed to run non-hermetic actions. Hers' a simple example: https://github.com/bazelbuild/bazel/blob/80a34dc97799961201e6dce20fd58dd08022c032/src/test/shell/bazel/list_source_repository.bzl




--
László Csomor | Software Engineer | laszlo...@google.com

Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/aa63283b-3b5a-4864-828f-21ec008ab3ab%40googlegroups.com.

László Csomor

unread,
Oct 13, 2017, 10:46:08 AM10/13/17
to sbag...@grailbio.com, bazel-discuss
Sorry, I should've written --action_env instead of --client_env.


--
László Csomor | Software Engineer | laszlo...@google.com

Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

sbag...@grailbio.com

unread,
Oct 13, 2017, 10:53:40 AM10/13/17
to bazel-discuss
Thank you for the hints. I have linked this thread on my issue; will be useful when I start working on it.
https://github.com/grailbio/rules_r/issues/1
Reply all
Reply to author
Forward
0 new messages