Workaround Errno::ENOENT error in rubygems

When trying to `require` a gem running in an unlinked directory, an exception is
thrown because `Dir.pwd` is called (see rubygems/rubygems#3087).

Until the fix lands in an official release, we monkey patch `Dir.pwd` to return
'/' if a `Errno::ENOENT` exception is raised while loading the colorls gem.

This should work for all Unixes. On Windows, this error cannot happen since you
cannot unlink a directory which is still in use by a process.

Fixes #351.
This commit is contained in:
Claudio Bley 2020-05-10 10:49:46 +02:00
parent f8674d46df
commit f34453bce3
3 changed files with 22 additions and 3 deletions

View file

@ -1,7 +1,26 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# workaround https://github.com/rubygems/rubygems/issues/3087
# rubocop:disable Style/GlobalVars
$loading = true
class Dir
@@old_pwd = singleton_method(:pwd) # rubocop:disable Style/ClassVars
def self.pwd
@@old_pwd.call
rescue Errno::ENOENT => e
return '/' if $loading
raise e
end
end
require 'colorls'
$loading = false
# rubocop:enable Style/GlobalVars
ColorLS::Flags.new(*ARGV).process
true

View file

@ -39,7 +39,7 @@ module ColorLS
def process
init_locale
@args = [Dir.pwd] if @args.empty?
@args = ['.'] if @args.empty?
@args.sort!.each_with_index do |path, i|
next STDERR.puts "\n Specified path '#{path}' doesn't exist.".colorize(:red) unless File.exist?(path)

View file

@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "COLORLS" "1" "April 2020" "colorls 1.3.3" "colorls Manual"
.TH "COLORLS" "1" "April 2020" "colorls 1.4.0" "colorls Manual"
.
.SH "NAME"
\fBcolorls\fR \- list directory contents with colors and icons