-
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
java_binary picks an non-configurable working directory #2579
Comments
Hm it should be retrieved from
What did you mean by this? Can you elaborate? Isn't the data file always on the same path relative to the directory that contains the WORKSPACE file? |
I've provided a more complex example in the In the
import java.nio.file.*;
public class Config {
public static String getConfig() throws Exception {
Path path = Paths.get("./data.txt").toAbsolutePath();
return new String(Files.readAllBytes(path), "UTF-8");
}
}
public class X {
public static void main(String[] args) throws Exception {
System.out.println(Config.getConfig());
}
}
So this works:
But neither |
I renamed the ticket from "undesirable" to "non-configurable" (because I actually agree that the default working directory is correct). Which is to say, what I think I want is some kind of parameter to Then I could update java_binary(
name='x',
srcs=glob(['src/main/java/**']),
deps=['//config'],
main_class='X',
data=['data.txt'],
run_directory='./x',
) And then |
Related to #3325. Bazel run is specified to run the binary in its runfiles directory. I'm not convinced that we should make this configurable, or change it otherwise. If the file is shared between multiple projects, it's your job to make sure that it's in the same runfiles location in all cases, and I don't see why you can't do that. Actually, expecting a specific working directory for your binary seems odd in the first place - maybe you should put the file into resources, or make sure to resolve the file relative to the location of your binary ($0 in unix terms). What do you want to happen if you run the binary directly? |
We have no plans to make that directory configurable. |
Consider this git repository. https://github.com/dfabulich/java-binary-runfiles
It contains a blank
WORKSPACE
and anx
subdirectory, containing a one-line "Hello world!"data.txt
file and asrc/main/java
subdirectory containingX.java
.X.java
looks in the current directory fordata.txt
and prints it to the console.If I
cd
to thex
subdirectory andbazel run x
, it fails:That line is long, but if you scroll right you'll see it's looking in
x/x.runfiles/__main__/./data.txt
, butdata.txt
is really in the__main__/x
subdirectory.In this trivial example, it would be straightforward to change
X.java
to read from./x/data.txt
instead of./data.txt
, but in my actual environment, the file-read code is shared between multiple projects, so the Java file can't know which subdirectory to use at compile time; this is something that I would learn at runtime.What I'd want is some way to say in my
java_binary
declaration, "don't run from the__main__
directory; run from the__main__/x
directory."Is there any way to do this?
Environment info
Operating System:
macOS 10.12.3
Bazel version (output of
bazel info release
):release 0.4.4
The text was updated successfully, but these errors were encountered: