Repository: ruby/rubygems (Ruby)

Issue:
"This file is owned by your OS, not by rubygems upstream" is not specifying file location with absolute path but only relative path with unclear base
I'm having a problem or would like to suggest a feature.

My current problem is that rubygems update crashes with unclear error

Running `gem update --system` results in:

> Loading the rubygems/defaults/operating_system.rb file caused an error. This file is owned by your OS, not by rubygems upstream. Please find out which OS package this file belongs to and follow the guidelines from your OS to report the problem and ask for help.                                                                                                                     

It is not specifying actual file location, but some relative path, relative to base unknown to me.

This error should specify absolute path to make easier to track down offending file and fix it.

(
```
cat rubygems/defaults/operating_system.rb
cat: rubygems/defaults/operating_system.rb: No such file or directory
```
)

This issue is related to:

  - [ ] Network problems
  - [ ] Installing a library
  - [ ] Publishing a library
  - [x] The command line `gem`
  - [ ] Other

Here are my current environment details:

```
$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 3.1.2
  - RUBY VERSION: 2.7.0 (2019-12-25 patchlevel 0) [x86_64-linux-gnu]
  - INSTALLATION DIRECTORY: /var/lib/gems/2.7.0
  - USER INSTALLATION DIRECTORY: /home/mateusz/.gem/ruby/2.7.0
  - RUBY EXECUTABLE: /usr/bin/ruby2.7
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /usr/local/bin
  - SPEC CACHE DIRECTORY: /home/mateusz/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /var/lib/gems/2.7.0
     - /home/mateusz/.gem/ruby/2.7.0
     - /usr/lib/ruby/gems/2.7.0
     - /usr/share/rubygems-integration/2.7.0
     - /usr/share/rubygems-integration/all
     - /usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gem" => "--no-document"
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /home/mateusz/.local/bin
     - /home/mateusz/.gem/ruby/2.8.0/bin
     - /home/mateusz/.gem/ruby/2.7.0/bin
     - /home/mateusz/Install
     - /git_commands_as_script_files
     - /home/mateusz/.local/lib
     - /home/mateusz/.local/lib/python3.9
     - /home/mateusz/.local/lib/python3.9/site-packages
     - /usr/lib/go-1.10/bin
     - /home/mateusz/Documents/install/gopath_target/bin
     - /usr/local/bin/
     - /opt/telegram/
     - /home/mateusz/.local/bin/
     - /home/mateusz/.local/bin
     - /usr/local/sbin
     - /usr/local/bin
     - /usr/sbin
     - /usr/bin
     - /sbin
     - /bin
     - /usr/games
     - /usr/local/games
     - /snap/bin
     - /opt/mssql-tools/bin
```

> Before reporting an issue, make sure you're using the latest version of rubygems
> by running `gem update --system`, and double check whether the issue has already
> been fixed in the latest version.

I have problem with `gem update --system` itself.



Discussion:
@matkoniecz I'm happy to accept such a change, and also an enhancement to the documentation to point to [manual upgrade instructions](https://rubygems.org/pages/download) if it's `gem update --system` itself that's failing.
@matkoniecz the error you mention is happening at:

https://github.com/rubygems/rubygems/blob/7c774fbc8497979e119465e7020620f1c3464763/lib/rubygems.rb#L1320-L1335

This file is not loaded relatively, but in classic gem load file paths. It could be in theory in all those GEM_PATH folders if I understand it well.

You, as an user, should not be messing with this file and report problem back to upstream (OS). RubyGems itself are not distributing this file. It is most likely distributed by your OS. That's the purpose of the message.

If you would like to maintain Ruby manually (not by the system packaging manager) I suggest to use some kind of Ruby installer (like ruby-install/chruby combination). That way you'll get RubyGems (and Ruby) under your user control and there will be no `rubygems/defaults/operating_system.rb` breaking your installation.
Yes, I agree, that's our idea and we should try to communicate this as best as possible.

But once this load error happens, we do know the exact file that failed to load, so I don't think there's any harm on using an absolute path here, and let the user inspect the file and better understand what's going on and maybe even add a direct quick fix while the issue is fixed on the OS side.
> You, as an user, should not be messing with this file and report problem back to upstream (OS).

I know, but ideally such bug report would include location of such file.

It is also possible that I screwed something up and file is actually located in `~/install/terrible_temporary_hack/rubygems/defaults/operating_system.rb` that I created year ago and forgot in meantime. In such case reporting problem to OS authors would be a waste of time.

I would at least reset `?????/rubygems/defaults/operating_system.rb` file with `apt-get`  if it is in some system folder - before reporting the issue to OS maintainers.

And I bet that the first question asked back after filing bug report will be "what is the absolute path of the problematic file" to handle exactly such cases. (I am not an OS developer, but I recently closed two issues that were present only in forked code developed as a separate project - my first question was "which exact version of code you are running" as there was no sane way to trigger it on official build)
@matkoniecz reporting full path is ok for me as well. Would you mind to contribute this? I can take a look as well.
@simi

> I can take a look as well.

feel free! It would be really welcome!

I am not expecting to do it within predictable future - I would need to setup development, familiarize with new codebase and do something new to me.

In near term I plan to review some pull requests on projects already familiar to me and where I can merge them


I have the same issue. Windows 11, fresh install. Using Windows Terminal with Ubuntu WSL.
In Windows, use command prompt (cmd) don't Windows Terminal
Any solution for running `sudo gem update --system` on WSL2?
@dkirrane we're going to need more than that to be able to help you. Feel free to open a new issue answering all the questions in the bug report template so that we can (at least try) to help you.
@deivid-rodriguez - I'm getting `This file is owned by your OS, not by rubygems upstream` on WSL2 when running `sudo gem update --system`

```
Updating rubygems-update
Successfully installed rubygems-update-3.4.6
Parsing documentation for rubygems-update-3.4.6
Done installing documentation for rubygems-update after 0 seconds
Parsing documentation for rubygems-update-3.4.6
Done installing documentation for rubygems-update after 0 seconds
Installing RubyGems 3.4.6
Traceback (most recent call last):
        5: from setup.rb:22:in `<main>'
        4: from setup.rb:22:in `require'
        3: from /var/lib/gems/2.7.0/gems/rubygems-update-3.4.6/lib/rubygems.rb:1332:in `<top (required)>'
        2: from /var/lib/gems/2.7.0/gems/rubygems-update-3.4.6/lib/rubygems.rb:1332:in `require'
        1: from /usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb:49:in `<top (required)>'
/usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb:50:in `<class:Specification>': undefined method `rubyforge_project=' for class `Gem::Specification' (NameError)
        3: from setup.rb:22:in `<main>'
        2: from setup.rb:22:in `require'
        1: from /var/lib/gems/2.7.0/gems/rubygems-update-3.4.6/lib/rubygems.rb:1328:in `<top (required)>'
/var/lib/gems/2.7.0/gems/rubygems-update-3.4.6/lib/rubygems.rb:1342:in `rescue in <top (required)>': undefined method `rubyforge_project=' for class `Gem::Specification' (NameError)
Loading the /usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb file caused an error. This file is owned by your OS, not by rubygems upstream. Please find out which OS package this file belongs to and follow the guidelines from your OS to report the problem and ask for help.
```

You need to figure out which Linux distribution you installed, find which package is providing the `/usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb` file, and report an issue to that package. Probably using a more recent distribution should fix the problem.

Alternatively, don't use `sudo gem update --system` at all, and install a version of Ruby provided by a version manager instead (I use https://github.com/asdf-vm/asdf-ruby, for example).



Patch:
diff --git a/lib/rubygems.rb b/lib/rubygems.rb
index add403483716..0e71f7b50e8b 100644
--- a/lib/rubygems.rb
+++ b/lib/rubygems.rb
@@ -1323,8 +1323,9 @@ def default_gem_load_paths
 rescue LoadError
   # Ignored
 rescue StandardError => e
+  path = e.backtrace_locations.reverse.find {|l| l.path.end_with?("rubygems/defaults/operating_system.rb") }.path
   msg = "#{e.message}\n" \
-    "Loading the rubygems/defaults/operating_system.rb file caused an error. " \
+    "Loading the #{path} file caused an error. " \
     "This file is owned by your OS, not by rubygems upstream. " \
     "Please find out which OS package this file belongs to and follow the guidelines from your OS to report " \
     "the problem and ask for help."


Test patch:
diff --git a/test/rubygems/test_rubygems.rb b/test/rubygems/test_rubygems.rb
index 739a9985b08e..e5c0a22caf51 100644
--- a/test/rubygems/test_rubygems.rb
+++ b/test/rubygems/test_rubygems.rb
@@ -16,7 +16,7 @@ def test_operating_system_other_exceptions
     output = Gem::Util.popen(*ruby_with_rubygems_and_fake_operating_system_in_load_path(path), '-e', "'require \"rubygems\"'", { :err => [:child, :out] }).strip
     assert !$?.success?
     assert_includes output, "undefined local variable or method `intentionally_not_implemented_method'"
-    assert_includes output, "Loading the rubygems/defaults/operating_system.rb file caused an error. " \
+    assert_includes output, "Loading the #{operating_system_rb_at(path)} file caused an error. " \
     "This file is owned by your OS, not by rubygems upstream. " \
     "Please find out which OS package this file belongs to and follow the guidelines from your OS to report " \
     "the problem and ask for help."
@@ -53,16 +53,19 @@ def self.default_dir
 
   def util_install_operating_system_rb(content)
     dir_lib = Dir.mktmpdir("test_operating_system_lib", @tempdir)
-    dir_lib_arg = File.join dir_lib
+    dir_lib_arg = File.join dir_lib, "lib"
 
-    dir_lib_rubygems_defaults_arg = File.join dir_lib_arg, "lib", "rubygems", "defaults"
-    FileUtils.mkdir_p dir_lib_rubygems_defaults_arg
+    operating_system_rb = operating_system_rb_at(dir_lib_arg)
 
-    operating_system_rb = File.join dir_lib_rubygems_defaults_arg, "operating_system.rb"
+    FileUtils.mkdir_p File.dirname(operating_system_rb)
 
     File.open(operating_system_rb, 'w') {|f| f.write content }
 
-    File.join dir_lib_arg, "lib"
+    dir_lib_arg
+  end
+
+  def operating_system_rb_at(dir)
+    File.join dir, "rubygems", "defaults", "operating_system.rb"
   end
 
   def ruby_with_rubygems_and_fake_operating_system_in_load_path(operating_system_path)