diff --git a/lib/bundler/environment_preserver.rb b/lib/bundler/environment_preserver.rb index a77f7e0816..0f08e049d8 100644 --- a/lib/bundler/environment_preserver.rb +++ b/lib/bundler/environment_preserver.rb @@ -38,7 +38,10 @@ module Bundler # Replaces `ENV` with the bundler environment variables backed up def replace_with_backup - ENV.replace(backup) unless Gem.win_platform? + unless Gem.win_platform? + ENV.replace(backup) + return + end # Fallback logic for Windows below to workaround # https://bugs.ruby-lang.org/issues/16798. Can be dropped once all diff --git a/spec/bundler/realworld/ffi_spec.rb b/spec/bundler/realworld/ffi_spec.rb new file mode 100644 index 0000000000..083ea38901 --- /dev/null +++ b/spec/bundler/realworld/ffi_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +RSpec.describe "loading dinamically linked library on a bundle exec context", :realworld => true do + it "passes ENV right after argv in memory" do + create_file "foo.rb", <<~RUBY + require 'ffi' + + module FOO + extend FFI::Library + ffi_lib './libfoo.so' + + attach_function :Hello, [], :void + end + + FOO.Hello() + RUBY + + create_file "libfoo.c", <<~'C' + #include + + static int foo_init(int argc, char** argv, char** envp) { + if (argv[argc+1] == NULL) { + printf("FAIL\n"); + } else { + printf("OK\n"); + } + + return 0; + } + + #if defined(__APPLE__) && defined(__MACH__) + __attribute__((section("__DATA,__mod_init_func"), used, aligned(sizeof(void*)))) + #else + __attribute__((section(".init_array"))) + #endif + static void *ctr = &foo_init; + + extern char** environ; + + void Hello() { + return; + } + C + + sys_exec "gcc -g -o libfoo.so -shared -fpic libfoo.c" + + install_gemfile <<-G + source "https://rubygems.org" + + gem 'ffi' + G + + bundle "exec ruby foo.rb" + + expect(out).to eq("OK") + end +end diff --git a/spec/bundler/support/artifice/vcr_cassettes/realworld/rubygems.org/gems/ffi-1.15.4.gem/GET/request b/spec/bundler/support/artifice/vcr_cassettes/realworld/rubygems.org/gems/ffi-1.15.4.gem/GET/request new file mode 100644 index 0000000000..0c9337dad2 --- /dev/null +++ b/spec/bundler/support/artifice/vcr_cassettes/realworld/rubygems.org/gems/ffi-1.15.4.gem/GET/request @@ -0,0 +1,7 @@ +> GET /gems/ffi-1.15.4.gem +> accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3 +> accept: */* +> user-agent: Ruby +> connection: keep-alive +> keep-alive: 30 +> host: rubygems.org \ No newline at end of file diff --git a/spec/bundler/support/artifice/vcr_cassettes/realworld/rubygems.org/gems/ffi-1.15.4.gem/GET/response b/spec/bundler/support/artifice/vcr_cassettes/realworld/rubygems.org/gems/ffi-1.15.4.gem/GET/response new file mode 100644 index 0000000000..6e2c35b670 Binary files /dev/null and b/spec/bundler/support/artifice/vcr_cassettes/realworld/rubygems.org/gems/ffi-1.15.4.gem/GET/response differ