Java binary startup script broken when targeting windows (cygwin)
- Dominant language
- Java
- Stars
- 25.8k
- Forks
- 4.6k
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 72
Description
### Description of the bug:
I am building Java binaries on Linux which target Windows. The workflow is essentially build an archive containing the runfiles for my binary, copy that onto windows, extract and then run via the standard startup script which looks like it is designed to support cygwin. However the script is broken in a number of ways when I try to run on cygwin.
### Which category does this issue belong to?
Java Rules
### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
I've created a minimal repository for reproducing the issue:
1. Clone https://github.com/th0masb/bazel-java-startup-bug on a Linux environment
2. Run `bazel build --platforms //:windows_x86_64 :dist.tar`
3. Copy the resulting dist.tar archive to a windows machine with cygwin
4. Unpack the dist.tar in cygwin
5. Execute the entrypoint script from cygwin, it should fail. Execute the script with `--classpath_limit=1`, it should fail with a different reason.
Sample output:
```
$ ./.runfiles/__main__/main
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
$ ./.runfiles/__main__/main --classpath_limit=1
./.runfiles/__main__/main: line 366: D:\Tools\Tom\.runfiles/remotejdk17_win/bin/java/jar.exe: No such file or directory
./.runfiles/__main__/main: ERROR: /cygdrive/d/Tools/Tom/./.runfiles/__main__/main failed because D:\Tools\Tom\.runfiles/remotejdk17_win/bin/java/jar.exe failed
```
### Which operating system are you running Bazel on?
Host + Execution Linux (Pop!_OS 22.04 LTS x86_64), target is Windows x86_64
### What is the output of `bazel info release`?
release 6.4.0
### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.
_No response_
### What's the output of `git remote get-url origin; git rev-parse master; git rev-parse HEAD` ?
_No response_
### Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.
Unsure
### Have you found anything relevant by searching the web?
No
### Any other information, logs, or outputs that you want to share?
I have identified how this could be fixed, the changes should be fairly minimal and low risk. I am happy to try and open a PR with a fix myself.
Contributor guide
Research direction
Start with the minimal repository at https://github.com/th0masb/bazel-java-startup-bug and reproduce the issue with `bazel build --platforms //:windows_x86_64 :dist.tar`, then run `.runfiles/__main__/main` under Cygwin. Trace the generated startup script and its `--classpath_limit=1` path; done means the entrypoint starts the Java binary and both reported Cygwin failures are resolved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, shell
- Domain
- build-system, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100