Skip to content

Commit

Permalink
Merge pull request #231 in G/truffleruby from pitr-ch/patching to master
Browse files Browse the repository at this point in the history
* commit '74ba6cdb7e12d4a196584cc8250fe10f1f154efc':
  Simpler and more robust patching
  • Loading branch information
nirvdrum committed Jul 25, 2018
2 parents d164b01 + 74ba6cd commit 8827d24
Show file tree
Hide file tree
Showing 27 changed files with 171 additions and 227 deletions.
34 changes: 20 additions & 14 deletions doc/contributor/workarounds.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
# Patching system

The patching system is loaded in `post-boot.rb` and is able to patch stdlib and
installed gems. The code implementing the patching itself can be found in
`lib/truffle/truffle/patching.rb`. It can be disabled by passing the
`-Xpatching=false` option.

The patching system works for gems as follows. When a gem *g* is activated,
there is a directory `g` in `lib/patches`, and the directory is listed in
`lib/truffle/truffle/patching.rb`, the directory is inserted into `$LOAD_PATH`
before the original load-paths of the *g* gem. As a result the patching files
are loaded first before the original files in the gem. The patching file is
responsible for loading the original file (if desirable), which can be done with
`Truffle::Patching.require_original __FILE__`.
# Ruby files patching system

When `require 'some/patch'` (has to be a relative path) is called
and an original file exists on `$LOAD_PATH`
and there is a patch file `some/patch.rb` in a special directory `lib/patches`
then the TruffleRuby will first evaluate (not require) the file
`lib/patches/some/patch.rb` instead.

To require the original file call `require 'some/patch'` in the
`lib/patches/some/patch.rb` file. It will require the original ruby
file found on `$LOAD_PATH` ( stdlib file or a gem file).

When requiring the original file is not desired just omit the
`require 'some/patch'` in the patch file. The patch file will be evaluated
only once.

The evaluated patch files are not visible in `$LOAD_PATH` nor `$LOADED_FEATURES`.

Patching can be disabled by passing the `-Xpatching=false` option.
`-X.log=CONFIG` can be used to see paths of loaded patch files.

# C file preprocessing

Expand Down
2 changes: 1 addition & 1 deletion lib/patches/bundler/bundler.rb → lib/patches/bundler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Truffle::Patching.require_original __FILE__
require 'bundler'

unless Bundler::VERSION =~ /^1\.16\./
raise "unsupported bundler version #{Bundler::VERSION}, please use 1.16.x"
Expand Down
19 changes: 0 additions & 19 deletions lib/patches/bundler/bundler/runtime.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Truffle::Patching.require_original __FILE__
require 'bundler/cli/exec'

module Bundler
class CLI::Exec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Truffle::Patching.require_original __FILE__
require 'bundler/current_ruby'

module Bundler
class CurrentRuby
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Truffle::Patching.require_original __FILE__
require 'bundler/dependency'

class Bundler::Dependency

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Truffle::Patching.require_original __FILE__
require 'bundler/ruby_version'

module Bundler
class RubyVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Truffle::Patching.require_original __FILE__
require 'bundler/source/rubygems'

# TruffleRuby: do not skips gems with extensions not built

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Truffle::Patching.require_original __FILE__
require 'launchy/detect/ruby_engine'

module Launchy::Detect
class RubyEngine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Truffle::Patching.require_original __FILE__
require 'launchy/detect/runner'

module Launchy::Detect
class Runner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Truffle::Patching.require_original __FILE__
require 'rake/baseextensiontask'

module Truffle::Patching::RakeBaseExtensionTaskOverrideBinaryExt
def binary(platform = nil)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Copyright (c) 2008-2011 Luis Lavena.
#
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand All @@ -19,7 +19,7 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Truffle::Patching.require_original __FILE__
require 'rake/extensiontask'

module Truffle::Patching::RakeExtensionTaskOverridePatterns
def init(name = nil, gem_spec = nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -21,7 +21,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

Truffle::Patching.require_original __FILE__
require 'rspec/core/source'

module RSpec
module Core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -19,7 +19,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

Truffle::Patching.require_original __FILE__
require 'rspec/support'

module RSpec
module Support
Expand Down
3 changes: 1 addition & 2 deletions lib/patches/stdlib/rubygems.rb → lib/patches/rubygems.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Truffle::Patching.require_original __FILE__
Truffle::Patching.install_gem_activation_hook
require 'rubygems'

# Because did_you_mean was required directly without RubyGems
if Truffle::Boot.get_option 'did_you_mean'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Truffle::Patching.require_original __FILE__
require 'rubygems/basic_specification'

if ENV['TRUFFLERUBY_CEXT_ENABLED'] == "false"
module Truffle::Patching::NoWarnIfBuildingCextDisabled
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Truffle::Patching.require_original __FILE__
require 'rubygems/ext'

# TruffleRuby: build C extensions conditionally

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Truffle::Patching.require_original __FILE__
require 'rubygems/remote_fetcher'

# Hardcode DNS Resolution to rubygems.org for gem install

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Truffle::Patching.require_original __FILE__
require 'rubygems/request_set/gem_dependency_api'

class Gem::RequestSet::GemDependencyAPI

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Truffle::Patching.require_original __FILE__
require 'thread_safe'

# Copyright (c) 2012 thedarkone
#
Expand Down Expand Up @@ -177,4 +177,4 @@ def #{method}(*args)
RUBY_EVAL
end
end
end
end
112 changes: 0 additions & 112 deletions lib/truffle/truffle/patching.rb

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/java/org/truffleruby/RubyContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,8 @@ private String searchRubyHome(Options options) throws IOException {

private boolean isRubyHome(File path) {
return Paths.get(path.toString(), "lib", "truffle").toFile().isDirectory() &&
Paths.get(path.toString(), "lib", "ruby").toFile().isDirectory();
Paths.get(path.toString(), "lib", "ruby").toFile().isDirectory() &&
Paths.get(path.toString(), "lib", "patches").toFile().isDirectory();
}

public TruffleNFIPlatform getTruffleNFI() {
Expand Down
Loading

0 comments on commit 8827d24

Please sign in to comment.