Fix shell script line endings for Unix compatibility#7946
Fix shell script line endings for Unix compatibility#7946
Conversation
- Add fixScriptLineEndings task to convert CRLF to LF in generated scripts - Add filter to distribution copy of shell.sh to strip CR characters - Scripts generated by CreateStartScripts on Windows now work on Unix When built on Windows, Gradle's CreateStartScripts generates scripts with CRLF line endings which fail on Unix systems with 'bad interpreter' errors. This fix ensures all shell scripts have Unix-compatible LF line endings.
There was a problem hiding this comment.
Pull request overview
This PR fixes shell script line ending issues that occur when building on Windows. It adds a new Gradle task to convert CRLF line endings to LF in generated start scripts and applies a filter to ensure the shell.sh script has Unix-compatible line endings when copied to the distribution.
Changes:
- Added fixScriptLineEndings task to convert CRLF to LF in scripts generated by CreateStartScripts
- Added filter to strip CR characters from shell.sh during distribution copy
- Added fixScriptLineEndings as a dependency to packagePrepWork task
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| tasks.register('fixScriptLineEndings') { | ||
| description = 'Convert CRLF to LF in shell scripts for Unix compatibility.' | ||
| dependsOn createStartScripts |
There was a problem hiding this comment.
The fixScriptLineEndings task depends only on createStartScripts but references startScripts.outputDir on line 276. Since both the startScripts task (from the application plugin) and createStartScripts task write to the same output directory, this task should also declare a dependency on startScripts to ensure proper task ordering and that all scripts are present before line ending conversion occurs.
| dependsOn createStartScripts | |
| dependsOn createStartScripts | |
| dependsOn startScripts |
| description = 'Convert CRLF to LF in shell scripts for Unix compatibility.' | ||
| dependsOn createStartScripts | ||
| doLast { | ||
| // Fix scripts generated by createStartScripts | ||
| fileTree(startScripts.outputDir).each { file -> | ||
| if (!file.name.endsWith('.bat')) { | ||
| def content = file.text | ||
| if (content.contains('\r\n')) { | ||
| file.text = content.replaceAll('\r\n', '\n') |
There was a problem hiding this comment.
The line ending detection and replacement logic may not handle all cases correctly. The check on line 279 only detects CRLF sequences ('\r\n'), which means files with standalone CR characters ('\r') or mixed line endings won't be detected. Similarly, the replacement on line 280 only replaces CRLF with LF, leaving any standalone CR characters unchanged. Consider using a more comprehensive approach such as replacing all CR characters with file.text = content.replaceAll('\r', '') which will handle both CRLF (by removing the CR, leaving the LF) and standalone CR cases.
| description = 'Convert CRLF to LF in shell scripts for Unix compatibility.' | |
| dependsOn createStartScripts | |
| doLast { | |
| // Fix scripts generated by createStartScripts | |
| fileTree(startScripts.outputDir).each { file -> | |
| if (!file.name.endsWith('.bat')) { | |
| def content = file.text | |
| if (content.contains('\r\n')) { | |
| file.text = content.replaceAll('\r\n', '\n') | |
| description = 'Convert CRLF and CR line endings to LF in shell scripts for Unix compatibility.' | |
| dependsOn createStartScripts | |
| doLast { | |
| // Fix scripts generated by createStartScripts | |
| fileTree(startScripts.outputDir).each { file -> | |
| if (!file.name.endsWith('.bat')) { | |
| def content = file.text | |
| if (content.contains('\r')) { | |
| file.text = content.replaceAll('\r', '') |
When built on Windows, Gradle's CreateStartScripts generates scripts with CRLF line endings which fail on Unix systems with 'bad interpreter' errors. This fix ensures all shell scripts have Unix-compatible LF line endings.