Skip to content
Permalink
Browse files

Manipulate RUBYLIB only

Instead of using --disable=gems, it's better to change the search path
and supply our own rubygems.rb. A `-r` option in RUBYOPT is evaluated
_after_ those supplied on the command line, so it's too late to provide
gem-loading behaviour for `ruby -r rack -e p`. This way we get to insert
our own file that will be loaded first, by the gem prelude. It's also
just one less environment variable to modify.

Also clean up some remaining requires: we should now never add, or
expect anyone else to add, `lib` to the search path, outside of tests.
  • Loading branch information...
matthewd committed Mar 10, 2019
1 parent 4432314 commit 8f990187a0dcd4cb631cbbe803a68c2d63d3322e
@@ -7,8 +7,8 @@ Rake::TestTask.class_eval do
end

Rake::TestTask.new(:test) do |t|
t.ruby_opts = ["--disable=gems", "-r", "gel/runtime"]
t.libs << "test"
t.libs << "lib/gel/compatibility"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"]
end
@@ -1,3 +1,3 @@
#!/bin/bash

exec ruby --disable=gems -I `dirname $0`/../lib -r gel/runtime "$@"
exec ruby -I "`dirname $0`/../lib/gel/compatibility" "$@"
13 exe/gel
@@ -1,12 +1,13 @@
#!/usr/bin/env ruby --disable=gems
# frozen_string_literal: true

if defined?(::Gel)
require "gel/command"
elsif defined?(::Gem)
exec ::Gem.ruby, "--disable=gems", "--", __FILE__, *ARGV
else
require_relative "../lib/gel/command"
if defined?(::Gem) && !defined?(::Gel)
exec ::Gem.ruby,
"-I", File.expand_path("../lib/gel/compatibility", __dir__),
"--",
__FILE__,
*ARGV
end

require_relative "../lib/gel/command"
Gel::Command.run(ARGV)
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "runtime"
require_relative "compatibility"
require_relative "error"

class Gel::Command
@@ -12,15 +12,7 @@ def run(command_line)
ENV["GEL_LOCKFILE"] = File.expand_path(Gel::Environment.lockfile_name(gemfile))
end

opt = (ENV["RUBYOPT"] || "").split(" ")
opt.unshift "-rgel/runtime" unless opt.include?("-rgel/runtime") || opt.each_cons(2).to_a.include?(["-r", "gel/runtime"])
opt.unshift "--disable=gems" unless opt.include?("--disable=gems") || opt.each_cons(2).to_a.include?(["--disable", "gems"])
ENV["RUBYOPT"] = opt.join(" ")

lib = (ENV["RUBYLIB"] || "").split(File::PATH_SEPARATOR)
dir = File.expand_path("../..", __dir__)
lib.unshift dir unless lib.include?(dir)
ENV["RUBYLIB"] = lib.join(File::PATH_SEPARATOR)
ENV["RUBYLIB"] = Gel::Environment.modified_rubylib

if execute_inline?(expanded_command)
if command_source == :path || command_source == :original
@@ -6,6 +6,8 @@
# The threshold is basically "things that already-popular/established
# gems assume are there without checking".

require_relative "../runtime"

module Gem
Version = Gel::Support::GemVersion
Requirement = Gel::Support::GemRequirement
@@ -0,0 +1,4 @@
module Gem::Command
def self.build_args=(value)
end
end
@@ -44,6 +44,23 @@ def self.open(store)
@store = store
end

def self.original_rubylib
lib = (ENV["RUBYLIB"] || "").split(File::PATH_SEPARATOR)
lib.delete File.expand_path("compatibility", __dir__)
#lib.delete File.expand_path("..", __dir__)
return nil if lib.empty?
lib.join(File::PATH_SEPARATOR)
end

def self.modified_rubylib
lib = (ENV["RUBYLIB"] || "").split(File::PATH_SEPARATOR)
dir = File.expand_path("compatibility", __dir__)
lib.unshift dir unless lib.include?(dir)
#dir = File.expand_path("..", __dir__)
#lib.unshift dir unless lib.include?(dir)
lib.join(File::PATH_SEPARATOR)
end

def self.search_upwards(name, dir = Dir.pwd)
until (file = File.join(dir, name)) && File.exist?(file)
next_dir = File.dirname(dir)
@@ -58,11 +58,10 @@ def self.parse(content, filename, lineno = 1, root: File.dirname(filename), isol
in_read, in_write = IO.pipe
out_read, out_write = IO.pipe

pid = spawn({ "GEL_GEMFILE" => "", "GEL_LOCKFILE" => "" },
RbConfig.ruby, "--disable=gems",
"-I", File.expand_path("..", __dir__),
"-r", "gel",
"-r", "gel/gemspec_parser",
pid = spawn({ "RUBYLIB" => Gel::Environment.modified_rubylib, "GEL_GEMFILE" => "", "GEL_LOCKFILE" => "" },
RbConfig.ruby,
"-r", File.expand_path("compatibility", __dir__),
"-r", File.expand_path("gemspec_parser", __dir__),
"-e", "puts Marshal.dump(Gel::GemspecParser.parse($stdin.read, ARGV.shift, ARGV.shift.to_i, root: ARGV.shift, isolate: false))",
filename, lineno.to_s, root,
in: in_read, out: out_write)
@@ -122,7 +122,7 @@ def build_environment
gemfile, lockfile = gemfile_and_lockfile

{
"RUBYOPT" => nil,
"RUBYLIB" => Gel::Environment.modified_rubylib,
"GEL_STORE" => File.expand_path(@root_store.root),
"GEL_GEMFILE" => gemfile,
"GEL_LOCKFILE" => lockfile,
@@ -151,9 +151,7 @@ def compile_extconf(ext, install_dir)
status = build_command(
work_dir, log,
{ "MAKEFLAGS" => "-j3" },
RbConfig.ruby, "--disable=gems",
"-I", File.expand_path("../..", __dir__),
"-r", "gel/runtime",
RbConfig.ruby,
"-r", local_config_path,
File.basename(ext),
*Shellwords.shellsplit(@config[:build, @spec.name] || ""),
@@ -176,7 +174,7 @@ def compile_rakefile(ext, install_dir)
if File.basename(ext) =~ /mkrf_conf/i
status = build_command(
work_dir, log,
RbConfig.ruby, "--disable-gems",
RbConfig.ruby,
"-r", local_config_path,
File.basename(ext),
)
@@ -186,10 +184,8 @@ def compile_rakefile(ext, install_dir)
status = build_command(
work_dir, log,
{ "RUBYARCHDIR" => short_install_dir, "RUBYLIBDIR" => short_install_dir },
RbConfig.ruby, "--disable-gems",
"-I", File.expand_path("../..", __dir__),
"-r", "gel/runtime",
"-r", "gel/command",
RbConfig.ruby,
"-r", File.expand_path("../command", __dir__),
"-e", "Gel::Command.run(ARGV)",
"--",
"exec",
@@ -25,5 +25,3 @@
if ENV["GEL_LOCKFILE"] && ENV["GEL_LOCKFILE"] != ""
Gel::Environment.activate(output: $stderr)
end

require_relative "compatibility"
@@ -45,8 +45,8 @@ def test_exec_inline

assert_equal "#{dir}/Gemfile", ENV["GEL_GEMFILE"]
assert_equal "#{dir}/Gemfile.lock", ENV["GEL_LOCKFILE"]
assert_equal "--disable=gems -rgel/runtime", ENV["RUBYOPT"]
assert_equal File.expand_path("../lib", __dir__), ENV["RUBYLIB"]
assert_nil ENV["RUBYOPT"]
assert_equal File.expand_path("../lib/gel/compatibility", __dir__), ENV["RUBYLIB"]

assert_equal "ruby-executable", $0
assert_equal ["some", "args"], ARGV
@@ -74,8 +74,8 @@ def test_exec_nonruby

assert_equal "#{dir}/Gemfile", ENV["GEL_GEMFILE"]
assert_equal "#{dir}/Gemfile.lock", ENV["GEL_LOCKFILE"]
assert_equal "--disable=gems -rgel/runtime", ENV["RUBYOPT"]
assert_equal File.expand_path("../lib", __dir__), ENV["RUBYLIB"]
assert_nil ENV["RUBYOPT"]
assert_equal File.expand_path("../lib/gel/compatibility", __dir__), ENV["RUBYLIB"]
end.throws(:exit)

Dir.chdir(dir) do
@@ -134,9 +134,7 @@ def subprocess_output(code, **kwargs)
config.input = StringIO.new.tap(&:close_write).to_input_stream
config.output = java.io.PrintStream.new(io.to_output_stream)

config.disable_gems = true
config.load_paths = [File.expand_path("../lib", __dir__)]
config.required_libraries << "gel" << "gel/compatibility"
config.required_libraries << File.expand_path("../gel/compatibility", __dir__)

wrapped_code = kwargs.map { |name, value| "#{name} = Marshal.load(#{Marshal.dump(value).inspect})\n" }.join +
"eval(#{code.inspect}, binding, #{source.path.inspect}, #{source.lineno + 1})"
@@ -155,11 +153,13 @@ def subprocess_output(code, **kwargs)
r, w = IO.pipe

pid = spawn(
{ "RUBYOPT" => nil, "GEL_STORE" => nil, "GEL_LOCKFILE" => nil },
RbConfig.ruby, "--disable=gems",
"-I", File.expand_path("../lib", __dir__),
"-r", "gel",
"-r", "gel/compatibility",
{
"RUBYLIB" => File.expand_path("../lib/gel/compatibility", __dir__),
"GEL_STORE" => nil,
"GEL_LOCKFILE" => nil,
},
RbConfig.ruby,
"-r", File.expand_path("../lib/gel/compatibility", __dir__),
"-e", wrapped_code,
in: IO::NULL,
out: w,

0 comments on commit 8f99018

Please sign in to comment.
You can’t perform that action at this time.