Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: gem can't load relative dependencies #20359

Closed
gouravkhunger opened this issue May 30, 2024 · 6 comments
Closed

[Bug]: gem can't load relative dependencies #20359

gouravkhunger opened this issue May 30, 2024 · 6 comments
Labels
android10bootstrap Issue related to using Android 10 bootstrap bug report Something is not working properly ruby Issue is about Ruby related stuff, including gem

Comments

@gouravkhunger
Copy link
Contributor

Problem description

Hi team, I have custom prefix packaged bootstraps built for android10. I install them on a local fork of the termux-app where I've changed the package name. All packages in the bootstrap work good so far, except the gem tool.

1|:/data/data/sh.gourav.jekyllex/files/home $ ruby --version
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [aarch64-linux-android]
1|:/data/data/sh.gourav.jekyllex/files/home $ gem -v
/data/data/sh.gourav.jekyllex/files/usr/lib/ruby/3.2.0/rubygems.rb:15:in `require_relative': cannot load such file -- /data/app/~~MgtAZLOFHbuuc198BXGOIg==/sh.gourav.jekyllex-vGlBDbZNe0Nx_qSZi9YAkA==/lib/arm64/rubygems/compatibility (LoadError)
        from /data/data/sh.gourav.jekyllex/files/usr/lib/ruby/3.2.0/rubygems.rb:15:in `<top (required)>'
        from <internal:gem_prelude>:2:in `require'
        from <internal:gem_prelude>:2:in `<internal:gem_prelude>'
1|:/data/data/sh.gourav.jekyllex/files/home $

Upon inspecting the bootstrap zip I do find the relevant file under /lib/ruby/3.2.0/rubygems/. But since it is a require_relative directive, I suppose the path isn't being found under app's file structure. Maybe there needs to be symlinks for it too.

What steps will reproduce the bug?

Build the bootstrap with required packages for ruby using:

./scripts/run-docker.sh ./clean.sh
./scripts/run-docker.sh ./scripts/build-bootstraps.sh --android10

What is the expected behavior?

In the original non-android10 termux app, the command works fine.

System information

Not sure if applicable.
@gouravkhunger gouravkhunger added bug report Something is not working properly untriaged labels May 30, 2024
@Grimler91
Copy link
Member

Could you please explain what require_relative means to non-ruby experts?
What path is it trying to load (load means import? Or dlopen? Or execute?), and what would be the correct (absolute) path?

@gouravkhunger
Copy link
Contributor Author

gouravkhunger commented May 31, 2024

@Grimler91 In ruby require imports code from a file and execute it so that the variables and functions become available to the current file. require_relative assumes the file to be loaded is relative to the current file.

In /data/data/<package>/files/usr/lib/ruby/3.2.0/rubygems.rb, there are require_relative statements:

# ...
require_relative "rubygems/compatibility"

require_relative "rubygems/defaults"
require_relative "rubygems/deprecate"
# ...

It expects that the compatibility.rb, defaults.rb file must be in /data/data/<package>/files/usr/lib/ruby/3.2.0/rubygems/*, which in fact are there when I inspect the bootstrap and the device file explorer.

But in android 10 gem is not able to find them as the binary is loaded from a dynamic path which is /data/app/~~MgtAZLOFHbuuc198BXGOIg==/sh.gourav.jekyllex-vGlBDbZNe0Nx_qSZi9YAkA==/lib/arm64/ here, but the required files aren't able to be loaded relatively.

@truboxl truboxl added ruby Issue is about Ruby related stuff, including gem and removed untriaged labels May 31, 2024
@truboxl
Copy link
Contributor

truboxl commented May 31, 2024

I think we need a new tag for tracking this issue

@Grimler91 Grimler91 added the android-10 Issue happens on devices running Android 10 label Jun 3, 2024
@Grimler91
Copy link
Member

Grimler91 commented Jun 3, 2024

I guess then that this is something that will be solved when/if we update Termux to be android >= 10 compatible. See @agnostic-apollo's docs on the subject.

Maybe it is possible to patch ruby's require_relative to handle both types of prefix'es. I guess it is unlikely that anyone here will work on that though, since Termux targets a lower targetSdkVersion and is unaffected

@agnostic-apollo
Copy link
Member

There seem to be related upstream issues as well, realpath is apparently used to prevent double loading of files. Ideally, they should fix it, however, we could patch too, i.e if file being executed has a symlink target under /data/app/*, then do no use realpath, but currently do not know full scale of issues that would arise if that is done. Although, technically, files cannot be created by users themselves or packages under /data/app and is done by Android itself, so unlikely that a file that exists there is being executed in some non standard way outside of standard package behaviour, unless it is a custom user APK package.

https://bugs.ruby-lang.org/issues/16978

I'll add this issue to the docs, thanks for that!.

Copying the script file to /data/data/<package>/files/usr/lib during setup instead of symlinking may work for loading a file, but will not work for direct execution /path/to/*.rb, unless its a symlink to /data/app/* or executed as ruby /path/to/*.rb. For now termux/termux-exec#24 (comment) solution could be used too.

@gouravkhunger
Copy link
Contributor Author

gouravkhunger commented Jun 7, 2024

@agnostic-apollo Thank you so much for your help! I was able to add a little patch to get through this issue. The gem binary works fine now!

I have documented it in this repo here and also added a demo for default com.termux prefixed bootstraps built with android-10 to verify.

Maybe it is not relevant right now as termux doesn't itself support android 10 but do you think making a PR for such a fix to this repo would be any good? A patch for require_relative that is applied when --android10 bootstrap is being created. If not, please consider this issue closed.

@truboxl truboxl added android10bootstrap Issue related to using Android 10 bootstrap and removed android-10 Issue happens on devices running Android 10 labels Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android10bootstrap Issue related to using Android 10 bootstrap bug report Something is not working properly ruby Issue is about Ruby related stuff, including gem
Projects
None yet
Development

No branches or pull requests

4 participants