-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
bazel run
child should run with bazel
invocation's current directory
#3325
Comments
As I said on the other bug, I'm not sure we should change this. If you want to run your binary from a different working directory, don't use bazel run? |
Closing this in favour of #2579; will comment there. |
Actually reopening this as I think they're not the same issue. I don't know that the behaviour requested in #2579 is desirable.
|
One workaround for your problem is to use absolute path for passed files. Say, I want to check in CI build the formatting of all java files in specific commit and I have defined this
and
Now, i can use
|
It lets you have runtime dependencies that are different than what's in your source tree. There are a couple of nice things this gets you:
One thing that our shell tests do that you might want to consider is starting scripts with "if |
I believe you can use
(Obviously doesn't work on Windows.) |
As you point out, you can always do this by looking them up in the runfiles by searching adjacent to the executable. Since the binary might be started in ways other than
Again this seems like a binary implementor's responsibility: they should look up runtime dependencies in runfiles. It seems wrong to rely on your binary only ever being called via Neither of these seem like reasons to have the child from
@davido, I first came across this with formatting tools as well!
This is a pretty annoying workaround. |
Well, at least for format checking only, you don't need any hacks, just use Bazel's
Now, to check the format for all files, would just be Failure:
Success:
|
I understand the current state, I think it's probably the right default choice. However there are lots of use cases where you really do want to know the place you were invoked from because it's a tool that modifies the source (formatters, automated fixers, refactoring tools etc) and at the moment the information is not recoverable. |
Bazel now has a |
--direct_run is deprecated because it's enabled by default now. |
Closes #467 PiperOrigin-RevId: 264859144
Closes bazel-contrib#467 PiperOrigin-RevId: 264859144
Closes bazel-contrib#467 PiperOrigin-RevId: 264859144
bazelbuild/bazel#3325 Tested: mkdir examples/tmp cd examples/tmp bazel run @com_benchsci_rules_kustomize//:kustomize -- create Confirm a `kustomize.yaml` file is created in the working directory. Previously, the tool would mutate the underlying runfiles directory.
bazelbuild/bazel#3325 Tested: mkdir examples/tmp cd examples/tmp bazel run @com_benchsci_rules_kustomize//:kustomize -- create Confirm a `kustomize.yaml` file is created in the working directory. Previously, the tool would mutate the underlying runfiles directory.
As others have said, resetting the working directory is a weird choice. The environment variables aren't reset. Executables have to locate runfiles independent of working dir. Fortunately, fixing this is quite simple. Add to .bazelrc: run --run_under='cd "$BUILD_WORKING_DIRECTORY" && exec' |
@bazel-io reopen |
Note, I documented a more useful workaround https://github.com/theoremlp/rules_multitool?tab=readme-ov-file#creating-convenience-scripts |
That is annoying (#10782). A workaround is to always set it: .bazelrc
tools/bazel/run.sh (executable) #!/usr/bin/env sh
[ -z "$BUILD_WORKING_DIRECTORY" ] || cd "$BUILD_WORKING_DIRECTORY"
exec "$@" tools/bazel/BUILD.bazel sh_binary(
name = "run",
srcs = ["run.sh"],
) (FYI I tried inlining the script into |
) It's common for users of multitool to want to run tools in the current working directory. In #26, @alexeagle documented a technique we've used for a while with creating a script and symlinking to it. Our internal copy of this script is a bit more complicated to help avoid expensive calls to Bazel that simple `bazel run` calls don't really need. More refinements have been proposed in #27. All of these things are fundamentally workarounds for bazelbuild/bazel#3325. To help simplify things, this PR adds a convenience wrapper that captures the execpath, switches to $BUILD_WORKING_DIRECTORY, and then runs the desired tool. The resulting shell script gets to use a very simple `bazel run`, should be compatible across any slew of Bazel options, and cache as well as any typical run target.
Description of the problem / feature request / question:
When using the
bazel run
command, the spawned child has a working directory different from the directory where the command is run. This leads tobazel run //path/to/target
andbazel-bin/path/to/target/target
being quite different. If possible, it would be ideal to have the child inherit thebazel
invocation's working directory.If possible, provide a minimal example to reproduce the problem:
Note difference between output of last two commands.
Environment info
bazel info release
):Have you found anything relevant by searching the web?
Original
bazel-discuss
thread: https://groups.google.com/d/msgid/bazel-discuss/CAA%3DsxTgFY9D1kyGaGKCuqUftUBAizNdXu28o_zsKRkgA3F-ZwAAnything else, information or logs or outputs that would be helpful?
The working directory seems to be set here to the runfiles dir:
bazel/src/main/java/com/google/devtools/build/lib/runtime/commands/RunCommand.java
Line 220 in 5e60e38
The text was updated successfully, but these errors were encountered: