Skip to content

Commit 5d995ed

Browse files
authored
fix(cli/mobile): normalize gradlew CRLF on Windows hosts too (#16017)
* fix(cli/mobile): normalize gradlew CRLF on Windows hosts too ensure_gradlew was gated behind #[cfg(unix)], so the CRLF-to-LF normalization only ran on unix hosts. The CRLF rewrite itself is platform-neutral: a CRLF gradlew breaks sh ./gradlew on every host, including Git-Bash on Windows. Split the function so the unix-only exec-bit fix stays gated while the LF rewrite runs everywhere. This unblocks Windows hosts that previously failed at the gradlew invocation with "'gradlew' is not recognized as an internal or external command" when gradlew carried CRLF endings. * fix(cli/mobile): only rewrite gradlew when CRLF is actually present OCR review: the unconditional rewrite churned the file's mtime on every tauri android dev/build even when it was already LF, and could fail on read-only or AV-locked files. Gate the write on the CRLF check. * style(cli/mobile): collapse fs::write args per rustfmt * refactor(cli/mobile): make gradlew CRLF normalization best-effort Hoist gradlew_path above the cfg(unix) block and downgrade CRLF normalization failures to warnings: on Windows the CLI invokes gradlew.bat, so a locked or read-only gradlew must not abort builds that would otherwise succeed. Read failures are logged at debug level. * docs(changes): add change file for gradlew CRLF normalization fix * refactor(cli/mobile): keep hard error on unix, warn only on Windows On unix a gradlew that cannot be rewritten will fail the build anyway with a cryptic sh error, so restore the previous fs_context errors there; on Windows the CLI invokes gradlew.bat, so failures only warn. Also reword the comment to plainly state why the rewrite runs on all platforms and reference this PR.
1 parent 127aa17 commit 5d995ed

2 files changed

Lines changed: 44 additions & 18 deletions

File tree

‎.changes/cli-gradlew-crlf.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
Normalize `gen/android/gradlew` CRLF line endings to LF on all host platforms, not only Unix. A `gradlew` checked out with CRLF broke `sh ./gradlew` on Windows hosts using Git Bash. The rewrite only runs when a CRLF is actually present. A failure to rewrite aborts on Unix, where the script is executed directly; on Windows the CLI invokes `gradlew.bat`, so failures there only warn.

‎crates/tauri-cli/src/mobile/mod.rs‎

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,6 @@ fn ensure_init(
492492
.join("app/src/main/java")
493493
.join(tauri_config.identifier.replace('.', "/").replace('-', "_"));
494494
if java_folder.exists() {
495-
#[cfg(unix)]
496495
ensure_gradlew(&project_dir)?;
497496
} else {
498497
project_outdated_reasons
@@ -585,26 +584,47 @@ fn ensure_init(
585584
Ok(())
586585
}
587586

588-
#[cfg(unix)]
589587
fn ensure_gradlew(project_dir: &std::path::Path) -> Result<()> {
590-
use std::os::unix::fs::PermissionsExt;
591-
592588
let gradlew_path = project_dir.join("gradlew");
593-
if let Ok(metadata) = gradlew_path.metadata() {
594-
let mut permissions = metadata.permissions();
595-
let is_executable = permissions.mode() & 0o111 != 0;
596-
if !is_executable {
597-
permissions.set_mode(permissions.mode() | 0o111);
598-
std::fs::set_permissions(&gradlew_path, permissions)
599-
.fs_context("failed to mark gradlew as executable", gradlew_path.clone())?;
589+
590+
#[cfg(unix)]
591+
{
592+
use std::os::unix::fs::PermissionsExt;
593+
594+
if let Ok(metadata) = gradlew_path.metadata() {
595+
let mut permissions = metadata.permissions();
596+
let is_executable = permissions.mode() & 0o111 != 0;
597+
if !is_executable {
598+
permissions.set_mode(permissions.mode() | 0o111);
599+
std::fs::set_permissions(&gradlew_path, permissions)
600+
.fs_context("failed to mark gradlew as executable", &gradlew_path)?;
601+
}
602+
}
603+
}
604+
605+
// A gradlew with CRLF line endings cannot run: sh fails with
606+
// "/usr/bin/env: 'sh\r': No such file or directory" or similar, which
607+
// also happens under Git Bash on Windows, so the rewrite runs on all
608+
// platforms (https://github.com/tauri-apps/tauri/pull/16017). Windows
609+
// builds invoke gradlew.bat, so there a gradlew that cannot be
610+
// rewritten only draws a warning; on unix the error is returned.
611+
if gradlew_path.exists() {
612+
let result = std::fs::read_to_string(&gradlew_path)
613+
.fs_context("failed to read gradlew", &gradlew_path)
614+
.and_then(|contents| {
615+
if contents.contains("\r\n") {
616+
std::fs::write(&gradlew_path, contents.replace("\r\n", "\n"))
617+
.fs_context("failed to replace gradlew CRLF with LF", &gradlew_path)
618+
} else {
619+
Ok(())
620+
}
621+
});
622+
#[cfg(unix)]
623+
result?;
624+
#[cfg(not(unix))]
625+
if let Err(error) = result {
626+
log::warn!("failed to normalize gradlew line endings: {error}");
600627
}
601-
std::fs::write(
602-
&gradlew_path,
603-
std::fs::read_to_string(&gradlew_path)
604-
.fs_context("failed to read gradlew", gradlew_path.clone())?
605-
.replace("\r\n", "\n"),
606-
)
607-
.fs_context("failed to replace gradlew CRLF with LF", gradlew_path)?;
608628
}
609629

610630
Ok(())

0 commit comments

Comments
 (0)