[ruby-core:109612] [Ruby master Bug#18970] CRuby adds an invalid header to bin/bundle (and others) which makes it unusable in Bash on Windows
From:
"Eregon (Benoit Daloze)" <noreply@...>
Date:
2022-08-21 11:38:43 UTC
List:
ruby-core #109612
Issue #18970 has been updated by Eregon (Benoit Daloze).
I also noted from the run above, on 2.6-3.0:
```
# Output from 2.6
$ ls -l $(dirname $(which ruby))
total 3627
-rw-r--r-- 1 runneradmin None 672 Apr 19 20:27 bundle.cmd
-rw-r--r-- 1 runneradmin None 674 Apr 19 20:27 bundler.cmd
-rwxr-xr-x 1 runneradmin None 5086 Apr 19 20:27 erb
-rw-r--r-- 1 runneradmin None 5228 Apr 19 20:27 erb.cmd
-rwxr-xr-x 1 runneradmin None 546 Apr 19 20:27 gem
-rw-r--r-- 1 runneradmin None 688 Apr 19 20:27 gem.cmd
-rwxr-xr-x 1 runneradmin None 508 Apr 19 20:27 irb
-rw-r--r-- 1 runneradmin None 685 Apr 19 20:27 irb.cmd
-rwxr-xr-x 1 runneradmin None 502 Aug 21 11:20 rake
-rw-r--r-- 1 runneradmin None 41 Aug 21 11:20 rake.bat
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:27 rake.cmd
-rwxr-xr-x 1 runneradmin None 514 Apr 19 20:27 rdoc
-rw-r--r-- 1 runneradmin None 656 Apr 19 20:27 rdoc.cmd
-rwxr-xr-x 1 runneradmin None 510 Apr 19 20:27 ri
-rw-r--r-- 1 runneradmin None 652 Apr 19 20:27 ri.cmd
-rw-r--r-- 1 runneradmin None 694 Apr 19 20:27 ridk.cmd
-rw-r--r-- 1 runneradmin None 876 Apr 19 20:27 ridk.ps1
-rwxr-xr-x 1 runneradmin None 34304 Apr 19 20:27 ruby.exe
drwxr-xr-x 1 runneradmin None 0 Apr 19 20:27 ruby_builtin_dlls
-rwxr-xr-x 1 runneradmin None 34304 Apr 19 20:27 rubyw.exe
-rw-r--r-- 1 runneradmin None 312 Apr 19 20:27 setrbvars.cmd
-rwxr-xr-x 1 runneradmin None 3597824 Apr 19 20:27 x64-msvcrt-ruby260.dll
```
vs on 3.1-master:
```
$ ls -l $(dirname $(which ruby))
total 4148
-rw-r--r-- 1 runneradmin None 707 Apr 19 20:22 bundle
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:22 bundle.bat
-rw-r--r-- 1 runneradmin None 709 Apr 19 20:22 bundler
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:22 bundler.bat
-rw-r--r-- 1 runneradmin None 668 Apr 19 20:22 erb
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:22 erb.bat
-rwxr-xr-x 1 runneradmin None 546 Apr 19 20:22 gem
-rw-r--r-- 1 runneradmin None 689 Apr 19 20:22 gem.cmd
-rw-r--r-- 1 runneradmin None 668 Apr 19 20:22 irb
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:22 irb.bat
-rw-r--r-- 1 runneradmin None 674 Apr 19 20:22 racc
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:22 racc.bat
-rw-r--r-- 1 runneradmin None 674 Apr 19 20:22 rake
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:22 rake.bat
-rw-r--r-- 1 runneradmin None 668 Apr 19 20:22 rbs
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:22 rbs.bat
-rw-r--r-- 1 runneradmin None 674 Apr 19 20:22 rdoc
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:22 rdoc.bat
-rw-r--r-- 1 runneradmin None 670 Apr 19 20:22 ri
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:22 ri.bat
-rw-r--r-- 1 runneradmin None 694 Apr 19 20:22 ridk.cmd
-rw-r--r-- 1 runneradmin None 876 Apr 19 20:22 ridk.ps1
-rwxr-xr-x 1 runneradmin None 35840 Apr 19 20:22 ruby.exe
drwxr-xr-x 1 runneradmin None 0 Apr 19 20:22 ruby_builtin_dlls
-rwxr-xr-x 1 runneradmin None 35840 Apr 19 20:22 rubyw.exe
-rw-r--r-- 1 runneradmin None 312 Apr 19 20:22 setrbvars.cmd
-rw-r--r-- 1 runneradmin None 698 Apr 19 20:22 typeprof
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:22 typeprof.bat
-rwxr-xr-x 1 runneradmin None 4140544 Apr 19 20:22 x64-ucrt-ruby310.dll
```
So `erb/gem/irb/rdoc/ri` are all seen as executables on 2.6-3.0, and they all start with `#!/usr/bin/env ruby` (so no header).
`rake` has the header on 2.6-3.0 though and is seen as executable:
```
-rw-r--r-- 1 runneradmin None 672 Apr 19 20:22 bundle.cmd
...
-rwxr-xr-x 1 runneradmin None 519 Aug 21 11:19 rake
-rw-r--r-- 1 runneradmin None 41 Aug 21 11:19 rake.bat
-rw-r--r-- 1 runneradmin None 41 Apr 19 20:22 rake.cmd
```
So maybe the `.cmd` is needed to be seen as executable?
On 3.1-master, none of the bin/* are seen as executable and all have the header.
Except for `gem` which is seen as executable and has no header.
----------------------------------------
Bug #18970: CRuby adds an invalid header to bin/bundle (and others) which makes it unusable in Bash on Windows
https://bugs.ruby-lang.org/issues/18970#change-98796
* Author: Eregon (Benoit Daloze)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
Same as https://github.com/oneclick/rubyinstaller2/issues/299, but I figured it's extremely likely to be a bug in CRuby and not in RubyInstaller2.
The original user issue is: https://github.com/ruby/setup-ruby/issues/371.
`bundle` does not work in a Bash shell on Windows -- without an extra `gem install bundler` --, and the reason is building CRuby on Windows either does not produce a `bin/bundle` or it has the wrong permissions and the wrong start.
I downloaded all latest releases from https://github.com/ruby/setup-ruby/blob/master/windows-versions.json and extracted them (I'm on Linux FWIW).
```
$ ls
rubyinstaller-2.4.10-1-x64 rubyinstaller-2.6.10-1-x64 rubyinstaller-3.0.4-1-x64 rubyinstaller-head-x64 ruby-mswin
rubyinstaller-2.4.10-1-x64.7z rubyinstaller-2.6.10-1-x64.7z rubyinstaller-3.0.4-1-x64.7z rubyinstaller-head-x64.7z ruby-mswin.7z
rubyinstaller-2.5.9-1-x64 rubyinstaller-2.7.6-1-x64 rubyinstaller-3.1.2-1-x64 ruby-mingw ruby-ucrt
rubyinstaller-2.5.9-1-x64.7z rubyinstaller-2.7.6-1-x64.7z rubyinstaller-3.1.2-1-x64.7z ruby-mingw.7z ruby-ucrt.7z
```
Of course only Ruby 2.7+ ships with Bundler, so for <=2.6 it's expected to be missing.
```
$ ls -l */bin/bundle
-rw-r--r--. 1 eregon eregon 707 Apr 19 22:22 rubyinstaller-3.1.2-1-x64/bin/bundle
-rw-r--r--. 1 eregon eregon 707 Aug 19 22:40 rubyinstaller-head-x64/bin/bundle
-rw-rw-rw-. 1 eregon eregon 564 Aug 20 11:15 ruby-mingw/bin/bundle
-rw-rw-rw-. 1 eregon eregon 829 Aug 20 11:09 ruby-mswin/bin/bundle
-rw-rw-rw-. 1 eregon eregon 564 Aug 20 11:20 ruby-ucrt/bin/bundle
```
So only 3.1 and head have bin/bundle.
But those 2 bin/bundle do not have the executable bit set.
They also start like this which sounds invalid for Bash:
```
$ cat rubyinstaller-3.1.2-1-x64/bin/bundle
:""||{ ""=> %q<-*- ruby -*-
@"%~dp0ruby" -x "%~f0" %*
@exit /b %ERRORLEVEL%
};{ #
bindir="${0%/*}" #
exec "$bindir/ruby" "-x" "$0" "$@" #
>,
}
#!/usr/bin/env ruby
#
# This file was generated by RubyGems.
...
```
On https://github.com/eregon/setup-ruby/runs/7843304711?check_suite_focus=true we can see 3.0, 3.1 and head fail for `echo ~ && which -a bundle` in bash.
2.7 avoids the issue in that CI run because the Bundler version is considered too old by setup-ruby and so `gem install bundler` is done there.
## Needed fix
In general, I think it is *very* important that CRuby does NOT modify files in `bin/`, and so that they are exactly the same as when RubyGems would write them when installing the corresponding gem.
This has been a problem not only here but also in these two other issues:
* `gem install bundler` fails on Windows, needs `--force` to workaround: https://github.com/rubygems/rubygems/issues/5245
* `gem install bundler` fails on `--enable-load-relative` Rubies: https://github.com/ruby/setup-ruby/issues/98#issuecomment-719950719
`tool/rbinstall.rb` seems to be responsible for changing the `bin/` files and therefore causing those bugs:
https://github.com/ruby/ruby/blob/209631a45f9682dedf718f4b4a140efe7d21a6fc/tool/rbinstall.rb#L487
Can we remove that?
Not that this issue cannot be solved in RubyGems, it's CRuby breaking `bin/bundle` (and others) in Bash on Windows.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>