Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(java): pass javac its sources in an argument file, so Windows' command line limit isn't hit (#1295)

jakalijey4e00fcc11068
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 861cc30d..4bf003cb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+- Java `record`, `diff` and the other commands that compile the driver pass javac the source
+ files in an argument file instead of on the command line, so a project with hundreds of
+ sources no longer fails on Windows with `os error 206` (#1292 by @jakalijey).
- `diff` no longer calls a library stale on a file system that keeps only whole seconds (a crate
on a Windows drive under WSL): a library dated on a whole second is compared with the sources
by seconds, so the `Cargo.lock` its build wrote earlier in that second is not newer (#1293 by
diff --git a/src/harness_java.rs b/src/harness_java.rs
index 41a52b83..183548d4 100644
--- a/src/harness_java.rs
+++ b/src/harness_java.rs
@@ -84,13 +84,19 @@ fn compile(crate_dir: &Path, m: &Manifest, rust: bool) -> Result<PathBuf> {
);
}
let mut javac = Command::new("javac");
- javac.args(["-encoding", "UTF-8", "-nowarn"]);
+ javac.args(["-J-Dfile.encoding=UTF-8", "-encoding", "UTF-8", "-nowarn"]);
let user = user_classpath();
if !user.is_empty() {
javac.arg("-cp").arg(std::env::join_paths(user)?);
}
+ // The sources go in an argument file: a mid-size project's paths on the command line pass
+ // Windows' 32,767-character limit (os error 206).
+ let argfile = classes.with_extension("javac-sources");
+ std::fs::write(&argfile, javac_argfile(&sources))?;
+ let mut at = std::ffi::OsString::from("@");
+ at.push(&argfile);
run(
- javac.arg("-d").arg(&classes).args(&sources),
+ javac.arg("-d").arg(&classes).arg(at),
if rust {
"javac (driver and wrapper classes)"
} else {
@@ -112,6 +118,20 @@ fn compile(crate_dir: &Path, m: &Manifest, rust: bool) -> Result<PathBuf> {
Ok(classes)
}
+/// An argument file for javac naming `sources`, one per line. Each path is quoted, so a space
+/// in it stays part of it, and inside quotes javac reads `\` as an escape (`C:\src\tdir` would
+/// hold a tab), so a backslash and a quote are escaped.
+fn javac_argfile(sources: &[PathBuf]) -> String {
+ let mut out = String::new();
+ for p in sources {
+ let path = p.to_string_lossy();
+ out.push('"');
+ out.push_str(&path.replace('\\', "\\\\").replace('"', "\\\""));
+ out.push_str("\"\n");
+ }
+ out
+}
+
/// Whether javac failed on types it could not find, as a Gradle or Maven module compiled
/// without its dependencies does.
fn missing_classes(javac: &str) -> bool {
@@ -264,6 +284,53 @@ pub fn bench(crate_dir: &Path, m: &Manifest, min_ms: u64) -> Result<Vec<BenchRow
mod tests {
use super::*;
+ #[test]
+ fn the_javac_argfile_quotes_and_escapes_each_path() {
+ let sources = [
+ PathBuf::from("src/a b/A.java"),
+ PathBuf::from(r"C:\src\tdir\B.java"),
+ ];
+ assert_eq!(
+ javac_argfile(&sources),
+ "\"src/a b/A.java\"\n\"C:\\\\src\\\\tdir\\\\B.java\"\n"
+ );
+ }
+
+ /// javac reads the file back as the paths written, a space and (on Windows) backslashes
+ /// included: unescaped, `\t` in `tdir` would be a tab and the file not found.
+ #[test]
+ fn javac_compiles_the_sources_an_argfile_names() {
+ if Command::new("javac").arg("-version").output().is_err() {
+ return;
+ }
+ let root = std::env::temp_dir().join(format!(
+ "rust-anything-javac-argfile-{}",
+ std::process::id()
+ ));
+ let _ = std::fs::remove_dir_all(&root);
+ let src = root.join("a b").join("tdir");
+ std::fs::create_dir_all(&src).unwrap();
+ let file = src.join("A.java");
+ std::fs::write(&file, "public class A {}\n").unwrap();
+ let argfile = root.join("sources");
+ std::fs::write(&argfile, javac_argfile(&[file])).unwrap();
+ let mut at = std::ffi::OsString::from("@");
+ at.push(&argfile);
+ let out = Command::new("javac")
+ .arg("-d")
+ .arg(root.join("out"))
+ .arg(at)
+ .output()
+ .unwrap();
+ assert!(
+ out.status.success(),
+ "{}",
+ String::from_utf8_lossy(&out.stderr)
+ );
+ assert!(root.join("out").join("A.class").exists());
+ let _ = std::fs::remove_dir_all(&root);
+ }
+
#[test]
fn missing_classes_spots_the_two_javac_errors_a_classpath_fixes() {
assert!(missing_classes(