Skip to content

Commit 986fa33

Browse files
committed
Prevent rake compiler from defining application defined task:
- Gem that define a Rake Compiler task (pretty much all) interfere with our own. The reason we can't rely on the user defined task is because it's not configured for compiling fat gems, it is usually only configured to compile natively for development purposes. I also would like the configuration of cibuildgem to not interfere with the gem defined task and not have to ask maintainers to change basic configuration just to make compiling fat gems possible, cibuildgem should take care of this automatically. For reference, I originally decided to choose running the whole compilation dance in a subprocess and require the gem Rakefile because I felt this would be the right entrypoint to configure CIbuildgem if needed.
1 parent d5edc6b commit 986fa33

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

lib/cibuildgem/cli.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,13 @@ def run_rake_tasks!(*tasks)
137137
rake_path = rake_specs.full_require_paths
138138
prism_path = Gem.loaded_specs["prism"].full_require_paths
139139
load_paths = (rake_compiler_path + rake_path + prism_path).join(File::PATH_SEPARATOR)
140+
patch = File.expand_path("../extension_patch.rb", __FILE__)
140141

141-
system({ "RUBYLIB" => load_paths }, "bundle exec #{RbConfig.ruby} #{rake_executable} #{all_tasks} -R#{rakelibdir}", exception: true)
142+
system(
143+
{ "RUBYLIB" => load_paths },
144+
"bundle exec #{RbConfig.ruby} #{rake_executable} #{all_tasks} -R#{rakelibdir} -r #{patch}",
145+
exception: true,
146+
)
142147
end
143148

144149
def compilation_task

lib/cibuildgem/compilation_tasks.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def initialize(create_packaging_task = false, gemspec = nil)
1717
end
1818

1919
def setup
20+
Rake::ExtensionTask.enable!
21+
2022
gemspec.extensions.each do |path|
2123
binary_name = parse_extconf(path)
2224
define_task(path, binary_name)

lib/cibuildgem/extension_patch.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require "rake/extensiontask"
4+
5+
module Cibuildgem
6+
module ExtensionPatch
7+
class << self
8+
def prepended(mod)
9+
class << mod
10+
attr_accessor :enabled
11+
12+
def enable!
13+
@enabled = true
14+
end
15+
end
16+
end
17+
end
18+
19+
def define
20+
super if self.class.enabled
21+
end
22+
end
23+
end
24+
25+
Rake::ExtensionTask.prepend(Cibuildgem::ExtensionPatch)

test/cli_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "test_helper"
4+
require "english"
45

56
module Cibuildgem
67
class CLITest < Minitest::Test
@@ -214,6 +215,14 @@ def test_cli_test_command_when_no_test_or_spec_rake_task_is_defined
214215
end
215216
end
216217

218+
def test_package_when_a_rakefile_defines_an_extension_task
219+
Dir.chdir("test/fixtures/with_ext") do
220+
CLI.start(["package"])
221+
end
222+
223+
assert_predicate($CHILD_STATUS, :success?)
224+
end
225+
217226
private
218227

219228
def raise_instead_of_exit(&block)

test/fixtures/with_ext/Rakefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
require "rake/extensiontask"
4+
5+
gemspec = Gem::Specification.load("foo.gemspec")
6+
Rake::ExtensionTask.new("foo", gemspec)
7+
8+
raise "No rake task should have been defined" if Rake::Task.task_defined?(:native)
9+
10+
exit(0)

test/fixtures/with_ext/foo.gemspec

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = "edouard-dummy_gem"
5+
spec.version = "0.0.1"
6+
spec.authors = ["Edouard CHIN"]
7+
spec.email = ["[email protected]"]
8+
spec.summary = "A gem that do nothing"
9+
spec.description = "Don't use it, really."
10+
spec.license = "MIT"
11+
spec.files = []
12+
spec.require_paths = ["lib"]
13+
spec.extensions = ["ext/hello_world/extconf.rb"]
14+
end

0 commit comments

Comments
 (0)