Stepan Dyatkovskiy
Stepan Dyatkovskiy

Reputation: 1000

Bazel + C++ project: how to specify working directory for 'run' command

I'm trying to run a bazel target with bazel run command from workspace root. And I want to pass relative path as an argument to this target. Currently it doesn't recognize this pass, whilst manual run from console works fine.

So I conclude that bazel`s working dir differs from workspace root at this point .

I have checked for bazel docs, google, and didn't find an explicit way to specify working directory for run command. Does anybody know proper way to do that? Thanks!

P.S.: Namely I'm trying to run mediapipe iris example, which I build with following command (from project root):

bazel build -c opt --define MEDIAPIPE_DISABLE_GPU=1 mediapipe/examples/desktop/iris_tracking:iris_tracking_cpu

Then if I run it manually (also from project root) it works fine:

GLOG_logtostderr=1 bazel-bin/mediapipe/examples/desktop/hand_tracking/hand_tracking_gpu   --calculator_graph_config_file=mediapipe/graphs/hand_tracking/hand_tracking_mobile.pbtxt

Now if I run it with bazel run it fails:

bazel run -c dbg --define MEDIAPIPE_DISABLE_GPU=1 mediapipe/examples/desktop/iris_tracking:iris_tracking_cpu -- --calculator_graph_config_file=mediapipe/graphs/iris_tracking/iris_tracking_cpu.pbtxt

Not, that using $PWD in front of graph path doesn't help, for mediapipe engine still relies on relative path and proper CWD value.

Upvotes: 5

Views: 8663

Answers (2)

sdgfsdh
sdgfsdh

Reputation: 37045

If you are using sh_binary then you can work around this with a script like so:

#!/bin/bash

set -e

LAUNCH_WORKING_DIRECTORY=$(pwd)

if test "${BUILD_WORKING_DIRECTORY+x}"; then
  cd $BUILD_WORKING_DIRECTORY
fi

$LAUNCH_WORKING_DIRECTORY/tool.sh "${@:1}"
sh_binary(
  name = "tool",
  srcs = [ "launcher.sh" ],
  data = [ "tool.sh" ],
)

Bazel makes this absurdly complicated, it must be said.

Upvotes: 2

Benjamin Peterson
Benjamin Peterson

Reputation: 20520

bazel run executes the binary within its runfiles tree, and there is official no way to change that. Possible solutions if this is a problem:

  • Don't use bazel run; execute the binary directly.
  • bazel run puts the working directory it was executed from in the $BUILD_WORKING_DIRECTORY environmental variable, so the binary can resolve paths relative to that if desired.
  • On platforms with a POSIX shell, there is a hacky workaround: bazel run --run_under="cd $PWD &&" //some:target

Upvotes: 11

Related Questions