Home > Enterprise >  Ruby - Gems Env - Encoding Error (windows)
Ruby - Gems Env - Encoding Error (windows)

Time:06-12

I have installed the latest Ruby version from RubyInstaller on Windows and then installed Rails through gems.

Everytime I try to run rails new app_name command I get an encoding error.

C:/Ruby31-x64/lib/ruby/3.1.0/pathname.rb:52:in `match?': invalid byte sequence in UTF-8 (ArgumentError)
        from C:/Ruby31-x64/lib/ruby/3.1.0/pathname.rb:52:in `chop_basename'
        from C:/Ruby31-x64/lib/ruby/3.1.0/pathname.rb:376:in `plus'
        from C:/Ruby31-x64/lib/ruby/3.1.0/pathname.rb:356:in ` '
        from C:/Ruby31-x64/lib/ruby/3.1.0/pathname.rb:422:in `join'
        from C:/Ruby31-x64/lib/ruby/site_ruby/3.1.0/bundler/settings.rb:445:in `global_config_file'
        from C:/Ruby31-x64/lib/ruby/site_ruby/3.1.0/bundler/settings.rb:93:in `initialize'
        from C:/Ruby31-x64/lib/ruby/site_ruby/3.1.0/bundler.rb:343:in `new'
        from C:/Ruby31-x64/lib/ruby/site_ruby/3.1.0/bundler.rb:343:in `settings'
        from C:/Ruby31-x64/lib/ruby/site_ruby/3.1.0/bundler/env.rb:20:in `report'
        from C:/Ruby31-x64/lib/ruby/site_ruby/3.1.0/bundler/friendly_errors.rb:71:in `request_issue_report_for'
        from C:/Ruby31-x64/lib/ruby/site_ruby/3.1.0/bundler/friendly_errors.rb:50:in `log_error'
        from C:/Ruby31-x64/lib/ruby/site_ruby/3.1.0/bundler/friendly_errors.rb:123:in `rescue in with_friendly_errors'
        from C:/Ruby31-x64/lib/ruby/site_ruby/3.1.0/bundler/friendly_errors.rb:115:in `with_friendly_errors'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/bundler-2.3.15/exe/bundle:36:in `<main>'

I suspected something was probably wrong with my PATH's so I checked my gem env and I saw that the problem lies in my Windows User folder name which has a special character in it and apparently ruby does not accept it, find below my gem env:

RubyGems Environment:
  - RUBYGEMS VERSION: 3.3.15
  - RUBY VERSION: 3.1.2 (2022-04-12 patchlevel 20) [x64-mingw-ucrt]
  - INSTALLATION DIRECTORY: C:/Ruby31-x64/lib/ruby/gems/3.1.0
  - USER INSTALLATION DIRECTORY: C:/Users/Tiago Galv�o/.local/share/gem/ruby/3.1.0
  - RUBY EXECUTABLE: C:/Ruby31-x64/bin/ruby.exe
  - GIT EXECUTABLE: C:\Program Files\Git\cmd/git.EXE
  - EXECUTABLE DIRECTORY: C:/Ruby31-x64/bin
  - SPEC CACHE DIRECTORY: C:/Users/Tiago Galv�o/.local/share/gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: C:/ProgramData
  - RUBYGEMS PLATFORMS:

I have tried to change my User folder name trough the windows registry and the commands indeed start working. However I needed to revert everything to the original name because my system started acting strange.

So my question is:

  • How can I change the USER INSTALLATION DIRECTORY and SPEC CACHE DIRECTORY of my gem env in order to be able to run rails commands in Windows? I would like to change the folder to a folder without any special chars or even to make ruby accept this special char in my name.

Any ideas on how I can accomplish it?

CodePudding user response:

rubyinstaller takes a DIR argument

rubyinstaller-w.x.y-z-x64.exe /DIR="%ProgramFiles%\Ruby"

CodePudding user response:

This seems like a bug in Ruby, or more specifically Bundler. I would report it as a bug.

In the meantime you can try setting GEM_HOME. It should be the base setting for the rest of the folders. There is also GEM_SPEC_CACHE, which allows you to move the cache folder separately.

However the error you are getting is from Bundler, when it is running global_config_file.

From there we can see that you might want to set BUNDLE_CONFIG or BUNDLE_USER_CONFIG. Note that this may have to point to an existing file, which can most likely be empty though.

One of those env variables, or a combination of them, will hopefully allow everything to work.

  • Related