diff --git a/.gdbinit b/.gdbinit
index 63cd7064766117..1806196a42abb8 100644
--- a/.gdbinit
+++ b/.gdbinit
@@ -156,8 +156,12 @@ define rp
else
if ($flags & RUBY_T_MASK) == RUBY_T_HASH
printf "%sT_HASH%s: ", $color_type, $color_end,
- if ((struct RHash *)($arg0))->ntbl
- printf "len=%ld ", ((struct RHash *)($arg0))->ntbl->num_entries
+ if (((struct RHash *)($arg0))->basic->flags & RHASH_ST_TABLE_FLAG)
+ printf "st len=%ld ", ((struct RHash *)($arg0))->as.st->num_entries
+ else
+ printf "li len=%ld bound=%ld ", \
+ ((((struct RHash *)($arg0))->basic->flags & RHASH_ARRAY_LEN_MASK) >> RHASH_ARRAY_LEN_SHIFT), \
+ ((((struct RHash *)($arg0))->basic->flags & RHASH_ARRAY_BOUND_MASK) >> RHASH_ARRAY_BOUND_SHIFT)
end
print (struct RHash *)($arg0)
else
diff --git a/.gitignore b/.gitignore
index 9849721d2a66f3..e03e17e9d1cc14 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@
*.a
*.bak
*.bc
+*.bundle
*.dSYM
*.dmyh
*.dylib
@@ -20,6 +21,8 @@
*.rej
*.s
*.sav
+*.sl
+*.so
*.swp
*.yarb
*~
@@ -92,6 +95,7 @@ lcov*.info
/libruby*.*
/miniprelude.c
/miniruby
+/mjit_build_dir.c
/newdate.rb
/newline.c
/newver.rb
@@ -139,6 +143,10 @@ lcov*.info
/enc/jis/props.h
/enc/unicode/data
+# /coroutine/
+!/coroutine/**/*.s
+/coroutine/**/.time
+
# /enc/trans/
/enc/trans/*.c
/enc/trans/*.def
diff --git a/.travis.yml b/.travis.yml
index c9ad5322265ac1..f00efeb8c7da7e 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -17,9 +17,16 @@
# is also a good place to look at.
language: c
-dist: trusty
+
+dist: xenial
+
+osx_image: xcode10.1
sudo: false
+
+git:
+ quiet: true
+
addons:
apt:
config:
@@ -48,160 +55,319 @@ addons:
- libffi
- openssl@1.1
- zlib
+ - ccache
+
+cache:
+ ccache: true
+ directories:
+ - $HOME/config_2nd
+ - $HOME/.downloaded-cache
+
+env:
+ global:
+ - CONFIGURE_TTY=no
+ - CCACHE_COMPILERCHECK=none
+ - CCACHE_NOCOMPRESS=1
+ - CCACHE_MAXSIZE=512Mi
+ - >-
+ NPROC="`case ${TRAVIS_OS_NAME} in
+ osx) sysctl -n hw.activecpu ;;
+ linux) nproc ;;
+ esac`"
+ # JOBS and SETARCH are overridden when necessary; see below.
+ - JOBS=-j$((1+${NPROC}))
+ - SETARCH=
+ - RUBY_PREFIX=/tmp/ruby-prefix
+ - GEMS_FOR_TEST='timezone tzinfo'
+
+.org.ruby-lang.ci.matrix-definitions:
+
+ - &cron-only
+ if: (type = cron) AND (branch = trunk) AND (fork = false)
+
+ - &make-test-only
+ script:
+ - $SETARCH make -s test TESTOPTS="${TESTOPTS=$JOBS -q --tty=no}"
+
+ - &linux
+ os: linux
+ compiler: gcc-8
+
+ - &osx
+ os: osx
+ compiler: clang
+ before_install:
+ - /usr/local/opt/openssl@1.1/bin/openssl version
+
+ # --------
+
+ - &x86_64-linux
+ name: x86_64-linux
+ <<: *linux
+
+ - &jemalloc
+ name: --with-jemalloc
+ <<: *linux
+ <<: *cron-only
+ env:
+ - CONFIG_FLAG='--with-gmp --with-jemalloc --with-valgrind'
+
+ - &VM_CHECK_MODE
+ name: VM_CHECK_MODE=3
+ <<: *linux
+ <<: *cron-only
+ <<: *make-test-only
+ env:
+ - cppflags=-DVM_CHECK_MODE=0x0003
+
+ - &FIBER_USE_sjlj
+ name: FIBER_USE_NATIVE=0
+ <<: *linux
+ <<: *cron-only
+ env:
+ - cppflags=-DFIBER_USE_NATIVE=0
+
+ - &TOKEN_THREADED_CODE
+ name: TOKEN_THREADED_CODE
+ <<: *linux
+ <<: *cron-only
+ <<: *make-test-only
+ env:
+ - cppflags=-DOPT_THREADED_CODE=1
+
+ - &CALL_THREADED_CODE
+ name: CALL_THREADED_CODE
+ <<: *linux
+ <<: *cron-only
+ <<: *make-test-only
+ env:
+ - cppflags=-DOPT_THREADED_CODE=2
+
+ - &NO_THREADED_CODE
+ name: NO_THREADED_CODE
+ <<: *linux
+ <<: *cron-only
+ <<: *make-test-only
+ env:
+ - cppflags=-DOPT_THREADED_CODE=3
+
+ - &ASAN
+ name: -fsanitize=address
+ <<: *linux
+ #<<: *cron-only
+ <<: *make-test-only
+ compiler: clang
+ env:
+ - ASAN_OPTIONS=detect_leaks=0
+ - cflags='-march=native -fsanitize=address -fno-omit-frame-pointer'
+ - debugflags=-ggdb3
+ - optflags=-O1
+ - LD=clang
+ - LDFLAGS=-fsanitize=address
+
+ - &MSAN
+ name: -fsanitize=memory
+ <<: *linux
+ #<<: *cron-only
+ <<: *make-test-only
+ compiler: clang
+ env:
+ - cflags='-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer'
+ - optflags=-O1
+ - LD=clang
+ - LDFLAGS=-fsanitize=memory
+ - CONFIG_FLAG=--with-out-ext=openssl
+
+ - &UBSAN
+ name: -fsanitize=undefined
+ <<: *linux
+ #<<: *cron-only
+ <<: *make-test-only
+ compiler: clang
+ env:
+ - cflags='-fsanitize=undefined,integer,nullability -fno-omit-frame-pointer'
+ - cppflags=-DUNALIGNED_WORD_ACCESS=0
+ - debugflags=-ggdb3
+ - optflags='-O1 -march=native'
+ - LD=clang
+ - LDFLAGS=-fsanitize=undefined,integer,nullability
+
+ - &i686-linux
+ name: i686-linux
+ <<: *linux
+ sudo: required
+ env:
+ - GCC_FLAGS=-m32
+ - debugflags=-g0
+ - SETARCH='setarch i686 --verbose --3gb'
+ addons:
+ apt:
+ config:
+ retries: true
+ update: true
+ sources:
+ - ubuntu-toolchain-r-test
+ packages:
+ - gcc-8-multilib
+ - libffi-dev:i386
+ - libffi6:i386
+ - libgdbm-dev:i386
+ - libgdbm3:i386
+ - libncurses5-dev:i386
+ - libncurses5:i386
+ - libncursesw5-dev:i386
+ - libreadline6-dev:i386
+ - libreadline6:i386
+ - libssl-dev:i386
+ - libssl1.0.0:i386
+ - linux-libc-dev:i386
+ - zlib1g-dev:i386
+ - zlib1g:i386
+
+ - &pedanticism
+ name: -ansi -pedantic
+ <<: *linux
+ <<: *make-test-only
+ compiler: clang
+ env:
+ - GCC_FLAGS='-ansi -Werror=pedantic -pedantic-errors -std=iso9899:1990'
+ - CONFIG_FLAG=
+ - JOBS=
+ - >-
+ warnflags='
+ -Wall
+ -Wextra
+ -Werror=declaration-after-statement
+ -Werror=deprecated-declarations
+ -Werror=division-by-zero
+ -Werror=extra-tokens
+ -Werror=implicit-function-declaration
+ -Werror=implicit-int
+ -Werror=long-long
+ -Werror=pointer-arith
+ -Werror=shorten-64-to-32
+ -Werror=write-strings
+ -Wmissing-noreturn
+ -Wno-constant-logical-operand
+ -Wno-missing-field-initializers
+ -Wno-overlength-strings
+ -Wno-parentheses-equality
+ -Wno-self-assign
+ -Wno-tautological-compare
+ -Wno-unused-local-typedef
+ -Wno-unused-parameter
+ -Wunused-variable
+ '
+ - LDFLAGS=-Wno-unused-command-line-argument
+
+ - &rubyspec
+ name: ruby/spec on Ruby 2.3 # to ensure version guards are correctly added
+ <<: *linux
+ language: ruby
+ rvm: 2.3.8
+ addons:
+ apt:
+ packages:
+ before_install:
+ install:
+ before_script: chmod -R u+w spec/ruby
+ # -j randomly hangs. Using -fs to make sure we can know problematic spec on failure.
+ script: ruby -C spec/ruby ../mspec/bin/mspec -fs .
+
+ - &x86_64-darwin17
+ name: x86_64-darwin17
+ <<: *osx
+ env:
+ - CONFIG_FLAG=--with-opt-dir=/usr/local/opt/openssl@1.1:/usr/local/opt/zlib
+ - TEST_ALL_OPTS="$JOBS -q --tty=no --excludes=\$(TESTSDIR)/excludes/_travis/osx"
+
+ - &universal-darwin17
+ name: uinversal.x86_64h-darwin17
+ <<: *osx
+ <<: *cron-only
+ <<: *make-test-only
+ env:
+ - CONFIG_FLAG=--with-arch=x86_64h,x86_64,i386
+ - TEST_ALL_OPTS="$JOBS -q --tty=no --excludes=\$(TESTSDIR)/excludes/_travis/osx"
matrix:
include:
# to reduce time for finishing all jobs, run the slowest osx build first.
- - name: x86_64-darwin17
- os: osx
- compiler: clang
- env:
- - "CONFIG_FLAG='--with-opt-dir=/usr/local/opt/openssl@1.1:/usr/local/opt/zlib'"
- - "JOBS=" # osx build randomly fails with `-j`
- # `-v` prints too long outputs, but sometimes it osx build hangs and we can't know which test is bad without `-v`.
- - "TEST_ALL_TESTOPTS='-v --color=never --job-status=normal --excludes=$(TESTSDIR)/excludes/_travis'"
- before_install:
- # Bare "brew update" nukes everything.
- # These steps are very carefully chosen to avoid breaking things.
- - brew_core_dir=/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/
- - git -C $brew_core_dir fetch -q origin
- - git -C $brew_core_dir checkout -q 25590710e3f7af9cfa173c5cd3eb41a0532107b6
- - HOMEBREW_NO_AUTO_UPDATE=1 brew upgrade openssl@1.1
- - /usr/local/opt/openssl@1.1/bin/openssl version
- - name: x86_64-linux --with-jemalloc
- os: linux
- compiler: gcc
- env:
- - "CONFIG_FLAG='--with-gmp --with-jemalloc --with-valgrind'"
- - "JOBS=-j`nproc`"
- - name: x86_64-linux
- os: linux
- compiler: gcc-8
- env:
- - "JOBS=-j`nproc`"
- # - name: uinversal.i386-darwin17
- # os: osx
- # compiler: clang
- # env:
- # - "CONFIG_FLAG='--with-arch=i386'"
- # - "JOBS=\"-j`sysctl -n hw.activecpu`\""
- # - name: uinversal.x86_64h-darwin17
- # os: osx
- # compiler: clang
- # env:
- # - "CONFIG_FLAG='--with-arch=x86_64h'"
- # - "JOBS=\"-j`sysctl -n hw.activecpu`\""
- - name: i686-linux
- os: linux
- sudo: required
- compiler: "'gcc-8 -m32'"
- env:
- - "CONFIG_FLAG='debugflags=-g0'"
- - "JOBS=-j`nproc`"
- - "SETARCH='setarch i686 --verbose --3gb'"
- addons:
- apt:
- config:
- retries: true
- update: true
- sources:
- - ubuntu-toolchain-r-test
- packages:
- - gcc-8-multilib
- - libffi-dev:i386
- - libffi6:i386
- - libgdbm-dev:i386
- - libgdbm3:i386
- - libncurses5-dev:i386
- - libncurses5:i386
- - libncursesw5-dev:i386
- - libreadline6-dev:i386
- - libreadline6:i386
- - libssl-dev:i386
- - libssl1.0.0:i386
- - linux-libc-dev:i386
- - zlib1g-dev:i386
- - zlib1g:i386
- - name: pedanticism
- os: linux
- compiler: "'clang -ansi -Werror=pedantic -pedantic-errors -std=iso9899:1990'"
- env:
- - "CONFIG_FLAG="
- - "JOBS="
- # construct warnflags (using bashism...)
- before_install: |
- warnflags_array=(
- -Wall
- -Wextra
- -Werror=declaration-after-statement
- -Werror=deprecated-declarations
- -Werror=division-by-zero
- -Werror=extra-tokens
- -Werror=implicit-function-declaration
- -Werror=implicit-int
- -Werror=long-long
- -Werror=pointer-arith
- -Werror=shorten-64-to-32
- -Werror=write-strings
- -Wmissing-noreturn
- -Wno-constant-logical-operand
- -Wno-extended-offsetof
- -Wno-missing-field-initializers
- -Wno-overlength-strings
- -Wno-parentheses-equality
- -Wno-self-assign
- -Wno-tautological-compare
- -Wno-unused-local-typedef
- -Wno-unused-parameter
- -Wunused-variable
- )
- CONFIG_FLAG_ARRAY=(
- warnflags="${warnflags_array[*]}"
- LDFLAGS=-Wno-unused-command-line-argument)
- - os: linux
- language: ruby
- rvm: 2.3.8
- name: Running ruby/spec on 2.3 to ensure version guards are correctly added
- addons:
- apt:
- packages:
- before_install:
- install:
- before_script: chmod -R u+w spec/ruby
- # -j randomly hangs. Using -fs to make sure we can know problematic spec on failure.
- script: ruby -C spec/ruby ../mspec/bin/mspec -fs .
+ - <<: *x86_64-darwin17
+ - <<: *x86_64-linux
+ - <<: *i686-linux
+ - <<: *jemalloc
+ - <<: *pedanticism
+ - <<: *ASAN
+ - <<: *MSAN
+ - <<: *UBSAN
+ - <<: *VM_CHECK_MODE
+ - <<: *FIBER_USE_sjlj
+ - <<: *TOKEN_THREADED_CODE
+ - <<: *CALL_THREADED_CODE
+ - <<: *NO_THREADED_CODE
+ - <<: *rubyspec
allow_failures:
- - name: pedanticism
+ - name: uinversal.x86_64h-darwin17
+ - name: -fsanitize=address
+ - name: -fsanitize=memory
+ - name: -fsanitize=undefined
fast_finish: true
before_script:
- - "echo JOBS=$JOBS SETARCH=${SETARCH=}"
- - "$SETARCH uname -a"
- - "$SETARCH uname -r"
- - "rm -fr .ext autom4te.cache"
- - "echo $TERM"
+ - echo JOBS=${JOBS} SETARCH=${SETARCH}
+ - $SETARCH uname -a
+ - $SETARCH uname -r
+ - rm -fr .ext autom4te.cache
+ - echo $TERM
+ - |-
+ [ -d ~/.downloaded-cache ] ||
+ mkdir ~/.downloaded-cache
+ - ln -s ~/.downloaded-cache
- "> config.status"
- - "sed -f tool/prereq.status Makefile.in common.mk > Makefile"
- - "make update-config_files"
- - "make touch-unicode-files"
- - "make -s $JOBS srcs UNICODE_FILES=."
- - "rm config.status Makefile rbconfig.rb .rbconfig.time"
- - "mkdir build config_1st config_2nd"
- - "chmod -R a-w ."
- - "chmod u+w build config_1st config_2nd"
- - "cd build"
- - "$SETARCH ../configure -C --disable-install-doc --prefix=/tmp/ruby-prefix --with-gcc=\"$CC\" $CONFIG_FLAG \"${CONFIG_FLAG_ARRAY[@]}\""
- - "cp -pr config.cache config.status .ext/include ../config_1st"
- - "$SETARCH make reconfig"
- - "cp -pr config.cache config.status .ext/include ../config_2nd"
- - "(cd .. && exec diff -ru config_1st config_2nd)"
- - "$SETARCH make -s $JOBS && make install"
+ - sed -f tool/prereq.status Makefile.in common.mk > Makefile
+ - make -s $JOBS update-download
+ - make -s $JOBS srcs
+ - rm config.status Makefile rbconfig.rb .rbconfig.time
+ - |-
+ if [ -d ~/config_2nd ]; then
+ cp -pr ~/config_2nd build
+ else
+ mkdir build
+ fi
+ - mkdir config_1st config_2nd
+ - chmod -R a-w .
+ - chmod -R u+w build config_1st config_2nd
+ - cd build
+ - ccache --show-stats
+ - |-
+ case "$CC" in
+ gcc*) CC="ccache $CC${GCC_FLAGS:+ }$GCC_FLAGS -fno-diagnostics-color";;
+ clang*) CC="ccache $CC${GCC_FLAGS:+ }$GCC_FLAGS -fno-color-diagnostics";;
+ esac
+ - |-
+ [ ! -f config.cache ] ||
+ [ "$CC" = "`sed -n s/^ac_cv_prog_CC=//p config.cache`" ] ||
+ (set -x; exec rm config.cache)
+ - $SETARCH ../configure -C --disable-install-doc --prefix=$RUBY_PREFIX $CONFIG_FLAG
+ - cp -pr config.cache config.status .ext/include ../config_1st
+ - $SETARCH make reconfig
+ - cp -pr config.cache config.status .ext/include ../config_2nd
+ - (cd .. && exec diff -ru config_1st config_2nd)
+ - chmod u+w ..
+ - rm -r ~/config_2nd
+ - mv ../config_2nd ~
+ - chmod u-w ..
+ - $SETARCH make -s $JOBS && make install
+ - ccache --show-stats
+ - |-
+ [ -z "${GEMS_FOR_TEST}" ] ||
+ $RUBY_PREFIX/bin/gem install --no-document $GEMS_FOR_TEST
script:
- - "$SETARCH make -s test TESTOPTS=--color=never"
- - "export TEST_ALL_TESTOPTS=\"${TEST_ALL_TESTOPTS:- -q --color=never --job-status=normal}\""
- - "$SETARCH make -s $JOBS test-all -o exts TESTOPTS=\"$TEST_ALL_TESTOPTS\""
- - "$SETARCH make -s $JOBS test-spec MSPECOPT=-fs" # not using `-j` because sometimes `mspec -j` silently dies
+ - $SETARCH make -s test TESTOPTS="${TESTOPTS=$JOBS -q --tty=no}"
+ - $SETARCH make -s test-all -o exts TESTOPTS="${TEST_ALL_OPTS=$TESTOPTS}"
+ - $SETARCH make -s test-spec MSPECOPT=-ff # not using `-j` because sometimes `mspec -j` silently dies
# Branch matrix. Not all branches are Travis-ready so we limit branches here.
branches:
diff --git a/LEGAL b/LEGAL
index 04797516910f68..4ff1726cbaae48 100644
--- a/LEGAL
+++ b/LEGAL
@@ -848,3 +848,33 @@ test/rubygems:
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE.
+
+lib/bundler:
+lib/bundler.rb:
+lib/bundler.gemspec:
+spec/bundler:
+man/bundle-*,gemfile.*:
+
+ Portions copyright (c) 2010 Andre Arko
+ Portions copyright (c) 2009 Engine Yard
+
+ MIT License
+
+ 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
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/Makefile.in b/Makefile.in
index 73f0608544a674..ac8652ea1db8dc 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -16,6 +16,7 @@ top_srcdir = $(srcdir)
hdrdir = $(srcdir)/include
PLATFORM_DIR = @PLATFORM_DIR@
+CC_WRAPPER = @XCC_WRAPPER@
CC = @CC@
CPP = @CPP@
LD = @LD@
@@ -99,6 +100,7 @@ LIBS = @LIBS@ $(EXTLIBS)
MISSING = @LIBOBJS@ @ALLOCA@
ENABLE_SHARED = @ENABLE_SHARED@
LDSHARED = @LIBRUBY_LDSHARED@
+DLDSHARED = @DLDSHARED@
DLDFLAGS = @LIBRUBY_DLDFLAGS@ $(XLDFLAGS) $(ARCH_FLAG)
SOLIBS = @SOLIBS@
ENABLE_DEBUG_ENV = @ENABLE_DEBUG_ENV@
@@ -141,6 +143,9 @@ XRUBY_RUBYLIBDIR = @XRUBY_RUBYLIBDIR@
XRUBY_RUBYHDRDIR = @XRUBY_RUBYHDRDIR@
BOOTSTRAPRUBY = @BOOTSTRAPRUBY@
+COROUTINE_H = @X_FIBER_COROUTINE_H@
+COROUTINE_OBJ = $(COROUTINE_H:.h=.@OBJEXT@)
+
#### End of system configuration section. ####
MAJOR= @MAJOR@
@@ -239,7 +244,7 @@ DESTDIR = @DESTDIR@
configure_args = @configure_args@
#### End of variables
-.SUFFIXES: .inc .h .c .y .i .$(DTRACE_EXT)
+.SUFFIXES: .inc .h .c .y .i .$(ASMEXT) .$(DTRACE_EXT)
all:
@@ -338,12 +343,13 @@ uncommon.mk: $(srcdir)/common.mk
.PHONY: reconfig
reconfig-args = $(srcdir)/$(CONFIGURE) $(configure_args)
config.status-args = ./config.status --recheck
-reconfig-exec-0 = test -t 1 && { CONFIGURE_TTY=yes; export CONFIGURE_TTY; }; exec 3>&1; exit `exec 4>&1; { "$$@" 3>&- 4>&-; echo $$? 1>&4; } | fgrep -v '(cached)' 1>&3 3>&- 4>&-`
+reconfig-exec-0 = test -t 1 && { : $${CONFIGURE_TTY=yes}; export CONFIGURE_TTY; }; exec 3>&1; exit `exec 4>&1; { "$$@" 3>&- 4>&-; echo $$? 1>&4; } | fgrep -v '(cached)' 1>&3 3>&- 4>&-`
reconfig-exec-1 = set -x; "$$@"
reconfig config.status: $(srcdir)/$(CONFIGURE) $(srcdir)/enc/Makefile.in \
$(srcdir)/include/ruby/version.h
@PWD= MINIRUBY="$(MINIRUBY)"; export MINIRUBY; \
+ warnflags="@warnflags@"; export warnflags; \
set $(SHELL) $($@-args); $(reconfig-exec-$(V))
$(srcdir)/$(CONFIGURE): $(srcdir)/configure.ac $(srcdir)/aclocal.m4
@@ -401,11 +407,11 @@ $(srcdir)/enc/jis/props.h: enc/jis/props.kwd
@$(ECHO) compiling $<
$(Q) $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@ -c $<
-.s.@OBJEXT@:
+.$(ASMEXT).@OBJEXT@:
@$(ECHO) assembling $<
- $(Q) $(AS) $(ASFLAGS) -o $@ $<
+ $(Q) $(CC) $(ASFLAGS) -o $@ -c $<
-.c.S:
+.c.$(ASMEXT):
@$(ECHO) translating $<
$(Q) $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@ -S $<
@@ -443,6 +449,7 @@ clean-local::
$(Q)$(RM) $(MJIT_MIN_HEADER) $(MJIT_MIN_HEADER:.h=)$(MJIT_HEADER_SUFFIX:%=*).h
$(Q)$(RM) $(MJIT_HEADER_INSTALL_DIR)/rb_mjit_min_header-*.h
$(Q)$(RM) $(TIMESTAMPDIR)/$(MJIT_HEADER:.h=)$(MJIT_HEADER_SUFFIX).time mjit_config.h
+ $(Q)$(RM) -r mjit_build_dir.*
-$(Q) $(RMDIRS) $(MJIT_HEADER_INSTALL_DIR) 2> $(NULL) || exit 0
# DTrace static library hacks described here:
@@ -455,7 +462,7 @@ clean-local::
$(Q)$(RM) ext/extinit.c ext/extinit.$(OBJEXT) ext/ripper/y.output \
enc/encinit.c enc/encinit.$(OBJEXT)
-$(Q)$(RM) $(pkgconfig_DATA)
- -$(Q)$(RMALL) exe/ ruby-runner.h *.dSYM
+ -$(Q)$(RMALL) exe/ ruby-runner.$(OBJEXT) ruby-runner.h *.dSYM
distclean-local::
$(Q)$(RM) ext/config.cache $(RBCONFIG) Doxyfile
@@ -512,6 +519,8 @@ ext/extinit.$(OBJEXT): ext/extinit.c $(SETUP)
enc/encinit.$(OBJEXT): enc/encinit.c $(SETUP)
+cont.$(OBJEXT): $(COROUTINE_H)
+
test-bundled-gems-run:
$(Q) set -e; while read gem _; do \
echo testing $$gem gem && \
@@ -553,6 +562,10 @@ update-simplecov:
update-coverage: update-simplecov update-simplecov-html update-doclie
+update-known-errors:
+ errno --list | cut -d' ' -f1 | sort -u - $(srcdir)/defs/known_errors.def | \
+ $(IFCHANGE) $(srcdir)/defs/known_errors.def -
+
INSNS = opt_sc.inc optinsn.inc optunifs.inc insns.inc insns_info.inc \
vmtc.inc vm.inc mjit_compile.inc
@@ -576,55 +589,34 @@ un-runnable:
mjit_config.h:
$(ECHO) making $@
@{ \
- quote() { \
- printf "#define $$1"; shift; \
- $${1+printf} $${1+' "%s"'$$sep} $${1+"$$@"}; \
- echo; \
- }; \
- parse_arch_flag() { \
- while [ "$$#" -gt 0 ]; do \
- case "$$1" in \
- -arch) \
- shift; \
- archs="$${archs:+$$archs }$$1"; \
- shift; \
- ;; \
- *) \
- arch_flag="$${arch_flag:+$${arch_flag} }$$1"; \
- shift; \
- ;; \
- esac; \
- done; \
- }; \
+ . $(srcdir)/tool/mjit_archflag.sh; \
+ parse_arch_flags "$(UNIVERSAL_ARCHNAMES)" $(ARCH_FLAG); \
test "$(Q)" = @ || set -x; \
- archs="$(UNIVERSAL_ARCHNAMES)"; \
- arch_flag=""; \
- parse_arch_flag $(ARCH_FLAG); \
- need_mjit_archflag="$${archs}$${arch_flag}"; \
echo '#ifndef RUBY_MJIT_CONFIG_H'; \
echo '#define RUBY_MJIT_CONFIG_H 1'; \
echo; \
sep=; \
- quote MJIT_BUILD_DIR "`$(CHDIR) . && pwd`"; \
quote MJIT_MIN_HEADER_NAME "/$(MJIT_HEADER_INSTALL_DIR)/$(MJIT_MIN_HEADER_NAME)"; \
sep=,; \
quote "MJIT_CC_COMMON " $(MJIT_CC); \
- quote "MJIT_CFLAGS $${need_mjit_archflag:+ MJIT_ARCHFLAG}" $(MJIT_CFLAGS); \
+ quote "MJIT_CFLAGS MJIT_ARCHFLAG" $(MJIT_CFLAGS); \
quote "MJIT_OPTFLAGS " $(MJIT_OPTFLAGS); \
quote "MJIT_DEBUGFLAGS " $(MJIT_DEBUGFLAGS); \
quote "MJIT_LDSHARED " $(MJIT_LDSHARED); \
- quote "MJIT_DLDFLAGS $${need_mjit_archflag:+ MJIT_ARCHFLAG}" $(MJIT_DLDFLAGS); \
+ quote "MJIT_DLDFLAGS MJIT_ARCHFLAG" $(MJIT_DLDFLAGS); \
quote "MJIT_LIBS " $(LIBRUBYARG_SHARED); \
- $${archs:+echo} $${archs:+'#if 0'}; \
- for arch in $$archs; do \
- echo "#elif defined __$${arch%=*}__"; \
- quote " MJIT_ARCHFLAG " -arch $${arch%=*}; \
- done; \
- $${archs:+echo} $${archs:+'#else'}; \
- if [ "$${arch_flag}" != "" ]; then \
- quote " MJIT_ARCHFLAG " $${arch_flag}; \
- fi; \
- $${archs:+echo} $${archs:+'#endif'}; \
+ quote 'PRELOADENV "@PRELOADENV@"'; \
+ indent=$${archs:+' '}; \
+ define_arch_flags; \
echo; \
echo '#endif /* RUBY_MJIT_CONFIG_H */'; \
} > $@
+
+yes-test-almost yes-test-all: mjit_build_dir.$(SOEXT)
+mjit_build_dir.$(SOEXT): $(MJIT_MIN_HEADER)
+ $(ECHO) making $@
+ $(Q) { \
+ echo 'const char MJIT_BUILD_DIR[] = "'"`$(CHDIR) . && pwd`"'";'; \
+ } > $(@:.$(SOEXT)=.c)
+ $(Q) cat $(@:.$(SOEXT)=.c)
+ $(Q) $(DLDSHARED) $(MJIT_DLDFLAGS) $(ARCH_FLAG) $(CFLAGS) $(CPPFLAGS) $(@:.$(SOEXT)=.c) $(OUTFLAG)$@
diff --git a/NEWS b/NEWS
index 722ef2520ac98e..189916a04a5310 100644
--- a/NEWS
+++ b/NEWS
@@ -16,7 +16,11 @@ sufficient information, see the ChangeLog file or Redmine
* $SAFE
is a process global state and we can set 0 again. [Feature #14250]
-* refinements take place at block passing. [Feature #14223]
+* refinements takes place at block passing. [Feature #14223]
+
+* refinements takes place at Kernel#public_send. [Feature #15326]
+
+* refinements takes place at Kernel#respond_to?. [Feature #15327]
* +else+ without +rescue+ now causes a syntax error. [EXPERIMENTAL]
@@ -39,7 +43,7 @@ sufficient information, see the ChangeLog file or Redmine
* Print exception backtrace and error message in reverse order when the
exception is not caught and STDOUT is unchanged and a tty. [Feature #8661]
-* Print `cause` of the exception if the exception is not caught and printed
+* Print +cause+ of the exception if the exception is not caught and printed
its backtraces and error message [Feature #8257]
=== Core classes updates (outstanding ones only)
@@ -82,6 +86,12 @@ sufficient information, see the ChangeLog file or Redmine
[Enumerable]
+ [New methods]
+
+ * Enumerable#chain returns an enumerator object that iterates over the
+ elements of the receiver and then those of each argument in sequence.
+ [Feature #15144]
+
[Modified methods]
* Enumerable#to_h now maps elements to new keys and values by the block if
@@ -91,6 +101,14 @@ sufficient information, see the ChangeLog file or Redmine
* Enumerable#filter is a new alias for Enumerable#select. [Feature #13784]
+[Enumerator]
+
+ [New methods]
+
+ * Enumerator#+ returns an enumerator object that iterates over the
+ elements of the receiver and then those of the other operand.
+ [Feature #15144]
+
[Enumerator::ArithmeticSequence]
* This is a new class to represent a generator of an arithmetic sequence,
@@ -98,6 +116,12 @@ sufficient information, see the ChangeLog file or Redmine
for representing what is similar to Python's slice. You can get an
instance of this class from Numeric#step and Range#step.
+[Enumerator::Chain]
+
+ * This is a new class to represent a chain of enumerables that works as a
+ single enumerator, generated by such methods as Enumerable#chain and
+ Enumerator#+.
+
[Enumerator::Lazy]
[Aliased methods]
@@ -168,8 +192,14 @@ sufficient information, see the ChangeLog file or Redmine
[New options]
- * KeyError#initialize accepts +:receiver+ and +:key+ options to
- set receiver and key in Ruby code. [Feature #14313]
+ * KeyError.new accepts +:receiver+ and +:key+ options to set receiver and
+ key in Ruby code. [Feature #14313]
+
+[Method]
+
+ [New methods]
+
+ * added Method#<< and Method#>> for Proc composition. [Feature #6284]
[Module]
@@ -184,15 +214,21 @@ sufficient information, see the ChangeLog file or Redmine
[New options]
- * NameError#initialize accepts +:receiver+ option to set receiver
- in Ruby code. [Feature #14313]
+ * NameError.new accepts +:receiver+ option to set receiver in Ruby
+ code. [Feature #14313]
+
+[NilClass]
+
+ [New methods]
+
+ * NilClass#=~ is added for compatibility. [Feature #15231]
[NoMethodError]
[New options]
- * NoMethodError#initialize accepts +:receiver+ option to set receiver
- in Ruby code. [Feature #14313]
+ * NoMethodError.new accepts +:receiver+ option to set receiver in Ruby
+ code. [Feature #14313]
[Numeric]
@@ -203,6 +239,10 @@ sufficient information, see the ChangeLog file or Redmine
[Proc]
+ [New methods]
+
+ * added Proc#<< and Proc#>> for Proc composition. [Feature #6284]
+
[Incompatible changes]
* Proc#call doesn't change $SAFE
any more. [Feature #14250]
@@ -226,16 +266,19 @@ sufficient information, see the ChangeLog file or Redmine
* Range#step now returns an instance of Enumerator::ArithmeticSequence
class rather than one of Enumerator class.
-[RubyVM::AST]
+[RubyVM::AbstractSyntaxTree]
[New methods]
- * RubyVM::AST.parse parses a given string and returns AST
+ * RubyVM::AbstractSyntaxTree.parse parses a given string and returns AST
nodes. [experimental]
- * RubyVM::AST.parse_file parses a given file and returns AST
+ * RubyVM::AbstractSyntaxTree.parse_file parses a given file and returns AST
nodes. [experimental]
+ * RubyVM::AbstractSyntaxTree.of returns AST nodes of the given proc or method.
+ [experimental]
+
[String]
[New features]
@@ -269,6 +312,12 @@ sufficient information, see the ChangeLog file or Redmine
=== Stdlib updates (outstanding ones only)
+[Bundler]
+
+ * Add Bundler to Standard Library. [Feature #12733]
+
+ * Use 1.17.1. It's latest stable version.
+
[Coverage]
A oneshot_lines mode is added. [Feature #15022]
@@ -311,9 +360,15 @@ sufficient information, see the ChangeLog file or Redmine
[New methods]
- * Matrix#antisymmetric?
+ * Matrix#antisymmetric? / #skew_symmetric?
+
+ * Matrix#map! / #collect! [Feature #14151]
+
+ * Matrix#[]=
+
+ * Vector#map! / #collect!
- * Matrix#reflexive?
+ * Vector#[]=
[Net]
@@ -387,16 +442,20 @@ sufficient information, see the ChangeLog file or Redmine
=== Compatibility issues (excluding feature bug fixes)
+[Dir]
+
+ * Dir.glob with '\0'
-separated pattern list will be deprecated,
+ and is now warned. [Feature #14643]
+
[File]
* File.read, File.binread, File.write, File.binwrite, File.foreach, and
File.readlines do not invoke external commands even if the path starts
with the pipe character '|'
. [Feature #14245]
-[Dir]
+[Object]
- * Dir.glob with '\0'
-separated pattern list will be deprecated,
- and is now warned. [Feature #14643]
+ * Object#=~ is deprecated. [Feature #15231]
=== Stdlib compatibility issues (excluding feature bug fixes)
@@ -454,6 +513,11 @@ sufficient information, see the ChangeLog file or Redmine
* timer thread is eliminated for platforms with POSIX timers [Misc #14937]
+* Transient Heap (theap) is supported. [Bug #14858] [Feature #14989]
+ theap is managed heap for short-living memory objects. For example,
+ making small and short-living Hash object is x2 faster. With rdoc benchmark,
+ we maesured 6-7% performance improvement.
+
=== Miscellaneous changes
* On macOS, shared libraries no longer include a full version number of Ruby
diff --git a/README.md b/README.md
index 099bc1435edfdc..1e76fda4f06a03 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,6 @@
-[](https://travis-ci.org/ruby/ruby)
+[](https://travis-ci.org/ruby/ruby)
[](https://ci.appveyor.com/project/ruby/ruby/branch/trunk)
+[](https://app.wercker.com/project/byKey/e5e7e1704f62b76525022aa424aef6ef)
# What's Ruby
diff --git a/addr2line.c b/addr2line.c
index 30e7509f4ec540..a11d32b73d1efc 100644
--- a/addr2line.c
+++ b/addr2line.c
@@ -10,6 +10,7 @@
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wpedantic"
+#pragma clang diagnostic ignored "-Wgcc-compat"
#elif defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
@@ -65,7 +66,11 @@ void *alloca();
#endif
#ifdef HAVE_MACH_O_LOADER_H
+# include
+# include
# include
+# include
+# include
#endif
#ifdef USE_ELF
@@ -1519,10 +1524,10 @@ debug_info_read(DebugInfoReader *reader, int num_traces, void **traces,
/* enumerate abbrev */
for (;;) {
DebugInfoValue v = {{}};
- //ptrdiff_t pos = reader->p - reader->p0;
+ /* ptrdiff_t pos = reader->p - reader->p0; */
if (!di_read_record(reader, &v)) break;
- //fprintf(stderr,"\n%d:%tx: AT:%lx FORM:%lx\n",__LINE__,pos,v.at,v.form);
- //div_inspect(&v);
+ /* fprintf(stderr,"\n%d:%tx: AT:%lx FORM:%lx\n",__LINE__,pos,v.at,v.form); */
+ /* div_inspect(&v); */
switch (v.at) {
case DW_AT_name:
line.sname = get_cstr_value(&v);
@@ -1542,10 +1547,10 @@ debug_info_read(DebugInfoReader *reader, int num_traces, void **traces,
goto skip_die;
case DW_AT_inline:
/* 1 or 3 */
- break; // goto skip_die;
+ break; /* goto skip_die; */
case DW_AT_abstract_origin:
read_abstract_origin(reader, v.as.uint64, &line);
- break; //goto skip_die;
+ break; /* goto skip_die; */
}
}
/* ranges_inspect(reader, &ranges); */
@@ -1819,11 +1824,16 @@ static uintptr_t
fill_lines(int num_traces, void **traces, int check_debuglink,
obj_info_t **objp, line_info_t *lines, int offset)
{
+# ifdef __LP64__
+# define LP(x) x##_64
+# else
+# define LP(x) x
+# endif
int fd;
off_t filesize;
- char *file, *p;
+ char *file, *p = NULL;
obj_info_t *obj = *objp;
- struct mach_header_64 *header;
+ struct LP(mach_header) *header;
uintptr_t dladdr_fbase = 0;
{
@@ -1834,6 +1844,7 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
size_t basesize = size - (base - binary_filename);
s += size;
max -= size;
+ p = s;
size = strlcpy(s, ".dSYM/Contents/Resources/DWARF/", max);
if (size == 0) goto fail;
s += size;
@@ -1841,12 +1852,17 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
if (max <= basesize) goto fail;
memcpy(s, base, basesize);
s[basesize] = 0;
- }
- fd = open(binary_filename, O_RDONLY);
- if (fd < 0) {
- goto fail;
+ fd = open(binary_filename, O_RDONLY);
+ if (fd < 0) {
+ *p = 0; /* binary_filename becomes original file name */
+ fd = open(binary_filename, O_RDONLY);
+ if (fd < 0) {
+ goto fail;
+ }
+ }
}
+
filesize = lseek(fd, 0, SEEK_END);
if (filesize < 0) {
int e = errno;
@@ -1875,19 +1891,56 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
obj->mapped = file;
obj->mapped_size = (size_t)filesize;
- header = (struct mach_header_64 *)file;
- if (header->magic != MH_MAGIC_64) {
- /* TODO: universal binaries */
- kprintf("'%s' is not a 64-bit Mach-O file!\n",binary_filename);
+ header = (struct LP(mach_header) *)file;
+ if (header->magic == LP(MH_MAGIC)) {
+ /* non universal binary */
+ p = file;
+ }
+ else if (header->magic == FAT_CIGAM) {
+ struct fat_header *fat = (struct fat_header *)file;
+ char *q = file + sizeof(*fat);
+ uint32_t nfat_arch = __builtin_bswap32(fat->nfat_arch);
+ /* fprintf(stderr,"%d: fat:%s %d\n",__LINE__, binary_filename,nfat_arch); */
+ for (uint32_t i = 0; i < nfat_arch; i++) {
+ struct fat_arch *arch = (struct fat_arch *)q;
+ cpu_type_t cputype = __builtin_bswap32(arch->cputype);
+ cpu_subtype_t cpusubtype = __builtin_bswap32(arch->cpusubtype);
+ uint32_t offset = __builtin_bswap32(arch->offset);
+ /* fprintf(stderr,"%d: fat %d %x/%x %x/%x\n",__LINE__, i, _mh_execute_header.cputype,_mh_execute_header.cpusubtype, cputype,cpusubtype); */
+ if (_mh_execute_header.cputype == cputype &&
+ (_mh_execute_header.cpusubtype & ~CPU_SUBTYPE_MASK) == cpusubtype) {
+ p = file + offset;
+ file = p;
+ header = (struct LP(mach_header) *)p;
+ if (header->magic == LP(MH_MAGIC)) {
+ goto found_mach_header;
+ }
+ break;
+ }
+ q += sizeof(*arch);
+ }
+ kprintf("'%s' is not a Mach-O universal binary file!\n",binary_filename);
+ close(fd);
+ goto fail;
+ }
+ else {
+ kprintf("'%s' is not a "
+# ifdef __LP64__
+ "64"
+# else
+ "32"
+# endif
+ "-bit Mach-O file!\n",binary_filename);
close(fd);
goto fail;
}
+found_mach_header:
+ p += sizeof(*header);
- p = file + sizeof(struct mach_header_64);
for (uint32_t i = 0; i < (uint32_t)header->ncmds; i++) {
struct load_command *lcmd = (struct load_command *)p;
switch (lcmd->cmd) {
- case LC_SEGMENT_64:
+ case LP(LC_SEGMENT):
{
static const char *debug_section_names[] = {
"__debug_abbrev",
@@ -1896,15 +1949,15 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
"__debug_ranges",
"__debug_str"
};
- struct segment_command_64 *scmd = (struct segment_command_64 *)lcmd;
+ struct LP(segment_command) *scmd = (struct LP(segment_command) *)lcmd;
if (strcmp(scmd->segname, "__TEXT") == 0) {
obj->vmaddr = scmd->vmaddr;
}
else if (strcmp(scmd->segname, "__DWARF") == 0) {
- p += sizeof(struct segment_command_64);
+ p += sizeof(struct LP(segment_command));
for (uint64_t i = 0; i < scmd->nsects; i++) {
- struct section_64 *sect = (struct section_64 *)p;
- p += sizeof(struct section_64);
+ struct LP(section) *sect = (struct LP(section) *)p;
+ p += sizeof(struct LP(section));
for (int j=0; j < DWARF_SECTION_COUNT; j++) {
struct dwarf_section *s = obj_dwarf_section_at(obj, j);
@@ -1924,23 +1977,39 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
}
break;
-#if 0
case LC_SYMTAB:
{
- struct symtab_command *c = (struct symtab_command *)lcmd;
- struct nlist_64 *nl = (struct nlist *)(file + c->symoff);
- char *strtab = file + c->stroff;
+ struct symtab_command *cmd = (struct symtab_command *)lcmd;
+ struct LP(nlist) *nl = (struct LP(nlist) *)(file + cmd->symoff);
+ char *strtab = file + cmd->stroff, *sname;
uint32_t j;
- kprintf("[%2d]: %x/symtab %lx\n", i, c->cmd, p);
- for (j = 0; j < c->nsyms; j++) {
- struct nlist_64 *e = &nl[j];
- if (!(e->n_type & N_STAB)) continue;
- /* if (e->n_type != N_FUN) continue; */
- kprintf("[%2d][%4d]: %02x/%x/%x: %s %lx\n", i, j,
- e->n_type,e->n_sect,e->n_desc,strtab+e->n_un.n_strx,e->n_value);
+ uintptr_t saddr;
+ /* kprintf("[%2d]: %x/symtab %p\n", i, cmd->cmd, p); */
+ for (j = 0; j < cmd->nsyms; j++) {
+ uintptr_t symsize, d;
+ struct LP(nlist) *e = &nl[j];
+ /* kprintf("[%2d][%4d]: %02x/%x/%x: %s %llx\n", i, j, e->n_type,e->n_sect,e->n_desc,strtab+e->n_un.n_strx,e->n_value); */
+ if (e->n_type != N_FUN) continue;
+ if (e->n_sect) {
+ saddr = (uintptr_t)e->n_value + obj->base_addr - obj->vmaddr;
+ sname = strtab + e->n_un.n_strx;
+ /* kprintf("[%2d][%4d]: %02x/%x/%x: %s %llx\n", i, j, e->n_type,e->n_sect,e->n_desc,strtab+e->n_un.n_strx,e->n_value); */
+ continue;
+ }
+ for (int k = offset; k < num_traces; k++) {
+ d = (uintptr_t)traces[k] - saddr;
+ symsize = e->n_value;
+ /* kprintf("%lx %lx %lx\n",saddr,symsize,traces[k]); */
+ if (lines[k].line > 0 || d > (uintptr_t)symsize)
+ continue;
+ /* fill symbol name and addr from .symtab */
+ if (!lines[k].sname) lines[k].sname = sname;
+ lines[k].saddr = saddr;
+ lines[k].path = obj->path;
+ lines[k].base_addr = obj->base_addr;
+ }
}
}
-#endif
}
p += lcmd->cmdsize;
}
@@ -2101,6 +2170,8 @@ rb_dump_backtrace_with_lines(int num_traces, void **traces)
path = info.dli_fname;
obj->path = path;
lines[i].path = path;
+ lines[i].sname = info.dli_sname;
+ lines[i].saddr = (uintptr_t)info.dli_saddr;
strlcpy(binary_filename, path, PATH_MAX);
if (fill_lines(num_traces, traces, 1, &obj, lines, i) == (uintptr_t)-1)
break;
diff --git a/appveyor.yml b/appveyor.yml
index 90dc6ee2460a54..3bf1568cd93ed2 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -16,6 +16,7 @@ environment:
- build: vs
vs: 140
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
+ GEMS_FOR_TEST: "timezone tzinfo"
notifications:
- provider: Webhook
url:
@@ -89,13 +90,14 @@ for:
- nmake -l
- nmake install-nodoc
- \usr\bin\ruby -v -e "p :locale => Encoding.find('locale'), :filesystem => Encoding.find('filesystem')"
+ - if not "%GEMS_FOR_TEST%" == "" \usr\bin\gem install --no-document %GEMS_FOR_TEST%
test_script:
- set /a JOBS=%NUMBER_OF_PROCESSORS%
- nmake -l "TESTOPTS=-v -q" btest
- nmake -l "TESTOPTS=-v -q" test-basic
- - nmake -l "TESTOPTS=-q -j%JOBS% --subprocess-timeout-scale=3.0 --exclude win32ole --exclude test_syntax --exclude test_open-uri --exclude test_bundled_ca" test-all
- # separately execute tests that may crash worker without -j.
- - nmake -l "TESTOPTS=-v --subprocess-timeout-scale=3.0" test-all TESTS="../test/win32ole ../test/ruby/test_syntax.rb ../test/open-uri/test_open-uri.rb ../test/rubygems/test_bundled_ca.rb"
+ - nmake -l "TESTOPTS=-q --subprocess-timeout-scale=3.0 --excludes=../test/excludes/_appveyor -j%JOBS% --exclude win32ole --exclude test_bignum --exclude test_syntax --exclude test_open-uri --exclude test_bundled_ca" test-all
+ # separately execute tests without -j which may crash worker with -j.
+ - nmake -l "TESTOPTS=-v --subprocess-timeout-scale=3.0 --excludes=../test/excludes/_appveyor" test-all TESTS="../test/win32ole ../test/ruby/test_bignum.rb ../test/ruby/test_syntax.rb ../test/open-uri/test_open-uri.rb ../test/rubygems/test_bundled_ca.rb"
- nmake -l test-spec MSPECOPT=-fs # not using `-j` because sometimes `mspec -j` silently dies on Windows
-
matrix:
@@ -134,9 +136,10 @@ for:
- mingw32-make -j%JOBS% up
- mingw32-make -j%JOBS%
- mingw32-make DESTDIR=../install install-nodoc
+ - if not "%GEMS_FOR_TEST%" == "" ..\install\bin\gem install --no-document %GEMS_FOR_TEST%
test_script:
- mingw32-make test
- - mingw32-make test-all TESTOPTS="--retry --job-status=normal --show-skip --subprocess-timeout-scale=1.5 -j %JOBS% --exclude win32ole --exclude test_open-uri"
- # separately execute tests that may crash worker without -j.
- - mingw32-make test-all TESTOPTS="--retry --job-status=normal --show-skip --subprocess-timeout-scale=1.5" TESTS="../ruby/test/win32ole ../ruby/test/open-uri/test_open-uri.rb"
+ - mingw32-make test-all TESTOPTS="--retry --job-status=normal --show-skip --subprocess-timeout-scale=1.5 --excludes=../ruby/test/excludes/_appveyor -j %JOBS% --exclude win32ole --exclude test_open-uri"
+ # separately execute tests without -j which may crash worker with -j.
+ - mingw32-make test-all TESTOPTS="--retry --job-status=normal --show-skip --subprocess-timeout-scale=1.5 --excludes=../ruby/test/excludes/_appveyor" TESTS="../ruby/test/win32ole ../ruby/test/open-uri/test_open-uri.rb"
- mingw32-make test-spec MSPECOPT=-fs # not using `-j` because sometimes `mspec -j` silently dies on Windows
diff --git a/array.c b/array.c
index 83a5ff41f56137..38ed1753bd83ae 100644
--- a/array.c
+++ b/array.c
@@ -14,12 +14,14 @@
#include "ruby/encoding.h"
#include "ruby/util.h"
#include "ruby/st.h"
-#include "internal.h"
#include "probes.h"
#include "id.h"
#include "debug_counter.h"
+#include "gc.h"
+#include "transient_heap.h"
+#include "internal.h"
-#ifndef ARRAY_DEBUG
+#if !ARRAY_DEBUG
# define NDEBUG
#endif
#include "ruby_assert.h"
@@ -42,17 +44,21 @@ VALUE rb_cArray;
#define ARY_HEAP_PTR(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr)
#define ARY_HEAP_LEN(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len)
+#define ARY_HEAP_CAPA(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.aux.capa)
+
#define ARY_EMBED_PTR(a) (assert(ARY_EMBED_P(a)), RARRAY(a)->as.ary)
#define ARY_EMBED_LEN(a) \
(assert(ARY_EMBED_P(a)), \
(long)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \
(RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT)))
-#define ARY_HEAP_SIZE(a) (assert(!ARY_EMBED_P(a)), assert(ARY_OWNS_HEAP_P(a)), RARRAY(a)->as.heap.aux.capa * sizeof(VALUE))
+#define ARY_HEAP_SIZE(a) (assert(!ARY_EMBED_P(a)), assert(ARY_OWNS_HEAP_P(a)), ARY_HEAP_CAPA(a) * sizeof(VALUE))
#define ARY_OWNS_HEAP_P(a) (!FL_TEST((a), ELTS_SHARED|RARRAY_EMBED_FLAG))
#define FL_SET_EMBED(a) do { \
assert(!ARY_SHARED_P(a)); \
FL_SET((a), RARRAY_EMBED_FLAG); \
+ RARY_TRANSIENT_UNSET(a); \
+ ary_verify(a); \
} while (0)
#define FL_UNSET_EMBED(ary) FL_UNSET((ary), RARRAY_EMBED_FLAG|RARRAY_EMBED_LEN_MASK)
#define FL_SET_SHARED(ary) do { \
@@ -102,7 +108,7 @@ VALUE rb_cArray;
} while (0)
#define ARY_CAPA(ary) (ARY_EMBED_P(ary) ? RARRAY_EMBED_LEN_MAX : \
- ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : RARRAY(ary)->as.heap.aux.capa)
+ ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : ARY_HEAP_CAPA(ary))
#define ARY_SET_CAPA(ary, n) do { \
assert(!ARY_EMBED_P(ary)); \
assert(!ARY_SHARED_P(ary)); \
@@ -130,11 +136,82 @@ VALUE rb_cArray;
} while (0)
#define FL_SET_SHARED_ROOT(ary) do { \
assert(!ARY_EMBED_P(ary)); \
+ assert(!RARRAY_TRANSIENT_P(ary)); \
FL_SET((ary), RARRAY_SHARED_ROOT_FLAG); \
} while (0)
#define ARY_SET(a, i, v) RARRAY_ASET((assert(!ARY_SHARED_P(a)), (a)), (i), (v))
+
+#if ARRAY_DEBUG
+#define ary_verify(ary) ary_verify_(ary, __FILE__, __LINE__)
+
+static VALUE
+ary_verify_(VALUE ary, const char *file, int line)
+{
+ assert(RB_TYPE_P(ary, T_ARRAY));
+
+ if (FL_TEST(ary, ELTS_SHARED)) {
+ VALUE root = RARRAY(ary)->as.heap.aux.shared;
+ const VALUE *ptr = ARY_HEAP_PTR(ary);
+ const VALUE *root_ptr = RARRAY_CONST_PTR_TRANSIENT(root);
+ long len = ARY_HEAP_LEN(ary), root_len = RARRAY_LEN(root);
+ assert(FL_TEST(root, RARRAY_SHARED_ROOT_FLAG));
+ assert(root_ptr <= ptr && ptr + len <= root_ptr + root_len);
+ ary_verify(root);
+ }
+ else if (ARY_EMBED_P(ary)) {
+ assert(!RARRAY_TRANSIENT_P(ary));
+ assert(!ARY_SHARED_P(ary));
+ assert(RARRAY_LEN(ary) <= RARRAY_EMBED_LEN_MAX);
+ }
+ else {
+#if 1
+ const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(ary);
+ long i, len = RARRAY_LEN(ary);
+ volatile VALUE v;
+ if (len > 1) len = 1; /* check only HEAD */
+ for (i=0; ias.heap.ptr, VALUE, new_capa, old_capa);
+ }
+ ary_verify(ary);
+}
+
+#if USE_TRANSIENT_HEAP
+static inline void
+rb_ary_transient_heap_evacuate_(VALUE ary, int transient, int promote)
+{
+ if (transient) {
+ VALUE *new_ptr;
+ const VALUE *old_ptr = ARY_HEAP_PTR(ary);
+ long capa = ARY_HEAP_CAPA(ary);
+ long len = ARY_HEAP_LEN(ary);
+
+ if (ARY_SHARED_ROOT_P(ary)) {
+ capa = len;
+ }
+
+ assert(ARY_OWNS_HEAP_P(ary));
+ assert(RARRAY_TRANSIENT_P(ary));
+ assert(!ARY_PTR_USING_P(ary));
+
+ if (promote) {
+ new_ptr = ALLOC_N(VALUE, capa);
+ RARY_TRANSIENT_UNSET(ary);
+ }
+ else {
+ new_ptr = ary_heap_alloc(ary, capa);
+ }
+
+ MEMCPY(new_ptr, old_ptr, VALUE, capa);
+ /* do not use ARY_SET_PTR() because they assert !frozen */
+ RARRAY(ary)->as.heap.ptr = new_ptr;
+ }
+
+ ary_verify(ary);
+}
+
+void
+rb_ary_transient_heap_evacuate(VALUE ary, int promote)
+{
+ rb_ary_transient_heap_evacuate_(ary, RARRAY_TRANSIENT_P(ary), promote);
+}
+
+void
+rb_ary_detransient(VALUE ary)
+{
+ assert(RARRAY_TRANSIENT_P(ary));
+ rb_ary_transient_heap_evacuate_(ary, TRUE, TRUE);
+}
+#else
+void
+rb_ary_detransient(VALUE ary)
+{
+ /* do nothing */
+}
+#endif
+
static void
ary_resize_capa(VALUE ary, long capacity)
{
assert(RARRAY_LEN(ary) <= capacity);
assert(!OBJ_FROZEN(ary));
assert(!ARY_SHARED_P(ary));
+
if (capacity > RARRAY_EMBED_LEN_MAX) {
if (ARY_EMBED_P(ary)) {
long len = ARY_EMBED_LEN(ary);
- VALUE *ptr = ALLOC_N(VALUE, (capacity));
+ VALUE *ptr = ary_heap_alloc(ary, capacity);
+
MEMCPY(ptr, ARY_EMBED_PTR(ary), VALUE, len);
FL_UNSET_EMBED(ary);
ARY_SET_PTR(ary, ptr);
ARY_SET_HEAP_LEN(ary, len);
}
else {
- SIZED_REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, capacity, RARRAY(ary)->as.heap.aux.capa);
+ ary_heap_realloc(ary, capacity);
}
- ARY_SET_CAPA(ary, (capacity));
+ ARY_SET_CAPA(ary, capacity);
}
else {
if (!ARY_EMBED_P(ary)) {
- long len = RARRAY_LEN(ary);
- const VALUE *ptr = RARRAY_CONST_PTR(ary);
+ long len = ARY_HEAP_LEN(ary);
+ long old_capa = ARY_HEAP_CAPA(ary);
+ const VALUE *ptr = ARY_HEAP_PTR(ary);
- if (len > capacity) len = capacity;
+ if (len > capacity) len = capacity;
MEMCPY((VALUE *)RARRAY(ary)->as.ary, ptr, VALUE, len);
+ ary_heap_free_ptr(ary, ptr, old_capa);
+
FL_SET_EMBED(ary);
ARY_SET_LEN(ary, len);
- ruby_sized_xfree((VALUE *)ptr, RARRAY(ary)->as.heap.aux.capa);
}
}
+
+ ary_verify(ary);
}
static inline void
ary_shrink_capa(VALUE ary)
{
long capacity = ARY_HEAP_LEN(ary);
- long old_capa = RARRAY(ary)->as.heap.aux.capa;
+ long old_capa = ARY_HEAP_CAPA(ary);
assert(!ARY_SHARED_P(ary));
assert(old_capa >= capacity);
- if (old_capa > capacity)
- SIZED_REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, capacity, old_capa);
+ if (old_capa > capacity) ary_heap_realloc(ary, capacity);
+
+ ary_verify(ary);
}
static void
@@ -253,6 +456,8 @@ ary_double_capa(VALUE ary, long min)
}
new_capa += min;
ary_resize_capa(ary, new_capa);
+
+ ary_verify(ary);
}
static void
@@ -308,6 +513,7 @@ static inline void
rb_ary_modify_check(VALUE ary)
{
rb_check_frozen(ary);
+ ary_verify(ary);
}
void
@@ -317,6 +523,9 @@ rb_ary_modify(VALUE ary)
if (ARY_SHARED_P(ary)) {
long shared_len, len = RARRAY_LEN(ary);
VALUE shared = ARY_SHARED(ary);
+
+ ary_verify(shared);
+
if (len <= RARRAY_EMBED_LEN_MAX) {
const VALUE *ptr = ARY_HEAP_PTR(ary);
FL_UNSET_SHARED(ary);
@@ -326,9 +535,9 @@ rb_ary_modify(VALUE ary)
ARY_SET_EMBED_LEN(ary, len);
}
else if (ARY_SHARED_OCCUPIED(shared) && len > ((shared_len = RARRAY_LEN(shared))>>1)) {
- long shift = RARRAY_CONST_PTR(ary) - RARRAY_CONST_PTR(shared);
+ long shift = RARRAY_CONST_PTR_TRANSIENT(ary) - RARRAY_CONST_PTR_TRANSIENT(shared);
FL_UNSET_SHARED(ary);
- ARY_SET_PTR(ary, RARRAY_CONST_PTR(shared));
+ ARY_SET_PTR(ary, RARRAY_CONST_PTR_TRANSIENT(shared));
ARY_SET_CAPA(ary, shared_len);
RARRAY_PTR_USE(ary, ptr, {
MEMMOVE(ptr, ptr+shift, VALUE, len);
@@ -337,8 +546,8 @@ rb_ary_modify(VALUE ary)
rb_ary_decrement_share(shared);
}
else {
- VALUE *ptr = ALLOC_N(VALUE, len);
- MEMCPY(ptr, RARRAY_CONST_PTR(ary), VALUE, len);
+ VALUE *ptr = ary_heap_alloc(ary, len);
+ MEMCPY(ptr, ARY_HEAP_PTR(ary), VALUE, len);
rb_ary_unshare(ary);
ARY_SET_CAPA(ary, len);
ARY_SET_PTR(ary, ptr);
@@ -346,6 +555,7 @@ rb_ary_modify(VALUE ary)
rb_gc_writebarrier_remember(ary);
}
+ ary_verify(ary);
}
static VALUE
@@ -362,9 +572,12 @@ ary_ensure_room_for_push(VALUE ary, long add_len)
if (new_len > RARRAY_EMBED_LEN_MAX) {
VALUE shared = ARY_SHARED(ary);
if (ARY_SHARED_OCCUPIED(shared)) {
- if (RARRAY_CONST_PTR(ary) - RARRAY_CONST_PTR(shared) + new_len <= RARRAY_LEN(shared)) {
+ if (ARY_HEAP_PTR(ary) - RARRAY_CONST_PTR_TRANSIENT(shared) + new_len <= RARRAY_LEN(shared)) {
rb_ary_modify_check(ary);
- return shared;
+
+ ary_verify(ary);
+ ary_verify(shared);
+ return shared;
}
else {
/* if array is shared, then it is likely it participate in push/shift pattern */
@@ -373,11 +586,13 @@ ary_ensure_room_for_push(VALUE ary, long add_len)
if (new_len > capa - (capa >> 6)) {
ary_double_capa(ary, new_len);
}
+ ary_verify(ary);
return ary;
}
}
}
- rb_ary_modify(ary);
+ ary_verify(ary);
+ rb_ary_modify(ary);
}
else {
rb_ary_modify_check(ary);
@@ -387,6 +602,7 @@ ary_ensure_room_for_push(VALUE ary, long add_len)
ary_double_capa(ary, new_len);
}
+ ary_verify(ary);
return ary;
}
@@ -459,7 +675,7 @@ ary_new(VALUE klass, long capa)
ary = ary_alloc(klass);
if (capa > RARRAY_EMBED_LEN_MAX) {
- ptr = ALLOC_N(VALUE, capa);
+ ptr = ary_heap_alloc(ary, capa);
FL_UNSET_EMBED(ary);
ARY_SET_PTR(ary, ptr);
ARY_SET_CAPA(ary, capa);
@@ -523,7 +739,9 @@ rb_ary_new_from_values(long n, const VALUE *elts)
VALUE
rb_ary_tmp_new(long capa)
{
- return ary_new(0, capa);
+ VALUE ary = ary_new(0, capa);
+ rb_ary_transient_heap_evacuate(ary, TRUE);
+ return ary;
}
VALUE
@@ -532,6 +750,7 @@ rb_ary_tmp_new_fill(long capa)
VALUE ary = ary_new(0, capa);
ary_memfill(ary, 0, capa, Qnil);
ARY_SET_LEN(ary, capa);
+ rb_ary_transient_heap_evacuate(ary, TRUE);
return ary;
}
@@ -539,8 +758,13 @@ void
rb_ary_free(VALUE ary)
{
if (ARY_OWNS_HEAP_P(ary)) {
- RB_DEBUG_COUNTER_INC(obj_ary_ptr);
- ruby_sized_xfree((void *)ARY_HEAP_PTR(ary), ARY_HEAP_SIZE(ary));
+ if (RARRAY_TRANSIENT_P(ary)) {
+ RB_DEBUG_COUNTER_INC(obj_ary_transient);
+ }
+ else {
+ RB_DEBUG_COUNTER_INC(obj_ary_ptr);
+ ary_heap_free(ary);
+ }
}
else {
RB_DEBUG_COUNTER_INC(obj_ary_embed);
@@ -563,13 +787,15 @@ ary_discard(VALUE ary)
{
rb_ary_free(ary);
RBASIC(ary)->flags |= RARRAY_EMBED_FLAG;
- RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK;
+ RBASIC(ary)->flags &= ~(RARRAY_EMBED_LEN_MASK | RARRAY_TRANSIENT_FLAG);
}
static VALUE
ary_make_shared(VALUE ary)
{
assert(!ARY_EMBED_P(ary));
+ ary_verify(ary);
+
if (ARY_SHARED_P(ary)) {
return ARY_SHARED(ary);
}
@@ -577,6 +803,7 @@ ary_make_shared(VALUE ary)
return ary;
}
else if (OBJ_FROZEN(ary)) {
+ rb_ary_transient_heap_evacuate(ary, TRUE);
ary_shrink_capa(ary);
FL_SET_SHARED_ROOT(ary);
ARY_SET_SHARED_NUM(ary, 1);
@@ -584,18 +811,25 @@ ary_make_shared(VALUE ary)
}
else {
long capa = ARY_CAPA(ary), len = RARRAY_LEN(ary);
+ const VALUE *ptr;
NEWOBJ_OF(shared, struct RArray, 0, T_ARRAY | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0));
- FL_UNSET_EMBED(shared);
+ rb_ary_transient_heap_evacuate(ary, TRUE);
+ ptr = ARY_HEAP_PTR(ary);
+
+ FL_UNSET_EMBED(shared);
ARY_SET_LEN((VALUE)shared, capa);
- ARY_SET_PTR((VALUE)shared, RARRAY_CONST_PTR(ary));
- ary_mem_clear((VALUE)shared, len, capa - len);
+ ARY_SET_PTR((VALUE)shared, ptr);
+ ary_mem_clear((VALUE)shared, len, capa - len);
FL_SET_SHARED_ROOT(shared);
ARY_SET_SHARED_NUM((VALUE)shared, 1);
FL_SET_SHARED(ary);
ARY_SET_SHARED(ary, (VALUE)shared);
OBJ_FREEZE(shared);
- return (VALUE)shared;
+
+ ary_verify((VALUE)shared);
+ ary_verify(ary);
+ return (VALUE)shared;
}
}
@@ -606,7 +840,7 @@ ary_make_substitution(VALUE ary)
if (len <= RARRAY_EMBED_LEN_MAX) {
VALUE subst = rb_ary_new2(len);
- ary_memcpy(subst, 0, len, RARRAY_CONST_PTR(ary));
+ ary_memcpy(subst, 0, len, RARRAY_CONST_PTR_TRANSIENT(ary));
ARY_SET_EMBED_LEN(subst, len);
return subst;
}
@@ -729,8 +963,8 @@ rb_ary_initialize(int argc, VALUE *argv, VALUE ary)
rb_ary_modify(ary);
if (argc == 0) {
- if (ARY_OWNS_HEAP_P(ary) && RARRAY_CONST_PTR(ary) != 0) {
- ruby_sized_xfree((void *)RARRAY_CONST_PTR(ary), ARY_HEAP_SIZE(ary));
+ if (ARY_OWNS_HEAP_P(ary) && ARY_HEAP_PTR(ary) != NULL) {
+ ary_heap_free(ary);
}
rb_ary_unshare_safe(ary);
FL_SET_EMBED(ary);
@@ -837,7 +1071,7 @@ ary_make_partial(VALUE ary, VALUE klass, long offset, long len)
if (len <= RARRAY_EMBED_LEN_MAX) {
VALUE result = ary_alloc(klass);
- ary_memcpy(result, 0, len, RARRAY_CONST_PTR(ary) + offset);
+ ary_memcpy(result, 0, len, RARRAY_CONST_PTR_TRANSIENT(ary) + offset);
ARY_SET_EMBED_LEN(result, len);
return result;
}
@@ -846,12 +1080,15 @@ ary_make_partial(VALUE ary, VALUE klass, long offset, long len)
FL_UNSET_EMBED(result);
shared = ary_make_shared(ary);
- ARY_SET_PTR(result, RARRAY_CONST_PTR(ary));
+ ARY_SET_PTR(result, RARRAY_CONST_PTR_TRANSIENT(ary));
ARY_SET_LEN(result, RARRAY_LEN(ary));
rb_ary_set_shared(result, shared);
ARY_INCREASE_PTR(result, offset);
ARY_SET_LEN(result, len);
+
+ ary_verify(shared);
+ ary_verify(result);
return result;
}
}
@@ -910,12 +1147,13 @@ ary_take_first_or_last(int argc, const VALUE *argv, VALUE ary, enum ary_take_pos
VALUE
rb_ary_push(VALUE ary, VALUE item)
{
- long idx = RARRAY_LEN(ary);
+ long idx = RARRAY_LEN((ary_verify(ary), ary));
VALUE target_ary = ary_ensure_room_for_push(ary, 1);
RARRAY_PTR_USE(ary, ptr, {
RB_OBJ_WRITE(target_ary, &ptr[idx], item);
});
ARY_SET_LEN(ary, idx + 1);
+ ary_verify(ary);
return ary;
}
@@ -967,6 +1205,7 @@ rb_ary_pop(VALUE ary)
}
--n;
ARY_SET_LEN(ary, n);
+ ary_verify(ary);
return RARRAY_AREF(ary, n);
}
@@ -1000,6 +1239,7 @@ rb_ary_pop_m(int argc, VALUE *argv, VALUE ary)
rb_ary_modify_check(ary);
result = ary_take_first_or_last(argc, argv, ary, ARY_TAKE_LAST);
ARY_INCREASE_LEN(ary, -RARRAY_LEN(result));
+ ary_verify(ary);
return result;
}
@@ -1018,6 +1258,7 @@ rb_ary_shift(VALUE ary)
MEMMOVE(ptr, ptr+1, VALUE, len-1);
}); /* WB: no new reference */
ARY_INCREASE_LEN(ary, -1);
+ ary_verify(ary);
return top;
}
assert(!ARY_EMBED_P(ary)); /* ARY_EMBED_LEN_MAX < ARY_DEFAULT_SIZE */
@@ -1031,6 +1272,8 @@ rb_ary_shift(VALUE ary)
ARY_INCREASE_PTR(ary, 1); /* shift ptr */
ARY_INCREASE_LEN(ary, -1);
+ ary_verify(ary);
+
return top;
}
@@ -1101,6 +1344,7 @@ rb_ary_behead(VALUE ary, long n)
}
ARY_INCREASE_LEN(ary, -n);
+ ary_verify(ary);
return ary;
}
@@ -1120,8 +1364,8 @@ ary_ensure_room_for_unshift(VALUE ary, int argc)
VALUE shared = ARY_SHARED(ary);
capa = RARRAY_LEN(shared);
if (ARY_SHARED_OCCUPIED(shared) && capa > new_len) {
- head = RARRAY_CONST_PTR(ary);
- sharedp = RARRAY_CONST_PTR(shared);
+ head = RARRAY_CONST_PTR_TRANSIENT(ary);
+ sharedp = RARRAY_CONST_PTR_TRANSIENT(shared);
goto makeroom_if_need;
}
}
@@ -1134,11 +1378,13 @@ ary_ensure_room_for_unshift(VALUE ary, int argc)
/* use shared array for big "queues" */
if (new_len > ARY_DEFAULT_SIZE * 4) {
- /* make a room for unshifted items */
+ ary_verify(ary);
+
+ /* make a room for unshifted items */
capa = ARY_CAPA(ary);
ary_make_shared(ary);
- head = sharedp = RARRAY_CONST_PTR(ary);
+ head = sharedp = RARRAY_CONST_PTR_TRANSIENT(ary);
goto makeroom;
makeroom_if_need:
if (head - sharedp < argc) {
@@ -1151,6 +1397,8 @@ ary_ensure_room_for_unshift(VALUE ary, int argc)
}
ARY_SET_PTR(ary, head - argc);
assert(ARY_SHARED_OCCUPIED(ARY_SHARED(ary)));
+
+ ary_verify(ary);
return ARY_SHARED(ary);
}
else {
@@ -1159,6 +1407,7 @@ ary_ensure_room_for_unshift(VALUE ary, int argc)
MEMMOVE(ptr + argc, ptr, VALUE, len);
});
+ ary_verify(ary);
return ary;
}
}
@@ -1576,7 +1825,7 @@ rb_ary_splice(VALUE ary, long beg, long len, const VALUE *rptr, long rlen)
}
{
- const VALUE *optr = RARRAY_CONST_PTR(ary);
+ const VALUE *optr = RARRAY_CONST_PTR_TRANSIENT(ary);
rofs = (rptr >= optr && rptr < optr + olen) ? rptr - optr : -1;
}
@@ -1589,7 +1838,7 @@ rb_ary_splice(VALUE ary, long beg, long len, const VALUE *rptr, long rlen)
len = beg + rlen;
ary_mem_clear(ary, olen, beg - olen);
if (rlen > 0) {
- if (rofs != -1) rptr = RARRAY_CONST_PTR(ary) + rofs;
+ if (rofs != -1) rptr = RARRAY_CONST_PTR_TRANSIENT(ary) + rofs;
ary_memcpy0(ary, beg, rlen, rptr, target_ary);
}
ARY_SET_LEN(ary, len);
@@ -1613,7 +1862,7 @@ rb_ary_splice(VALUE ary, long beg, long len, const VALUE *rptr, long rlen)
ARY_SET_LEN(ary, alen);
}
if (rlen > 0) {
- if (rofs != -1) rptr = RARRAY_CONST_PTR(ary) + rofs;
+ if (rofs != -1) rptr = RARRAY_CONST_PTR_TRANSIENT(ary) + rofs;
/* give up wb-protected ary */
MEMMOVE(RARRAY_PTR(ary) + beg, rptr, VALUE, rlen);
}
@@ -1673,11 +1922,12 @@ rb_ary_resize(VALUE ary, long len)
}
else {
if (olen > len + ARY_DEFAULT_SIZE) {
- SIZED_REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, len, RARRAY(ary)->as.heap.aux.capa);
+ ary_heap_realloc(ary, len);
ARY_SET_CAPA(ary, len);
}
ARY_SET_HEAP_LEN(ary, len);
}
+ ary_verify(ary);
return ary;
}
@@ -1738,7 +1988,7 @@ rb_ary_aset(int argc, VALUE *argv, VALUE ary)
/* check if idx is Range */
range:
rpl = rb_ary_to_ary(argv[argc-1]);
- rb_ary_splice(ary, beg, len, RARRAY_CONST_PTR(rpl), RARRAY_LEN(rpl));
+ rb_ary_splice(ary, beg, len, RARRAY_CONST_PTR_TRANSIENT(rpl), RARRAY_LEN(rpl));
RB_GC_GUARD(rpl);
return argv[argc-1];
}
@@ -1820,7 +2070,7 @@ VALUE
rb_ary_each(VALUE ary)
{
long i;
-
+ ary_verify(ary);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
for (i=0; i 0) {
- const VALUE *p1 = RARRAY_CONST_PTR(ary);
- VALUE *p2 = (VALUE *)RARRAY_CONST_PTR(dup) + len - 1;
+ const VALUE *p1 = RARRAY_CONST_PTR_TRANSIENT(ary);
+ VALUE *p2 = (VALUE *)RARRAY_CONST_PTR_TRANSIENT(dup) + len - 1;
do *p2-- = *p1++; while (--len > 0);
}
ARY_SET_LEN(dup, RARRAY_LEN(ary));
@@ -2382,7 +2635,7 @@ rb_ary_rotate_m(int argc, VALUE *argv, VALUE ary)
rotated = rb_ary_new2(len);
if (len > 0) {
cnt = rotate_count(cnt, len);
- ptr = RARRAY_CONST_PTR(ary);
+ ptr = RARRAY_CONST_PTR_TRANSIENT(ary);
len -= cnt;
ary_memcpy(rotated, 0, len, ptr + cnt);
ary_memcpy(rotated, len, cnt, ptr);
@@ -2482,7 +2735,6 @@ rb_ary_sort_bang(VALUE ary)
VALUE tmp = ary_make_substitution(ary); /* only ary refers tmp */
struct ary_sort_data data;
long len = RARRAY_LEN(ary);
-
RBASIC_CLEAR_CLASS(tmp);
data.ary = tmp;
data.cmp_opt.opt_methods = 0;
@@ -2515,21 +2767,22 @@ rb_ary_sort_bang(VALUE ary)
rb_ary_unshare(ary);
}
else {
- ruby_sized_xfree((void *)ARY_HEAP_PTR(ary), ARY_HEAP_SIZE(ary));
+ ary_heap_free(ary);
}
- ARY_SET_PTR(ary, RARRAY_CONST_PTR(tmp));
+ ARY_SET_PTR(ary, ARY_HEAP_PTR(tmp));
ARY_SET_HEAP_LEN(ary, len);
- ARY_SET_CAPA(ary, RARRAY_LEN(tmp));
+ ARY_SET_CAPA(ary, ARY_HEAP_LEN(tmp));
}
/* tmp was lost ownership for the ptr */
FL_UNSET(tmp, FL_FREEZE);
FL_SET_EMBED(tmp);
ARY_SET_EMBED_LEN(tmp, 0);
FL_SET(tmp, FL_FREEZE);
- }
+ }
/* tmp will be GC'ed. */
RBASIC_SET_CLASS_RAW(tmp, rb_cArray); /* rb_cArray must be marked */
}
+ ary_verify(ary);
return ary;
}
@@ -2757,7 +3010,7 @@ rb_ary_collect(VALUE ary)
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
collect = rb_ary_new2(RARRAY_LEN(ary));
for (i = 0; i < RARRAY_LEN(ary); i++) {
- rb_ary_push(collect, rb_yield_force_blockarg(RARRAY_AREF(ary, i)));
+ rb_ary_push(collect, rb_yield(RARRAY_AREF(ary, i)));
}
return collect;
}
@@ -2833,7 +3086,7 @@ append_values_at_single(VALUE result, VALUE ary, long olen, VALUE idx)
/* check if idx is Range */
else if (rb_range_beg_len(idx, &beg, &len, olen, 1)) {
if (len > 0) {
- const VALUE *const src = RARRAY_CONST_PTR(ary);
+ const VALUE *const src = RARRAY_CONST_PTR_TRANSIENT(ary);
const long end = beg + len;
const long prevlen = RARRAY_LEN(result);
if (beg < olen) {
@@ -2886,18 +3139,22 @@ rb_ary_values_at(int argc, VALUE *argv, VALUE ary)
* call-seq:
* ary.select {|item| block} -> new_ary
* ary.select -> Enumerator
+ * ary.filter {|item| block} -> new_ary
+ * ary.filter -> Enumerator
*
* Returns a new array containing all elements of +ary+
* for which the given +block+ returns a true value.
*
* If no block is given, an Enumerator is returned instead.
*
- * [1,2,3,4,5].select {|num| num.even? } #=> [2, 4]
+ * [1,2,3,4,5].select {|num| num.even? } #=> [2, 4]
*
- * a = %w{ a b c d e f }
- * a.select {|v| v =~ /[aeiou]/} #=> ["a", "e"]
+ * a = %w[ a b c d e f ]
+ * a.select {|v| v =~ /[aeiou]/ } #=> ["a", "e"]
*
* See also Enumerable#select.
+ *
+ * Array#filter is an alias for Array#select.
*/
static VALUE
@@ -2962,8 +3219,10 @@ select_bang_ensure(VALUE a)
/*
* call-seq:
- * ary.select! {|item| block } -> ary or nil
- * ary.select! -> Enumerator
+ * ary.select! {|item| block } -> ary or nil
+ * ary.select! -> Enumerator
+ * ary.filter! {|item| block } -> ary or nil
+ * ary.filter! -> Enumerator
*
* Invokes the given block passing in successive elements from +self+,
* deleting elements for which the block returns a +false+ value.
@@ -2972,10 +3231,11 @@ select_bang_ensure(VALUE a)
*
* If changes were made, it will return +self+, otherwise it returns +nil+.
*
- * See also Array#keep_if
- *
* If no block is given, an Enumerator is returned instead.
*
+ * See also Array#keep_if.
+ *
+ * Array#filter! is an alias for Array#select!.
*/
static VALUE
@@ -2997,14 +3257,15 @@ rb_ary_select_bang(VALUE ary)
* ary.keep_if -> Enumerator
*
* Deletes every element of +self+ for which the given block evaluates to
- * +false+.
- *
- * See also Array#select!
+ * +false+, and returns +self+.
*
* If no block is given, an Enumerator is returned instead.
*
- * a = %w{ a b c d e f }
- * a.keep_if {|v| v =~ /[aeiou]/} #=> ["a", "e"]
+ * a = %w[ a b c d e f ]
+ * a.keep_if {|v| v =~ /[aeiou]/ } #=> ["a", "e"]
+ * a #=> ["a", "e"]
+ *
+ * See also Array#select!.
*/
static VALUE
@@ -3075,6 +3336,7 @@ rb_ary_delete(VALUE ary, VALUE item)
ary_resize_smaller(ary, i2);
+ ary_verify(ary);
return v;
}
@@ -3119,7 +3381,7 @@ rb_ary_delete_at(VALUE ary, long pos)
MEMMOVE(ptr+pos, ptr+pos+1, VALUE, len-pos-1);
});
ARY_INCREASE_LEN(ary, -1);
-
+ ary_verify(ary);
return del;
}
@@ -3187,7 +3449,7 @@ rb_ary_slice_bang(int argc, VALUE *argv, VALUE ary)
len = orig_len - pos;
}
if (len == 0) return rb_ary_new2(0);
- arg2 = rb_ary_new4(len, RARRAY_CONST_PTR(ary)+pos);
+ arg2 = rb_ary_new4(len, RARRAY_CONST_PTR_TRANSIENT(ary)+pos);
RBASIC_SET_CLASS(arg2, rb_obj_class(ary));
rb_ary_splice(ary, pos, len, 0, 0);
return arg2;
@@ -3223,7 +3485,8 @@ ary_reject(VALUE orig, VALUE result)
for (i = 0; i < RARRAY_LEN(orig); i++) {
VALUE v = RARRAY_AREF(orig, i);
- if (!RTEST(rb_yield(v))) {
+
+ if (!RTEST(rb_yield(v))) {
rb_ary_push(result, v);
}
}
@@ -3252,7 +3515,6 @@ static VALUE
ary_reject_bang(VALUE ary)
{
struct select_bang_arg args;
-
rb_ary_modify_check(ary);
args.ary = ary;
args.len[0] = args.len[1] = 0;
@@ -3326,6 +3588,7 @@ rb_ary_reject(VALUE ary)
static VALUE
rb_ary_delete_if(VALUE ary)
{
+ ary_verify(ary);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
ary_reject_bang(ary);
return ary;
@@ -3335,7 +3598,8 @@ static VALUE
take_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, cbarg))
{
VALUE *args = (VALUE *)cbarg;
- if (args[1]-- == 0) rb_iter_break();
+ if (args[1] == 0) rb_iter_break();
+ else args[1]--;
if (argc > 1) val = rb_ary_new4(argc, argv);
rb_ary_push(args[0], val);
return Qnil;
@@ -3504,14 +3768,14 @@ rb_ary_replace(VALUE copy, VALUE orig)
VALUE shared = 0;
if (ARY_OWNS_HEAP_P(copy)) {
- RARRAY_PTR_USE(copy, ptr, ruby_sized_xfree(ptr, ARY_HEAP_SIZE(copy)));
+ ary_heap_free(copy);
}
else if (ARY_SHARED_P(copy)) {
shared = ARY_SHARED(copy);
FL_UNSET_SHARED(copy);
}
FL_SET_EMBED(copy);
- ary_memcpy(copy, 0, RARRAY_LEN(orig), RARRAY_CONST_PTR(orig));
+ ary_memcpy(copy, 0, RARRAY_LEN(orig), RARRAY_CONST_PTR_TRANSIENT(orig));
if (shared) {
rb_ary_decrement_share(shared);
}
@@ -3520,16 +3784,17 @@ rb_ary_replace(VALUE copy, VALUE orig)
else {
VALUE shared = ary_make_shared(orig);
if (ARY_OWNS_HEAP_P(copy)) {
- RARRAY_PTR_USE(copy, ptr, ruby_sized_xfree(ptr, ARY_HEAP_SIZE(copy)));
+ ary_heap_free(copy);
}
else {
rb_ary_unshare_safe(copy);
}
FL_UNSET_EMBED(copy);
- ARY_SET_PTR(copy, RARRAY_CONST_PTR(orig));
- ARY_SET_LEN(copy, RARRAY_LEN(orig));
+ ARY_SET_PTR(copy, ARY_HEAP_PTR(orig));
+ ARY_SET_LEN(copy, ARY_HEAP_LEN(orig));
rb_ary_set_shared(copy, shared);
}
+ ary_verify(copy);
return copy;
}
@@ -3547,16 +3812,20 @@ VALUE
rb_ary_clear(VALUE ary)
{
rb_ary_modify_check(ary);
- ARY_SET_LEN(ary, 0);
if (ARY_SHARED_P(ary)) {
if (!ARY_EMBED_P(ary)) {
rb_ary_unshare(ary);
FL_SET_EMBED(ary);
+ ARY_SET_EMBED_LEN(ary, 0);
}
}
- else if (ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
- ary_resize_capa(ary, ARY_DEFAULT_SIZE * 2);
+ else {
+ ARY_SET_LEN(ary, 0);
+ if (ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
+ ary_resize_capa(ary, ARY_DEFAULT_SIZE * 2);
+ }
}
+ ary_verify(ary);
return ary;
}
@@ -3689,8 +3958,8 @@ rb_ary_plus(VALUE x, VALUE y)
len = xlen + ylen;
z = rb_ary_new2(len);
- ary_memcpy(z, 0, xlen, RARRAY_CONST_PTR(x));
- ary_memcpy(z, xlen, ylen, RARRAY_CONST_PTR(y));
+ ary_memcpy(z, 0, xlen, RARRAY_CONST_PTR_TRANSIENT(x));
+ ary_memcpy(z, xlen, ylen, RARRAY_CONST_PTR_TRANSIENT(y));
ARY_SET_LEN(z, len);
return z;
}
@@ -3700,7 +3969,7 @@ ary_append(VALUE x, VALUE y)
{
long n = RARRAY_LEN(y);
if (n > 0) {
- rb_ary_splice(x, RARRAY_LEN(x), 0, RARRAY_CONST_PTR(y), n);
+ rb_ary_splice(x, RARRAY_LEN(x), 0, RARRAY_CONST_PTR_TRANSIENT(y), n);
}
return x;
}
@@ -3742,6 +4011,7 @@ rb_ary_concat_multi(int argc, VALUE *argv, VALUE ary)
ary_append(ary, args);
}
+ ary_verify(ary);
return ary;
}
@@ -3796,16 +4066,16 @@ rb_ary_times(VALUE ary, VALUE times)
ary2 = ary_new(rb_obj_class(ary), len);
ARY_SET_LEN(ary2, len);
- ptr = RARRAY_CONST_PTR(ary);
+ ptr = RARRAY_CONST_PTR_TRANSIENT(ary);
t = RARRAY_LEN(ary);
if (0 < t) {
ary_memcpy(ary2, 0, t, ptr);
while (t <= len/2) {
- ary_memcpy(ary2, t, t, RARRAY_CONST_PTR(ary2));
+ ary_memcpy(ary2, t, t, RARRAY_CONST_PTR_TRANSIENT(ary2));
t *= 2;
}
if (t < len) {
- ary_memcpy(ary2, t, len-t, RARRAY_CONST_PTR(ary2));
+ ary_memcpy(ary2, t, len-t, RARRAY_CONST_PTR_TRANSIENT(ary2));
}
}
out:
@@ -3891,6 +4161,7 @@ recursive_equal(VALUE ary1, VALUE ary2, int recur)
if (recur) return Qtrue; /* Subtle! */
+ /* rb_equal() can evacuate ptrs */
p1 = RARRAY_CONST_PTR(ary1);
p2 = RARRAY_CONST_PTR(ary2);
len1 = RARRAY_LEN(ary1);
@@ -3903,8 +4174,8 @@ recursive_equal(VALUE ary1, VALUE ary2, int recur)
return Qfalse;
if (len1 < i)
return Qtrue;
- p1 = RARRAY_CONST_PTR(ary1) + i;
- p2 = RARRAY_CONST_PTR(ary2) + i;
+ p1 = RARRAY_CONST_PTR(ary1) + i;
+ p2 = RARRAY_CONST_PTR(ary2) + i;
}
else {
return Qfalse;
@@ -3941,7 +4212,7 @@ rb_ary_equal(VALUE ary1, VALUE ary2)
return rb_equal(ary2, ary1);
}
if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return Qfalse;
- if (RARRAY_CONST_PTR(ary1) == RARRAY_CONST_PTR(ary2)) return Qtrue;
+ if (RARRAY_CONST_PTR_TRANSIENT(ary1) == RARRAY_CONST_PTR_TRANSIENT(ary2)) return Qtrue;
return rb_exec_recursive_paired(recursive_equal, ary1, ary2, ary2);
}
@@ -3972,7 +4243,7 @@ rb_ary_eql(VALUE ary1, VALUE ary2)
if (ary1 == ary2) return Qtrue;
if (!RB_TYPE_P(ary2, T_ARRAY)) return Qfalse;
if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return Qfalse;
- if (RARRAY_CONST_PTR(ary1) == RARRAY_CONST_PTR(ary2)) return Qtrue;
+ if (RARRAY_CONST_PTR_TRANSIENT(ary1) == RARRAY_CONST_PTR_TRANSIENT(ary2)) return Qtrue;
return rb_exec_recursive_paired(recursive_eql, ary1, ary2, ary2);
}
@@ -4166,11 +4437,11 @@ static inline void
ary_recycle_hash(VALUE hash)
{
assert(RBASIC_CLASS(hash) == 0);
- if (RHASH(hash)->ntbl) {
- st_table *tbl = RHASH(hash)->ntbl;
+ if (RHASH_TABLE_P(hash)) {
+ st_table *tbl = RHASH_ST_TABLE(hash);
st_free_table(tbl);
+ RHASH_CLEAR(hash);
}
- rb_gc_force_recycle(hash);
}
/*
@@ -4213,7 +4484,7 @@ rb_ary_diff(VALUE ary1, VALUE ary2)
hash = ary_make_hash(ary2);
for (i=0; ibody.root) {
+ rb_ast_dispose(ast);
+ rb_exc_raise(GET_EC()->errinfo);
+ }
+
+ return ast_new_internal(ast, (NODE *)ast->body.root);
+}
+
/*
* call-seq:
- * RubyVM::AST.parse(string) -> RubyVM::AST::Node
+ * RubyVM::AbstractSyntaxTree.parse(string) -> RubyVM::AbstractSyntaxTree::Node
*
* Parses the given string into an abstract syntax tree,
* returning the root node of that tree.
*
* SyntaxError is raised if the given string is invalid syntax.
*
- * RubyVM::AST.parse("x = 1 + 2")
- * # => #
+ * RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
+ * # => #
*/
static VALUE
rb_ast_s_parse(VALUE module, VALUE str)
{
- VALUE obj;
- rb_ast_t *ast = 0;
+ return rb_ast_parse_str(str);
+}
- const VALUE parser = rb_parser_new();
+static VALUE
+rb_ast_parse_str(VALUE str)
+{
+ rb_ast_t *ast = 0;
str = rb_check_string_type(str);
- rb_parser_set_context(parser, NULL, 0);
- ast = rb_parser_compile_string_path(parser, rb_str_new_cstr("no file name"), str, 1);
-
- if (!ast->body.root) {
- rb_ast_dispose(ast);
- rb_exc_raise(GET_EC()->errinfo);
- }
-
- obj = ast_new_internal(ast, (NODE *)ast->body.root);
-
- return obj;
+ ast = rb_parser_compile_string_path(ast_parse_new(), Qnil, str, 1);
+ return ast_parse_done(ast);
}
/*
* call-seq:
- * RubyVM::AST.parse_file(pathname) -> RubyVM::AST::Node
+ * RubyVM::AbstractSyntaxTree.parse_file(pathname) -> RubyVM::AbstractSyntaxTree::Node
*
* Reads the file from pathname
, then parses it like ::parse,
* returning the root node of the abstract syntax tree.
@@ -95,34 +111,145 @@ rb_ast_s_parse(VALUE module, VALUE str)
* SyntaxError is raised if pathname
's contents are not
* valid Ruby syntax.
*
- * RubyVM::AST.parse_file("my-app/app.rb")
- * # => #
+ * RubyVM::AbstractSyntaxTree.parse_file("my-app/app.rb")
+ * # => #
*/
static VALUE
rb_ast_s_parse_file(VALUE module, VALUE path)
{
- VALUE obj, f;
+ return rb_ast_parse_file(path);
+}
+
+static VALUE
+rb_ast_parse_file(VALUE path)
+{
+ VALUE f;
rb_ast_t *ast = 0;
rb_encoding *enc = rb_utf8_encoding();
- const VALUE parser = rb_parser_new();
-
FilePathValue(path);
f = rb_file_open_str(path, "r");
rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
- rb_parser_set_context(parser, NULL, 0);
- ast = rb_parser_compile_file_path(parser, path, f, 1);
-
+ ast = rb_parser_compile_file_path(ast_parse_new(), Qnil, f, 1);
rb_io_close(f);
+ return ast_parse_done(ast);
+}
- if (!ast->body.root) {
- rb_ast_dispose(ast);
- rb_exc_raise(GET_EC()->errinfo);
+static VALUE
+lex_array(VALUE array, int index)
+{
+ VALUE str = rb_ary_entry(array, index);
+ if (!NIL_P(str)) {
+ StringValue(str);
+ if (!rb_enc_asciicompat(rb_enc_get(str))) {
+ rb_raise(rb_eArgError, "invalid source encoding");
+ }
+ }
+ return str;
+}
+
+static VALUE
+rb_ast_parse_array(VALUE array)
+{
+ rb_ast_t *ast = 0;
+
+ array = rb_check_array_type(array);
+ ast = rb_parser_compile_generic(ast_parse_new(), lex_array, Qnil, array, 1);
+ return ast_parse_done(ast);
+}
+
+static VALUE node_children(rb_ast_t*, NODE*);
+
+static VALUE
+node_find(VALUE self, const int node_id)
+{
+ VALUE ary;
+ long i;
+ struct ASTNodeData *data;
+ TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
+
+ if (nd_node_id(data->node) == node_id) return self;
+
+ ary = node_children(data->ast, data->node);
+
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
+ VALUE child = RARRAY_AREF(ary, i);
+
+ if (CLASS_OF(child) == rb_cNode) {
+ VALUE result = node_find(child, node_id);
+ if (RTEST(result)) return result;
+ }
}
- obj = ast_new_internal(ast, (NODE *)ast->body.root);
+ return Qnil;
+}
- return obj;
+extern VALUE rb_e_script;
+
+static VALUE
+script_lines(VALUE path)
+{
+ VALUE hash, lines;
+ ID script_lines;
+ CONST_ID(script_lines, "SCRIPT_LINES__");
+ if (!rb_const_defined_at(rb_cObject, script_lines)) return Qnil;
+ hash = rb_const_get_at(rb_cObject, script_lines);
+ if (!RB_TYPE_P(hash, T_HASH)) return Qnil;
+ lines = rb_hash_lookup(hash, path);
+ if (!RB_TYPE_P(lines, T_ARRAY)) return Qnil;
+ return lines;
+}
+
+/*
+ * call-seq:
+ * RubyVM::AbstractSyntaxTree.of(proc) -> RubyVM::AbstractSyntaxTree::Node
+ * RubyVM::AbstractSyntaxTree.of(method) -> RubyVM::AbstractSyntaxTree::Node
+ *
+ * Returns AST nodes of the given proc or method.
+ *
+ * RubyVM::AbstractSyntaxTree.of(proc {1 + 2})
+ * # => #
+ *
+ * def hello
+ * puts "hello, world"
+ * end
+ *
+ * RubyVM::AbstractSyntaxTree.of(method(:hello))
+ * # => #
+ */
+static VALUE
+rb_ast_s_of(VALUE module, VALUE body)
+{
+ VALUE path, node, lines;
+ int node_id;
+ const rb_iseq_t *iseq = NULL;
+
+ if (rb_obj_is_proc(body)) {
+ iseq = vm_proc_iseq(body);
+
+ if (!rb_obj_is_iseq((VALUE)iseq)) {
+ iseq = NULL;
+ }
+ }
+ else {
+ iseq = rb_method_iseq(body);
+ }
+
+ if (!iseq) return Qnil;
+
+ path = rb_iseq_path(iseq);
+ node_id = iseq->body->location.node_id;
+ if (!NIL_P(lines = script_lines(path))) {
+ node = rb_ast_parse_array(lines);
+ }
+ else if (RSTRING_LEN(path) == 2 && memcmp(RSTRING_PTR(path), "-e", 2) == 0) {
+ node = rb_ast_parse_str(rb_e_script);
+ }
+ else {
+ node = rb_ast_parse_file(path);
+ }
+
+ return node_find(node, node_id);
}
static VALUE
@@ -146,7 +273,7 @@ node_type_to_str(NODE *node)
*
* Returns the type of this node as a string.
*
- * root = RubyVM::AST.parse("x = 1 + 2")
+ * root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
* root.type # => "NODE_SCOPE"
* call = root.children[2]
* call.type # => "NODE_OPCALL"
@@ -605,20 +732,21 @@ void
Init_ast(void)
{
/*
- * AST provides methods to parse Ruby code into
+ * AbstractSyntaxTree provides methods to parse Ruby code into
* abstract syntax trees. The nodes in the tree
- * are instances of RubyVM::AST::Node.
+ * are instances of RubyVM::AbstractSyntaxTree::Node.
*/
- rb_mAST = rb_define_module_under(rb_cRubyVM, "AST");
+ rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
/*
- * RubyVM::AST::Node instances are created by parse methods in
- * RubyVM::AST.
+ * RubyVM::AbstractSyntaxTree::Node instances are created by parse methods in
+ * RubyVM::AbstractSyntaxTree.
*/
rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
rb_undef_alloc_func(rb_cNode);
rb_define_singleton_method(rb_mAST, "parse", rb_ast_s_parse, 1);
rb_define_singleton_method(rb_mAST, "parse_file", rb_ast_s_parse_file, 1);
+ rb_define_singleton_method(rb_mAST, "of", rb_ast_s_of, 1);
rb_define_method(rb_cNode, "type", rb_ast_node_type, 0);
rb_define_method(rb_cNode, "first_lineno", rb_ast_node_first_lineno, 0);
rb_define_method(rb_cNode, "first_column", rb_ast_node_first_column, 0);
diff --git a/benchmark/fiber_chain.rb b/benchmark/fiber_chain.rb
new file mode 100755
index 00000000000000..7e0a7f9d45cb4b
--- /dev/null
+++ b/benchmark/fiber_chain.rb
@@ -0,0 +1,40 @@
+# Check performance of fiber creation and transfer.
+
+def make_link(previous)
+ Fiber.new do
+ while message = previous.resume
+ Fiber.yield(message)
+ end
+ end
+end
+
+def make_chain(length, &block)
+ chain = Fiber.new(&block)
+
+ (length - 1).times do
+ chain = make_link(chain)
+ end
+
+ return chain
+end
+
+def run_benchmark(length, repeats, message = :hello)
+ chain = nil
+
+ chain = make_chain(length) do
+ while true
+ Fiber.yield(message)
+ end
+ end
+
+ repeats.times do
+ abort "invalid result" unless chain.resume == message
+ end
+end
+
+n = (ARGV[0] || 1000).to_i
+m = (ARGV[1] || 1000).to_i
+
+5.times do
+ run_benchmark(n, m)
+end
diff --git a/bignum.c b/bignum.c
index e53b2bd2c17723..b3046f20173fc3 100644
--- a/bignum.c
+++ b/bignum.c
@@ -385,6 +385,7 @@ bdigitdbl2bary(BDIGIT *ds, size_t n, BDIGIT_DBL num)
static int
bary_cmp(const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
{
+ size_t i;
BARY_TRUNC(xds, xn);
BARY_TRUNC(yds, yn);
@@ -393,11 +394,12 @@ bary_cmp(const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
if (xn > yn)
return 1;
- while (xn-- && xds[xn] == yds[xn])
- ;
- if (xn == (size_t)-1)
+ for (i = 0; i < xn; i++)
+ if (xds[xn - i - 1] != yds[yn - i - 1])
+ break;
+ if (i == xn)
return 0;
- return xds[xn] < yds[xn] ? -1 : 1;
+ return xds[xn - i - 1] < yds[yn - i - 1] ? -1 : 1;
}
static BDIGIT
@@ -418,15 +420,16 @@ bary_small_lshift(BDIGIT *zds, const BDIGIT *xds, size_t n, int shift)
static void
bary_small_rshift(BDIGIT *zds, const BDIGIT *xds, size_t n, int shift, BDIGIT higher_bdigit)
{
+ size_t i;
BDIGIT_DBL num = 0;
assert(0 <= shift && shift < BITSPERDIG);
num = BIGUP(higher_bdigit);
- while (n--) {
- BDIGIT x = xds[n];
+ for (i = 0; i < n; i++) {
+ BDIGIT x = xds[n - i - 1];
num = (num | x) >> shift;
- zds[n] = BIGLO(num);
+ zds[n - i - 1] = BIGLO(num);
num = BIGUP(x);
}
}
@@ -445,8 +448,9 @@ bary_zero_p(const BDIGIT *xds, size_t xn)
static void
bary_neg(BDIGIT *ds, size_t n)
{
- while (n--)
- ds[n] = BIGLO(~ds[n]);
+ size_t i;
+ for (i = 0; i < n; i++)
+ ds[n - i - 1] = BIGLO(~ds[n - i - 1]);
}
static int
@@ -616,8 +620,12 @@ static int
bytes_2comp(unsigned char *buf, size_t len)
{
size_t i;
- for (i = 0; i < len; i++)
- buf[i] = ~buf[i];
+ for (i = 0; i < len; i++) {
+ signed char c = buf[i];
+ signed int d = ~c;
+ unsigned int e = d & 0xFF;
+ buf[i] = e;
+ }
for (i = 0; i < len; i++) {
buf[i]++;
if (buf[i] != 0)
@@ -1439,7 +1447,9 @@ bary_add_one(BDIGIT *ds, size_t n)
{
size_t i;
for (i = 0; i < n; i++) {
- ds[i] = BIGLO(ds[i]+1);
+ BDIGIT_DBL n = ds[i];
+ n += 1;
+ ds[i] = BIGLO(n);
if (ds[i] != 0)
return 0;
}
@@ -1507,15 +1517,16 @@ bigdivrem_mulsub(BDIGIT *zds, size_t zn, BDIGIT x, const BDIGIT *yds, size_t yn)
i = 0;
do {
- BDIGIT_DBL ee;
+ BDIGIT_DBL_SIGNED ee;
t2 += (BDIGIT_DBL)yds[i] * x;
ee = num - BIGLO(t2);
- num = (BDIGIT_DBL)zds[i] + ee;
+ num = (BDIGIT_DBL_SIGNED)zds[i] + ee;
if (ee) zds[i] = BIGLO(num);
num = BIGDN(num);
t2 = BIGDN(t2);
} while (++i < yn);
- num += zds[i] - t2; /* borrow from high digit; don't update */
+ num -= (BDIGIT_DBL_SIGNED)t2;
+ num += (BDIGIT_DBL_SIGNED)zds[yn]; /* borrow from high digit; don't update */
return num;
}
@@ -2583,10 +2594,9 @@ bigdivrem_single1(BDIGIT *qds, const BDIGIT *xds, size_t xn, BDIGIT x_higher_bdi
size_t i;
BDIGIT_DBL t2;
t2 = x_higher_bdigit;
- i = xn;
- while (i--) {
- t2 = BIGUP(t2) + xds[i];
- qds[i] = (BDIGIT)(t2 / y);
+ for (i = 0; i < xn; i++) {
+ t2 = BIGUP(t2) + xds[xn - i - 1];
+ qds[xn - i - 1] = (BDIGIT)(t2 / y);
t2 %= y;
}
return (BDIGIT)t2;
@@ -5083,6 +5093,9 @@ rb_big2str(VALUE x, int base)
static unsigned long
big2ulong(VALUE x, const char *type)
{
+#if SIZEOF_LONG > SIZEOF_BDIGIT
+ size_t i;
+#endif
size_t len = BIGNUM_LEN(x);
unsigned long num;
BDIGIT *ds;
@@ -5097,9 +5110,9 @@ big2ulong(VALUE x, const char *type)
num = (unsigned long)ds[0];
#else
num = 0;
- while (len--) {
+ for (i = 0; i < len; i++) {
num <<= BITSPERDIG;
- num += (unsigned long)ds[len]; /* overflow is already checked */
+ num += (unsigned long)ds[len - i - 1]; /* overflow is already checked */
}
#endif
return num;
@@ -5141,6 +5154,9 @@ rb_big2long(VALUE x)
static unsigned LONG_LONG
big2ull(VALUE x, const char *type)
{
+#if SIZEOF_LONG_LONG > SIZEOF_BDIGIT
+ size_t i;
+#endif
size_t len = BIGNUM_LEN(x);
unsigned LONG_LONG num;
BDIGIT *ds = BDIGITS(x);
@@ -5153,9 +5169,9 @@ big2ull(VALUE x, const char *type)
num = (unsigned LONG_LONG)ds[0];
#else
num = 0;
- while (len--) {
+ for (i = 0; i < len; i++) {
num = BIGUP(num);
- num += ds[len];
+ num += ds[len - i - 1];
}
#endif
return num;
@@ -5262,8 +5278,13 @@ big2dbl(VALUE x)
}
}
if (carry) {
- dl &= BDIGMAX << bits;
- dl = BIGLO(dl + ((BDIGIT)1 << bits));
+ BDIGIT mask = BDIGMAX;
+ BDIGIT bit = 1;
+ mask <<= bits;
+ bit <<= bits;
+ dl &= mask;
+ dl += bit;
+ dl = BIGLO(dl);
if (!dl) d += 1;
}
}
@@ -6032,12 +6053,15 @@ rb_big_divide(VALUE x, VALUE y, ID op)
}
else if (RB_FLOAT_TYPE_P(y)) {
if (op == '/') {
- return DBL2NUM(rb_big2dbl(x) / RFLOAT_VALUE(y));
+ double dx = rb_big2dbl(x);
+ return rb_flo_div_flo(DBL2NUM(dx), y);
}
else {
+ VALUE v;
double dy = RFLOAT_VALUE(y);
if (dy == 0.0) rb_num_zerodiv();
- return rb_dbl2big(rb_big2dbl(x) / dy);
+ v = rb_big_divide(x, y, '/');
+ return rb_dbl2big(RFLOAT_VALUE(v));
}
}
else {
@@ -6170,6 +6194,7 @@ double
rb_big_fdiv_double(VALUE x, VALUE y)
{
double dx, dy;
+ VALUE v;
dx = big2dbl(x);
if (FIXNUM_P(y)) {
@@ -6190,7 +6215,8 @@ rb_big_fdiv_double(VALUE x, VALUE y)
else {
return NUM2DBL(rb_num_coerce_bin(x, y, rb_intern("fdiv")));
}
- return dx / dy;
+ v = rb_flo_div_flo(DBL2NUM(dx), DBL2NUM(dy));
+ return NUM2DBL(v);
}
VALUE
@@ -6211,7 +6237,7 @@ rb_big_pow(VALUE x, VALUE y)
if (RB_FLOAT_TYPE_P(y)) {
d = RFLOAT_VALUE(y);
if ((BIGNUM_NEGATIVE_P(x) && !BIGZEROP(x))) {
- return rb_dbl_complex_polar(pow(-rb_big2dbl(x), d), d);
+ return rb_dbl_complex_polar_pi(pow(-rb_big2dbl(x), d), d);
}
}
else if (RB_BIGNUM_TYPE_P(y)) {
diff --git a/bin/bundle b/bin/bundle
new file mode 100755
index 00000000000000..1a0b06b005ee0e
--- /dev/null
+++ b/bin/bundle
@@ -0,0 +1,27 @@
+#!/usr/bin/env ruby
+#
+# This file was generated by RubyGems.
+#
+# The application 'bundler' is installed as part of a gem, and
+# this file is here to facilitate running it.
+#
+
+require 'rubygems'
+
+version = ">= 0.a"
+
+str = ARGV.first
+if str
+ str = str.b[/\A_(.*)_\z/, 1]
+ if str and Gem::Version.correct?(str)
+ version = str
+ ARGV.shift
+ end
+end
+
+if Gem.respond_to?(:activate_bin_path)
+load Gem.activate_bin_path('bundler', 'bundle', version)
+else
+gem "bundler", version
+load Gem.bin_path("bundler", "bundle", version)
+end
diff --git a/bin/bundler b/bin/bundler
new file mode 100755
index 00000000000000..e15eb39ed70c10
--- /dev/null
+++ b/bin/bundler
@@ -0,0 +1,27 @@
+#!/usr/bin/env ruby
+#
+# This file was generated by RubyGems.
+#
+# The application 'bundler' is installed as part of a gem, and
+# this file is here to facilitate running it.
+#
+
+require 'rubygems'
+
+version = ">= 0.a"
+
+str = ARGV.first
+if str
+ str = str.b[/\A_(.*)_\z/, 1]
+ if str and Gem::Version.correct?(str)
+ version = str
+ ARGV.shift
+ end
+end
+
+if Gem.respond_to?(:activate_bin_path)
+load Gem.activate_bin_path('bundler', 'bundler', version)
+else
+gem "bundler", version
+load Gem.bin_path("bundler", "bundler", version)
+end
diff --git a/bin/irb b/bin/irb
index c64ee85fbdcd41..ae6d358c9db038 100755
--- a/bin/irb
+++ b/bin/irb
@@ -1,11 +1,27 @@
#!/usr/bin/env ruby
#
-# irb.rb - interactive ruby
-# $Release Version: 0.9.6 $
-# $Revision$
-# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+# This file was generated by RubyGems.
#
+# The application 'irb' is installed as part of a gem, and
+# this file is here to facilitate running it.
+#
+
+require 'rubygems'
+
+version = ">= 0.a"
-require "irb"
+str = ARGV.first
+if str
+ str = str.b[/\A_(.*)_\z/, 1]
+ if str and Gem::Version.correct?(str)
+ version = str
+ ARGV.shift
+ end
+end
-IRB.start(__FILE__)
+if Gem.respond_to?(:activate_bin_path)
+load Gem.activate_bin_path('irb', 'irb', version)
+else
+gem "irb", version
+load Gem.bin_path("irb", "irb", version)
+end
diff --git a/bin/rdoc b/bin/rdoc
index aaa23292dfd1ec..8fa948cddb2cd0 100755
--- a/bin/rdoc
+++ b/bin/rdoc
@@ -1,44 +1,27 @@
#!/usr/bin/env ruby
#
-# RDoc: Documentation tool for source code
-# (see lib/rdoc/rdoc.rb for more information)
+# This file was generated by RubyGems.
+#
+# The application 'rdoc' is installed as part of a gem, and
+# this file is here to facilitate running it.
#
-# Copyright (c) 2003 Dave Thomas
-# Released under the same terms as Ruby
-begin
- gem 'rdoc'
-rescue NameError => e # --disable-gems
- raise unless e.name == :gem
-rescue Gem::LoadError
-end
+require 'rubygems'
-require 'rdoc/rdoc'
+version = ">= 0.a"
-begin
- r = RDoc::RDoc.new
- r.document ARGV
-rescue Errno::ENOSPC
- $stderr.puts 'Ran out of space creating documentation'
- $stderr.puts
- $stderr.puts 'Please free up some space and try again'
-rescue SystemExit
- raise
-rescue Exception => e
- if $DEBUG_RDOC then
- $stderr.puts e.message
- $stderr.puts "#{e.backtrace.join "\n\t"}"
- $stderr.puts
- elsif Interrupt === e then
- $stderr.puts
- $stderr.puts 'Interrupted'
- else
- $stderr.puts "uh-oh! RDoc had a problem:"
- $stderr.puts e.message
- $stderr.puts
- $stderr.puts "run with --debug for full backtrace"
+str = ARGV.first
+if str
+ str = str.b[/\A_(.*)_\z/, 1]
+ if str and Gem::Version.correct?(str)
+ version = str
+ ARGV.shift
end
-
- exit 1
end
+if Gem.respond_to?(:activate_bin_path)
+load Gem.activate_bin_path('rdoc', 'rdoc', version)
+else
+gem "rdoc", version
+load Gem.bin_path("rdoc", "rdoc", version)
+end
diff --git a/bin/ri b/bin/ri
index 7fbed0c09900f5..0cc2f73bb69f61 100755
--- a/bin/ri
+++ b/bin/ri
@@ -1,12 +1,27 @@
#!/usr/bin/env ruby
+#
+# This file was generated by RubyGems.
+#
+# The application 'rdoc' is installed as part of a gem, and
+# this file is here to facilitate running it.
+#
-begin
- gem 'rdoc'
-rescue NameError => e # --disable-gems
- raise unless e.name == :gem
-rescue Gem::LoadError
-end
+require 'rubygems'
+
+version = ">= 0.a"
-require 'rdoc/ri/driver'
+str = ARGV.first
+if str
+ str = str.b[/\A_(.*)_\z/, 1]
+ if str and Gem::Version.correct?(str)
+ version = str
+ ARGV.shift
+ end
+end
-RDoc::RI::Driver.run ARGV
+if Gem.respond_to?(:activate_bin_path)
+load Gem.activate_bin_path('rdoc', 'ri', version)
+else
+gem "rdoc", version
+load Gem.bin_path("rdoc", "ri", version)
+end
diff --git a/bootstraptest/runner.rb b/bootstraptest/runner.rb
index a5d5d219cf95a4..feb8ada5c146a4 100755
--- a/bootstraptest/runner.rb
+++ b/bootstraptest/runner.rb
@@ -385,7 +385,7 @@ def assert_finish(timeout_seconds, testsrc, message = '')
if IO.select([io], nil, nil, diff)
begin
io.read_nonblock(1024)
- rescue Errno::EAGAIN, EOFError
+ rescue Errno::EAGAIN, IO::WaitReadable, EOFError
break
end while true
end
diff --git a/class.c b/class.c
index d140e8adea0812..5785cee84a2369 100644
--- a/class.c
+++ b/class.c
@@ -1804,11 +1804,10 @@ NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keyw
static void
unknown_keyword_error(VALUE hash, const ID *table, int keywords)
{
- st_table *tbl = rb_hash_tbl_raw(hash);
int i;
for (i = 0; i < keywords; i++) {
st_data_t key = ID2SYM(table[i]);
- st_delete(tbl, &key, NULL);
+ rb_hash_stlike_delete(hash, &key, NULL);
}
rb_keyword_error("unknown", rb_hash_keys(hash));
}
@@ -1855,7 +1854,7 @@ rb_extract_keywords(VALUE *orighash)
*orighash = 0;
return hash;
}
- st_foreach(rb_hash_tbl_raw(hash), separate_symbol, (st_data_t)&arg);
+ rb_hash_foreach(hash, separate_symbol, (st_data_t)&arg);
if (arg.kwdhash) {
if (arg.nonsymkey != Qundef) {
rb_raise(rb_eArgError, "non-symbol key in keyword arguments: %+"PRIsVALUE,
@@ -1876,8 +1875,8 @@ rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, V
#define extract_kwarg(keyword, val) \
(key = (st_data_t)(keyword), values ? \
- st_delete(rb_hash_tbl_raw(keyword_hash), &key, (val)) : \
- st_lookup(rb_hash_tbl_raw(keyword_hash), key, (val)))
+ rb_hash_stlike_delete(keyword_hash, &key, (val)) : \
+ rb_hash_stlike_lookup(keyword_hash, key, (val)))
if (NIL_P(keyword_hash)) keyword_hash = 0;
diff --git a/common.mk b/common.mk
index 42584f65a2129e..6ab6c2850085cf 100644
--- a/common.mk
+++ b/common.mk
@@ -2,7 +2,7 @@ bin: $(PROGRAM) $(WPROGRAM)
lib: $(LIBRUBY)
dll: $(LIBRUBY_SO)
-.SUFFIXES: .inc .h .c .y .i .$(DTRACE_EXT)
+.SUFFIXES: .inc .h .c .y .i .$(ASMEXT) .$(DTRACE_EXT)
# V=0 quiet, V=1 verbose. other values don't work.
V = 0
@@ -130,6 +130,7 @@ COMMONOBJS = array.$(OBJEXT) \
thread.$(OBJEXT) \
time.$(OBJEXT) \
transcode.$(OBJEXT) \
+ transient_heap.$(OBJEXT) \
util.$(OBJEXT) \
variable.$(OBJEXT) \
version.$(OBJEXT) \
@@ -137,6 +138,7 @@ COMMONOBJS = array.$(OBJEXT) \
vm_backtrace.$(OBJEXT) \
vm_dump.$(OBJEXT) \
vm_trace.$(OBJEXT) \
+ $(COROUTINE_OBJ) \
$(DTRACE_OBJ) \
$(BUILTIN_ENCOBJS) \
$(BUILTIN_TRANSOBJS) \
@@ -342,8 +344,12 @@ $(STATIC_RUBY)$(EXEEXT): $(MAINOBJ) $(DLDOBJS) $(EXTOBJS) $(LIBRUBY_A)
$(PURIFY) $(CC) $(MAINOBJ) $(DLDOBJS) $(LIBRUBY_A) $(MAINLIBS) $(EXTLIBS) $(LIBS) $(OUTFLAG)$@ $(LDFLAGS) $(XLDFLAGS)
ruby.imp: $(COMMONOBJS)
- $(Q)$(NM) -Pgp $(COMMONOBJS) | \
- awk 'BEGIN{print "#!"}; $$2~/^[BDT]$$/&&$$1!~/^(Init_|ruby_static_id_|.*_threadptr_|rb_ec_\.)/{print $$1}' | \
+ $(Q){ \
+ $(NM) -Pgp $(COMMONOBJS) | \
+ awk 'BEGIN{print "#!"}; $$2~/^[BDT]$$/&&$$1!~/^$(SYMBOL_PREFIX)(Init_|InitVM_|ruby_static_id_|.*_threadptr_|rb_ec_)|^\./{print $$1}'; \
+ ($(CHDIR) $(srcdir) && \
+ exec sed -n '/^MJIT_FUNC_EXPORTED/!d;N;s/.*\n\(rb_[a-zA-Z_0-9]*\).*/$(SYMBOL_PREFIX)\1/p' cont.c gc.c thread*c vm*.c) \
+ } | \
sort -u -o $@
install: install-$(INSTALLDOC)
@@ -536,15 +542,15 @@ post-install-gem::
rdoc: PHONY main
@echo Generating RDoc documentation
- $(Q) $(XRUBY) "$(srcdir)/bin/rdoc" --root "$(srcdir)" --page-dir "$(srcdir)/doc" --encoding=UTF-8 --no-force-update --all --ri --op "$(RDOCOUT)" $(RDOCFLAGS) "$(srcdir)"
+ $(Q) $(XRUBY) "$(srcdir)/libexec/rdoc" --root "$(srcdir)" --page-dir "$(srcdir)/doc" --encoding=UTF-8 --no-force-update --all --ri --op "$(RDOCOUT)" $(RDOCFLAGS) "$(srcdir)"
html: PHONY main
@echo Generating RDoc HTML files
- $(Q) $(XRUBY) "$(srcdir)/bin/rdoc" --root "$(srcdir)" --page-dir "$(srcdir)/doc" --encoding=UTF-8 --no-force-update --all --op "$(HTMLOUT)" $(RDOCFLAGS) "$(srcdir)"
+ $(Q) $(XRUBY) "$(srcdir)/libexec/rdoc" --root "$(srcdir)" --page-dir "$(srcdir)/doc" --encoding=UTF-8 --no-force-update --all --op "$(HTMLOUT)" $(RDOCFLAGS) "$(srcdir)"
rdoc-coverage: PHONY main
@echo Generating RDoc coverage report
- $(Q) $(XRUBY) "$(srcdir)/bin/rdoc" --root "$(srcdir)" --encoding=UTF-8 --all --quiet -C $(RDOCFLAGS) "$(srcdir)"
+ $(Q) $(XRUBY) "$(srcdir)/libexec/rdoc" --root "$(srcdir)" --encoding=UTF-8 --all --quiet -C $(RDOCFLAGS) "$(srcdir)"
RDOCBENCHOUT=/tmp/rdocbench
@@ -577,7 +583,8 @@ clean: clean-ext clean-enc clean-golf clean-docs clean-extout clean-local clean-
clean-local:: clean-runnable
$(Q)$(RM) $(OBJS) $(MINIOBJS) $(MAINOBJ) $(LIBRUBY_A) $(LIBRUBY_SO) $(LIBRUBY) $(LIBRUBY_ALIASES)
$(Q)$(RM) $(PROGRAM) $(WPROGRAM) miniruby$(EXEEXT) dmyext.$(OBJEXT) dmyenc.$(OBJEXT) $(ARCHFILE) .*.time
- $(Q)$(RM) y.tab.c y.output encdb.h transdb.h config.log rbconfig.rb $(ruby_pc) probes.h probes.$(OBJEXT) probes.stamp ruby-glommed.$(OBJEXT)
+ $(Q)$(RM) y.tab.c y.output encdb.h transdb.h config.log rbconfig.rb $(ruby_pc)
+ $(Q)$(RM) probes.h probes.$(OBJEXT) probes.stamp ruby-glommed.$(OBJEXT) ruby.imp
$(Q)$(RM) GNUmakefile.old Makefile.old $(arch)-fake.rb bisect.sh $(ENC_TRANS_D)
-$(Q) $(RMDIR) enc/jis enc/trans enc 2> $(NULL) || exit 0
clean-runnable:: PHONY
@@ -615,12 +622,26 @@ distclean-spec: clean-spec
distclean-rubyspec: distclean-spec
realclean:: realclean-ext realclean-local realclean-enc realclean-golf realclean-extout
-realclean-local:: distclean-local
+realclean-local:: distclean-local realclean-srcs-local
+
+clean-srcs:: clean-srcs-local clean-srcs-ext
+realclean-srcs:: realclean-srcs-local realclean-srcs-ext
+
+clean-srcs-local::
$(Q)$(RM) parse.c parse.h lex.c enc/trans/newline.c revision.h
- $(Q)$(RM) id.c id.h probes.dmyh
+ $(Q)$(RM) id.c id.h probes.dmyh probes.h
+ $(Q)$(RM) encdb.h transdb.h verconf.h ruby-runner.h
+ $(Q)$(RM) mjit_build_dir.c mjit_config.h rb_mjit_header.h
+ $(Q)$(RM) $(MJIT_MIN_HEADER) $(MJIT_MIN_HEADER:.h=)$(MJIT_HEADER_SUFFIX:%=*).h
+
+realclean-srcs-local:: clean-srcs-local
$(Q)$(CHDIR) $(srcdir) && $(exec) $(RM) parse.c parse.h lex.c enc/trans/newline.c $(PRELUDES) revision.h
$(Q)$(CHDIR) $(srcdir) && $(exec) $(RM) id.c id.h probes.dmyh
$(Q)$(CHDIR) $(srcdir) && $(exec) $(RM) configure aclocal.m4 tool/config.guess tool/config.sub gems/*.gem
+
+clean-srcs-ext::
+realclean-srcs-ext:: clean-srcs-ext
+
realclean-ext:: PHONY
realclean-golf: distclean-golf
$(Q)$(RM) $(GOLFPRELUDES)
@@ -705,12 +726,12 @@ $(arch)-fake.rb: $(srcdir)/template/fake.rb.in $(srcdir)/tool/generic_erb.rb ver
btest: $(TEST_RUNNABLE)-btest
no-btest: PHONY
yes-btest: fake miniruby$(EXEEXT) PHONY
- $(Q)$(exec) $(BOOTSTRAPRUBY) "$(srcdir)/bootstraptest/runner.rb" --ruby="$(BTESTRUBY) $(RUN_OPTS)" $(OPTS) $(TESTOPTS)
+ $(Q)$(exec) $(BOOTSTRAPRUBY) "$(srcdir)/bootstraptest/runner.rb" --ruby="$(BTESTRUBY) $(RUN_OPTS)" $(OPTS) $(TESTOPTS) $(BTESTS)
btest-ruby: $(TEST_RUNNABLE)-btest-ruby
no-btest-ruby: PHONY
yes-btest-ruby: prog PHONY
- $(Q)$(exec) $(RUNRUBY) "$(srcdir)/bootstraptest/runner.rb" --ruby="$(PROGRAM) -I$(srcdir)/lib $(RUN_OPTS)" -q $(OPTS) $(TESTOPTS)
+ $(Q)$(exec) $(RUNRUBY) "$(srcdir)/bootstraptest/runner.rb" --ruby="$(PROGRAM) -I$(srcdir)/lib $(RUN_OPTS)" -q $(OPTS) $(TESTOPTS) $(BTESTS)
test-basic: $(TEST_RUNNABLE)-test-basic
no-test-basic: PHONY
@@ -833,7 +854,7 @@ $(PLATFORM_D):
$(Q) $(MAKEDIRS) $(PLATFORM_DIR) $(@D)
@exit > $@
-exe/$(PROGRAM): ruby-runner.c ruby-runner.h exe/.time miniruby$(EXEEXT)
+exe/$(PROGRAM): ruby-runner.c ruby-runner.h exe/.time miniruby$(EXEEXT) {$(VPATH)}config.h
$(Q) $(CC) $(CFLAGS) $(CPPFLAGS) -DRUBY_INSTALL_NAME=$(@F) $(COUTFLAG)ruby-runner.$(OBJEXT) -c $(CSRCFLAG)$(srcdir)/ruby-runner.c
$(Q) $(PURIFY) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(OUTFLAG)$@ ruby-runner.$(OBJEXT) $(LIBS)
$(Q) $(POSTLINK)
@@ -846,7 +867,7 @@ exe/$(PROGRAM): ruby-runner.c ruby-runner.h exe/.time miniruby$(EXEEXT)
$(@F) $(@D)
exe/.time:
- $(Q) $(MAKEDIRS) exe $(@D)
+ $(Q) $(MAKEDIRS) $(@D)
@exit > $@
$(BUILTIN_ENCOBJS) $(BUILTIN_TRANSOBJS): $(ENC_TRANS_D)
@@ -889,6 +910,12 @@ strstr.$(OBJEXT): {$(VPATH)}strstr.c
nt.$(OBJEXT): {$(VPATH)}nt.c
ia64.$(OBJEXT): {$(VPATH)}ia64.s
$(CC) $(CFLAGS) -c $<
+.coroutine_obj $(COROUTINE_OBJ): \
+ {$(VPATH)}$(COROUTINE_H:.h=).$(ASMEXT) \
+ $(COROUTINE_H:/Context.h=/.time)
+$(COROUTINE_H:/Context.h=/.time):
+ $(Q) $(MAKEDIRS) $(@D)
+ @exit > $@
###
@@ -953,13 +980,20 @@ EXT_SRCS = $(srcdir)/ext/ripper/ripper.c \
$(srcdir)/ext/rbconfig/sizeof/sizes.c \
$(srcdir)/ext/rbconfig/sizeof/limits.c \
$(srcdir)/ext/socket/constdefs.c \
+ $(srcdir)/ext/etc/constdefs.h \
# EXT_SRCS
srcs-ext: $(EXT_SRCS)
+realclean-srcs-ext::
+ $(Q)$(RM) $(EXT_SRCS)
-srcs-extra: $(srcdir)/ext/json/parser/parser.c \
- $(srcdir)/ext/date/zonetab.h \
- $(empty)
+EXTRA_SRCS = $(srcdir)/ext/json/parser/parser.c \
+ $(srcdir)/ext/date/zonetab.h \
+ $(empty)
+
+srcs-extra: $(EXTRA_SRCS)
+realclean-srcs-extra::
+ $(Q)$(RM) $(EXTRA_SRCS)
LIB_SRCS = $(srcdir)/lib/unicode_normalize/tables.rb
@@ -1049,7 +1083,8 @@ preludes: {$(VPATH)}miniprelude.c
preludes: {$(srcdir)}golf_prelude.c
$(srcdir)/revision.h:
- @exit > $@
+ $(Q)$(gnumake:yes=#) $(RM) $(@F)
+ $(Q)$(gnumake:yes=#) exit > $@ || exit > $(@F)
$(REVISION_H): $(srcdir)/version.h $(srcdir)/tool/file2lastrev.rb $(REVISION_FORCE)
-$(Q) $(BASERUBY) $(srcdir)/tool/file2lastrev.rb -q --revision.h "$(srcdir)" > revision.tmp
@@ -1095,6 +1130,12 @@ $(srcdir)/ext/socket/constdefs.c: $(srcdir)/ext/socket/depend
$(exec) $(MAKE) -f - $(mflags) \
Q=$(Q) ECHO=$(ECHO) top_srcdir=../.. srcdir=. VPATH=../.. RUBY="$(BASERUBY)"
+$(srcdir)/ext/etc/constdefs.h: $(srcdir)/ext/etc/depend
+ $(Q) $(CHDIR) $(@D) && \
+ sed '/AUTOGENERATED/q' depend | \
+ $(exec) $(MAKE) -f - $(mflags) \
+ Q=$(Q) ECHO=$(ECHO) top_srcdir=../.. srcdir=. VPATH=../.. RUBY="$(BASERUBY)"
+
##
run: fake miniruby$(EXEEXT) PHONY
@@ -1181,7 +1222,7 @@ update-mspec:
update-rubyspec:
update-config_files: PHONY
- $(Q) $(BASERUBY) -C "$(srcdir)" tool/downloader.rb -d tool -e gnu \
+ $(Q) $(BASERUBY) -C "$(srcdir)" tool/downloader.rb -d tool --cache-dir=$(CACHE_DIR) -e gnu \
config.guess config.sub
update-gems: PHONY
@@ -1208,11 +1249,11 @@ extract-gems: PHONY
update-bundled_gems: PHONY
$(Q) $(RUNRUBY) -rrubygems \
-pla \
- -e '$$_=Gem::SpecFetcher.fetcher.detect(:latest) {'"|s|" \
- -e 'if s.platform=="ruby"&&s.name==$$F[0]' \
- -e 'break [s.name, s.version, *$$F[2..-1]].join(" ")' \
- -e 'end' \
+ -e '(gem,src), = Gem::SpecFetcher.fetcher.detect(:latest) {'"|s|" \
+ -e 's.platform=="ruby"&&s.name==$$F[0]' \
-e '}' \
+ -e 'gem = src.fetch_spec(gem)' \
+ -e '$$_ = [gem.name, gem.version, gem.metadata["source_code_uri"]||gem.homepage].join(" ")' \
"$(srcdir)/gems/bundled_gems" | \
"$(IFCHANGE)" "$(srcdir)/gems/bundled_gems" -
@@ -1222,7 +1263,7 @@ test-bundled-gems-fetch: $(PREP)
$(Q) $(BASERUBY) -C $(srcdir)/gems ../tool/fetch-bundled_gems.rb src bundled_gems
test-bundled-gems-prepare: test-bundled-gems-precheck test-bundled-gems-fetch
- $(XRUBY) -C "$(srcdir)" bin/gem install --no-ri --no-rdoc \
+ $(XRUBY) -C "$(srcdir)" bin/gem install --no-document \
--install-dir .bundle --conservative "bundler" "minitest:~> 5" 'test-unit' 'rake' 'hoe' 'yard' 'pry' 'packnga'
PREPARE_BUNDLED_GEMS = test-bundled-gems-prepare
@@ -1231,6 +1272,21 @@ yes-test-bundled-gems: test-bundled-gems-run
no-test-bundled-gems:
test-bundled-gems-run: $(PREPARE_BUNDLED_GEMS)
+test-bundler-precheck: $(arch)-fake.rb programs
+
+yes-test-bundler-prepare: test-bundler-precheck
+ $(XRUBY) -C "$(srcdir)" bin/gem install --no-document \
+ --install-dir .bundle --conservative "rspec:~> 3.5"
+
+RSPECOPTS = --format progress
+BUNDLER_SPECS =
+test-bundler: $(TEST_RUNNABLE)-test-bundler
+yes-test-bundler: yes-test-bundler-prepare
+ $(gnumake_recursive)$(Q) \
+ $(XRUBY) -C $(srcdir) -Ispec/bundler .bundle/bin/rspec \
+ --require spec_helper $(RSPECOPTS) spec/bundler/$(BUNDLER_SPECS)
+no-test-bundler:
+
UNICODE_FILES = $(UNICODE_SRC_DATA_DIR)/UnicodeData.txt \
$(UNICODE_SRC_DATA_DIR)/CompositionExclusions.txt \
$(UNICODE_SRC_DATA_DIR)/NormalizationTest.txt \
@@ -1247,13 +1303,18 @@ UNICODE_PROPERTY_FILES = \
$(UNICODE_SRC_DATA_DIR)/PropertyValueAliases.txt \
$(UNICODE_SRC_DATA_DIR)/Scripts.txt \
$(UNICODE_SRC_DATA_DIR)/auxiliary/GraphemeBreakProperty.txt \
+ $(UNICODE_SRC_DATA_DIR)/auxiliary/GraphemeBreakTest.txt \
$(empty)
UNICODE_EMOJI_FILES = \
$(UNICODE_SRC_EMOJI_DATA_DIR)/emoji-data.txt \
+ $(UNICODE_SRC_EMOJI_DATA_DIR)/emoji-sequences.txt \
+ $(UNICODE_SRC_EMOJI_DATA_DIR)/emoji-test.txt \
+ $(UNICODE_SRC_EMOJI_DATA_DIR)/emoji-variation-sequences.txt \
+ $(UNICODE_SRC_EMOJI_DATA_DIR)/emoji-zwj-sequences.txt \
$(empty)
-update-unicode: $(UNICODE_FILES)
+update-unicode: $(UNICODE_FILES) $(UNICODE_PROPERTY_FILES)
CACHE_DIR = $(srcdir)/.downloaded-cache
UNICODE_DOWNLOAD = \
@@ -1402,6 +1463,7 @@ help: PHONY
" test-all: all ruby tests [TESTOPTS=-j4 TESTS=]" \
" test-spec: run the Ruby spec suite" \
" test-rubyspec: same as test-spec" \
+ " test-bundler: run the Bundler spec" \
" test-bundled-gems: run the test suite of bundled gems" \
" up: update local copy and autogenerated files" \
" benchmark: benchmark this ruby and COMPARE_RUBY." \
@@ -1445,6 +1507,7 @@ array.$(OBJEXT): {$(VPATH)}probes.h
array.$(OBJEXT): {$(VPATH)}ruby_assert.h
array.$(OBJEXT): {$(VPATH)}st.h
array.$(OBJEXT): {$(VPATH)}subst.h
+array.$(OBJEXT): {$(VPATH)}transient_heap.h
array.$(OBJEXT): {$(VPATH)}util.h
ast.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h
ast.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h
@@ -1791,6 +1854,7 @@ enum.$(OBJEXT): {$(VPATH)}oniguruma.h
enum.$(OBJEXT): {$(VPATH)}st.h
enum.$(OBJEXT): {$(VPATH)}subst.h
enum.$(OBJEXT): {$(VPATH)}symbol.h
+enum.$(OBJEXT): {$(VPATH)}transient_heap.h
enum.$(OBJEXT): {$(VPATH)}util.h
enumerator.$(OBJEXT): $(hdrdir)/ruby/ruby.h
enumerator.$(OBJEXT): $(top_srcdir)/include/ruby.h
@@ -1935,6 +1999,7 @@ gc.$(OBJEXT): {$(VPATH)}subst.h
gc.$(OBJEXT): {$(VPATH)}thread.h
gc.$(OBJEXT): {$(VPATH)}thread_$(THREAD_MODEL).h
gc.$(OBJEXT): {$(VPATH)}thread_native.h
+gc.$(OBJEXT): {$(VPATH)}transient_heap.h
gc.$(OBJEXT): {$(VPATH)}util.h
gc.$(OBJEXT): {$(VPATH)}vm_core.h
gc.$(OBJEXT): {$(VPATH)}vm_debug.h
@@ -2032,14 +2097,20 @@ io.$(OBJEXT): {$(VPATH)}intern.h
io.$(OBJEXT): {$(VPATH)}internal.h
io.$(OBJEXT): {$(VPATH)}io.c
io.$(OBJEXT): {$(VPATH)}io.h
+io.$(OBJEXT): {$(VPATH)}method.h
io.$(OBJEXT): {$(VPATH)}missing.h
+io.$(OBJEXT): {$(VPATH)}node.h
io.$(OBJEXT): {$(VPATH)}onigmo.h
io.$(OBJEXT): {$(VPATH)}oniguruma.h
+io.$(OBJEXT): {$(VPATH)}ruby_assert.h
io.$(OBJEXT): {$(VPATH)}ruby_atomic.h
io.$(OBJEXT): {$(VPATH)}st.h
io.$(OBJEXT): {$(VPATH)}subst.h
io.$(OBJEXT): {$(VPATH)}thread.h
+io.$(OBJEXT): {$(VPATH)}thread_native.h
io.$(OBJEXT): {$(VPATH)}util.h
+io.$(OBJEXT): {$(VPATH)}vm_core.h
+io.$(OBJEXT): {$(VPATH)}vm_opts.h
iseq.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h
iseq.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h
iseq.$(OBJEXT): $(CCAN_DIR)/list/list.h
@@ -2803,6 +2874,7 @@ struct.$(OBJEXT): {$(VPATH)}struct.c
struct.$(OBJEXT): {$(VPATH)}subst.h
struct.$(OBJEXT): {$(VPATH)}thread_$(THREAD_MODEL).h
struct.$(OBJEXT): {$(VPATH)}thread_native.h
+struct.$(OBJEXT): {$(VPATH)}transient_heap.h
struct.$(OBJEXT): {$(VPATH)}vm_core.h
struct.$(OBJEXT): {$(VPATH)}vm_debug.h
struct.$(OBJEXT): {$(VPATH)}vm_opts.h
@@ -2898,6 +2970,15 @@ transcode.$(OBJEXT): {$(VPATH)}st.h
transcode.$(OBJEXT): {$(VPATH)}subst.h
transcode.$(OBJEXT): {$(VPATH)}transcode.c
transcode.$(OBJEXT): {$(VPATH)}transcode_data.h
+transient_heap.$(OBJEXT): $(hdrdir)/ruby/ruby.h
+transient_heap.$(OBJEXT): $(top_srcdir)/include/ruby.h
+transient_heap.$(OBJEXT): {$(VPATH)}debug_counter.h
+transient_heap.$(OBJEXT): {$(VPATH)}gc.h
+transient_heap.$(OBJEXT): {$(VPATH)}internal.h
+transient_heap.$(OBJEXT): {$(VPATH)}ruby_assert.h
+transient_heap.$(OBJEXT): {$(VPATH)}transient_heap.c
+transient_heap.$(OBJEXT): {$(VPATH)}transient_heap.h
+transient_heap.$(OBJEXT): {$(VPATH)}vm_debug.h
util.$(OBJEXT): $(hdrdir)/ruby/ruby.h
util.$(OBJEXT): $(top_srcdir)/include/ruby.h
util.$(OBJEXT): {$(VPATH)}config.h
@@ -2940,6 +3021,7 @@ variable.$(OBJEXT): {$(VPATH)}st.h
variable.$(OBJEXT): {$(VPATH)}subst.h
variable.$(OBJEXT): {$(VPATH)}thread_$(THREAD_MODEL).h
variable.$(OBJEXT): {$(VPATH)}thread_native.h
+variable.$(OBJEXT): {$(VPATH)}transient_heap.h
variable.$(OBJEXT): {$(VPATH)}util.h
variable.$(OBJEXT): {$(VPATH)}variable.c
variable.$(OBJEXT): {$(VPATH)}vm_core.h
diff --git a/compile.c b/compile.c
index ba6924e713700f..9d9560c3b527d6 100644
--- a/compile.c
+++ b/compile.c
@@ -370,7 +370,7 @@ NORETURN(static void append_compile_error(rb_iseq_t *iseq, int line, const char
#endif
static void
-append_compile_error(rb_iseq_t *iseq, int line, const char *fmt, ...)
+append_compile_error(const rb_iseq_t *iseq, int line, const char *fmt, ...)
{
VALUE err_info = ISEQ_COMPILE_DATA(iseq)->err_info;
VALUE file = rb_iseq_path(iseq);
@@ -567,6 +567,8 @@ APPEND_ELEM(ISEQ_ARG_DECLARE LINK_ANCHOR *const anchor, LINK_ELEMENT *before, LI
#define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
#endif
+#define ISEQ_LAST_LINE(iseq) (ISEQ_COMPILE_DATA(iseq)->last_line)
+
static int
iseq_add_mark_object_compile_time(const rb_iseq_t *iseq, VALUE v)
{
@@ -1393,7 +1395,8 @@ get_local_var_idx(const rb_iseq_t *iseq, ID id)
int idx = get_dyna_var_idx_at_raw(iseq->body->local_iseq, id);
if (idx < 0) {
- rb_bug("get_local_var_idx: %d", idx);
+ COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
+ "get_local_var_idx: %d", idx);
}
return idx;
@@ -1403,6 +1406,7 @@ static int
get_dyna_var_idx(const rb_iseq_t *iseq, ID id, int *level, int *ls)
{
int lv = 0, idx = -1;
+ const rb_iseq_t *const topmost_iseq = iseq;
while (iseq) {
idx = get_dyna_var_idx_at_raw(iseq, id);
@@ -1414,7 +1418,8 @@ get_dyna_var_idx(const rb_iseq_t *iseq, ID id, int *level, int *ls)
}
if (idx < 0) {
- rb_bug("get_dyna_var_idx: -1");
+ COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
+ "get_dyna_var_idx: -1");
}
*level = lv;
@@ -1510,7 +1515,7 @@ iseq_calc_param_size(rb_iseq_t *iseq)
body->param.size = body->param.lead_num + body->param.opt_num;
}
else {
- rb_bug("unreachable");
+ UNREACHABLE;
}
}
else {
@@ -1649,7 +1654,7 @@ iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *const optargs, const NODE *cons
opt_table = ALLOC_N(VALUE, i+1);
- MEMCPY(opt_table, RARRAY_CONST_PTR(labels), VALUE, i+1);
+ MEMCPY(opt_table, RARRAY_CONST_PTR_TRANSIENT(labels), VALUE, i+1);
for (j = 0; j < i+1; j++) {
opt_table[j] &= ~1;
}
@@ -2022,7 +2027,9 @@ iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
!(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
int line = iobj->insn_info.line_no;
- RARRAY_ASET(ISEQ_LINE_COVERAGE(iseq), line - 1, INT2FIX(0));
+ if (line >= 1) {
+ RARRAY_ASET(ISEQ_LINE_COVERAGE(iseq), line - 1, INT2FIX(0));
+ }
}
if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
while (RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
@@ -2154,7 +2161,10 @@ iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
unsigned int ic_index = FIX2UINT(operands[j]);
IC ic = (IC)&body->is_entries[ic_index];
if (UNLIKELY(ic_index >= body->is_size)) {
- rb_bug("iseq_set_sequence: ic_index overflow: index: %d, size: %d", ic_index, body->is_size);
+ BADINSN_DUMP(anchor, &iobj->link, 0);
+ COMPILE_ERROR(iseq, iobj->insn_info.line_no,
+ "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
+ ic_index, body->is_size);
}
generated_iseq[code_index + 1 + j] = (VALUE)ic;
break;
@@ -2297,14 +2307,14 @@ iseq_set_exception_table(rb_iseq_t *iseq)
struct iseq_catch_table_entry *entry;
tlen = (int)RARRAY_LEN(ISEQ_COMPILE_DATA(iseq)->catch_table_ary);
- tptr = RARRAY_CONST_PTR(ISEQ_COMPILE_DATA(iseq)->catch_table_ary);
+ tptr = RARRAY_CONST_PTR_TRANSIENT(ISEQ_COMPILE_DATA(iseq)->catch_table_ary);
if (tlen > 0) {
struct iseq_catch_table *table = xmalloc(iseq_catch_table_bytes(tlen));
table->size = tlen;
for (i = 0; i < table->size; i++) {
- ptr = RARRAY_CONST_PTR(tptr[i]);
+ ptr = RARRAY_CONST_PTR_TRANSIENT(tptr[i]);
entry = &table->entries[i];
entry->type = (enum catch_type)(ptr[0] & 0xffff);
entry->start = label_get_position((LABEL *)(ptr[1] & ~1));
@@ -3837,7 +3847,7 @@ enum compile_array_type_t {
};
static inline int
-static_literal_node_p(const NODE *node)
+static_literal_node_p(const NODE *node, const rb_iseq_t *iseq)
{
node = node->nd_head;
switch (nd_type(node)) {
@@ -3846,13 +3856,19 @@ static_literal_node_p(const NODE *node)
case NODE_TRUE:
case NODE_FALSE:
return TRUE;
+ case NODE_STR:
+ if (ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal) {
+ return TRUE;
+ } else {
+ return FALSE;
+ }
default:
return FALSE;
}
}
static inline VALUE
-static_literal_value(const NODE *node)
+static_literal_value(const NODE *node, rb_iseq_t *iseq)
{
node = node->nd_head;
switch (nd_type(node)) {
@@ -3862,6 +3878,17 @@ static_literal_value(const NODE *node)
return Qtrue;
case NODE_FALSE:
return Qfalse;
+ case NODE_STR:
+ if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
+ VALUE lit;
+ VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq), INT2FIX((int)nd_line(node)));
+ lit = rb_str_dup(node->nd_lit);
+ rb_ivar_set(lit, id_debug_created_info, rb_obj_freeze(debug_info));
+ return rb_str_freeze(lit);
+ }
+ else {
+ return rb_fstring(node->nd_lit);
+ }
default:
return node->nd_lit;
}
@@ -3911,7 +3938,7 @@ compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node_ro
}
break;
}
- if (opt_p && !static_literal_node_p(node)) {
+ if (opt_p && !static_literal_node_p(node, iseq)) {
opt_p = 0;
}
@@ -3933,15 +3960,15 @@ compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node_ro
node = start_node;
while (node != end_node) {
- rb_ary_push(ary, static_literal_value(node));
+ rb_ary_push(ary, static_literal_value(node, iseq));
node = node->nd_next;
}
while (node && node->nd_next &&
- static_literal_node_p(node) &&
- static_literal_node_p(node->nd_next)) {
+ static_literal_node_p(node, iseq) &&
+ static_literal_node_p(node->nd_next, iseq)) {
VALUE elem[2];
- elem[0] = static_literal_value(node);
- elem[1] = static_literal_value(node->nd_next);
+ elem[0] = static_literal_value(node, iseq);
+ elem[1] = static_literal_value(node->nd_next, iseq);
rb_ary_cat(ary, elem, 2);
node = node->nd_next->nd_next;
len++;
@@ -5017,7 +5044,7 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_nod
INIT_ANCHOR(body_seq);
INIT_ANCHOR(cond_seq);
- rb_hash_tbl_raw(literals)->type = &cdhash_type;
+ RHASH_TBL_RAW(literals)->type = &cdhash_type;
CHECK(COMPILE(head, "case base", node->nd_head));
@@ -6829,9 +6856,9 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
LABEL *lend = NEW_LABEL(line);
int ic_index = body->is_size++;
- ADD_INSN2(ret, line, getinlinecache, lend, INT2FIX(ic_index));
+ ADD_INSN2(ret, line, opt_getinlinecache, lend, INT2FIX(ic_index));
ADD_INSN1(ret, line, getconstant, ID2SYM(node->nd_vid));
- ADD_INSN1(ret, line, setinlinecache, INT2FIX(ic_index));
+ ADD_INSN1(ret, line, opt_setinlinecache, INT2FIX(ic_index));
ADD_LABEL(ret, lend);
}
else {
@@ -7187,7 +7214,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
CHECK(compile_const_prefix(iseq, node, pref, body));
if (LIST_INSN_SIZE_ZERO(pref)) {
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
- ADD_INSN2(ret, line, getinlinecache, lend, INT2FIX(ic_index));
+ ADD_INSN2(ret, line, opt_getinlinecache, lend, INT2FIX(ic_index));
}
else {
ADD_INSN(ret, line, putnil);
@@ -7196,7 +7223,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
ADD_SEQ(ret, body);
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
- ADD_INSN1(ret, line, setinlinecache, INT2FIX(ic_index));
+ ADD_INSN1(ret, line, opt_setinlinecache, INT2FIX(ic_index));
ADD_LABEL(ret, lend);
}
}
@@ -7224,7 +7251,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
/* add cache insn */
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
- ADD_INSN2(ret, line, getinlinecache, lend, INT2FIX(ic_index));
+ ADD_INSN2(ret, line, opt_getinlinecache, lend, INT2FIX(ic_index));
ADD_INSN(ret, line, pop);
}
@@ -7232,7 +7259,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
ADD_INSN1(ret, line, getconstant, ID2SYM(node->nd_mid));
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
- ADD_INSN1(ret, line, setinlinecache, INT2FIX(ic_index));
+ ADD_INSN1(ret, line, opt_setinlinecache, INT2FIX(ic_index));
ADD_LABEL(ret, lend);
}
@@ -7990,7 +8017,7 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *const anchor,
int i;
VALUE map = rb_hash_new_with_size(RARRAY_LEN(op)/2);
- rb_hash_tbl_raw(map)->type = &cdhash_type;
+ RHASH_TBL_RAW(map)->type = &cdhash_type;
op = rb_to_array_type(op);
for (i=0; ilocation.base_label, ibf_load_location_str(load, body->location.base_label));
RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, body->location.label));
load_body->location.first_lineno = body->location.first_lineno;
+ load_body->location.node_id = body->location.node_id;
load_body->location.code_location = body->location.code_location;
load_body->is_entries = ZALLOC_N(union iseq_inline_storage_entry, body->is_size);
@@ -9890,6 +9918,7 @@ rb_ibf_load_iseq_complete(rb_iseq_t *iseq)
ibf_load_iseq_each(load, iseq, offset);
ISEQ_COMPILE_DATA_CLEAR(iseq);
FL_UNSET(iseq, ISEQ_NOT_LOADED_YET);
+ rb_iseq_init_trace(iseq);
load->iseq = prev_src_iseq;
}
@@ -10038,8 +10067,6 @@ rb_iseq_ibf_load(VALUE str)
ibf_load_setup(load, loader_obj, str);
iseq = ibf_load_iseq(load, 0);
- rb_iseq_init_trace(iseq);
-
RB_GC_GUARD(loader_obj);
return iseq;
}
diff --git a/complex.c b/complex.c
index a6e2de60edf39d..7a52a4259e2552 100644
--- a/complex.c
+++ b/complex.c
@@ -546,7 +546,7 @@ f_complex_polar(VALUE klass, VALUE x, VALUE y)
/* returns a Complex or Float of ang*PI-rotated abs */
VALUE
-rb_dbl_complex_polar(double abs, double ang)
+rb_dbl_complex_polar_pi(double abs, double ang)
{
double fi;
const double fr = modf(ang, &fi);
diff --git a/configure.ac b/configure.ac
index 70d8ed9cc3b4f9..ed88ec15ff168e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -252,19 +252,27 @@ AC_SUBST(OUTFLAG)
AC_SUBST(COUTFLAG)
AC_SUBST(CSRCFLAG)
+: ${MJIT_CC=$CC}
+AS_IF([test "x$cross_compiling" = xno], [
+ AC_PATH_PROG([MJIT_CC], ${MJIT_CC})
+ AS_CASE([$target_os],
+ [*mingw*], [command -v cygpath > /dev/null && MJIT_CC=`cygpath -ma $MJIT_CC`])
+ shift 2
+ MJIT_CC="$MJIT_CC${1+ }$*"
+])
+
AS_CASE(["$build_os"],
[darwin1*.*], [
# Xcode linker warns for deprecated architecture and wrongly
# installed TBD files.
CC_WRAPPER=""
- AS_IF([! $CC -E -xc - </dev/null 2>&1], [
- @%:@if defined __APPLE_CC__ && defined __clang_major__ && __clang_major__ >= 10
- @%:@error ignore linker warnings
- @%:@endif
-SRC
+ echo 'int main(void) {return 0;}' > conftest.c
+ AS_IF([$CC -framework Foundation -o conftest conftest.c 2>&1 |
+ grep '^ld: warning: text-based stub file' >/dev/null], [
CC_WRAPPER=`cd -P "$srcdir/tool" && pwd`/darwin-cc
CC="$CC_WRAPPER $CC"
])
+ rm -fr conftest*
])
cc_version=
@@ -465,46 +473,48 @@ AC_ARG_ENABLE(werror,
[particular_werror_flags=$enableval])
rb_cv_warnflags="$warnflags"
-AS_IF([test "$GCC:${warnflags+set}:no" = yes::no], [
+AS_CASE(["$GCC:${warnflags+set}:${extra_warnflags:+set}:"],
+[yes::*|yes:*:set:], [# GCC && (!warnflags || extra_warnflags)
AS_IF([test $gcc_major -ge 4], [
- extra_warning=-Werror=extra-tokens
- ], [
- extra_warning=
+ extra_warnflags="$extra_warnflags -Werror=extra-tokens"
])
AS_IF([test $gcc_major -ge 5 -a $gcc_major -le 6], [
- extra_warning="$extra_warning -Wno-maybe-uninitialized"
+ extra_warnflags="$extra_warnflags -Wno-maybe-uninitialized"
])
# ICC doesn't support -Werror=
AS_IF([test $icc_version -gt 0], [
particular_werror_flags=no
])
- for wflag in -Wno-unused-parameter -Wno-long-long \
- -diag-disable=175,188,2259 \
- -Wno-missing-field-initializers \
- -Wno-tautological-compare \
- -Wno-parentheses-equality \
- -Wno-constant-logical-operand \
- -Wno-self-assign \
- -Wno-cast-function-type \
- -Wunused-variable \
- -Werror=implicit-int \
- -Werror=pointer-arith \
- -Werror=write-strings \
+ for wflag in \
-Werror=declaration-after-statement \
- -Werror=shorten-64-to-32 \
- -Werror=implicit-function-declaration \
- -Werror=division-by-zero \
-Werror=deprecated-declarations \
+ -Werror=division-by-zero \
+ -Werror=duplicated-cond \
+ -Werror=implicit-function-declaration \
+ -Werror=implicit-int \
-Werror=misleading-indentation \
+ -Werror=pointer-arith \
+ -Werror=restrict \
+ -Werror=shorten-64-to-32 \
+ -Werror=write-strings \
+ -Wimplicit-fallthrough=0 \
+ -Wmissing-noreturn \
+ -Wno-cast-function-type \
+ -Wno-constant-logical-operand \
+ -Wno-long-long \
+ -Wno-missing-field-initializers \
-Wno-overlength-strings \
-Wno-packed-bitfield-compat \
- -Wsuggest-attribute=noreturn \
+ -Wno-parentheses-equality \
+ -Wno-self-assign \
+ -Wno-tautological-compare \
+ -Wno-unused-parameter \
+ -Wno-unused-value \
-Wsuggest-attribute=format \
- -Wmissing-noreturn \
- -Wimplicit-fallthrough=0 \
- -Werror=duplicated-cond \
- -Werror=restrict \
- $extra_warning \
+ -Wsuggest-attribute=noreturn \
+ -Wunused-variable \
+ -diag-disable=175,188,2259 \
+ $extra_warnflags \
; do
AS_IF([test "$particular_werror_flags" != yes], [
wflag=`echo x$wflag | sed 's/^x-Werror=/-W/;s/^x//'`
@@ -948,9 +958,12 @@ main()
ac_cv_func_clock_gettime=yes
ac_cv_func_clock_getres=yes
ac_cv_func_malloc_usable_size=no
+ ac_cv_type_off_t=yes
+ ac_cv_sizeof_off_t=8
AS_IF([test "$target_cpu" = x64], [
- ac_cv_func___builtin_setjmp=no
+ ac_cv_func___builtin_setjmp=yes
ac_cv_func_round=no
+ rb_cv_fiber_coroutine=yes
])
ac_cv_func_tgamma=no
rb_cv_negative_time_t=yes
@@ -991,6 +1004,11 @@ main()
AS_CASE(["$target_cpu"], [powerpc64*], [
ac_cv_func___builtin_setjmp=no
])
+ # With gcc-8's -fcf-protection, MJIT's __builtin_longjmp fails.
+ AS_CASE(["$CC $CFLAGS "], [*" -fcf-protection "*], [cf_protection=yes], [cf_protection=no])
+ AS_IF([test "$cf_protection" = yes], [
+ ac_cv_func___builtin_setjmp=no
+ ])
],
[ LIBS="-lm $LIBS"])
: ${ORIG_LIBS=$LIBS}
@@ -1023,6 +1041,8 @@ AC_CHECK_HEADERS(malloc_np.h)
AC_CHECK_HEADERS(net/socket.h)
AC_CHECK_HEADERS(process.h)
AC_CHECK_HEADERS(pwd.h)
+AC_CHECK_HEADERS(sanitizer/asan_interface.h)
+AC_CHECK_HEADERS(sanitizer/msan_interface.h)
AC_CHECK_HEADERS(setjmpex.h)
AC_CHECK_HEADERS(stdalign.h)
AC_CHECK_HEADERS(sys/attr.h)
@@ -1127,7 +1147,6 @@ mv confdefs1.h confdefs.h
cat largefile.h >> confdefs.h
AS_CASE(["$target_os"],
- [mingw*], [ac_cv_type_off_t=yes;ac_cv_sizeof_off_t=8],
[aix*], [
AS_CASE(["$target_cpu:$ac_cv_sys_large_files"],
[ppc64:*|powerpc64:*], [],
@@ -1352,6 +1371,9 @@ RUBY_FUNC_ATTRIBUTE(__deprecated__("by "@%:@n), DEPRECATED_BY(n,x), rb_cv_func_d
RUBY_TYPE_ATTRIBUTE(__deprecated__ mesg, DEPRECATED_TYPE(mesg,x), rb_cv_type_deprecated)
RUBY_FUNC_ATTRIBUTE(__noinline__, NOINLINE)
RUBY_FUNC_ATTRIBUTE(__always_inline__, ALWAYS_INLINE)
+RUBY_FUNC_ATTRIBUTE(__no_sanitize__(san), NO_SANITIZE(san, x), rb_cv_func_no_sanitize)
+RUBY_FUNC_ATTRIBUTE(__no_sanitize_address__, NO_SANITIZE_ADDRESS)
+RUBY_FUNC_ATTRIBUTE(__no_address_safety_analysis__, NO_ADDRESS_SAFETY_ANALYSIS)
RUBY_FUNC_ATTRIBUTE(__warn_unused_result__, WARN_UNUSED_RESULT)
RUBY_FUNC_ATTRIBUTE(__unused__, MAYBE_UNUSED)
RUBY_FUNC_ATTRIBUTE(__error__ mesg, ERRORFUNC(mesg,x), rb_cv_func___error__)
@@ -1619,34 +1641,6 @@ AS_IF([test $rb_cv_stack_end_address != no], [
AC_DEFINE_UNQUOTED(STACK_END_ADDRESS, $rb_cv_stack_end_address)
])
-# posix_memalign(memptr, alignment, size) implemented for OpenBSD 4.8 doesn't work if alignment > MALLOC_PAGESIZE.
-# [ruby-core:42158] https://bugs.ruby-lang.org/issues/5901
-# OpenBSD 5.2 fixed the problem. (src/lib/libc/stdlib/malloc.c:1.142)
-# MirOS #10semel has the problem but fixed in the repository. (src/lib/libc/stdlib/malloc.c:1.9)
-AS_CASE(["$target_os"],
-[openbsd*|mirbsd*], [
- AC_CACHE_CHECK(for heap align log on openbsd, rb_cv_page_size_log,
- [rb_cv_page_size_log=no
- for page_log in 12 13; do
- AC_TRY_RUN([
-#include
-#include
-
-int
-main() {
- if ((int)log2((double)sysconf(_SC_PAGESIZE)) != $page_log) return 1;
- return 0;
-}
- ],
- rb_cv_page_size_log="$page_log"; break)
- done])
- AS_IF([test $rb_cv_page_size_log != no], [
- AC_DEFINE_UNQUOTED(HEAP_ALIGN_LOG, $rb_cv_page_size_log)
- ], [
- AC_DEFINE_UNQUOTED(HEAP_ALIGN_LOG, 12)
- ])
-])
-
dnl Checks for library functions.
AC_TYPE_GETGROUPS
AC_TYPE_SIGNAL
@@ -1980,6 +1974,7 @@ AS_IF([test x$rb_cv_builtin___builtin_choose_expr = xyes], [
])
])
RUBY_CHECK_BUILTIN_FUNC(__builtin_types_compatible_p, [__builtin_types_compatible_p(int, int)])
+RUBY_CHECK_BUILTIN_FUNC(__builtin_trap, [__builtin_trap()])
AS_IF([test "$ac_cv_func_qsort_r" != no], [
AC_CACHE_CHECK(whether qsort_r is GNU version, rb_cv_gnu_qsort_r,
@@ -2304,6 +2299,40 @@ AS_IF([test "${universal_binary-no}" = yes ], [
AC_DEFINE_UNQUOTED(STACK_GROW_DIRECTION, $dir)
])
+AC_ARG_ENABLE(fiber-coroutine,
+ AS_HELP_STRING([--disable-fiber-coroutine], [disable native coroutine implementation for fiber]),
+ [rb_cv_fiber_coroutine=$enableval])
+AS_CASE(["$rb_cv_fiber_coroutine"], [yes|''], [
+ AC_MSG_CHECKING(native coroutine implementation for ${target_cpu}-${target_os})
+ AS_CASE(["$target_cpu-$target_os"],
+ [x*64-darwin*], [
+ rb_cv_fiber_coroutine=amd64
+ ],
+ [x*64-linux], [
+ AS_CASE(["$ac_cv_sizeof_voidp"],
+ [8], [ rb_cv_fiber_coroutine=amd64 ],
+ [4], [ rb_cv_fiber_coroutine=x86 ],
+ [*], [ rb_cv_fiber_coroutine= ]
+ )
+ ],
+ [*86-linux], [
+ rb_cv_fiber_coroutine=x86
+ ],
+ [x64-mingw32], [
+ rb_cv_fiber_coroutine=win64
+ ],
+ [*], [
+ rb_cv_fiber_coroutine=
+ ]
+ )
+ AC_MSG_RESULT(${rb_cv_fiber_coroutine:-no})
+])
+AS_IF([test "${rb_cv_fiber_coroutine:-no}" != no], [
+ COROUTINE_H=coroutine/$rb_cv_fiber_coroutine/Context.h
+ AC_DEFINE_UNQUOTED(FIBER_USE_COROUTINE, ["$COROUTINE_H"])
+ AC_SUBST(X_FIBER_COROUTINE_H, [$COROUTINE_H])
+])
+
AS_IF([test x"$enable_pthread" = xyes], [
for pthread_lib in thr pthread pthreads c c_r root; do
AC_CHECK_LIB($pthread_lib, pthread_create,
@@ -2533,7 +2562,7 @@ AC_SUBST(DLDFLAGS)dnl
AC_SUBST(ARCH_FLAG)dnl
AC_SUBST(MJIT_HEADER_FLAGS)dnl
AC_SUBST(MJIT_HEADER_INSTALL_DIR)dnl
-AC_SUBST(MJIT_CC, [${MJIT_CC-'$(CC)'}])dnl
+AC_SUBST(MJIT_CC)dnl
AC_SUBST(MJIT_CFLAGS, [${MJIT_CFLAGS-"-w ${orig_cflags}"}])dnl
AC_SUBST(MJIT_OPTFLAGS, [${MJIT_OPTFLAGS-'$(optflags)'}])dnl
AC_SUBST(MJIT_DEBUGFLAGS, [${MJIT_DEBUGFLAGS-'$(debugflags)'}])dnl
@@ -2644,6 +2673,7 @@ AS_IF([test "$with_dln_a_out" != yes], [
])
rb_cv_dlopen=yes],
[darwin*], [ : ${LDSHARED='$(CC) -dynamic -bundle'}
+ : ${DLDSHARED='$(CC) -dynamiclib'}
: ${LDFLAGS=""}
: ${LIBPATHENV=DYLD_FALLBACK_LIBRARY_PATH}
: ${PRELOADENV=DYLD_INSERT_LIBRARIES}
@@ -2654,8 +2684,8 @@ AS_IF([test "$with_dln_a_out" != yes], [
XLDFLAGS="${linker_flag}"'-bE:$(ARCHFILE)'" ${linker_flag}-brtl"
XLDFLAGS="$XLDFLAGS ${linker_flag}-blibpath:${prefix}/lib:${LIBPATH:-/usr/lib:/lib}"
: ${ARCHFILE="ruby.imp"}
- TRY_LINK='$(CC) $(LDFLAGS) -oconftest $(INCFLAGS) -I$(hdrdir) $(CPPFLAGS)'
- TRY_LINK="$TRY_LINK"' $(CFLAGS) $(src) $(LIBPATH) $(LOCAL_LIBS) $(LIBS)'
+ TRY_LINK='$(CC) -oconftest $(INCFLAGS) -I$(hdrdir) $(CPPFLAGS)'
+ TRY_LINK="$TRY_LINK"' $(CFLAGS) $(src) $(LIBPATH) $(LDFLAGS) $(LOCAL_LIBS) $(LIBS)'
: ${LIBPATHENV=LIBPATH}
rb_cv_dlopen=yes],
[nto-qnx*], [ DLDFLAGS="$DLDFLAGS -L/lib -L/usr/lib -L/usr/local/lib"
@@ -3056,7 +3086,7 @@ AC_ARG_WITH(soname,
[RUBY_SO_NAME='$(RUBY_BASE_NAME)'])
])
-LIBRUBY_LDSHARED=$LDSHARED
+LIBRUBY_LDSHARED=${DLDSHARED=${LDSHARED}}
LIBRUBY_DLDFLAGS=$DLDFLAGS
LIBRUBY_SO='lib$(RUBY_SO_NAME).$(SOEXT).$(RUBY_PROGRAM_VERSION)'
LIBRUBY_SONAME='lib$(RUBY_SO_NAME).$(SOEXT).$(RUBY_API_VERSION)'
@@ -3149,7 +3179,6 @@ AS_CASE("$enable_shared", [yes], [
LIBRUBY_SO='lib$(RUBY_SO_NAME).$(SOEXT)'
LIBRUBY_SONAME='$(LIBRUBY_SO)'
LIBRUBY_ALIASES='lib$(RUBY_INSTALL_NAME).$(SOEXT)'
- LIBRUBY_LDSHARED='$(CC) -dynamiclib'
AS_IF([test "$load_relative" = yes], [
libprefix="@executable_path/../${libdir_basename}"
LIBRUBY_RELATIVE=yes
@@ -3325,10 +3354,10 @@ AC_SUBST(CAPITARGET)
AS_CASE(["$RDOCTARGET:$CAPITARGET"],[nodoc:nodoc],[INSTALLDOC=nodoc],[INSTALLDOC=all])
AC_SUBST(INSTALLDOC)
-AC_ARG_ENABLE(mjit-support,
- AS_HELP_STRING([--disable-mjit-support], [disable MJIT features]),
+AC_ARG_ENABLE(jit-support,
+ AS_HELP_STRING([--disable-jit-support], [disable JIT features]),
[MJIT_SUPPORT=$enableval
- AS_IF([test x"$enable_mjit_support" = "xyes"],
+ AS_IF([test x"$enable_jit_support" = "xyes"],
[AC_DEFINE(USE_MJIT, 1)],
[AC_DEFINE(USE_MJIT, 0)])],
[MJIT_SUPPORT=yes
@@ -3576,6 +3605,7 @@ AC_SUBST(LIBRUBYARG_STATIC)
AC_SUBST(LIBRUBYARG_SHARED)
AC_SUBST(SOLIBS)
AC_SUBST(DLDLIBS)
+AC_SUBST(DLDSHARED)
AC_SUBST(ENABLE_SHARED)
AC_SUBST(MAINLIBS)
AC_SUBST(COMMON_LIBS)
@@ -3862,12 +3892,14 @@ PACKAGE=$RUBY_BASE_NAME
AC_SUBST(PACKAGE)
AS_MESSAGE([$PACKAGE library version = $ruby_version])
-AS_CASE(["$build_os"], [darwin1*.*], [
- AS_IF([test x"$CC_WRAPPER" != x], [
- CC="${CC@%:@$CC_WRAPPER }"
- CPP="${CPP@%:@$CC_WRAPPER }"
- ])
+AS_IF([test x"$CC_WRAPPER" != x], [
+ CC='$(CC_WRAPPER) '"${CC@%:@$CC_WRAPPER }"
+ CPP='$(CC_WRAPPER) '"${CPP@%:@$CC_WRAPPER }"
+ CC_WRAPPER='$(rubyarchdir)/darwin-cc'
+ XCC_WRAPPER='$(top_srcdir)/tool/darwin-cc'
])
+AC_SUBST(CC_WRAPPER, '')
+AC_SUBST(XCC_WRAPPER)
AS_CASE([" $CPP "], [*" $CC "*], [CPP=`echo " $CPP " | sed "s| $CC |"' $(CC) |;s/^ *//;s/ *$//'`])
@@ -3997,7 +4029,7 @@ config_summary "debugflags" "$debugflags"
config_summary "warnflags" "$warnflags"
config_summary "strip command" "$STRIP"
config_summary "install doc" "$install_doc"
-config_summary "MJIT support" "$MJIT_SUPPORT"
+config_summary "JIT support" "$MJIT_SUPPORT"
config_summary "man page type" "$MANTYPE"
config_summary "search path" "$search_path"
config_summary "static-linked-ext" ${EXTSTATIC:+"yes"}
diff --git a/cont.c b/cont.c
index 95f2de9b87dd79..142713ad842ab7 100644
--- a/cont.c
+++ b/cont.c
@@ -26,6 +26,27 @@
* in Proc. of 51th Programming Symposium, pp.21--28 (2010) (in Japanese).
*/
+/*
+ Enable FIBER_USE_COROUTINE to make fiber yield/resume much faster by using native assembly implementations.
+
+ rvm install ruby-head-ioquatix-native-fiber --url https://github.com/ioquatix/ruby --branch native-fiber
+
+ # Without libcoro
+ koyoko% ./build/bin/ruby ./fiber_benchmark.rb 10000 1000
+ setup time for 10000 fibers: 0.099961
+ execution time for 1000 messages: 19.505909
+
+ # With libcoro
+ koyoko% ./build/bin/ruby ./fiber_benchmark.rb 10000 1000
+ setup time for 10000 fibers: 0.099268
+ execution time for 1000 messages: 8.491746
+*/
+
+#ifdef FIBER_USE_COROUTINE
+#include FIBER_USE_COROUTINE
+#define FIBER_USE_NATIVE 1
+#endif
+
#if !defined(FIBER_USE_NATIVE)
# if defined(HAVE_GETCONTEXT) && defined(HAVE_SETCONTEXT)
# if 0
@@ -139,7 +160,7 @@ enum fiber_status {
#define FIBER_TERMINATED_P(fib) ((fib)->status == FIBER_TERMINATED)
#define FIBER_RUNNABLE_P(fib) (FIBER_CREATED_P(fib) || FIBER_SUSPENDED_P(fib))
-#if FIBER_USE_NATIVE && !defined(_WIN32)
+#if FIBER_USE_NATIVE && !defined(FIBER_USE_COROUTINE) && !defined(_WIN32)
static inline int
fiber_context_create(ucontext_t *context, void (*func)(), void *arg, void *ptr, size_t size)
{
@@ -158,17 +179,6 @@ fiber_context_create(ucontext_t *context, void (*func)(), void *arg, void *ptr,
}
#endif
-#if FIBER_USE_NATIVE && !defined(_WIN32)
-#define MAX_MACHINE_STACK_CACHE 10
-static int machine_stack_cache_index = 0;
-typedef struct machine_stack_cache_struct {
- void *ptr;
- size_t size;
-} machine_stack_cache_t;
-static machine_stack_cache_t machine_stack_cache[MAX_MACHINE_STACK_CACHE];
-static machine_stack_cache_t terminated_machine_stack;
-#endif
-
struct rb_fiber_struct {
rb_context_t cont;
VALUE first_proc;
@@ -181,9 +191,15 @@ struct rb_fiber_struct {
unsigned int transferred : 1;
#if FIBER_USE_NATIVE
-#ifdef _WIN32
+#if defined(FIBER_USE_COROUTINE)
+#define FIBER_ALLOCATE_STACK
+ coroutine_context context;
+ void *ss_sp;
+ size_t ss_size;
+#elif defined(_WIN32)
void *fib_handle;
#else
+#define FIBER_ALLOCATE_STACK
ucontext_t context;
/* Because context.uc_stack.ss_sp and context.uc_stack.ss_size
* are not necessarily valid after makecontext() or swapcontext(),
@@ -195,6 +211,17 @@ struct rb_fiber_struct {
#endif
};
+#ifdef FIBER_ALLOCATE_STACK
+#define MAX_MACHINE_STACK_CACHE 10
+static int machine_stack_cache_index = 0;
+typedef struct machine_stack_cache_struct {
+ void *ptr;
+ size_t size;
+} machine_stack_cache_t;
+static machine_stack_cache_t machine_stack_cache[MAX_MACHINE_STACK_CACHE];
+static machine_stack_cache_t terminated_machine_stack;
+#endif
+
static const char *
fiber_status_name(enum fiber_status s)
{
@@ -250,7 +277,7 @@ fiber_status_set(rb_fiber_t *fib, enum fiber_status s)
}
void
-ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size)
+rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size)
{
ec->vm_stack = stack;
ec->vm_stack_size = size;
@@ -381,9 +408,22 @@ cont_free(void *ptr)
}
else {
/* fiber */
- const rb_fiber_t *fib = (rb_fiber_t*)cont;
+ rb_fiber_t *fib = (rb_fiber_t*)cont;
+#if defined(FIBER_USE_COROUTINE)
+ coroutine_destroy(&fib->context);
+ if (fib->ss_sp != NULL) {
+ if (fiber_is_root_p(fib)) {
+ rb_bug("Illegal root fiber parameter");
+ }
#ifdef _WIN32
- if (!fiber_is_root_p(fib)) {
+ VirtualFree((void*)fib->ss_sp, 0, MEM_RELEASE);
+#else
+ munmap((void*)fib->ss_sp, fib->ss_size);
+#endif
+ fib->ss_sp = NULL;
+ }
+#elif defined(_WIN32)
+ if (!fiber_is_root_p(fib)) {
/* don't delete root fiber handle */
if (fib->fib_handle) {
DeleteFiber(fib->fib_handle);
@@ -673,7 +713,7 @@ cont_capture(volatile int *volatile stat)
cont->saved_vm_stack.ptr = ALLOC_N(VALUE, ec->vm_stack_size);
MEMCPY(cont->saved_vm_stack.ptr, ec->vm_stack, VALUE, ec->vm_stack_size);
#endif
- ec_set_vm_stack(&cont->saved_ec, NULL, 0);
+ rb_ec_set_vm_stack(&cont->saved_ec, NULL, 0);
cont_save_machine_stack(th, cont);
/* backup ensure_list to array for search in another context */
@@ -738,6 +778,10 @@ cont_restore_thread(rb_context_t *cont)
ec_switch(th, fib);
}
+ if (th->ec->trace_arg != sec->trace_arg) {
+ rb_raise(rb_eRuntimeError, "can't call across trace_func");
+ }
+
/* copy vm stack */
#ifdef CAPTURE_JUST_VALID_VM_STACK
MEMCPY(th->ec->vm_stack,
@@ -760,16 +804,6 @@ cont_restore_thread(rb_context_t *cont)
th->ec->ensure_list = sec->ensure_list;
th->ec->errinfo = sec->errinfo;
- /* trace on -> trace off */
- if (th->ec->trace_arg != NULL && sec->trace_arg == NULL) {
- GET_VM()->trace_running--;
- }
- /* trace off -> trace on */
- else if (th->ec->trace_arg == NULL && sec->trace_arg != NULL) {
- GET_VM()->trace_running++;
- }
- th->ec->trace_arg = sec->trace_arg;
-
VM_ASSERT(th->ec->vm_stack != NULL);
}
else {
@@ -779,7 +813,13 @@ cont_restore_thread(rb_context_t *cont)
}
#if FIBER_USE_NATIVE
-#ifdef _WIN32
+#if defined(FIBER_USE_COROUTINE)
+static COROUTINE
+fiber_entry(coroutine_context * from, coroutine_context * to)
+{
+ rb_fiber_start();
+}
+#elif defined(_WIN32)
static void
fiber_set_stack_location(void)
{
@@ -797,15 +837,17 @@ fiber_entry(void *arg)
fiber_set_stack_location();
rb_fiber_start();
}
-#else /* _WIN32 */
-
+#else
NORETURN(static void fiber_entry(void *arg));
static void
fiber_entry(void *arg)
{
rb_fiber_start();
}
+#endif
+#endif
+#ifdef FIBER_ALLOCATE_STACK
/*
* FreeBSD require a first (i.e. addr) argument of mmap(2) is not NULL
* if MAP_STACK is passed.
@@ -823,46 +865,70 @@ static char*
fiber_machine_stack_alloc(size_t size)
{
char *ptr;
+#ifdef _WIN32
+ DWORD old_protect;
+#endif
if (machine_stack_cache_index > 0) {
- if (machine_stack_cache[machine_stack_cache_index - 1].size == (size / sizeof(VALUE))) {
- ptr = machine_stack_cache[machine_stack_cache_index - 1].ptr;
- machine_stack_cache_index--;
- machine_stack_cache[machine_stack_cache_index].ptr = NULL;
- machine_stack_cache[machine_stack_cache_index].size = 0;
- }
- else{
+ if (machine_stack_cache[machine_stack_cache_index - 1].size == (size / sizeof(VALUE))) {
+ ptr = machine_stack_cache[machine_stack_cache_index - 1].ptr;
+ machine_stack_cache_index--;
+ machine_stack_cache[machine_stack_cache_index].ptr = NULL;
+ machine_stack_cache[machine_stack_cache_index].size = 0;
+ } else {
/* TODO handle multiple machine stack size */
- rb_bug("machine_stack_cache size is not canonicalized");
- }
- }
- else {
- void *page;
- STACK_GROW_DIR_DETECTION;
+ rb_bug("machine_stack_cache size is not canonicalized");
+ }
+ } else {
+#ifdef _WIN32
+ ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
- errno = 0;
- ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, FIBER_STACK_FLAGS, -1, 0);
- if (ptr == MAP_FAILED) {
- rb_raise(rb_eFiberError, "can't alloc machine stack to fiber: %s", ERRNOMSG);
- }
+ if (!ptr) {
+ rb_raise(rb_eFiberError, "can't allocate machine stack to fiber: %s", ERRNOMSG);
+ }
- /* guard page setup */
- page = ptr + STACK_DIR_UPPER(size - RB_PAGE_SIZE, 0);
- if (mprotect(page, RB_PAGE_SIZE, PROT_NONE) < 0) {
- rb_raise(rb_eFiberError, "can't set a guard page: %s", ERRNOMSG);
- }
+ if (!VirtualProtect(ptr, RB_PAGE_SIZE, PAGE_READWRITE | PAGE_GUARD, &old_protect)) {
+ rb_raise(rb_eFiberError, "can't set a guard page: %s", ERRNOMSG);
+ }
+#else
+ void *page;
+ STACK_GROW_DIR_DETECTION;
+
+ errno = 0;
+ ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, FIBER_STACK_FLAGS, -1, 0);
+ if (ptr == MAP_FAILED) {
+ rb_raise(rb_eFiberError, "can't alloc machine stack to fiber: %s", ERRNOMSG);
+ }
+
+ /* guard page setup */
+ page = ptr + STACK_DIR_UPPER(size - RB_PAGE_SIZE, 0);
+ if (mprotect(page, RB_PAGE_SIZE, PROT_NONE) < 0) {
+ rb_raise(rb_eFiberError, "can't set a guard page: %s", ERRNOMSG);
+ }
+#endif
}
return ptr;
}
#endif
+#if FIBER_USE_NATIVE
static void
fiber_initialize_machine_stack_context(rb_fiber_t *fib, size_t size)
{
rb_execution_context_t *sec = &fib->cont.saved_ec;
-#ifdef _WIN32
+#if defined(FIBER_USE_COROUTINE)
+ char *ptr;
+ STACK_GROW_DIR_DETECTION;
+
+ ptr = fiber_machine_stack_alloc(size);
+ fib->ss_sp = ptr;
+ fib->ss_size = size;
+ coroutine_initialize(&fib->context, fiber_entry, ptr+size, size);
+ sec->machine.stack_start = (VALUE*)(ptr + STACK_DIR_UPPER(0, size));
+ sec->machine.stack_maxsize = size - RB_PAGE_SIZE;
+#elif defined(_WIN32)
# if defined(_MSC_VER) && _MSC_VER <= 1200
# define CreateFiberEx(cs, stacksize, flags, entry, param) \
CreateFiber((stacksize), (entry), (param))
@@ -925,19 +991,19 @@ fiber_setcontext(rb_fiber_t *newfib, rb_fiber_t *oldfib)
/* restore thread context */
fiber_restore_thread(th, newfib);
-#ifndef _WIN32
- if (!newfib->context.uc_stack.ss_sp && th->root_fiber != newfib) {
- rb_bug("non_root_fiber->context.uc_stac.ss_sp should not be NULL");
- }
-#endif
/* swap machine context */
-#ifdef _WIN32
+#if defined(FIBER_USE_COROUTINE)
+ coroutine_transfer(&oldfib->context, &newfib->context);
+#elif defined(_WIN32)
SwitchToFiber(newfib->fib_handle);
#else
+ if (!newfib->context.uc_stack.ss_sp && th->root_fiber != newfib) {
+ rb_bug("non_root_fiber->context.uc_stac.ss_sp should not be NULL");
+ }
swapcontext(&oldfib->context, &newfib->context);
#endif
}
-#endif
+#endif /* FIBER_USE_NATIVE */
NOINLINE(NORETURN(static void cont_restore_1(rb_context_t *)));
@@ -1199,7 +1265,7 @@ rollback_ensure_stack(VALUE self,rb_ensure_list_t *current,rb_ensure_entry_t *ta
{
rb_ensure_list_t *p;
rb_ensure_entry_t *entry;
- size_t i;
+ size_t i, j;
size_t cur_size;
size_t target_size;
size_t base_point;
@@ -1237,11 +1303,11 @@ rollback_ensure_stack(VALUE self,rb_ensure_list_t *current,rb_ensure_entry_t *ta
cur_size--;
}
/* push ensure stack */
- while (i--) {
- func = (VALUE (*)(ANYARGS)) lookup_rollback_func(target[i].e_proc);
- if ((VALUE)func != Qundef) {
- (*func)(target[i].data2);
- }
+ for (j = 0; j < i; j++) {
+ func = (VALUE (*)(ANYARGS)) lookup_rollback_func(target[i - j - 1].e_proc);
+ if ((VALUE)func != Qundef) {
+ (*func)(target[i - j - 1].data2);
+ }
}
}
@@ -1425,7 +1491,7 @@ fiber_init(VALUE fibval, VALUE proc)
else {
vm_stack = ruby_xmalloc(fib_stack_bytes);
}
- ec_set_vm_stack(sec, vm_stack, fib_stack_bytes / sizeof(VALUE));
+ rb_ec_set_vm_stack(sec, vm_stack, fib_stack_bytes / sizeof(VALUE));
sec->cfp = (void *)(sec->vm_stack + sec->vm_stack_size);
rb_vm_push_frame(sec,
@@ -1532,7 +1598,9 @@ root_fiber_alloc(rb_thread_t *th)
fib->cont.self = fibval;
#if FIBER_USE_NATIVE
-#ifdef _WIN32
+#if defined(FIBER_USE_COROUTINE)
+ coroutine_initialize(&fib->context, NULL, NULL, 0);
+#elif defined(_WIN32)
/* setup fib_handle for root Fiber */
if (fib->fib_handle == 0) {
if ((fib->fib_handle = ConvertThreadToFiber(0)) == 0) {
@@ -1625,11 +1693,11 @@ fiber_store(rb_fiber_t *next_fib, rb_thread_t *th)
rb_fiber_t *fib;
if (th->ec->fiber_ptr != NULL) {
- fib = th->ec->fiber_ptr;
+ fib = th->ec->fiber_ptr;
}
else {
- /* create root fiber */
- fib = root_fiber_alloc(th);
+ /* create root fiber */
+ fib = root_fiber_alloc(th);
}
VM_ASSERT(FIBER_RESUMED_P(fib) || FIBER_TERMINATED_P(fib));
@@ -1637,7 +1705,7 @@ fiber_store(rb_fiber_t *next_fib, rb_thread_t *th)
#if FIBER_USE_NATIVE
if (FIBER_CREATED_P(next_fib)) {
- fiber_initialize_machine_stack_context(next_fib, th->vm->default_params.fiber_machine_stack_size);
+ fiber_initialize_machine_stack_context(next_fib, th->vm->default_params.fiber_machine_stack_size);
}
#endif
@@ -1655,19 +1723,23 @@ fiber_store(rb_fiber_t *next_fib, rb_thread_t *th)
/* restored */
#ifdef MAX_MACHINE_STACK_CACHE
if (terminated_machine_stack.ptr) {
- if (machine_stack_cache_index < MAX_MACHINE_STACK_CACHE) {
- machine_stack_cache[machine_stack_cache_index++] = terminated_machine_stack;
- }
- else {
- if (terminated_machine_stack.ptr != fib->cont.machine.stack) {
- munmap((void*)terminated_machine_stack.ptr, terminated_machine_stack.size * sizeof(VALUE));
- }
- else {
- rb_bug("terminated fiber resumed");
- }
- }
- terminated_machine_stack.ptr = NULL;
- terminated_machine_stack.size = 0;
+ if (machine_stack_cache_index < MAX_MACHINE_STACK_CACHE) {
+ machine_stack_cache[machine_stack_cache_index++] = terminated_machine_stack;
+ }
+ else {
+ if (terminated_machine_stack.ptr != fib->cont.machine.stack) {
+#ifdef _WIN32
+ VirtualFree(terminated_machine_stack.ptr, 0, MEM_RELEASE);
+#else
+ munmap((void*)terminated_machine_stack.ptr, terminated_machine_stack.size * sizeof(VALUE));
+#endif
+ }
+ else {
+ rb_bug("terminated fiber resumed");
+ }
+ }
+ terminated_machine_stack.ptr = NULL;
+ terminated_machine_stack.size = 0;
}
#endif /* not _WIN32 */
fib = th->ec->fiber_ptr;
@@ -1676,19 +1748,19 @@ fiber_store(rb_fiber_t *next_fib, rb_thread_t *th)
#else /* FIBER_USE_NATIVE */
if (ruby_setjmp(fib->cont.jmpbuf)) {
- /* restored */
- fib = th->ec->fiber_ptr;
- if (fib->cont.argc == -1) rb_exc_raise(fib->cont.value);
- if (next_fib->cont.value == Qundef) {
- cont_restore_0(&next_fib->cont, &next_fib->cont.value);
- VM_UNREACHABLE(fiber_store);
- }
- return fib->cont.value;
+ /* restored */
+ fib = th->ec->fiber_ptr;
+ if (fib->cont.argc == -1) rb_exc_raise(fib->cont.value);
+ if (next_fib->cont.value == Qundef) {
+ cont_restore_0(&next_fib->cont, &next_fib->cont.value);
+ VM_UNREACHABLE(fiber_store);
+ }
+ return fib->cont.value;
}
else {
- VALUE undef = Qundef;
- cont_restore_0(&next_fib->cont, &undef);
- VM_UNREACHABLE(fiber_store);
+ VALUE undef = Qundef;
+ cont_restore_0(&next_fib->cont, &undef);
+ VM_UNREACHABLE(fiber_store);
}
#endif /* FIBER_USE_NATIVE */
}
@@ -1777,7 +1849,7 @@ rb_fiber_close(rb_fiber_t *fib)
else {
ruby_xfree(vm_stack);
}
- ec_set_vm_stack(ec, NULL, 0);
+ rb_ec_set_vm_stack(ec, NULL, 0);
#if !FIBER_USE_NATIVE
/* should not mark machine stack any more */
@@ -1794,9 +1866,13 @@ rb_fiber_terminate(rb_fiber_t *fib, int need_interrupt)
VM_ASSERT(FIBER_RESUMED_P(fib));
rb_fiber_close(fib);
-#if FIBER_USE_NATIVE && !defined(_WIN32)
+#if FIBER_USE_NATIVE
+#if defined(FIBER_USE_COROUTINE)
+ coroutine_destroy(&fib->context);
+#elif !defined(_WIN32)
fib->context.uc_stack.ss_sp = NULL;
#endif
+
#ifdef MAX_MACHINE_STACK_CACHE
/* Ruby must not switch to other thread until storing terminated_machine_stack */
terminated_machine_stack.ptr = fib->ss_sp;
@@ -1804,6 +1880,7 @@ rb_fiber_terminate(rb_fiber_t *fib, int need_interrupt)
fib->ss_sp = NULL;
fib->cont.machine.stack = NULL;
fib->cont.machine.stack_size = 0;
+#endif
#endif
ret_fib = return_fiber();
@@ -1833,10 +1910,8 @@ rb_fiber_yield(int argc, const VALUE *argv)
}
void
-rb_fiber_reset_root_local_storage(VALUE thval)
+rb_fiber_reset_root_local_storage(rb_thread_t *th)
{
- rb_thread_t *th = rb_thread_ptr(thval);
-
if (th->root_fiber && th->root_fiber != th->ec->fiber_ptr) {
th->ec->local_storage = th->root_fiber->cont.saved_ec.local_storage;
}
diff --git a/coroutine/amd64/Context.S b/coroutine/amd64/Context.S
new file mode 100644
index 00000000000000..6193993e0374a1
--- /dev/null
+++ b/coroutine/amd64/Context.S
@@ -0,0 +1,46 @@
+##
+## This file is part of the "Coroutine" project and released under the MIT License.
+##
+## Created by Samuel Williams on 10/5/2018.
+## Copyright, 2018, by Samuel Williams. All rights reserved.
+##
+
+.text
+
+# For older linkers
+.globl _coroutine_transfer
+_coroutine_transfer:
+
+.globl coroutine_transfer
+coroutine_transfer:
+ # Save caller state
+ pushq %rbp
+ pushq %rbx
+ pushq %r12
+ pushq %r13
+ pushq %r14
+ pushq %r15
+
+ # Save caller stack pointer
+ movq %rsp, (%rdi)
+
+ # Restore callee stack pointer
+ movq (%rsi), %rsp
+
+ # Restore callee stack
+ popq %r15
+ popq %r14
+ popq %r13
+ popq %r12
+ popq %rbx
+ popq %rbp
+
+ # Put the first argument into the return value
+ movq %rdi, %rax
+
+ # We pop the return address and jump to it
+ ret
+
+#if defined(__linux__) && defined(__ELF__)
+.section .note.GNU-stack,"",%progbits
+#endif
diff --git a/coroutine/amd64/Context.h b/coroutine/amd64/Context.h
new file mode 100644
index 00000000000000..1801c1e2c5d63e
--- /dev/null
+++ b/coroutine/amd64/Context.h
@@ -0,0 +1,59 @@
+/*
+ * This file is part of the "Coroutine" project and released under the MIT License.
+ *
+ * Created by Samuel Williams on 10/5/2018.
+ * Copyright, 2018, by Samuel Williams. All rights reserved.
+*/
+
+#pragma once
+
+#include
+#include
+
+#if __cplusplus
+extern "C" {
+#endif
+
+#define COROUTINE __attribute__((noreturn)) void
+
+const size_t COROUTINE_REGISTERS = 6;
+
+typedef struct
+{
+ void **stack_pointer;
+} coroutine_context;
+
+typedef COROUTINE(* coroutine_start)(coroutine_context *from, coroutine_context *self);
+
+static inline void coroutine_initialize(
+ coroutine_context *context,
+ coroutine_start start,
+ void *stack_pointer,
+ size_t stack_size
+) {
+ /* Force 16-byte alignment */
+ context->stack_pointer = (void**)((uintptr_t)stack_pointer & ~0xF);
+
+ if (!start) {
+ assert(!context->stack_pointer);
+ /* We are main coroutine for this thread */
+ return;
+ }
+
+ *--context->stack_pointer = NULL;
+ *--context->stack_pointer = (void*)start;
+
+ context->stack_pointer -= COROUTINE_REGISTERS;
+ memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
+}
+
+coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target);
+
+static inline void coroutine_destroy(coroutine_context * context)
+{
+ context->stack_pointer = NULL;
+}
+
+#if __cplusplus
+}
+#endif
diff --git a/coroutine/arm32/Context.S b/coroutine/arm32/Context.S
new file mode 100644
index 00000000000000..c2b93d0a3464c1
--- /dev/null
+++ b/coroutine/arm32/Context.S
@@ -0,0 +1,14 @@
+##
+## This file is part of the "Coroutine" project and released under the MIT License.
+##
+## Created by Samuel Williams on 10/5/2018.
+## Copyright, 2018, by Samuel Williams. All rights reserved.
+##
+
+.text
+
+.globl coroutine_transfer
+coroutine_transfer:
+ stmia r1!, {r4-r11,sp,lr}
+ ldmia r0!, {r4-r11,sp,pc}
+ bx lr
diff --git a/coroutine/arm32/Context.h b/coroutine/arm32/Context.h
new file mode 100644
index 00000000000000..60732df7a0f203
--- /dev/null
+++ b/coroutine/arm32/Context.h
@@ -0,0 +1,56 @@
+/*
+ * This file is part of the "Coroutine" project and released under the MIT License.
+ *
+ * Created by Samuel Williams on 10/5/2018.
+ * Copyright, 2018, by Samuel Williams. All rights reserved.
+*/
+
+#pragma once
+
+#include
+#include
+
+#if __cplusplus
+extern "C" {
+#endif
+
+#define COROUTINE __attribute__((noreturn)) void
+
+const size_t COROUTINE_REGISTERS = 9;
+
+typedef struct
+{
+ void **stack_pointer;
+} coroutine_context;
+
+typedef COROUTINE(* coroutine_start)(coroutine_context *from, coroutine_context *self);
+
+static inline void coroutine_initialize(
+ coroutine_context *context,
+ coroutine_start start,
+ void *stack_pointer,
+ size_t stack_size
+) {
+ context->stack_pointer = (void**)stack_pointer;
+
+ if (!start) {
+ assert(!context->stack_pointer);
+ /* We are main coroutine for this thread */
+ return;
+ }
+
+ *--context->stack_pointer = (void*)start;
+
+ context->stack_pointer -= COROUTINE_REGISTERS;
+ memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
+}
+
+coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target);
+
+static inline void coroutine_destroy(coroutine_context * context)
+{
+}
+
+#if __cplusplus
+}
+#endif
diff --git a/coroutine/arm64/Context.S b/coroutine/arm64/Context.S
new file mode 100644
index 00000000000000..f6e5f0a6bc83e5
--- /dev/null
+++ b/coroutine/arm64/Context.S
@@ -0,0 +1,59 @@
+##
+## This file is part of the "Coroutine" project and released under the MIT License.
+##
+## Created by Samuel Williams on 10/5/2018.
+## Copyright, 2018, by Samuel Williams. All rights reserved.
+##
+
+.text
+.align 2
+
+.global coroutine_transfer
+coroutine_transfer:
+
+ # Make space on the stack for caller registers
+ sub sp, sp, 0xb0
+
+ # Save caller registers
+ stp d8, d9, [sp, 0x00]
+ stp d10, d11, [sp, 0x10]
+ stp d12, d13, [sp, 0x20]
+ stp d14, d15, [sp, 0x30]
+ stp x19, x20, [sp, 0x40]
+ stp x21, x22, [sp, 0x50]
+ stp x23, x24, [sp, 0x60]
+ stp x25, x26, [sp, 0x70]
+ stp x27, x28, [sp, 0x80]
+ stp x29, x30, [sp, 0x90]
+
+ # Save return address
+ str x30, [sp, 0xa0]
+
+ # Save stack pointer to x0 (first argument)
+ mov x2, sp
+ str x2, [x0, 0]
+
+ # Load stack pointer from x1 (second argument)
+ ldr x3, [x1, 0]
+ mov sp, x3
+
+ # Restore caller registers
+ ldp d8, d9, [sp, 0x00]
+ ldp d10, d11, [sp, 0x10]
+ ldp d12, d13, [sp, 0x20]
+ ldp d14, d15, [sp, 0x30]
+ ldp x19, x20, [sp, 0x40]
+ ldp x21, x22, [sp, 0x50]
+ ldp x23, x24, [sp, 0x60]
+ ldp x25, x26, [sp, 0x70]
+ ldp x27, x28, [sp, 0x80]
+ ldp x29, x30, [sp, 0x90]
+
+ # Load return address into x4
+ ldr x4, [sp, 0xa0]
+
+ # Pop stack frame
+ add sp, sp, 0xb0
+
+ # Jump to return address (in x4)
+ ret x4
diff --git a/coroutine/arm64/Context.h b/coroutine/arm64/Context.h
new file mode 100644
index 00000000000000..03b91fd93725b5
--- /dev/null
+++ b/coroutine/arm64/Context.h
@@ -0,0 +1,57 @@
+/*
+ * This file is part of the "Coroutine" project and released under the MIT License.
+ *
+ * Created by Samuel Williams on 10/5/2018.
+ * Copyright, 2018, by Samuel Williams. All rights reserved.
+*/
+
+#pragma once
+
+#include
+#include
+
+#if __cplusplus
+extern "C" {
+#endif
+
+#define COROUTINE __attribute__((noreturn)) void
+
+const size_t COROUTINE_REGISTERS = 0xb0 / 8;
+
+typedef struct
+{
+ void **stack_pointer;
+} coroutine_context;
+
+typedef COROUTINE(* coroutine_start)(coroutine_context *from, coroutine_context *self);
+
+static inline void coroutine_initialize(
+ coroutine_context *context,
+ coroutine_start start,
+ void *stack_pointer,
+ size_t stack_size
+) {
+ /* Force 16-byte alignment */
+ context->stack_pointer = (void**)((uintptr_t)stack_pointer & ~0xF);
+
+ if (!start) {
+ assert(!context->stack_pointer);
+ /* We are main coroutine for this thread */
+ return;
+ }
+
+ context->stack_pointer -= COROUTINE_REGISTERS;
+ memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
+
+ context->stack_pointer[0xa0 / 8] = (void*)start;
+}
+
+coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target);
+
+static inline void coroutine_destroy(coroutine_context * context)
+{
+}
+
+#if __cplusplus
+}
+#endif
diff --git a/coroutine/win32/Context.asm b/coroutine/win32/Context.asm
new file mode 100644
index 00000000000000..2647ea4bc43cf5
--- /dev/null
+++ b/coroutine/win32/Context.asm
@@ -0,0 +1,55 @@
+;;
+;; This file is part of the "Coroutine" project and released under the MIT License.
+;;
+;; Created by Samuel Williams on 10/5/2018.
+;; Copyright, 2018, by Samuel Williams. All rights reserved.
+;;
+
+.386
+.model flat
+
+.code
+
+assume fs:nothing
+
+; Using fastcall is a big win (and it's the same has how x64 works).
+; In coroutine transfer, the arguments are passed in ecx and edx. We don't need
+; to touch these in order to pass them to the destination coroutine.
+
+@coroutine_transfer@8 proc
+ ; Save the thread information block:
+ push fs:[0]
+ push fs:[4]
+ push fs:[8]
+
+ ; Save caller registers:
+ push ebp
+ push ebx
+ push edi
+ push esi
+
+ ; Save caller stack pointer:
+ mov dword ptr [ecx], esp
+
+ ; Restore callee stack pointer:
+ mov esp, dword ptr [edx]
+
+ ; Restore callee stack:
+ pop esi
+ pop edi
+ pop ebx
+ pop ebp
+
+ ; Restore the thread information block:
+ pop fs:[8]
+ pop fs:[4]
+ pop fs:[0]
+
+ ; Save the first argument as the return value:
+ mov eax, dword ptr ecx
+
+ ; Jump to the address on the stack:
+ ret
+@coroutine_transfer@8 endp
+
+end
diff --git a/coroutine/win32/Context.h b/coroutine/win32/Context.h
new file mode 100644
index 00000000000000..aa9f17ddab54a3
--- /dev/null
+++ b/coroutine/win32/Context.h
@@ -0,0 +1,62 @@
+/*
+ * This file is part of the "Coroutine" project and released under the MIT License.
+ *
+ * Created by Samuel Williams on 10/5/2018.
+ * Copyright, 2018, by Samuel Williams. All rights reserved.
+*/
+
+#pragma once
+
+#include
+#include
+
+#if __cplusplus
+extern "C" {
+#endif
+
+#define COROUTINE __declspec(noreturn) void __fastcall
+
+/* This doesn't include thread information block */
+const size_t COROUTINE_REGISTERS = 4;
+
+typedef struct
+{
+ void **stack_pointer;
+} coroutine_context;
+
+typedef void(__fastcall * coroutine_start)(coroutine_context *from, coroutine_context *self);
+
+static inline void coroutine_initialize(
+ coroutine_context *context,
+ coroutine_start start,
+ void *stack_pointer,
+ size_t stack_size
+) {
+ context->stack_pointer = (void**)stack_pointer;
+
+ if (!start) {
+ assert(!context->stack_pointer);
+ /* We are main coroutine for this thread */
+ return;
+ }
+
+ *--context->stack_pointer = (void*)start;
+
+ /* Windows Thread Information Block */
+ *--context->stack_pointer = 0; /* fs:[0] */
+ *--context->stack_pointer = (void*)stack_pointer; /* fs:[4] */
+ *--context->stack_pointer = (void*)((char *)stack_pointer - stack_size); /* fs:[8] */
+
+ context->stack_pointer -= COROUTINE_REGISTERS;
+ memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
+}
+
+coroutine_context * __fastcall coroutine_transfer(coroutine_context * current, coroutine_context * target);
+
+static inline void coroutine_destroy(coroutine_context * context)
+{
+}
+
+#if __cplusplus
+}
+#endif
diff --git a/coroutine/win64/Context.S b/coroutine/win64/Context.S
new file mode 100644
index 00000000000000..4b16e0ce8c0316
--- /dev/null
+++ b/coroutine/win64/Context.S
@@ -0,0 +1,77 @@
+##
+## This file is part of the "Coroutine" project and released under the MIT License.
+##
+## Created by Samuel Williams on 4/11/2018.
+## Copyright, 2018, by Samuel Williams. All rights reserved.
+##
+
+.text
+
+.globl coroutine_transfer
+coroutine_transfer:
+ # Save the thread information block:
+ pushq %gs:8
+ pushq %gs:16
+
+ # Save caller registers:
+ pushq %rbp
+ pushq %rbx
+ pushq %rdi
+ pushq %rsi
+ pushq %r12
+ pushq %r13
+ pushq %r14
+ pushq %r15
+
+ movaps %xmm15, -168(%rsp)
+ movaps %xmm14, -152(%rsp)
+ movaps %xmm13, -136(%rsp)
+ movaps %xmm12, -120(%rsp)
+ movaps %xmm11, -104(%rsp)
+ movaps %xmm10, -88(%rsp)
+ movaps %xmm9, -72(%rsp)
+ movaps %xmm8, -56(%rsp)
+ movaps %xmm7, -40(%rsp)
+ movaps %xmm6, -24(%rsp)
+
+ # Save caller stack pointer:
+ mov %rsp, (%rcx)
+
+ # Restore callee stack pointer:
+ mov (%rdx), %rsp
+
+ movaps -24(%rsp), %xmm6
+ movaps -40(%rsp), %xmm7
+ movaps -56(%rsp), %xmm8
+ movaps -72(%rsp), %xmm9
+ movaps -88(%rsp), %xmm10
+ movaps -104(%rsp), %xmm11
+ movaps -120(%rsp), %xmm12
+ movaps -136(%rsp), %xmm13
+ movaps -152(%rsp), %xmm14
+ movaps -168(%rsp), %xmm15
+
+ # Restore callee stack:
+ popq %r15
+ popq %r14
+ popq %r13
+ popq %r12
+ popq %rsi
+ popq %rdi
+ popq %rbx
+ popq %rbp
+
+ # Restore the thread information block:
+ popq %gs:16
+ popq %gs:8
+
+ # Put the first argument into the return value:
+ mov %rcx, %rax
+
+ # We pop the return address and jump to it:
+ ret
+
+.globl coroutine_trampoline
+coroutine_trampoline:
+ # Do not remove this. This forces 16-byte alignment when entering the coroutine.
+ ret
diff --git a/coroutine/win64/Context.asm b/coroutine/win64/Context.asm
new file mode 100644
index 00000000000000..59673ffa3eb324
--- /dev/null
+++ b/coroutine/win64/Context.asm
@@ -0,0 +1,79 @@
+;;
+;; This file is part of the "Coroutine" project and released under the MIT License.
+;;
+;; Created by Samuel Williams on 10/5/2018.
+;; Copyright, 2018, by Samuel Williams. All rights reserved.
+;;
+
+.code
+
+coroutine_transfer proc
+ ; Save the thread information block:
+ push qword ptr gs:[8]
+ push qword ptr gs:[16]
+
+ ; Save caller registers:
+ push rbp
+ push rbx
+ push rdi
+ push rsi
+ push r12
+ push r13
+ push r14
+ push r15
+
+ movaps [rsp - 24], xmm6
+ movaps [rsp - 40], xmm7
+ movaps [rsp - 56], xmm8
+ movaps [rsp - 72], xmm9
+ movaps [rsp - 88], xmm10
+ movaps [rsp - 104], xmm11
+ movaps [rsp - 120], xmm12
+ movaps [rsp - 136], xmm13
+ movaps [rsp - 152], xmm14
+ movaps [rsp - 168], xmm15
+
+ ; Save caller stack pointer:
+ mov [rcx], rsp
+
+ ; Restore callee stack pointer:
+ mov rsp, [rdx]
+
+ movaps xmm15, [rsp - 168]
+ movaps xmm14, [rsp - 152]
+ movaps xmm13, [rsp - 136]
+ movaps xmm12, [rsp - 120]
+ movaps xmm11, [rsp - 104]
+ movaps xmm10, [rsp - 88]
+ movaps xmm9, [rsp - 72]
+ movaps xmm8, [rsp - 56]
+ movaps xmm7, [rsp - 40]
+ movaps xmm6, [rsp - 24]
+
+ ; Restore callee stack:
+ pop r15
+ pop r14
+ pop r13
+ pop r12
+ pop rsi
+ pop rdi
+ pop rbx
+ pop rbp
+
+ ; Restore the thread information block:
+ pop qword ptr gs:[16]
+ pop qword ptr gs:[8]
+
+ ; Put the first argument into the return value:
+ mov rax, rcx
+
+ ; We pop the return address and jump to it:
+ ret
+coroutine_transfer endp
+
+coroutine_trampoline proc
+ ; Do not remove this. This forces 16-byte alignment when entering the coroutine.
+ ret
+coroutine_trampoline endp
+
+end
diff --git a/coroutine/win64/Context.h b/coroutine/win64/Context.h
new file mode 100644
index 00000000000000..16a8f583ababf5
--- /dev/null
+++ b/coroutine/win64/Context.h
@@ -0,0 +1,72 @@
+/*
+ * This file is part of the "Coroutine" project and released under the MIT License.
+ *
+ * Created by Samuel Williams on 10/5/2018.
+ * Copyright, 2018, by Samuel Williams. All rights reserved.
+*/
+
+#pragma once
+
+#include
+#include
+
+#if __cplusplus
+extern "C" {
+#endif
+
+#define COROUTINE __declspec(noreturn) void
+
+const size_t COROUTINE_REGISTERS = 8;
+const size_t COROUTINE_XMM_REGISTERS = 1+10*2;
+
+typedef struct
+{
+ void **stack_pointer;
+} coroutine_context;
+
+typedef void(* coroutine_start)(coroutine_context *from, coroutine_context *self);
+
+void coroutine_trampoline();
+
+static inline void coroutine_initialize(
+ coroutine_context *context,
+ coroutine_start start,
+ void *stack_pointer,
+ size_t stack_size
+) {
+ /* Force 16-byte alignment */
+ context->stack_pointer = (void**)((uintptr_t)stack_pointer & ~0xF);
+
+ if (!start) {
+ assert(!context->stack_pointer);
+ /* We are main coroutine for this thread */
+ return;
+ }
+
+ /* Win64 ABI requires space for arguments */
+ context->stack_pointer -= 4;
+
+ /* Return address */
+ *--context->stack_pointer = 0;
+ *--context->stack_pointer = (void*)start;
+ *--context->stack_pointer = (void*)coroutine_trampoline;
+
+ /* Windows Thread Information Block */
+ /* *--context->stack_pointer = 0; */ /* gs:[0x00] is not used */
+ *--context->stack_pointer = (void*)stack_pointer; /* gs:[0x08] */
+ *--context->stack_pointer = (void*)((char *)stack_pointer - stack_size); /* gs:[0x10] */
+
+ context->stack_pointer -= COROUTINE_REGISTERS;
+ memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
+ memset(context->stack_pointer - COROUTINE_XMM_REGISTERS, 0, sizeof(void*) * COROUTINE_XMM_REGISTERS);
+}
+
+coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target);
+
+static inline void coroutine_destroy(coroutine_context * context)
+{
+}
+
+#if __cplusplus
+}
+#endif
diff --git a/coroutine/x86/Context.S b/coroutine/x86/Context.S
new file mode 100644
index 00000000000000..5fc0cccc2479c9
--- /dev/null
+++ b/coroutine/x86/Context.S
@@ -0,0 +1,39 @@
+##
+## This file is part of the "Coroutine" project and released under the MIT License.
+##
+## Created by Samuel Williams on 3/11/2018.
+## Copyright, 2018, by Samuel Williams. All rights reserved.
+##
+
+.text
+
+.globl coroutine_transfer
+coroutine_transfer:
+
+# For older linkers
+.globl _coroutine_transfer
+_coroutine_transfer:
+
+ # Save caller registers
+ pushl %ebp
+ pushl %ebx
+ pushl %edi
+ pushl %esi
+
+ # Save caller stack pointer
+ movl %esp, (%ecx)
+
+ # Restore callee stack pointer
+ movl (%edx), %esp
+
+ # Restore callee stack
+ popl %esi
+ popl %edi
+ popl %ebx
+ popl %ebp
+
+ # Save the first argument as the return value
+ movl %ecx, %eax
+
+ # Jump to the address on the stack
+ ret
diff --git a/coroutine/x86/Context.h b/coroutine/x86/Context.h
new file mode 100644
index 00000000000000..b077227a1dcddd
--- /dev/null
+++ b/coroutine/x86/Context.h
@@ -0,0 +1,59 @@
+/*
+ * This file is part of the "Coroutine" project and released under the MIT License.
+ *
+ * Created by Samuel Williams on 3/11/2018.
+ * Copyright, 2018, by Samuel Williams. All rights reserved.
+*/
+
+#pragma once
+
+#include
+#include
+
+#if __cplusplus
+extern "C" {
+#endif
+
+#define COROUTINE __attribute__((noreturn, fastcall)) void
+
+const size_t COROUTINE_REGISTERS = 4;
+
+typedef struct
+{
+ void **stack_pointer;
+} coroutine_context;
+
+typedef COROUTINE(* coroutine_start)(coroutine_context *from, coroutine_context *self) __attribute__((fastcall));
+
+static inline void coroutine_initialize(
+ coroutine_context *context,
+ coroutine_start start,
+ void *stack_pointer,
+ size_t stack_size
+) {
+ /* Force 16-byte alignment */
+ context->stack_pointer = (void**)((uintptr_t)stack_pointer & ~0xF);
+
+ if (!start) {
+ assert(!context->stack_pointer);
+ /* We are main coroutine for this thread */
+ return;
+ }
+
+ *--context->stack_pointer = NULL;
+ *--context->stack_pointer = (void*)start;
+
+ context->stack_pointer -= COROUTINE_REGISTERS;
+ memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
+}
+
+coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target) __attribute__((fastcall));
+
+static inline void coroutine_destroy(coroutine_context * context)
+{
+ context->stack_pointer = NULL;
+}
+
+#if __cplusplus
+}
+#endif
diff --git a/debug_counter.h b/debug_counter.h
index 3fcb562f6552cd..b390563a908c8b 100644
--- a/debug_counter.h
+++ b/debug_counter.h
@@ -141,6 +141,7 @@ RB_DEBUG_COUNTER(gc_major_oldmalloc)
* * [attr]
* * _ptr: R?? is not embed.
* * _embed: R?? is embed.
+ * * _transient: R?? uses transient heap.
* * type specific attr.
* * str_shared: str is shared.
* * str_nofree: nofree
@@ -162,8 +163,9 @@ RB_DEBUG_COUNTER(obj_free)
RB_DEBUG_COUNTER(obj_promote)
RB_DEBUG_COUNTER(obj_wb_unprotect)
-RB_DEBUG_COUNTER(obj_obj_ptr)
RB_DEBUG_COUNTER(obj_obj_embed)
+RB_DEBUG_COUNTER(obj_obj_transient)
+RB_DEBUG_COUNTER(obj_obj_ptr)
RB_DEBUG_COUNTER(obj_str_ptr)
RB_DEBUG_COUNTER(obj_str_embed)
@@ -171,16 +173,23 @@ RB_DEBUG_COUNTER(obj_str_shared)
RB_DEBUG_COUNTER(obj_str_nofree)
RB_DEBUG_COUNTER(obj_str_fstr)
-RB_DEBUG_COUNTER(obj_ary_ptr)
RB_DEBUG_COUNTER(obj_ary_embed)
+RB_DEBUG_COUNTER(obj_ary_transient)
+RB_DEBUG_COUNTER(obj_ary_ptr)
RB_DEBUG_COUNTER(obj_hash_empty)
RB_DEBUG_COUNTER(obj_hash_under4)
RB_DEBUG_COUNTER(obj_hash_ge4)
RB_DEBUG_COUNTER(obj_hash_ge8)
+RB_DEBUG_COUNTER(obj_hash_array)
+RB_DEBUG_COUNTER(obj_hash_st)
+RB_DEBUG_COUNTER(obj_hash_transient)
+
+RB_DEBUG_COUNTER(obj_hash_force_convert)
-RB_DEBUG_COUNTER(obj_struct_ptr)
RB_DEBUG_COUNTER(obj_struct_embed)
+RB_DEBUG_COUNTER(obj_struct_transient)
+RB_DEBUG_COUNTER(obj_struct_ptr)
RB_DEBUG_COUNTER(obj_regexp_ptr)
@@ -219,6 +228,11 @@ RB_DEBUG_COUNTER(heap_xmalloc)
RB_DEBUG_COUNTER(heap_xrealloc)
RB_DEBUG_COUNTER(heap_xfree)
+/* transient_heap */
+RB_DEBUG_COUNTER(theap_alloc)
+RB_DEBUG_COUNTER(theap_alloc_fail)
+RB_DEBUG_COUNTER(theap_evacuate)
+
/* load (not implemented yet) */
/*
RB_DEBUG_COUNTER(load_files)
diff --git a/defs/gmake.mk b/defs/gmake.mk
index 8af2106d868250..49934c5f014133 100644
--- a/defs/gmake.mk
+++ b/defs/gmake.mk
@@ -64,7 +64,7 @@ endif
ORDERED_TEST_TARGETS := $(filter $(TEST_TARGETS), \
btest-ruby test-knownbug test-basic \
test-testframework test-ruby test-almost test-all \
- test-spec \
+ test-spec test-bundler-prepare test-bundler \
)
prev_test := $(if $(filter test-spec,$(ORDERED_TEST_TARGETS)),test-spec-precheck)
$(foreach test,$(ORDERED_TEST_TARGETS), \
@@ -109,7 +109,7 @@ endif
STUBPROGRAM = rubystub$(EXEEXT)
IGNOREDPATTERNS = %~ .% %.orig %.rej \#%\#
SCRIPTBINDIR := $(if $(EXEEXT),,exec/)
-SCRIPTPROGRAMS = $(addprefix $(SCRIPTBINDIR),$(addsuffix $(EXEEXT),$(filter-out $(IGNOREDPATTERNS),$(notdir $(wildcard $(srcdir)/bin/*)))))
+SCRIPTPROGRAMS = $(addprefix $(SCRIPTBINDIR),$(addsuffix $(EXEEXT),$(filter-out $(IGNOREDPATTERNS),$(notdir $(wildcard $(srcdir)/libexec/*)))))
stub: $(STUBPROGRAM)
scriptbin: $(SCRIPTPROGRAMS)
@@ -184,3 +184,22 @@ $(MJIT_MIN_HEADER): $(mjit_min_headers) $(PREP)
$(Q) $(MAKE_LINK) $@ $(MJIT_HEADER_INSTALL_DIR)/$(@F)
endif
+
+# GNU make treat the target as unmodified when its dependents get
+# updated but it is not updated, while others may not.
+$(srcdir)/revision.h: $(REVISION_H)
+
+# Query on the generated rdoc
+#
+# $ make rdoc:Integer#+
+rdoc\:%: PHONY
+ $(Q)$(RUNRUBY) $(srcdir)/libexec/ri --no-standard-docs --doc-dir=$(RDOCOUT) $(patsubst rdoc:%,%,$@)
+
+test_%.rb test/%: programs PHONY
+ +$(Q)$(exec) $(RUNRUBY) "$(srcdir)/test/runner.rb" --ruby="$(RUNRUBY)" $(TEST_EXCLUDES) $(TESTOPTS) $(patsubst test/%,%,$@)
+
+clean-srcs-ext::
+ $(Q)$(RM) $(patsubst $(srcdir)/%,%,$(EXT_SRCS))
+
+clean-srcs-extra::
+ $(Q)$(RM) $(patsubst $(srcdir)/%,%,$(EXTRA_SRCS))
diff --git a/defs/known_errors.def b/defs/known_errors.def
index b9c490d3a2aab1..e9694cfbda684b 100644
--- a/defs/known_errors.def
+++ b/defs/known_errors.def
@@ -1,148 +1,157 @@
-EPERM
-ENOENT
-ESRCH
-EINTR
-EIO
-ENXIO
E2BIG
-ENOEXEC
-EBADF
-ECHILD
-EAGAIN
-ENOMEM
EACCES
-EFAULT
-ENOTBLK
-EBUSY
-EEXIST
-EXDEV
-ENODEV
-ENOTDIR
-EISDIR
-EINVAL
-ENFILE
-EMFILE
-ENOTTY
-ETXTBSY
-EFBIG
-ENOSPC
-ESPIPE
-EROFS
-EMLINK
-EPIPE
-EDOM
-ERANGE
-EDEADLK
-ENAMETOOLONG
-ENOLCK
-ENOSYS
-ENOTEMPTY
-ELOOP
-EWOULDBLOCK
-ENOMSG
-EIDRM
-ECHRNG
-EL2NSYNC
-EL3HLT
-EL3RST
-ELNRNG
-EUNATCH
-ENOCSI
-EL2HLT
+EADDRINUSE
+EADDRNOTAVAIL
+EADV
+EAFNOSUPPORT
+EAGAIN
+EALREADY
+EAUTH
+EBADARCH
EBADE
+EBADEXEC
+EBADF
+EBADFD
+EBADMACHO
+EBADMSG
EBADR
-EXFULL
-ENOANO
+EBADRPC
EBADRQC
EBADSLT
-EDEADLOCK
EBFONT
-ENOSTR
-ENODATA
-ETIME
-ENOSR
-ENONET
-ENOPKG
-EREMOTE
-ENOLINK
-EADV
-ESRMNT
+EBUSY
+ECANCELED
+ECAPMODE
+ECHILD
+ECHRNG
ECOMM
-EPROTO
-EMULTIHOP
-EDOTDOT
-EBADMSG
-EOVERFLOW
-ENOTUNIQ
-EBADFD
-EREMCHG
-ELIBACC
-ELIBBAD
-ELIBSCN
-ELIBMAX
-ELIBEXEC
-EILSEQ
-ERESTART
-ESTRPIPE
-EUSERS
-ENOTSOCK
-EDESTADDRREQ
-EMSGSIZE
-EPROTOTYPE
-ENOPROTOOPT
-EPROTONOSUPPORT
-ESOCKTNOSUPPORT
-EOPNOTSUPP
-EPFNOSUPPORT
-EAFNOSUPPORT
-EADDRINUSE
-EADDRNOTAVAIL
-ENETDOWN
-ENETUNREACH
-ENETRESET
ECONNABORTED
-ECONNRESET
-ENOBUFS
-EISCONN
-ENOTCONN
-ESHUTDOWN
-ETOOMANYREFS
-ETIMEDOUT
ECONNREFUSED
+ECONNRESET
+EDEADLK
+EDEADLOCK
+EDESTADDRREQ
+EDEVERR
+EDOM
+EDOOFUS
+EDOTDOT
+EDQUOT
+EEXIST
+EFAULT
+EFBIG
+EFTYPE
EHOSTDOWN
EHOSTUNREACH
-EALREADY
+EHWPOISON
+EIDRM
+EILSEQ
EINPROGRESS
-ESTALE
-EUCLEAN
-ENOTNAM
-ENAVAIL
+EINTR
+EINVAL
+EIO
+EIPSEC
+EISCONN
+EISDIR
EISNAM
-EREMOTEIO
-EDQUOT
-ECANCELED
EKEYEXPIRED
EKEYREJECTED
EKEYREVOKED
+EL2HLT
+EL2NSYNC
+EL3HLT
+EL3RST
+ELAST
+ELIBACC
+ELIBBAD
+ELIBEXEC
+ELIBMAX
+ELIBSCN
+ELNRNG
+ELOOP
EMEDIUMTYPE
+EMFILE
+EMLINK
+EMSGSIZE
+EMULTIHOP
+ENAMETOOLONG
+ENAVAIL
+ENEEDAUTH
+ENETDOWN
+ENETRESET
+ENETUNREACH
+ENFILE
+ENOANO
+ENOATTR
+ENOBUFS
+ENOCSI
+ENODATA
+ENODEV
+ENOENT
+ENOEXEC
ENOKEY
+ENOLCK
+ENOLINK
ENOMEDIUM
+ENOMEM
+ENOMSG
+ENONET
+ENOPKG
+ENOPOLICY
+ENOPROTOOPT
+ENOSPC
+ENOSR
+ENOSTR
+ENOSYS
+ENOTBLK
+ENOTCAPABLE
+ENOTCONN
+ENOTDIR
+ENOTEMPTY
+ENOTNAM
ENOTRECOVERABLE
-EOWNERDEAD
-ERFKILL
-EAUTH
-EBADRPC
-EDOOFUS
-EFTYPE
-ENEEDAUTH
-ENOATTR
+ENOTSOCK
ENOTSUP
+ENOTTY
+ENOTUNIQ
+ENXIO
+EOPNOTSUPP
+EOVERFLOW
+EOWNERDEAD
+EPERM
+EPFNOSUPPORT
+EPIPE
EPROCLIM
EPROCUNAVAIL
EPROGMISMATCH
EPROGUNAVAIL
+EPROTO
+EPROTONOSUPPORT
+EPROTOTYPE
+EPWROFF
+EQFULL
+ERANGE
+EREMCHG
+EREMOTE
+EREMOTEIO
+ERESTART
+ERFKILL
+EROFS
ERPCMISMATCH
-EIPSEC
-EHWPOISON
-ECAPMODE
-ENOTCAPABLE
+ESHLIBVERS
+ESHUTDOWN
+ESOCKTNOSUPPORT
+ESPIPE
+ESRCH
+ESRMNT
+ESTALE
+ESTRPIPE
+ETIME
+ETIMEDOUT
+ETOOMANYREFS
+ETXTBSY
+EUCLEAN
+EUNATCH
+EUSERS
+EWOULDBLOCK
+EXDEV
+EXFULL
diff --git a/doc/maintainers.rdoc b/doc/maintainers.rdoc
index 89f1d306178752..e7baf933c68686 100644
--- a/doc/maintainers.rdoc
+++ b/doc/maintainers.rdoc
@@ -178,6 +178,9 @@ Zachary Scott (zzak)
=== Libraries
+[lib/bundler.rb, lib/bundler/*]
+ Hiroshi SHIBATA (hsbt)
+ https://github.com/bundler/bundler
[lib/cmath.rb]
_unmaintained_
https://github.com/ruby/cmath
diff --git a/doc/syntax/literals.rdoc b/doc/syntax/literals.rdoc
index b8ed7f7c54b0e7..62b80db04bd09e 100644
--- a/doc/syntax/literals.rdoc
+++ b/doc/syntax/literals.rdoc
@@ -71,6 +71,33 @@ Examples:
All these numbers have the same decimal value, 170. Like integers and floats
you may use an underscore for readability.
+=== Rational numbers
+
+Numbers suffixed by +r+ are Rational numbers.
+
+ 12r #=> (12/1)
+ 12.3r #=> (123/10)
+
+Rational numbers are exact, whereas Float numbers are inexact.
+
+ 0.1r + 0.2r #=> (3/10)
+ 0.1 + 0.2 #=> 0.30000000000000004
+
+=== Complex numbers
+
+Numbers suffixed by +i+ are Complex (or imaginary) numbers.
+
+ 1i #=> (0+1i)
+ 1i * 1i #=> (-1+0i)
+
+Also Rational numbers may be imaginary numbers.
+
+ 12.3ri #=> (0+(123/10)*i)
+
++i+ must be placed after +r+, the opposite is not allowed.
+
+ 12.3ir #=> syntax error
+
== Strings
The most common way of writing strings is using ":
diff --git a/enc/unicode.c b/enc/unicode.c
index 0d692520e85d54..b08b1450ba9935 100644
--- a/enc/unicode.c
+++ b/enc/unicode.c
@@ -683,8 +683,10 @@ onigenc_unicode_case_map(OnigCaseFoldType* flagP,
MODIFIED;
if (flags & ONIGENC_CASE_FOLD_TURKISH_AZERI && code == 'i')
code = I_WITH_DOT_ABOVE;
- else
- code += 'A' - 'a';
+ else {
+ code -= 'a';
+ code += 'A';
+ }
}
}
else if (code >= 'A' && code <= 'Z') {
@@ -770,10 +772,15 @@ onigenc_unicode_case_map(OnigCaseFoldType* flagP,
}
}
}
- else if ((folded = onigenc_unicode_unfold1_lookup(code)) != 0 /* data about character found in CaseUnfold_11_Table */
- && flags & OnigCaseFoldFlags(folded->n)) { /* needs and data availability match */
- MODIFIED;
- code = folded->code[(flags & OnigCaseFoldFlags(folded->n) & ONIGENC_CASE_TITLECASE) ? 1 : 0];
+ else if ((folded = onigenc_unicode_unfold1_lookup(code)) != 0) { /* data about character found in CaseUnfold_11_Table */
+ if ((flags & ONIGENC_CASE_TITLECASE) /* Titlecase needed, */
+ && (OnigCaseFoldFlags(folded->n) & ONIGENC_CASE_IS_TITLECASE)) { /* but already Titlecase */
+ /* already Titlecase, no changes needed */
+ }
+ else if (flags & OnigCaseFoldFlags(folded->n)) { /* needs and data availability match */
+ MODIFIED;
+ code = folded->code[(flags & OnigCaseFoldFlags(folded->n) & ONIGENC_CASE_TITLECASE) ? 1 : 0];
+ }
}
}
to += ONIGENC_CODE_TO_MBC(enc, code, to);
@@ -786,8 +793,8 @@ onigenc_unicode_case_map(OnigCaseFoldType* flagP,
return (int )(to - to_start);
}
-/* for extended grapheme cluster */
-/* TODO: generate from Unicode data */
+/* These GAZ/E_Base/Emoji tables are for extended grapheme cluster */
+/* TODO: generate these 3 tables from Unicode data */
const OnigCodePoint
onigenc_unicode_GCB_ranges_GAZ[] = {
0,
diff --git a/enc/unicode/10.0.0/name2ctype.h b/enc/unicode/10.0.0/name2ctype.h
index 4a1422d17a974b..dc23d54ab8a712 100644
--- a/enc/unicode/10.0.0/name2ctype.h
+++ b/enc/unicode/10.0.0/name2ctype.h
@@ -34756,9 +34756,11 @@ struct uniname2ctype_struct {
};
#define uniname2ctype_offset(str) offsetof(struct uniname2ctype_pool_t, uniname2ctype_pool_##str)
-#if !1+0
-static const struct uniname2ctype_struct *uniname2ctype_p(const char *, unsigned int);
+static const struct uniname2ctype_struct *uniname2ctype_p(
+#if !(1+0) /* if ANSI, old style not to conflict with generated prototype */
+ const char *, unsigned int
#endif
+);
#ifndef USE_UNICODE_PROPERTIES
#define TOTAL_KEYWORDS 15
@@ -38379,3 +38381,12 @@ uniname2ctype(const UChar *name, unsigned int len)
#define ONIG_UNICODE_VERSION_MAJOR 10
#define ONIG_UNICODE_VERSION_MINOR 0
#define ONIG_UNICODE_VERSION_TEENY 0
+#if defined ONIG_UNICODE_EMOJI_VERSION_STRING && !( \
+ ONIG_UNICODE_EMOJI_VERSION_MAJOR == 5 && \
+ ONIG_UNICODE_EMOJI_VERSION_MINOR == 0 && \
+ 1)
+# error ONIG_UNICODE_EMOJI_VERSION_STRING mismatch
+#endif
+#define ONIG_UNICODE_EMOJI_VERSION_STRING "5.0"
+#define ONIG_UNICODE_EMOJI_VERSION_MAJOR 5
+#define ONIG_UNICODE_EMOJI_VERSION_MINOR 0
diff --git a/enc/unicode/case-folding.rb b/enc/unicode/case-folding.rb
index 829efefaf17f8b..362d6ebfd9bc23 100755
--- a/enc/unicode/case-folding.rb
+++ b/enc/unicode/case-folding.rb
@@ -264,7 +264,6 @@ def flags(from, type, to)
from = Array(from).map {|i| "%04X" % i}.join(" ")
to = Array(to).map {|i| "%04X" % i}.join(" ")
item = map(from)
- specials_index = nil
specials = []
case type
when 'CaseFold_11'
@@ -309,7 +308,7 @@ def flags(from, type, to)
end
unless item.upper == item.title
if item.code == item.title
- raise "Unpredicted case 1 in enc/unicode/case_folding.rb. Please contact https://bugs.ruby-lang.org/."
+ flags += '|IT' # was unpredicted case 1
elsif item.title==to[1]
flags += '|ST'
else
@@ -410,8 +409,8 @@ def debug!() end
s = f.string
end
if dest
- open(dest, "wb") do |f|
- f.print(s)
+ open(dest, "wb") do |file|
+ file.print(s)
end
else
STDOUT.print(s)
diff --git a/enum.c b/enum.c
index 9485b7eb604a54..3be6941c3a8880 100644
--- a/enum.c
+++ b/enum.c
@@ -14,6 +14,7 @@
#include "ruby/util.h"
#include "id.h"
#include "symbol.h"
+#include "transient_heap.h"
#include
@@ -1171,9 +1172,10 @@ enum_sort_by(VALUE obj)
rb_ary_concat(ary, buf);
}
if (RARRAY_LEN(ary) > 2) {
- RARRAY_PTR_USE(ary, ptr,
- ruby_qsort(ptr, RARRAY_LEN(ary)/2, 2*sizeof(VALUE),
- sort_by_cmp, (void *)ary));
+ rb_ary_transient_heap_evacuate(ary, TRUE); /* should be malloc heap */
+ RARRAY_PTR_USE(ary, ptr,
+ ruby_qsort(ptr, RARRAY_LEN(ary)/2, 2*sizeof(VALUE),
+ sort_by_cmp, (void *)ary));
}
if (RBASIC(ary)->klass) {
rb_raise(rb_eRuntimeError, "sort_by reentered");
diff --git a/enumerator.c b/enumerator.c
index 466e0645604ded..2eb0c24a2bc100 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -12,6 +12,7 @@
************************************************/
+#include "ruby/ruby.h"
#include "internal.h"
#include "id.h"
@@ -161,6 +162,13 @@ struct proc_entry {
static VALUE generator_allocate(VALUE klass);
static VALUE generator_init(VALUE obj, VALUE proc);
+static VALUE rb_cEnumChain;
+
+struct enum_chain {
+ VALUE enums;
+ long pos;
+};
+
static VALUE rb_cArithSeq;
/*
@@ -2411,6 +2419,300 @@ stop_result(VALUE self)
return rb_attr_get(self, id_result);
}
+/*
+ * Document-class: Enumerator::Chain
+ *
+ * Enumerator::Chain is a subclass of Enumerator, which represents a
+ * chain of enumerables that works as a single enumerator.
+ *
+ * This type of objects can be created by Enumerable#chain and
+ * Enumerator#+.
+ */
+
+static void
+enum_chain_mark(void *p)
+{
+ struct enum_chain *ptr = p;
+ rb_gc_mark(ptr->enums);
+}
+
+#define enum_chain_free RUBY_TYPED_DEFAULT_FREE
+
+static size_t
+enum_chain_memsize(const void *p)
+{
+ return sizeof(struct enum_chain);
+}
+
+static const rb_data_type_t enum_chain_data_type = {
+ "chain",
+ {
+ enum_chain_mark,
+ enum_chain_free,
+ enum_chain_memsize,
+ },
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
+};
+
+static struct enum_chain *
+enum_chain_ptr(VALUE obj)
+{
+ struct enum_chain *ptr;
+
+ TypedData_Get_Struct(obj, struct enum_chain, &enum_chain_data_type, ptr);
+ if (!ptr || ptr->enums == Qundef) {
+ rb_raise(rb_eArgError, "uninitialized chain");
+ }
+ return ptr;
+}
+
+/* :nodoc: */
+static VALUE
+enum_chain_allocate(VALUE klass)
+{
+ struct enum_chain *ptr;
+ VALUE obj;
+
+ obj = TypedData_Make_Struct(klass, struct enum_chain, &enum_chain_data_type, ptr);
+ ptr->enums = Qundef;
+ ptr->pos = -1;
+
+ return obj;
+}
+
+/*
+ * call-seq:
+ * Enumerator::Chain.new(*enums) -> enum
+ *
+ * Generates a new enumerator object that iterates over the elements
+ * of given enumerable objects in sequence.
+ *
+ * e = Enumerator::Chain.new(1..3, [4, 5])
+ * e.to_a #=> [1, 2, 3, 4, 5]
+ * e.size #=> 5
+ */
+static VALUE
+enum_chain_initialize(VALUE obj, VALUE enums)
+{
+ struct enum_chain *ptr;
+
+ rb_check_frozen(obj);
+ TypedData_Get_Struct(obj, struct enum_chain, &enum_chain_data_type, ptr);
+
+ if (!ptr) rb_raise(rb_eArgError, "unallocated chain");
+
+ ptr->enums = rb_obj_freeze(enums);
+ ptr->pos = -1;
+
+ return obj;
+}
+
+/* :nodoc: */
+static VALUE
+enum_chain_init_copy(VALUE obj, VALUE orig)
+{
+ struct enum_chain *ptr0, *ptr1;
+
+ if (!OBJ_INIT_COPY(obj, orig)) return obj;
+ ptr0 = enum_chain_ptr(orig);
+
+ TypedData_Get_Struct(obj, struct enum_chain, &enum_chain_data_type, ptr1);
+
+ if (!ptr1) rb_raise(rb_eArgError, "unallocated chain");
+
+ ptr1->enums = ptr0->enums;
+ ptr1->pos = ptr0->pos;
+
+ return obj;
+}
+
+static VALUE
+enum_chain_total_size(VALUE enums)
+{
+ VALUE total = INT2FIX(0);
+
+ RARRAY_PTR_USE(enums, ptr, {
+ long i;
+
+ for (i = 0; i < RARRAY_LEN(enums); i++) {
+ VALUE size = enum_size(ptr[i]);
+
+ if (NIL_P(size) || (RB_TYPE_P(size, T_FLOAT) && isinf(NUM2DBL(size)))) {
+ return size;
+ }
+ if (!RB_INTEGER_TYPE_P(size)) {
+ return Qnil;
+ }
+
+ total = rb_funcall(total, '+', 1, size);
+ }
+ });
+
+ return total;
+}
+
+/*
+ * call-seq:
+ * obj.size -> int, Float::INFINITY or nil
+ *
+ * Returns the total size of the enumerator chain calculated by
+ * summing up the size of each enumerable in the chain. If any of the
+ * enumerables reports its size as nil or Float::INFINITY, that value
+ * is returned as the total size.
+ */
+static VALUE
+enum_chain_size(VALUE obj)
+{
+ return enum_chain_total_size(enum_chain_ptr(obj)->enums);
+}
+
+static VALUE
+enum_chain_enum_size(VALUE obj, VALUE args, VALUE eobj)
+{
+ return enum_chain_size(obj);
+}
+
+static VALUE
+enum_chain_yield_block(VALUE arg, VALUE block, int argc, VALUE *argv)
+{
+ return rb_funcallv(block, id_call, argc, argv);
+}
+
+static VALUE
+enum_chain_enum_no_size(VALUE obj, VALUE args, VALUE eobj)
+{
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * obj.each(*args) { |...| ... } -> obj
+ * obj.each(*args) -> enumerator
+ *
+ * Iterates over the elements of the first enumerable by calling the
+ * "each" method on it with the given arguments, then proceeds to the
+ * following enumerables in sequence until all of the enumerables are
+ * exhausted.
+ *
+ * If no block is given, returns an enumerator.
+ */
+static VALUE
+enum_chain_each(int argc, VALUE *argv, VALUE obj)
+{
+ VALUE enums, block;
+ struct enum_chain *objptr;
+
+ RETURN_SIZED_ENUMERATOR(obj, argc, argv, argc > 0 ? enum_chain_enum_no_size : enum_chain_enum_size);
+
+ objptr = enum_chain_ptr(obj);
+ enums = objptr->enums;
+ block = rb_block_proc();
+
+ RARRAY_PTR_USE(enums, ptr, {
+ long i;
+
+ for (i = 0; i < RARRAY_LEN(enums); i++) {
+ objptr->pos = i;
+ rb_block_call(ptr[i], id_each, argc, argv, enum_chain_yield_block, block);
+ }
+ });
+
+ return obj;
+}
+
+/*
+ * call-seq:
+ * obj.rewind -> obj
+ *
+ * Rewinds the enumerator chain by calling the "rewind" method on each
+ * enumerable in reverse order. Each call is performed only if the
+ * enumerable responds to the method.
+ */
+static VALUE
+enum_chain_rewind(VALUE obj)
+{
+ struct enum_chain *objptr = enum_chain_ptr(obj);
+ VALUE enums = objptr->enums;
+
+ RARRAY_PTR_USE(enums, ptr, {
+ long i;
+
+ for (i = objptr->pos; 0 <= i && i < RARRAY_LEN(enums); objptr->pos = --i) {
+ rb_check_funcall(ptr[i], id_rewind, 0, 0);
+ }
+ });
+
+ return obj;
+}
+
+static VALUE
+inspect_enum_chain(VALUE obj, VALUE dummy, int recur)
+{
+ VALUE klass = rb_obj_class(obj);
+ struct enum_chain *ptr;
+
+ TypedData_Get_Struct(obj, struct enum_chain, &enum_chain_data_type, ptr);
+
+ if (!ptr || ptr->enums == Qundef) {
+ return rb_sprintf("#<%"PRIsVALUE": uninitialized>", rb_class_path(klass));
+ }
+
+ if (recur) {
+ return rb_sprintf("#<%"PRIsVALUE": ...>", rb_class_path(klass));
+ }
+
+ return rb_sprintf("#<%"PRIsVALUE": %+"PRIsVALUE">", rb_class_path(klass), ptr->enums);
+}
+
+/*
+ * call-seq:
+ * obj.inspect -> string
+ *
+ * Returns a printable version of the enumerator chain.
+ */
+static VALUE
+enum_chain_inspect(VALUE obj)
+{
+ return rb_exec_recursive(inspect_enum_chain, obj, 0);
+}
+
+/*
+ * call-seq:
+ * e.chain(*enums) -> enumerator
+ *
+ * Returns an enumerator object generated from this enumerator and
+ * given enumerables.
+ *
+ * e = (1..3).chain([4, 5])
+ * e.to_a #=> [1, 2, 3, 4, 5]
+ */
+static VALUE
+enum_chain(int argc, VALUE *argv, VALUE obj)
+{
+ VALUE enums = rb_ary_new_from_values(1, &obj);
+ rb_ary_cat(enums, argv, argc);
+
+ return enum_chain_initialize(enum_chain_allocate(rb_cEnumChain), enums);
+}
+
+/*
+ * call-seq:
+ * e + enum -> enumerator
+ *
+ * Returns an enumerator object generated from this enumerator and a
+ * given enumerable.
+ *
+ * e = (1..3).each + [4, 5]
+ * e.to_a #=> [1, 2, 3, 4, 5]
+ */
+static VALUE
+enumerator_plus(VALUE obj, VALUE eobj)
+{
+ VALUE enums = rb_ary_new_from_args(2, obj, eobj);
+
+ return enum_chain_initialize(enum_chain_allocate(rb_cEnumChain), enums);
+}
+
/*
* Document-class: Enumerator::ArithmeticSequence
*
@@ -2790,15 +3092,16 @@ static double
arith_seq_float_step_size(double beg, double end, double step, int excl)
{
double const epsilon = DBL_EPSILON;
- double n = (end - beg) / step;
- double err = (fabs(beg) + fabs(end) + fabs(end - beg)) / fabs(step) * epsilon;
+ double n, err;
- if (isinf(step)) {
- return step > 0 ? beg <= end : beg >= end;
- }
if (step == 0) {
return HUGE_VAL;
}
+ n = (end - beg) / step;
+ err = (fabs(beg) + fabs(end) + fabs(end - beg)) / fabs(step) * epsilon;
+ if (isinf(step)) {
+ return step > 0 ? beg <= end : beg >= end;
+ }
if (err > 0.5) err = 0.5;
if (excl) {
if (n <= 0) return 0;
@@ -2906,6 +3209,8 @@ InitVM_Enumerator(void)
rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);
rb_define_method(rb_cEnumerator, "inspect", enumerator_inspect, 0);
rb_define_method(rb_cEnumerator, "size", enumerator_size, 0);
+ rb_define_method(rb_cEnumerator, "+", enumerator_plus, 1);
+ rb_define_method(rb_mEnumerable, "chain", enum_chain, -1);
/* Lazy */
rb_cLazy = rb_define_class_under(rb_cEnumerator, "Lazy", rb_cEnumerator);
@@ -2959,6 +3264,16 @@ InitVM_Enumerator(void)
rb_define_method(rb_cYielder, "yield", yielder_yield, -2);
rb_define_method(rb_cYielder, "<<", yielder_yield_push, 1);
+ /* Chain */
+ rb_cEnumChain = rb_define_class_under(rb_cEnumerator, "Chain", rb_cEnumerator);
+ rb_define_alloc_func(rb_cEnumChain, enum_chain_allocate);
+ rb_define_method(rb_cEnumChain, "initialize", enum_chain_initialize, -2);
+ rb_define_method(rb_cEnumChain, "initialize_copy", enum_chain_init_copy, 1);
+ rb_define_method(rb_cEnumChain, "each", enum_chain_each, -1);
+ rb_define_method(rb_cEnumChain, "size", enum_chain_size, 0);
+ rb_define_method(rb_cEnumChain, "rewind", enum_chain_rewind, 0);
+ rb_define_method(rb_cEnumChain, "inspect", enum_chain_inspect, 0);
+
/* ArithmeticSequence */
rb_cArithSeq = rb_define_class_under(rb_cEnumerator, "ArithmeticSequence", rb_cEnumerator);
rb_undef_alloc_func(rb_cArithSeq);
diff --git a/error.c b/error.c
index fc3022f8541712..8e97f4919df819 100644
--- a/error.c
+++ b/error.c
@@ -830,6 +830,13 @@ rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
return 1;
}
+#undef rb_typeddata_is_instance_of
+int
+rb_typeddata_is_instance_of(VALUE obj, const rb_data_type_t *data_type)
+{
+ return rb_typeddata_is_instance_of_inline(obj, data_type);
+}
+
void *
rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
{
diff --git a/eval.c b/eval.c
index 35255e09753075..ecb79ccdcb6340 100644
--- a/eval.c
+++ b/eval.c
@@ -233,7 +233,7 @@ ruby_cleanup(volatile int ex)
}
}
- mjit_finish(); /* We still need ISeqs here. */
+ mjit_finish(TRUE); /* We still need ISeqs here. */
ruby_finalize_1();
@@ -531,7 +531,7 @@ setup_exception(rb_execution_context_t *ec, int tag, volatile VALUE mesg, VALUE
mesg = rb_obj_dup(mesg);
}
}
- if (cause != Qundef) {
+ if (cause != Qundef && !THROW_DATA_P(cause)) {
exc_setup_cause(mesg, cause);
}
if (NIL_P(bt)) {
diff --git a/eval_error.c b/eval_error.c
index 82ada4dc40f0c1..8aab7ba05d0b72 100644
--- a/eval_error.c
+++ b/eval_error.c
@@ -195,7 +195,7 @@ print_backtrace(const VALUE eclass, const VALUE errat, const VALUE str, int reve
long len = RARRAY_LEN(errat);
int skip = eclass == rb_eSysStackError;
const int threshold = 1000000000;
- int width = ((int)log10((double)(len > threshold ?
+ int width = (len <= 1) ? INT_MIN : ((int)log10((double)(len > threshold ?
((len - 1) / threshold) :
len - 1)) +
(len < threshold ? 0 : 9) + 1);
diff --git a/ext/date/date_core.c b/ext/date/date_core.c
index ac81268193a540..5a7eb385c9ad2e 100644
--- a/ext/date/date_core.c
+++ b/ext/date/date_core.c
@@ -51,6 +51,9 @@ static double positive_inf, negative_inf;
#define f_add3(x,y,z) f_add(f_add(x, y), z)
#define f_sub3(x,y,z) f_sub(f_sub(x, y), z)
+static VALUE date_initialize(int argc, VALUE *argv, VALUE self);
+static VALUE datetime_initialize(int argc, VALUE *argv, VALUE self);
+
inline static int
f_cmp(VALUE x, VALUE y)
{
@@ -94,7 +97,7 @@ f_ge_p(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y))
return f_boolcast(FIX2LONG(x) >= FIX2LONG(y));
- return rb_funcall(x, rb_intern(">="), 1, y);
+ return rb_funcall(x, id_ge_p, 1, y);
}
inline static VALUE
@@ -102,7 +105,7 @@ f_eqeq_p(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y))
return f_boolcast(FIX2LONG(x) == FIX2LONG(y));
- return rb_funcall(x, rb_intern("=="), 1, y);
+ return rb_funcall(x, id_eqeq_p, 1, y);
}
inline static VALUE
@@ -322,7 +325,7 @@ do {\
(x)->year = _year;\
(x)->mon = _mon;\
(x)->mday = _mday;\
- (x)->flags = _flags;\
+ (x)->flags = (_flags) & ~COMPLEX_DAT;\
} while (0)
#else
#define set_to_simple(obj, x, _nth, _jd ,_sg, _year, _mon, _mday, _flags) \
@@ -332,7 +335,7 @@ do {\
(x)->sg = (date_sg_t)(_sg);\
(x)->year = _year;\
(x)->pc = PACK2(_mon, _mday);\
- (x)->flags = _flags;\
+ (x)->flags = (_flags) & ~COMPLEX_DAT;\
} while (0)
#endif
@@ -352,7 +355,7 @@ do {\
(x)->hour = _hour;\
(x)->min = _min;\
(x)->sec = _sec;\
- (x)->flags = _flags;\
+ (x)->flags = (_flags) | COMPLEX_DAT;\
} while (0)
#else
#define set_to_complex(obj, x, _nth, _jd ,_df, _sf, _of, _sg,\
@@ -366,7 +369,7 @@ do {\
(x)->sg = (date_sg_t)(_sg);\
(x)->year = _year;\
(x)->pc = PACK5(_mon, _mday, _hour, _min, _sec);\
- (x)->flags = _flags;\
+ (x)->flags = (_flags) | COMPLEX_DAT;\
} while (0)
#endif
@@ -2966,7 +2969,7 @@ d_simple_new_internal(VALUE klass,
obj = TypedData_Make_Struct(klass, struct SimpleDateData,
&d_lite_type, dat);
- set_to_simple(obj, dat, nth, jd, sg, y, m, d, flags & ~COMPLEX_DAT);
+ set_to_simple(obj, dat, nth, jd, sg, y, m, d, flags);
assert(have_jd_p(dat) || have_civil_p(dat));
@@ -2988,7 +2991,7 @@ d_complex_new_internal(VALUE klass,
obj = TypedData_Make_Struct(klass, struct ComplexDateData,
&d_lite_type, dat);
set_to_complex(obj, dat, nth, jd, df, sf, of, sg,
- y, m, d, h, min, s, flags | COMPLEX_DAT);
+ y, m, d, h, min, s, flags);
assert(have_jd_p(dat) || have_civil_p(dat));
assert(have_df_p(dat) || have_time_p(dat));
@@ -3382,10 +3385,21 @@ date_s_ordinal(int argc, VALUE *argv, VALUE klass)
*/
static VALUE
date_s_civil(int argc, VALUE *argv, VALUE klass)
+{
+ return date_initialize(argc, argv, d_lite_s_alloc_simple(klass));
+}
+
+static VALUE
+date_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE vy, vm, vd, vsg, y, fr, fr2, ret;
int m, d;
double sg;
+ struct SimpleDateData *dat = rb_check_typeddata(self, &d_lite_type);
+
+ if (!simple_dat_p(dat)) {
+ rb_raise(rb_eTypeError, "Date expected");
+ }
rb_scan_args(argc, argv, "04", &vy, &vm, &vd, &vsg);
@@ -3415,11 +3429,7 @@ date_s_civil(int argc, VALUE *argv, VALUE klass)
&rm, &rd))
rb_raise(rb_eArgError, "invalid date");
- ret = d_simple_new_internal(klass,
- nth, 0,
- sg,
- ry, rm, rd,
- HAVE_CIVIL);
+ set_to_simple(self, dat, nth, 0, sg, ry, rm, rd, HAVE_CIVIL);
}
else {
VALUE nth;
@@ -3431,12 +3441,9 @@ date_s_civil(int argc, VALUE *argv, VALUE klass)
&ns))
rb_raise(rb_eArgError, "invalid date");
- ret = d_simple_new_internal(klass,
- nth, rjd,
- sg,
- ry, rm, rd,
- HAVE_JD | HAVE_CIVIL);
+ set_to_simple(self, dat, nth, rjd, sg, ry, rm, rd, HAVE_JD | HAVE_CIVIL);
}
+ ret = self;
add_frac();
return ret;
}
@@ -4696,7 +4703,7 @@ do {\
}\
} while (0)
-#ifndef NDEBUG
+#if 0
static VALUE
d_lite_initialize(int argc, VALUE *argv, VALUE self)
{
@@ -4749,7 +4756,7 @@ d_lite_initialize(int argc, VALUE *argv, VALUE self)
"cannot load complex into simple");
set_to_complex(self, &dat->c, nth, rjd, df, sf, of, sg,
- 0, 0, 0, 0, 0, 0, HAVE_JD | HAVE_DF | COMPLEX_DAT);
+ 0, 0, 0, 0, 0, 0, HAVE_JD | HAVE_DF);
}
}
return self;
@@ -4768,8 +4775,28 @@ d_lite_initialize_copy(VALUE copy, VALUE date)
{
get_d2(copy, date);
if (simple_dat_p(bdat)) {
- adat->s = bdat->s;
- adat->s.flags &= ~COMPLEX_DAT;
+ if (simple_dat_p(adat)) {
+ adat->s = bdat->s;
+ }
+ else {
+ adat->c.flags = bdat->s.flags | COMPLEX_DAT;
+ adat->c.nth = bdat->s.nth;
+ adat->c.jd = bdat->s.jd;
+ adat->c.df = 0;
+ adat->c.sf = INT2FIX(0);
+ adat->c.of = 0;
+ adat->c.sg = bdat->s.sg;
+ adat->c.year = bdat->s.year;
+#ifndef USE_PACK
+ adat->c.mon = bdat->s.mon;
+ adat->c.mday = bdat->s.mday;
+ adat->c.hour = bdat->s.hour;
+ adat->c.min = bdat->s.min;
+ adat->c.sec = bdat->s.sec;
+#else
+ adat->c.pc = bdat->s.pc;
+#endif
+ }
}
else {
if (!complex_dat_p(adat))
@@ -4777,7 +4804,6 @@ d_lite_initialize_copy(VALUE copy, VALUE date)
"cannot load complex into simple");
adat->c = bdat->c;
- adat->c.flags |= COMPLEX_DAT;
}
}
return copy;
@@ -6245,7 +6271,7 @@ cmp_gen(VALUE self, VALUE other)
return INT2FIX(f_cmp(m_ajd(dat), other));
else if (k_date_p(other))
return INT2FIX(f_cmp(m_ajd(dat), f_ajd(other)));
- return rb_num_coerce_cmp(self, other, rb_intern("<=>"));
+ return rb_num_coerce_cmp(self, other, id_cmp);
}
static VALUE
@@ -6374,7 +6400,7 @@ equal_gen(VALUE self, VALUE other)
return f_eqeq_p(m_real_local_jd(dat), other);
else if (k_date_p(other))
return f_eqeq_p(m_real_local_jd(dat), f_jd(other));
- return rb_num_coerce_cmp(self, other, rb_intern("=="));
+ return rb_num_coerce_cmp(self, other, id_eqeq_p);
}
/*
@@ -7102,6 +7128,10 @@ d_lite_marshal_dump(VALUE self)
static VALUE
d_lite_marshal_load(VALUE self, VALUE a)
{
+ VALUE nth, sf;
+ int jd, df, of;
+ double sg;
+
get_d1(self);
rb_check_frozen(self);
@@ -7114,63 +7144,33 @@ d_lite_marshal_load(VALUE self, VALUE a)
case 2: /* 1.6.x */
case 3: /* 1.8.x, 1.9.2 */
{
- VALUE ajd, of, sg, nth, sf;
- int jd, df, rof;
- double rsg;
-
+ VALUE ajd, vof, vsg;
if (RARRAY_LEN(a) == 2) {
ajd = f_sub(RARRAY_AREF(a, 0), half_days_in_day);
- of = INT2FIX(0);
- sg = RARRAY_AREF(a, 1);
- if (!k_numeric_p(sg))
- sg = DBL2NUM(RTEST(sg) ? GREGORIAN : JULIAN);
+ vof = INT2FIX(0);
+ vsg = RARRAY_AREF(a, 1);
+ if (!k_numeric_p(vsg))
+ vsg = DBL2NUM(RTEST(vsg) ? GREGORIAN : JULIAN);
}
else {
ajd = RARRAY_AREF(a, 0);
- of = RARRAY_AREF(a, 1);
- sg = RARRAY_AREF(a, 2);
+ vof = RARRAY_AREF(a, 1);
+ vsg = RARRAY_AREF(a, 2);
}
- old_to_new(ajd, of, sg,
- &nth, &jd, &df, &sf, &rof, &rsg);
-
- if (!df && f_zero_p(sf) && !rof) {
- set_to_simple(self, &dat->s, nth, jd, rsg, 0, 0, 0, HAVE_JD);
- } else {
- if (!complex_dat_p(dat))
- rb_raise(rb_eArgError,
- "cannot load complex into simple");
-
- set_to_complex(self, &dat->c, nth, jd, df, sf, rof, rsg,
- 0, 0, 0, 0, 0, 0,
- HAVE_JD | HAVE_DF | COMPLEX_DAT);
- }
+ old_to_new(ajd, vof, vsg,
+ &nth, &jd, &df, &sf, &of, &sg);
}
break;
case 6:
{
- VALUE nth, sf;
- int jd, df, of;
- double sg;
-
nth = RARRAY_AREF(a, 0);
jd = NUM2INT(RARRAY_AREF(a, 1));
df = NUM2INT(RARRAY_AREF(a, 2));
sf = RARRAY_AREF(a, 3);
of = NUM2INT(RARRAY_AREF(a, 4));
sg = NUM2DBL(RARRAY_AREF(a, 5));
- if (!df && f_zero_p(sf) && !of) {
- set_to_simple(self, &dat->s, nth, jd, sg, 0, 0, 0, HAVE_JD);
- } else {
- if (!complex_dat_p(dat))
- rb_raise(rb_eArgError,
- "cannot load complex into simple");
-
- set_to_complex(self, &dat->c, nth, jd, df, sf, of, sg,
- 0, 0, 0, 0, 0, 0,
- HAVE_JD | HAVE_DF | COMPLEX_DAT);
- }
}
break;
default:
@@ -7178,6 +7178,18 @@ d_lite_marshal_load(VALUE self, VALUE a)
break;
}
+ if (simple_dat_p(dat)) {
+ if (df || !f_zero_p(sf) || of) {
+ rb_raise(rb_eArgError,
+ "cannot load complex into simple");
+ }
+ set_to_simple(self, &dat->s, nth, jd, sg, 0, 0, 0, HAVE_JD);
+ } else {
+ set_to_complex(self, &dat->c, nth, jd, df, sf, of, sg,
+ 0, 0, 0, 0, 0, 0,
+ HAVE_JD | HAVE_DF);
+ }
+
if (FL_TEST(a, FL_EXIVAR)) {
rb_copy_generic_ivar(self, a);
FL_SET(self, FL_EXIVAR);
@@ -7357,10 +7369,21 @@ datetime_s_ordinal(int argc, VALUE *argv, VALUE klass)
*/
static VALUE
datetime_s_civil(int argc, VALUE *argv, VALUE klass)
+{
+ return datetime_initialize(argc, argv, d_lite_s_alloc_complex(klass));
+}
+
+static VALUE
+datetime_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE vy, vm, vd, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
int m, d, h, min, s, rof;
double sg;
+ struct ComplexDateData *dat = rb_check_typeddata(self, &d_lite_type);
+
+ if (!complex_dat_p(dat)) {
+ rb_raise(rb_eTypeError, "DateTime expected");
+ }
rb_scan_args(argc, argv, "08", &vy, &vm, &vd, &vh, &vmin, &vs, &vof, &vsg);
@@ -7404,13 +7427,13 @@ datetime_s_civil(int argc, VALUE *argv, VALUE klass)
rb_raise(rb_eArgError, "invalid date");
canon24oc();
- ret = d_complex_new_internal(klass,
- nth, 0,
- 0, INT2FIX(0),
- rof, sg,
- ry, rm, rd,
- rh, rmin, rs,
- HAVE_CIVIL | HAVE_TIME);
+ set_to_complex(self, dat,
+ nth, 0,
+ 0, INT2FIX(0),
+ rof, sg,
+ ry, rm, rd,
+ rh, rmin, rs,
+ HAVE_CIVIL | HAVE_TIME);
}
else {
VALUE nth;
@@ -7429,14 +7452,15 @@ datetime_s_civil(int argc, VALUE *argv, VALUE klass)
time_to_df(rh, rmin, rs),
rof);
- ret = d_complex_new_internal(klass,
- nth, rjd2,
- 0, INT2FIX(0),
- rof, sg,
- ry, rm, rd,
- rh, rmin, rs,
- HAVE_JD | HAVE_CIVIL | HAVE_TIME);
+ set_to_complex(self, dat,
+ nth, rjd2,
+ 0, INT2FIX(0),
+ rof, sg,
+ ry, rm, rd,
+ rh, rmin, rs,
+ HAVE_JD | HAVE_CIVIL | HAVE_TIME);
}
+ ret = self;
add_frac();
return ret;
}
@@ -9232,7 +9256,7 @@ Init_date_core(void)
*/
rb_define_const(cDate, "GREGORIAN", DBL2NUM(GREGORIAN));
- rb_define_alloc_func(cDate, d_lite_s_alloc);
+ rb_define_alloc_func(cDate, d_lite_s_alloc_simple);
#ifndef NDEBUG
#define de_define_private_method rb_define_private_method
@@ -9285,7 +9309,6 @@ Init_date_core(void)
rb_define_singleton_method(cDate, "jd", date_s_jd, -1);
rb_define_singleton_method(cDate, "ordinal", date_s_ordinal, -1);
rb_define_singleton_method(cDate, "civil", date_s_civil, -1);
- rb_define_singleton_method(cDate, "new", date_s_civil, -1);
rb_define_singleton_method(cDate, "commercial", date_s_commercial, -1);
#ifndef NDEBUG
@@ -9313,10 +9336,7 @@ Init_date_core(void)
rb_define_singleton_method(cDate, "_jisx0301", date_s__jisx0301, 1);
rb_define_singleton_method(cDate, "jisx0301", date_s_jisx0301, -1);
-#ifndef NDEBUG
-#define de_define_method rb_define_method
- de_define_method(cDate, "initialize", d_lite_initialize, -1);
-#endif
+ rb_define_method(cDate, "initialize", date_initialize, -1);
rb_define_method(cDate, "initialize_copy", d_lite_initialize_copy, 1);
#ifndef NDEBUG
@@ -9574,6 +9594,7 @@ Init_date_core(void)
*/
cDateTime = rb_define_class("DateTime", cDate);
+ rb_define_alloc_func(cDateTime, d_lite_s_alloc_complex);
rb_define_singleton_method(cDateTime, "jd", datetime_s_jd, -1);
rb_define_singleton_method(cDateTime, "ordinal", datetime_s_ordinal, -1);
diff --git a/ext/json/json.gemspec b/ext/json/json.gemspec
index b8f3009a9c7b79..1c18efbedc712c 100644
Binary files a/ext/json/json.gemspec and b/ext/json/json.gemspec differ
diff --git a/ext/objspace/objspace.c b/ext/objspace/objspace.c
index f9157d6c0e1d81..1ac69af844da3a 100644
--- a/ext/objspace/objspace.c
+++ b/ext/objspace/objspace.c
@@ -824,7 +824,7 @@ static int
collect_values_of_values(VALUE category, VALUE category_objects, VALUE categories)
{
VALUE ary = rb_ary_new();
- st_foreach(rb_hash_tbl(category_objects), collect_values, ary);
+ rb_hash_foreach(category_objects, collect_values, ary);
rb_hash_aset(categories, category, ary);
return ST_CONTINUE;
}
diff --git a/ext/openssl/ossl.c b/ext/openssl/ossl.c
index 38e650e1a37d13..69758aed7a53fb 100644
--- a/ext/openssl/ossl.c
+++ b/ext/openssl/ossl.c
@@ -398,7 +398,7 @@ ossl_debug_set(VALUE self, VALUE val)
}
/*
- * call-seq
+ * call-seq:
* OpenSSL.fips_mode -> true | false
*/
static VALUE
diff --git a/ext/openssl/ossl.h b/ext/openssl/ossl.h
index 5a15839cb45bcc..39699bd5e6b758 100644
--- a/ext/openssl/ossl.h
+++ b/ext/openssl/ossl.h
@@ -13,8 +13,8 @@
#include RUBY_EXTCONF_H
#include
-#include
#include
+#include
#include
#include
#include
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index 3baf818f083ee6..7fb923c6ae2509 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -503,6 +503,7 @@ def each_child(with_directory=true, &b)
# ArgumentError is raised when it cannot find a relative path.
#
def relative_path_from(base_directory)
+ base_directory = Pathname.new(base_directory) unless Pathname === base_directory
dest_directory = self.cleanpath.to_s
base_directory = base_directory.cleanpath.to_s
dest_prefix = dest_directory
diff --git a/ext/psych/lib/psych.rb b/ext/psych/lib/psych.rb
index 6ca5724d2dc115..5b24ebf0917b5d 100644
--- a/ext/psych/lib/psych.rb
+++ b/ext/psych/lib/psych.rb
@@ -294,10 +294,10 @@ def self.load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false,
# * Hash
#
# Recursive data structures are not allowed by default. Arbitrary classes
- # can be allowed by adding those classes to the +whitelist_classes+ keyword argument. They are
+ # can be allowed by adding those classes to the +permitted_classes+ keyword argument. They are
# additive. For example, to allow Date deserialization:
#
- # Psych.safe_load(yaml, whitelist_classes: [Date])
+ # Psych.safe_load(yaml, permitted_classes: [Date])
#
# Now the Date class can be loaded in addition to the classes listed above.
#
@@ -311,7 +311,7 @@ def self.load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false,
# Psych.safe_load yaml, aliases: true # => loads the aliases
#
# A Psych::DisallowedClass exception will be raised if the yaml contains a
- # class that isn't in the whitelist.
+ # class that isn't in the +permitted_classes+ list.
#
# A Psych::BadAlias exception will be raised if the yaml contains aliases
# but the +aliases+ keyword argument is set to false.
@@ -325,15 +325,15 @@ def self.load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false,
# Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"}
# Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
#
- def self.safe_load yaml, legacy_whitelist_classes = NOT_GIVEN, legacy_whitelist_symbols = NOT_GIVEN, legacy_aliases = NOT_GIVEN, legacy_filename = NOT_GIVEN, whitelist_classes: [], whitelist_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false
- if legacy_whitelist_classes != NOT_GIVEN
- warn 'warning: Passing whitelist_classes with the 2nd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, whitelist_classes: ...) instead.'
- whitelist_classes = legacy_whitelist_classes
+ def self.safe_load yaml, legacy_permitted_classes = NOT_GIVEN, legacy_permitted_symbols = NOT_GIVEN, legacy_aliases = NOT_GIVEN, legacy_filename = NOT_GIVEN, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false
+ if legacy_permitted_classes != NOT_GIVEN
+ warn 'warning: Passing permitted_classes with the 2nd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, permitted_classes: ...) instead.'
+ permitted_classes = legacy_permitted_classes
end
- if legacy_whitelist_symbols != NOT_GIVEN
- warn 'warning: Passing whitelist_symbols with the 3rd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, whitelist_symbols: ...) instead.'
- whitelist_symbols = legacy_whitelist_symbols
+ if legacy_permitted_symbols != NOT_GIVEN
+ warn 'warning: Passing permitted_symbols with the 3rd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, permitted_symbols: ...) instead.'
+ permitted_symbols = legacy_permitted_symbols
end
if legacy_aliases != NOT_GIVEN
@@ -349,8 +349,8 @@ def self.safe_load yaml, legacy_whitelist_classes = NOT_GIVEN, legacy_whitelist_
result = parse(yaml, filename: filename)
return fallback unless result
- class_loader = ClassLoader::Restricted.new(whitelist_classes.map(&:to_s),
- whitelist_symbols.map(&:to_s))
+ class_loader = ClassLoader::Restricted.new(permitted_classes.map(&:to_s),
+ permitted_symbols.map(&:to_s))
scanner = ScalarScanner.new class_loader
visitor = if aliases
Visitors::ToRuby.new scanner, class_loader
diff --git a/ext/pty/pty.c b/ext/pty/pty.c
index fc2e529408002d..10eb0ac3121ff5 100644
--- a/ext/pty/pty.c
+++ b/ext/pty/pty.c
@@ -680,7 +680,7 @@ static VALUE cPTY;
/*
* Document-class: PTY
*
- * Creates and managed pseudo terminals (PTYs). See also
+ * Creates and manages pseudo terminals (PTYs). See also
* http://en.wikipedia.org/wiki/Pseudo_terminal
*
* PTY allows you to allocate new terminals using ::open or ::spawn a new
diff --git a/ext/socket/init.c b/ext/socket/init.c
index c26ab135aba5ae..44d150697357eb 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -435,7 +435,7 @@ rsock_socket0(int domain, int type, int proto)
static int cloexec_state = -1; /* <0: unknown, 0: ignored, >0: working */
if (cloexec_state > 0) { /* common path, if SOCK_CLOEXEC is defined */
- ret = socket(domain, type|SOCK_CLOEXEC, proto);
+ ret = socket(domain, type|SOCK_CLOEXEC|RSOCK_NONBLOCK_DEFAULT, proto);
if (ret >= 0) {
if (ret <= 2)
goto fix_cloexec;
@@ -443,7 +443,7 @@ rsock_socket0(int domain, int type, int proto)
}
}
else if (cloexec_state < 0) { /* usually runs once only for detection */
- ret = socket(domain, type|SOCK_CLOEXEC, proto);
+ ret = socket(domain, type|SOCK_CLOEXEC|RSOCK_NONBLOCK_DEFAULT, proto);
if (ret >= 0) {
cloexec_state = rsock_detect_cloexec(ret);
if (cloexec_state == 0 || ret <= 2)
@@ -466,6 +466,9 @@ rsock_socket0(int domain, int type, int proto)
return -1;
fix_cloexec:
rb_maygvl_fd_fix_cloexec(ret);
+ if (RSOCK_NONBLOCK_DEFAULT) {
+ rsock_make_fd_nonblock(ret);
+ }
update_max_fd:
rb_update_max_fd(ret);
@@ -480,6 +483,9 @@ rsock_socket0(int domain, int type, int proto)
if (ret == -1)
return -1;
rb_fd_fix_cloexec(ret);
+ if (RSOCK_NONBLOCK_DEFAULT) {
+ rsock_make_fd_nonblock(ret);
+ }
return ret;
}
@@ -508,11 +514,30 @@ wait_connectable(int fd)
int sockerr, revents;
socklen_t sockerrlen;
- /* only to clear pending error */
sockerrlen = (socklen_t)sizeof(sockerr);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&sockerr, &sockerrlen) < 0)
return -1;
+ /* necessary for non-blocking sockets (at least ECONNREFUSED) */
+ switch (sockerr) {
+ case 0:
+ break;
+#ifdef EALREADY
+ case EALREADY:
+#endif
+#ifdef EISCONN
+ case EISCONN:
+#endif
+#ifdef ECONNREFUSED
+ case ECONNREFUSED:
+#endif
+#ifdef EHOSTUNREACH
+ case EHOSTUNREACH:
+#endif
+ errno = sockerr;
+ return -1;
+ }
+
/*
* Stevens book says, successful finish turn on RB_WAITFD_OUT and
* failure finish turn on both RB_WAITFD_IN and RB_WAITFD_OUT.
@@ -613,8 +638,8 @@ rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks)
return status;
}
-static void
-make_fd_nonblock(int fd)
+void
+rsock_make_fd_nonblock(int fd)
{
int flags;
#ifdef F_GETFL
@@ -640,6 +665,9 @@ cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len,
#ifdef HAVE_ACCEPT4
static int try_accept4 = 1;
#endif
+ if (RSOCK_NONBLOCK_DEFAULT) {
+ nonblock = 1;
+ }
if (address_len) len0 = *address_len;
#ifdef HAVE_ACCEPT4
if (try_accept4) {
@@ -659,7 +687,7 @@ cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len,
rb_maygvl_fd_fix_cloexec(ret);
#ifndef SOCK_NONBLOCK
if (nonblock) {
- make_fd_nonblock(ret);
+ rsock_make_fd_nonblock(ret);
}
#endif
if (address_len && len0 < *address_len) *address_len = len0;
@@ -676,7 +704,7 @@ cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len,
if (address_len && len0 < *address_len) *address_len = len0;
rb_maygvl_fd_fix_cloexec(ret);
if (nonblock) {
- make_fd_nonblock(ret);
+ rsock_make_fd_nonblock(ret);
}
return ret;
}
diff --git a/ext/socket/rubysocket.h b/ext/socket/rubysocket.h
index 922df9b87b7712..0ce77a5f6ef3bb 100644
--- a/ext/socket/rubysocket.h
+++ b/ext/socket/rubysocket.h
@@ -26,7 +26,13 @@
# if defined(_MSC_VER)
# undef HAVE_TYPE_STRUCT_SOCKADDR_DL
# endif
+/*
+ * FIXME: failures if we make nonblocking the default
+ * [ruby-core:89973] [ruby-core:89976] [ruby-core:89977] [Bug #14968]
+ */
+# define RSOCK_NONBLOCK_DEFAULT (0)
#else
+# define RSOCK_NONBLOCK_DEFAULT (0)
# include
# include
# ifdef HAVE_NETINET_IN_SYSTM_H
@@ -408,7 +414,7 @@ NORETURN(void rsock_sys_fail_raddrinfo_or_sockaddr(const char *, VALUE addr, VAL
* all cases. For some syscalls (e.g. accept/accept4), blocking on the
* syscall instead of relying on select/poll allows the kernel to use
* "wake-one" behavior and avoid the thundering herd problem.
- * This is likely safe on all other *nix-like systems, so this whitelist
+ * This is likely safe on all other *nix-like systems, so this safe list
* can be expanded by interested parties.
*/
#if defined(__linux__)
@@ -433,6 +439,8 @@ static inline void rsock_maybe_wait_fd(int fd) { }
VALUE rsock_read_nonblock(VALUE sock, VALUE length, VALUE buf, VALUE ex);
VALUE rsock_write_nonblock(VALUE sock, VALUE buf, VALUE ex);
+void rsock_make_fd_nonblock(int fd);
+
#if !defined HAVE_INET_NTOP && ! defined _WIN32
const char *inet_ntop(int, const void *, char *, size_t);
#elif defined __MINGW32__
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index ead3592d8aa326..0059595e1b9bcd 100644
--- a/ext/socket/socket.c
+++ b/ext/socket/socket.c
@@ -175,16 +175,17 @@ rsock_socketpair0(int domain, int type, int protocol, int sv[2])
{
int ret;
static int cloexec_state = -1; /* <0: unknown, 0: ignored, >0: working */
+ static const int default_flags = SOCK_CLOEXEC|RSOCK_NONBLOCK_DEFAULT;
if (cloexec_state > 0) { /* common path, if SOCK_CLOEXEC is defined */
- ret = socketpair(domain, type|SOCK_CLOEXEC, protocol, sv);
+ ret = socketpair(domain, type|default_flags, protocol, sv);
if (ret == 0 && (sv[0] <= 2 || sv[1] <= 2)) {
goto fix_cloexec; /* highly unlikely */
}
goto update_max_fd;
}
else if (cloexec_state < 0) { /* usually runs once only for detection */
- ret = socketpair(domain, type|SOCK_CLOEXEC, protocol, sv);
+ ret = socketpair(domain, type|default_flags, protocol, sv);
if (ret == 0) {
cloexec_state = rsock_detect_cloexec(sv[0]);
if ((cloexec_state == 0) || (sv[0] <= 2 || sv[1] <= 2))
@@ -213,6 +214,10 @@ rsock_socketpair0(int domain, int type, int protocol, int sv[2])
fix_cloexec:
rb_maygvl_fd_fix_cloexec(sv[0]);
rb_maygvl_fd_fix_cloexec(sv[1]);
+ if (RSOCK_NONBLOCK_DEFAULT) {
+ rsock_make_fd_nonblock(sv[0]);
+ rsock_make_fd_nonblock(sv[1]);
+ }
update_max_fd:
rb_update_max_fd(sv[0]);
@@ -231,6 +236,10 @@ rsock_socketpair0(int domain, int type, int protocol, int sv[2])
rb_fd_fix_cloexec(sv[0]);
rb_fd_fix_cloexec(sv[1]);
+ if (RSOCK_NONBLOCK_DEFAULT) {
+ rsock_make_fd_nonblock(sv[0]);
+ rsock_make_fd_nonblock(sv[1]);
+ }
return ret;
}
#endif /* !SOCK_CLOEXEC */
diff --git a/ext/stringio/stringio.gemspec b/ext/stringio/stringio.gemspec
index 510f071aea2248..df1d468f93e840 100644
--- a/ext/stringio/stringio.gemspec
+++ b/ext/stringio/stringio.gemspec
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = "stringio".freeze
- s.version = "0.0.1"
+ s.version = "0.0.2"
s.required_rubygems_version = Gem::Requirement.new(">= 2.6".freeze)
s.require_paths = ["lib".freeze]
@@ -19,8 +19,9 @@ Gem::Specification.new do |s|
s.required_ruby_version = Gem::Requirement.new(">= 2.2".freeze)
s.rubygems_version = "2.6.11".freeze
s.summary = "Pseudo IO on String".freeze
- s.cert_chain = %w[certs/nobu.pem]
- s.signing_key = File.expand_path("~/.ssh/gem-private_key.pem") if $0 =~ /gem\z/
+
+ # s.cert_chain = %w[certs/nobu.pem]
+ # s.signing_key = File.expand_path("~/.ssh/gem-private_key.pem") if $0 =~ /gem\z/
s.add_development_dependency 'rake-compiler'
end
diff --git a/ext/win32ole/win32ole_method.c b/ext/win32ole/win32ole_method.c
index 456a45cfc42c44..ffa93246570471 100644
--- a/ext/win32ole/win32ole_method.c
+++ b/ext/win32ole/win32ole_method.c
@@ -283,7 +283,7 @@ folemethod_initialize(VALUE self, VALUE oletype, VALUE method)
}
/*
- * call-seq
+ * call-seq:
* WIN32OLE_METHOD#name
*
* Returns the name of the method.
diff --git a/gc.c b/gc.c
index dc8f5808f2d691..4de27892e8c323 100644
--- a/gc.c
+++ b/gc.c
@@ -35,6 +35,7 @@
#include
#include "ruby_assert.h"
#include "debug_counter.h"
+#include "transient_heap.h"
#include "mjit.h"
#undef rb_data_object_wrap
@@ -60,16 +61,6 @@
# endif
#endif
-#if /* is ASAN enabled? */ \
- __has_feature(address_sanitizer) /* Clang */ || \
- defined(__SANITIZE_ADDRESS__) /* GCC 4.8.x */
- #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS \
- __attribute__((no_address_safety_analysis)) \
- __attribute__((noinline))
-#else
- #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS
-#endif
-
#ifdef HAVE_SYS_TIME_H
#include
#endif
@@ -658,10 +649,8 @@ typedef struct rb_objspace {
} rb_objspace_t;
-#ifndef HEAP_PAGE_ALIGN_LOG
/* default tiny heap size: 16KB */
#define HEAP_PAGE_ALIGN_LOG 14
-#endif
#define CEILDIV(i, mod) (((i) + (mod) - 1)/(mod))
enum {
HEAP_PAGE_ALIGN = (1UL << HEAP_PAGE_ALIGN_LOG),
@@ -845,8 +834,6 @@ static void rb_objspace_call_finalizer(rb_objspace_t *objspace);
static VALUE define_final0(VALUE obj, VALUE block);
static void negative_size_allocation_error(const char *);
-static void *aligned_malloc(size_t, size_t);
-static void aligned_free(void *);
static void init_mark_stack(mark_stack_t *stack);
@@ -875,7 +862,7 @@ static void gc_sweep_continue(rb_objspace_t *objspace, rb_heap_t *heap);
static inline void gc_mark(rb_objspace_t *objspace, VALUE ptr);
static void gc_mark_ptr(rb_objspace_t *objspace, VALUE ptr);
-static void gc_mark_maybe(rb_objspace_t *objspace, VALUE ptr);
+NO_SANITIZE("memory", static void gc_mark_maybe(rb_objspace_t *objspace, VALUE ptr));
static void gc_mark_children(rb_objspace_t *objspace, VALUE ptr);
static int gc_mark_stacked_objects_incremental(rb_objspace_t *, size_t count);
@@ -883,7 +870,7 @@ static int gc_mark_stacked_objects_all(rb_objspace_t *);
static void gc_grey(rb_objspace_t *objspace, VALUE ptr);
static inline int gc_mark_set(rb_objspace_t *objspace, VALUE obj);
-static inline int is_pointer_to_heap(rb_objspace_t *objspace, void *ptr);
+NO_SANITIZE("memory", static inline int is_pointer_to_heap(rb_objspace_t *objspace, void *ptr));
static void push_mark_stack(mark_stack_t *, VALUE);
static int pop_mark_stack(mark_stack_t *, VALUE *);
@@ -1190,6 +1177,7 @@ RVALUE_PAGE_OLD_UNCOLLECTIBLE_SET(rb_objspace_t *objspace, struct heap_page *pag
{
MARK_IN_BITMAP(&page->uncollectible_bits[0], obj);
objspace->rgengc.old_objects++;
+ rb_transient_heap_promote(obj);
#if RGENGC_PROFILE >= 2
objspace->profile.total_promoted_count++;
@@ -1444,6 +1432,7 @@ heap_page_add_freeobj(rb_objspace_t *objspace, struct heap_page *page, VALUE obj
if (RGENGC_CHECK_MODE && !is_pointer_to_heap(objspace, p)) {
rb_bug("heap_page_add_freeobj: %p is not rvalue.", (void *)p);
}
+ poison_object(obj);
gc_report(3, objspace, "heap_page_add_freeobj: add %p to freelist\n", (void *)obj);
}
@@ -1486,7 +1475,7 @@ heap_page_free(rb_objspace_t *objspace, struct heap_page *page)
{
heap_allocated_pages--;
objspace->profile.total_freed_pages++;
- aligned_free(GET_PAGE_BODY(page->start));
+ rb_aligned_free(GET_PAGE_BODY(page->start));
free(page);
}
@@ -1524,7 +1513,7 @@ heap_page_allocate(rb_objspace_t *objspace)
int limit = HEAP_PAGE_OBJ_LIMIT;
/* assign heap_page body (contains heap_page_header and RVALUEs) */
- page_body = (struct heap_page_body *)aligned_malloc(HEAP_PAGE_ALIGN, HEAP_PAGE_SIZE);
+ page_body = (struct heap_page_body *)rb_aligned_malloc(HEAP_PAGE_ALIGN, HEAP_PAGE_SIZE);
if (page_body == 0) {
rb_memerror();
}
@@ -1532,7 +1521,7 @@ heap_page_allocate(rb_objspace_t *objspace)
/* assign heap_page entry */
page = (struct heap_page *)calloc(1, sizeof(struct heap_page));
if (page == 0) {
- aligned_free(page_body);
+ rb_aligned_free(page_body);
rb_memerror();
}
@@ -1768,6 +1757,7 @@ heap_get_freeobj_from_next_freepage(rb_objspace_t *objspace, rb_heap_t *heap)
p = page->freelist;
page->freelist = NULL;
page->free_slots = 0;
+ unpoison_object((VALUE)p, true);
return p;
}
@@ -1778,6 +1768,7 @@ heap_get_freeobj_head(rb_objspace_t *objspace, rb_heap_t *heap)
if (LIKELY(p != NULL)) {
heap->freelist = p->as.free.next;
}
+ unpoison_object((VALUE)p, true);
return (VALUE)p;
}
@@ -1788,6 +1779,7 @@ heap_get_freeobj(rb_objspace_t *objspace, rb_heap_t *heap)
while (1) {
if (LIKELY(p != NULL)) {
+ unpoison_object((VALUE)p, true);
heap->freelist = p->as.free.next;
return (VALUE)p;
}
@@ -1954,10 +1946,10 @@ newobj_of(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, int wb_protect
#if GC_DEBUG_STRESS_TO_CLASS
if (UNLIKELY(stress_to_class)) {
- long i, cnt = RARRAY_LEN(stress_to_class);
- for (i = 0; i < cnt; ++i) {
+ long i, cnt = RARRAY_LEN(stress_to_class);
+ for (i = 0; i < cnt; ++i) {
if (klass == RARRAY_AREF(stress_to_class, i)) rb_memerror();
- }
+ }
}
#endif
if (!(during_gc ||
@@ -2215,15 +2207,18 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
switch (BUILTIN_TYPE(obj)) {
case T_OBJECT:
- if (!(RANY(obj)->as.basic.flags & ROBJECT_EMBED) &&
- RANY(obj)->as.object.as.heap.ivptr) {
- xfree(RANY(obj)->as.object.as.heap.ivptr);
- RB_DEBUG_COUNTER_INC(obj_obj_ptr);
- }
- else {
+ if ((RANY(obj)->as.basic.flags & ROBJECT_EMBED) ||
+ RANY(obj)->as.object.as.heap.ivptr == NULL) {
RB_DEBUG_COUNTER_INC(obj_obj_embed);
- }
- break;
+ }
+ else if (ROBJ_TRANSIENT_P(obj)) {
+ RB_DEBUG_COUNTER_INC(obj_obj_transient);
+ }
+ else {
+ xfree(RANY(obj)->as.object.as.heap.ivptr);
+ RB_DEBUG_COUNTER_INC(obj_obj_ptr);
+ }
+ break;
case T_MODULE:
case T_CLASS:
mjit_remove_class_serial(RCLASS_SERIAL(obj));
@@ -2259,26 +2254,46 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
rb_str_free(obj);
break;
case T_ARRAY:
- rb_ary_free(obj);
+ rb_ary_free(obj);
break;
case T_HASH:
- if (RANY(obj)->as.hash.ntbl) {
#if USE_DEBUG_COUNTER
- if (RHASH_SIZE(obj) >= 8) {
- RB_DEBUG_COUNTER_INC(obj_hash_ge8);
- }
- if (RHASH_SIZE(obj) >= 4) {
- RB_DEBUG_COUNTER_INC(obj_hash_ge4);
- }
- else {
- RB_DEBUG_COUNTER_INC(obj_hash_under4);
- }
-#endif
- st_free_table(RANY(obj)->as.hash.ntbl);
- }
+ if (RHASH_SIZE(obj) >= 8) {
+ RB_DEBUG_COUNTER_INC(obj_hash_ge8);
+ }
+ else if (RHASH_SIZE(obj) >= 4) {
+ RB_DEBUG_COUNTER_INC(obj_hash_ge4);
+ }
+ else if (RHASH_SIZE(obj) >= 1) {
+ RB_DEBUG_COUNTER_INC(obj_hash_under4);
+ }
else {
RB_DEBUG_COUNTER_INC(obj_hash_empty);
}
+
+ if (RHASH_ARRAY_P(obj)) {
+ RB_DEBUG_COUNTER_INC(obj_hash_array);
+ }
+ else {
+ RB_DEBUG_COUNTER_INC(obj_hash_st);
+ }
+#endif
+ if (/* RHASH_ARRAY_P(obj) */ !FL_TEST_RAW(obj, RHASH_ST_TABLE_FLAG)) {
+ li_table *tab = RHASH(obj)->as.li;
+
+ if (tab) {
+ if (RHASH_TRANSIENT_P(obj)) {
+ RB_DEBUG_COUNTER_INC(obj_hash_transient);
+ }
+ else {
+ ruby_xfree(tab);
+ }
+ }
+ }
+ else {
+ GC_ASSERT(RHASH_TABLE_P(obj));
+ st_free_table(RHASH(obj)->as.st);
+ }
break;
case T_REGEXP:
if (RANY(obj)->as.regexp.ptr) {
@@ -2380,14 +2395,17 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
break;
case T_STRUCT:
- if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
- RANY(obj)->as.rstruct.as.heap.ptr) {
- xfree((void *)RANY(obj)->as.rstruct.as.heap.ptr);
- RB_DEBUG_COUNTER_INC(obj_struct_ptr);
- }
- else {
+ if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
+ RANY(obj)->as.rstruct.as.heap.ptr == NULL) {
RB_DEBUG_COUNTER_INC(obj_struct_embed);
}
+ else if (RSTRUCT_TRANSIENT_P(obj)) {
+ RB_DEBUG_COUNTER_INC(obj_struct_transient);
+ }
+ else {
+ xfree((void *)RANY(obj)->as.rstruct.as.heap.ptr);
+ RB_DEBUG_COUNTER_INC(obj_struct_ptr);
+ }
break;
case T_SYMBOL:
@@ -2596,8 +2614,11 @@ static int
internal_object_p(VALUE obj)
{
RVALUE *p = (RVALUE *)obj;
+ void *ptr = __asan_region_is_poisoned(p, SIZEOF_VALUE);
+ bool used_p = p->as.basic.flags;
+ unpoison_object(obj, false);
- if (p->as.basic.flags) {
+ if (used_p) {
switch (BUILTIN_TYPE(p)) {
case T_NODE:
UNEXPECTED_NODE(internal_object_p);
@@ -2618,6 +2639,9 @@ internal_object_p(VALUE obj)
return 0;
}
}
+ if (ptr || ! used_p) {
+ poison_object(obj);
+ }
return 1;
}
@@ -2908,13 +2932,16 @@ static void
finalize_list(rb_objspace_t *objspace, VALUE zombie)
{
while (zombie) {
- VALUE next_zombie = RZOMBIE(zombie)->next;
- struct heap_page *page = GET_HEAP_PAGE(zombie);
+ VALUE next_zombie;
+ struct heap_page *page;
+ unpoison_object(zombie, false);
+ next_zombie = RZOMBIE(zombie)->next;
+ page = GET_HEAP_PAGE(zombie);
run_final(objspace, zombie);
RZOMBIE(zombie)->basic.flags = 0;
- heap_pages_final_slots--;
+ if (LIKELY(heap_pages_final_slots)) heap_pages_final_slots--;
page->final_slots--;
page->free_slots++;
heap_page_add_freeobj(objspace, GET_HEAP_PAGE(zombie), zombie);
@@ -3028,6 +3055,7 @@ rb_objspace_call_finalizer(rb_objspace_t *objspace)
for (i = 0; i < heap_allocated_pages; i++) {
p = heap_pages_sorted[i]->start; pend = p + heap_pages_sorted[i]->total_slots;
while (p < pend) {
+ unpoison_object((VALUE)p, false);
switch (BUILTIN_TYPE(p)) {
case T_DATA:
if (!DATA_PTR(p) || !RANY(p)->as.data.dfree) break;
@@ -3051,6 +3079,7 @@ rb_objspace_call_finalizer(rb_objspace_t *objspace)
}
break;
}
+ poison_object((VALUE)p);
p++;
}
}
@@ -3331,9 +3360,13 @@ obj_memsize_of(VALUE obj, int use_all_types)
size += rb_ary_memsize(obj);
break;
case T_HASH:
- if (RHASH(obj)->ntbl) {
- size += st_memsize(RHASH(obj)->ntbl);
+ if (RHASH_ARRAY_P(obj)) {
+ size += sizeof(li_table);
}
+ else {
+ VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
+ size += st_memsize(RHASH_ST_TABLE(obj));
+ }
break;
case T_REGEXP:
if (RREGEXP_PTR(obj)) {
@@ -3485,7 +3518,7 @@ count_objects(int argc, VALUE *argv, VALUE os)
hash = rb_hash_new();
}
else if (!RHASH_EMPTY_P(hash)) {
- st_foreach(RHASH_TBL_RAW(hash), set_zero, hash);
+ rb_hash_stlike_foreach(hash, set_zero, hash);
}
rb_hash_aset(hash, ID2SYM(rb_intern("TOTAL")), SIZET2NUM(total));
rb_hash_aset(hash, ID2SYM(rb_intern("FREE")), SIZET2NUM(freed));
@@ -3590,6 +3623,7 @@ gc_page_sweep(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *sweep_
if (bitset) {
p = offset + i * BITS_BITLENGTH;
do {
+ unpoison_object((VALUE)p, false);
if (bitset & 1) {
switch (BUILTIN_TYPE(p)) {
default: { /* majority case */
@@ -3608,6 +3642,7 @@ gc_page_sweep(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *sweep_
heap_page_add_freeobj(objspace, sweep_page, (VALUE)p);
gc_report(3, objspace, "page_sweep: %s is added to freelist\n", obj_info((VALUE)p));
freed_slots++;
+ poison_object((VALUE)p);
}
break;
}
@@ -4127,7 +4162,7 @@ ruby_stack_check(void)
return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
}
-ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS
+ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(static void mark_locations_array(rb_objspace_t *objspace, register const VALUE *x, register long n));
static void
mark_locations_array(rb_objspace_t *objspace, register const VALUE *x, register long n)
{
@@ -4219,7 +4254,23 @@ mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
}
static void
-mark_hash(rb_objspace_t *objspace, st_table *tbl)
+mark_hash(rb_objspace_t *objspace, VALUE hash)
+{
+ rb_hash_stlike_foreach(hash, mark_keyvalue, (st_data_t)objspace);
+
+ if (RHASH_ARRAY_P(hash)) {
+ if (objspace->mark_func_data == NULL && RHASH_TRANSIENT_P(hash)) {
+ rb_transient_heap_mark(hash, RHASH_ARRAY(hash));
+ }
+ }
+ else {
+ VM_ASSERT(!RHASH_TRANSIENT_P(hash));
+ }
+ gc_mark(objspace, RHASH(hash)->ifnone);
+}
+
+static void
+mark_st(rb_objspace_t *objspace, st_table *tbl)
{
if (!tbl) return;
st_foreach(tbl, mark_keyvalue, (st_data_t)objspace);
@@ -4228,7 +4279,7 @@ mark_hash(rb_objspace_t *objspace, st_table *tbl)
void
rb_mark_hash(st_table *tbl)
{
- mark_hash(&rb_objspace, tbl);
+ mark_st(&rb_objspace, tbl);
}
static void
@@ -4250,7 +4301,8 @@ mark_method_entry(rb_objspace_t *objspace, const rb_method_entry_t *me)
gc_mark(objspace, def->body.attr.location);
break;
case VM_METHOD_TYPE_BMETHOD:
- gc_mark(objspace, def->body.proc);
+ gc_mark(objspace, def->body.bmethod.proc);
+ if (def->body.bmethod.hooks) rb_hook_list_mark(def->body.bmethod.hooks);
break;
case VM_METHOD_TYPE_ALIAS:
gc_mark(objspace, (VALUE)def->body.alias.original_me);
@@ -4381,10 +4433,17 @@ gc_mark_maybe(rb_objspace_t *objspace, VALUE obj)
{
(void)VALGRIND_MAKE_MEM_DEFINED(&obj, sizeof(obj));
if (is_pointer_to_heap(objspace, (void *)obj)) {
- int type = BUILTIN_TYPE(obj);
+ int type;
+ void *ptr = __asan_region_is_poisoned((void *)obj, SIZEOF_VALUE);
+
+ unpoison_object(obj, false);
+ type = BUILTIN_TYPE(obj);
if (type != T_ZOMBIE && type != T_NONE) {
gc_mark_ptr(objspace, obj);
}
+ if (ptr) {
+ poison_object(obj);
+ }
}
}
@@ -4515,6 +4574,7 @@ static void
gc_mark_ptr(rb_objspace_t *objspace, VALUE obj)
{
if (LIKELY(objspace->mark_func_data == NULL)) {
+ if (RB_TYPE_P(obj, T_NONE)) rb_bug("...");
rgengc_check_relation(objspace, obj);
if (!gc_mark_set(objspace, obj)) return; /* already marked */
gc_aging(objspace, obj);
@@ -4671,21 +4731,28 @@ gc_mark_children(rb_objspace_t *objspace, VALUE obj)
break;
case T_ARRAY:
- if (FL_TEST(obj, ELTS_SHARED)) {
- gc_mark(objspace, any->as.array.as.heap.aux.shared);
+ if (FL_TEST(obj, ELTS_SHARED)) {
+ VALUE root = any->as.array.as.heap.aux.shared;
+ gc_mark(objspace, root);
}
else {
long i, len = RARRAY_LEN(obj);
- const VALUE *ptr = RARRAY_CONST_PTR(obj);
+ const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(obj);
for (i=0; i < len; i++) {
- gc_mark(objspace, *ptr++);
+ gc_mark(objspace, ptr[i]);
}
- }
+
+ if (objspace->mark_func_data == NULL) {
+ if (!FL_TEST_RAW(obj, RARRAY_EMBED_FLAG) &&
+ RARRAY_TRANSIENT_P(obj)) {
+ rb_transient_heap_mark(obj, ptr);
+ }
+ }
+ }
break;
case T_HASH:
- mark_hash(objspace, any->as.hash.ntbl);
- gc_mark(objspace, any->as.hash.ifnone);
+ mark_hash(objspace, obj);
break;
case T_STRING:
@@ -4708,10 +4775,18 @@ gc_mark_children(rb_objspace_t *objspace, VALUE obj)
case T_OBJECT:
{
- uint32_t i, len = ROBJECT_NUMIV(obj);
- VALUE *ptr = ROBJECT_IVPTR(obj);
- for (i = 0; i < len; i++) {
- gc_mark(objspace, *ptr++);
+ const VALUE * const ptr = ROBJECT_IVPTR(obj);
+
+ if (ptr) {
+ uint32_t i, len = ROBJECT_NUMIV(obj);
+ for (i = 0; i < len; i++) {
+ gc_mark(objspace, ptr[i]);
+ }
+
+ if (objspace->mark_func_data == NULL &&
+ ROBJ_TRANSIENT_P(obj)) {
+ rb_transient_heap_mark(obj, ptr);
+ }
}
}
break;
@@ -4755,12 +4830,18 @@ gc_mark_children(rb_objspace_t *objspace, VALUE obj)
case T_STRUCT:
{
- long len = RSTRUCT_LEN(obj);
- const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
+ long i;
+ const long len = RSTRUCT_LEN(obj);
+ const VALUE * const ptr = RSTRUCT_CONST_PTR(obj);
- while (len--) {
- gc_mark(objspace, *ptr++);
- }
+ for (i=0; imark_func_data == NULL &&
+ RSTRUCT_TRANSIENT_P(obj)) {
+ rb_transient_heap_mark(obj, ptr);
+ }
}
break;
@@ -5455,6 +5536,13 @@ rb_gc_verify_internal_consistency(void)
gc_verify_internal_consistency(Qnil);
}
+static VALUE
+gc_verify_transient_heap_internal_consistency(VALUE dmy)
+{
+ rb_transient_heap_verify();
+ return Qnil;
+}
+
/* marks */
static void
@@ -5671,6 +5759,8 @@ gc_marks_finish(rb_objspace_t *objspace)
#endif
}
+ rb_transient_heap_finish_marking();
+
gc_event_hook(objspace, RUBY_INTERNAL_EVENT_GC_END_MARK, 0);
return TRUE;
@@ -6562,6 +6652,7 @@ gc_start(rb_objspace_t *objspace, int reason)
objspace->profile.heap_used_at_gc_start = heap_allocated_pages;
gc_prof_setup_new_record(objspace, reason);
gc_reset_malloc_info(objspace);
+ rb_transient_heap_start_marking(do_full_mark);
gc_event_hook(objspace, RUBY_INTERNAL_EVENT_GC_START, 0 /* TODO: pass minor/immediate flag? */);
GC_ASSERT(during_gc);
@@ -7812,8 +7903,8 @@ rb_memerror(void)
EC_JUMP_TAG(ec, TAG_RAISE);
}
-static void *
-aligned_malloc(size_t alignment, size_t size)
+void *
+rb_aligned_malloc(size_t alignment, size_t size)
{
void *res;
@@ -7846,8 +7937,8 @@ aligned_malloc(size_t alignment, size_t size)
return res;
}
-static void
-aligned_free(void *ptr)
+void
+rb_aligned_free(void *ptr)
{
#if defined __MINGW32__
__mingw_aligned_free(ptr);
@@ -9543,6 +9634,13 @@ rb_raw_obj_info(char *buff, const int buff_size, VALUE obj)
{
if (SPECIAL_CONST_P(obj)) {
snprintf(buff, buff_size, "%s", obj_type_name(obj));
+
+ if (FIXNUM_P(obj)) {
+ snprintf(buff, buff_size, "%s %ld", buff, FIX2LONG(obj));
+ }
+ else if (SYMBOL_P(obj)) {
+ snprintf(buff, buff_size, "%s %s", buff, rb_id2name(SYM2ID(obj)));
+ }
}
else {
#define TF(c) ((c) != 0 ? "true" : "false")
@@ -9551,13 +9649,21 @@ rb_raw_obj_info(char *buff, const int buff_size, VALUE obj)
#if USE_RGENGC
const int age = RVALUE_FLAGS_AGE(RBASIC(obj)->flags);
- snprintf(buff, buff_size, "%p [%d%s%s%s%s] %s",
- (void *)obj, age,
- C(RVALUE_UNCOLLECTIBLE_BITMAP(obj), "L"),
- C(RVALUE_MARK_BITMAP(obj), "M"),
- C(RVALUE_MARKING_BITMAP(obj), "R"),
- C(RVALUE_WB_UNPROTECTED_BITMAP(obj), "U"),
- obj_type_name(obj));
+ if (is_pointer_to_heap(&rb_objspace, (void *)obj)) {
+ snprintf(buff, buff_size, "%p [%d%s%s%s%s] %s",
+ (void *)obj, age,
+ C(RVALUE_UNCOLLECTIBLE_BITMAP(obj), "L"),
+ C(RVALUE_MARK_BITMAP(obj), "M"),
+ C(RVALUE_MARKING_BITMAP(obj), "R"),
+ C(RVALUE_WB_UNPROTECTED_BITMAP(obj), "U"),
+ obj_type_name(obj));
+ }
+ else {
+ /* fake */
+ snprintf(buff, buff_size, "%p [%dXXXX] %s",
+ (void *)obj, age,
+ obj_type_name(obj));
+ }
#else
snprintf(buff, buff_size, "%p [%s] %s",
(void *)obj,
@@ -9587,15 +9693,37 @@ rb_raw_obj_info(char *buff, const int buff_size, VALUE obj)
UNEXPECTED_NODE(rb_raw_obj_info);
break;
case T_ARRAY:
- snprintf(buff, buff_size, "%s [%s%s] len: %d", buff,
- C(ARY_EMBED_P(obj), "E"),
- C(ARY_SHARED_P(obj), "S"),
- (int)RARRAY_LEN(obj));
+ if (FL_TEST(obj, ELTS_SHARED)) {
+ snprintf(buff, buff_size, "%s shared -> %s", buff,
+ rb_obj_info(RARRAY(obj)->as.heap.aux.shared));
+ }
+ else if (FL_TEST(obj, RARRAY_EMBED_FLAG)) {
+ snprintf(buff, buff_size, "%s [%s%s] len: %d (embed)", buff,
+ C(ARY_EMBED_P(obj), "E"),
+ C(ARY_SHARED_P(obj), "S"),
+ (int)RARRAY_LEN(obj));
+ }
+ else {
+ snprintf(buff, buff_size, "%s [%s%s%s] len: %d, capa:%d ptr:%p", buff,
+ C(ARY_EMBED_P(obj), "E"),
+ C(ARY_SHARED_P(obj), "S"),
+ C(RARRAY_TRANSIENT_P(obj), "T"),
+ (int)RARRAY_LEN(obj),
+ ARY_EMBED_P(obj) ? -1 : (int)RARRAY(obj)->as.heap.aux.capa,
+ (void *)RARRAY_CONST_PTR_TRANSIENT(obj));
+ }
break;
case T_STRING: {
snprintf(buff, buff_size, "%s %s", buff, RSTRING_PTR(obj));
break;
}
+ case T_HASH: {
+ snprintf(buff, buff_size, "%s [%c%c] %d", buff,
+ RHASH_ARRAY_P(obj) ? 'A' : 'S',
+ RHASH_TRANSIENT_P(obj) ? 'T' : ' ',
+ (int)RHASH_SIZE(obj));
+ break;
+ }
case T_CLASS: {
VALUE class_path = rb_class_path_cached(obj);
if (!NIL_P(class_path)) {
@@ -9603,6 +9731,19 @@ rb_raw_obj_info(char *buff, const int buff_size, VALUE obj)
}
break;
}
+ case T_OBJECT:
+ {
+ uint32_t len = ROBJECT_NUMIV(obj);
+
+ if (RANY(obj)->as.basic.flags & ROBJECT_EMBED) {
+ snprintf(buff, buff_size, "%s (embed) len:%d", buff, len);
+ }
+ else {
+ VALUE *ptr = ROBJECT_IVPTR(obj);
+ snprintf(buff, buff_size, "%s len:%d ptr:%p", buff, len, (void *)ptr);
+ }
+ }
+ break;
case T_DATA: {
const struct rb_block *block;
const rb_iseq_t *iseq;
@@ -9954,6 +10095,7 @@ Init_GC(void)
/* internal methods */
rb_define_singleton_method(rb_mGC, "verify_internal_consistency", gc_verify_internal_consistency, 0);
+ rb_define_singleton_method(rb_mGC, "verify_transient_heap_internal_consistency", gc_verify_transient_heap_internal_consistency, 0);
#if MALLOC_ALLOCATED_SIZE
rb_define_singleton_method(rb_mGC, "malloc_allocated_size", gc_malloc_allocated_size, 0);
rb_define_singleton_method(rb_mGC, "malloc_allocations", gc_malloc_allocations, 0);
diff --git a/hash.c b/hash.c
index 7a8f3f0faf3e08..ad9f49519263cf 100644
--- a/hash.c
+++ b/hash.c
@@ -20,7 +20,9 @@
#include "id.h"
#include "symbol.h"
#include "gc.h"
-
+#include "debug_counter.h"
+#include "transient_heap.h"
+#include "ruby_assert.h"
#ifdef __APPLE__
# ifdef HAVE_CRT_EXTERNS_H
# include
@@ -29,6 +31,10 @@
# endif
#endif
+#ifndef HASH_DEBUG
+#define HASH_DEBUG 0
+#endif
+
#define HAS_EXTRA_STATES(hash, klass) ( \
((klass = has_extra_methods(rb_obj_class(hash))) != 0) || \
FL_TEST((hash), FL_EXIVAR|FL_TAINT|HASH_PROC_DEFAULT) || \
@@ -287,7 +293,9 @@ rb_ident_hash(st_data_t n)
* many integers get interpreted as 2.0 or -2.0 [Bug #10761]
*/
if (FLONUM_P(n)) {
- n ^= (st_data_t)rb_float_value(n);
+ union { double d; st_data_t i; } u;
+ u.d = rb_float_value(n);
+ n ^= u.i;
}
#endif
@@ -299,6 +307,773 @@ static const struct st_hash_type identhash = {
rb_ident_hash,
};
+#define EQUAL(x,y) ((x) == (y) || (*objhash.compare)((x),(y)) == 0)
+#define PTR_EQUAL(ptr, hash_val, key_) \
+ ((ptr)->hash == (hash_val) && EQUAL((key_), (ptr)->key))
+
+#define RESERVED_HASH_VAL ((st_hash_t) 0)
+#define RESERVED_HASH_SUBSTITUTION_VAL (~(st_hash_t) 0)
+
+#define SET_KEY(entry, _key) (entry)->key = (_key)
+#define SET_HASH(entry, _hash) (entry)->hash = (_hash)
+#define SET_RECORD(entry, _value) (entry)->record = (_value)
+
+typedef st_data_t st_hash_t;
+
+static inline st_hash_t
+do_hash(st_data_t key)
+{
+ st_hash_t hash = (st_hash_t)(*objhash.hash)(key);
+ return (RESERVED_HASH_VAL == hash) ? RESERVED_HASH_SUBSTITUTION_VAL : hash;
+}
+
+static inline void
+set_entry(li_table_entry *entry, st_data_t key, st_data_t val, st_hash_t hash)
+{
+ SET_HASH(entry, hash);
+ SET_KEY(entry, key);
+ SET_RECORD(entry, val);
+}
+
+static inline void
+clear_entry(li_table_entry* entry)
+{
+ SET_KEY(entry, Qundef);
+ SET_RECORD(entry, Qundef);
+ SET_HASH(entry, 0);
+}
+
+static inline int
+empty_entry(li_table_entry *entry)
+{
+ return entry->hash == 0;
+}
+
+#define RHASH_ARRAY_SIZE(h) (HASH_ASSERT(RHASH_ARRAY_P(h)), \
+ RHASH_ARRAY_SIZE_RAW(h))
+
+#define RHASH_ARRAY_BOUND_RAW(h) \
+ ((unsigned int)((RBASIC(h)->flags >> RHASH_ARRAY_BOUND_SHIFT) & \
+ (RHASH_ARRAY_BOUND_MASK >> RHASH_ARRAY_BOUND_SHIFT)))
+
+#define RHASH_ARRAY_BOUND(h) (HASH_ASSERT(RHASH_ARRAY_P(h)), \
+ RHASH_ARRAY_BOUND_RAW(h))
+
+#define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s)
+#define RHASH_TYPE(hash) (RHASH_ARRAY_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type)
+#define RHASH_ARRAY_REF(hash, n) (&RHASH_ARRAY(hash)->entries[n])
+
+#if HASH_DEBUG
+#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
+#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(1, expr, #expr)
+
+void
+rb_hash_dump(VALUE hash)
+{
+ rb_obj_info_dump(hash);
+
+ if (RHASH_ARRAY_P(hash)) {
+ unsigned i, n = 0, bound = RHASH_ARRAY_BOUND(hash);
+
+ fprintf(stderr, " size:%u bound:%u\n",
+ RHASH_ARRAY_SIZE(hash), RHASH_ARRAY_BOUND(hash));
+
+ for (i=0; ihash; */
+ k = cur_entry->key;
+ v = cur_entry->record;
+ fprintf(stderr, " %d key:%s val:%s\n", i,
+ rb_raw_obj_info(b1, 0x100, k),
+ rb_raw_obj_info(b2, 0x100, v));
+ n++;
+ }
+ else {
+ fprintf(stderr, " %d empty\n", i);
+ }
+ }
+ }
+}
+
+static VALUE
+hash_verify_(VALUE hash, const char *file, int line)
+{
+ HASH_ASSERT(RB_TYPE_P(hash, T_HASH));
+
+ if (RHASH_ARRAY_P(hash)) {
+ unsigned i, n = 0, bound = RHASH_ARRAY_BOUND(hash);
+
+ for (i=0; ihash;
+ k = cur_entry->key;
+ v = cur_entry->record;
+ HASH_ASSERT(h != 0);
+ HASH_ASSERT(k != Qundef);
+ HASH_ASSERT(v != Qundef);
+ n++;
+ }
+ }
+ if (n != RHASH_ARRAY_SIZE(hash)) {
+ rb_bug("n:%u, RHASH_ARRAY_SIZE:%u", n, RHASH_ARRAY_SIZE(hash));
+ }
+ }
+ else {
+ HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
+ HASH_ASSERT(RHASH_ARRAY_SIZE_RAW(hash) == 0);
+ HASH_ASSERT(RHASH_ARRAY_BOUND_RAW(hash) == 0);
+ }
+
+ if (RHASH_TRANSIENT_P(hash)) {
+ volatile st_data_t MAYBE_UNUSED(key) = RHASH_ARRAY_REF(hash, 0)->key; /* read */
+ HASH_ASSERT(RHASH_ARRAY(hash) != NULL);
+ HASH_ASSERT(rb_transient_heap_managed_ptr_p(RHASH_ARRAY(hash)));
+ }
+ return hash;
+}
+
+#else
+#define hash_verify(h) ((void)0)
+#define HASH_ASSERT(e) ((void)0)
+#endif
+
+static inline int
+RHASH_TABLE_EMPTY(VALUE hash)
+{
+ if (RHASH(hash)->as.li == NULL) {
+ HASH_ASSERT(RHASH_ARRAY_P(hash));
+ return TRUE;
+ }
+ else {
+ return FALSE;
+ }
+}
+
+MJIT_FUNC_EXPORTED int
+rb_hash_array_p(VALUE hash)
+{
+ if (FL_TEST_RAW((hash), RHASH_ST_TABLE_FLAG)) {
+ HASH_ASSERT(RHASH(hash)->as.st != NULL);
+ return FALSE;
+ }
+ else {
+ return TRUE;
+ }
+}
+
+struct li_table *
+rb_hash_array(VALUE hash)
+{
+ HASH_ASSERT(RHASH_ARRAY_P(hash));
+ return RHASH(hash)->as.li;
+}
+
+MJIT_FUNC_EXPORTED st_table *
+rb_hash_st_table(VALUE hash)
+{
+ HASH_ASSERT(!RHASH_ARRAY_P(hash));
+ return RHASH(hash)->as.st;
+}
+
+void
+rb_hash_st_table_set(VALUE hash, st_table *st)
+{
+ HASH_ASSERT(st != NULL);
+ FL_SET_RAW((hash), RHASH_ST_TABLE_FLAG);
+ RHASH(hash)->as.st = st;
+}
+
+static void
+hash_array_set(VALUE hash, struct li_table *li)
+{
+ HASH_ASSERT(RHASH_ARRAY_P(hash));
+ HASH_ASSERT((RHASH_TRANSIENT_P(hash) && li == NULL) ? FALSE : TRUE);
+ RHASH(hash)->as.li = li;
+ hash_verify(hash);
+}
+
+#define RHASH_ARRAY_SET(h, a) hash_array_set(h, a)
+
+#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
+#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
+
+#define RHASH_ARRAY_BOUND_SET(h, n) do { \
+ st_index_t tmp_n = (n); \
+ HASH_ASSERT(RHASH_ARRAY_P(h)); \
+ HASH_ASSERT(tmp_n <= RHASH_ARRAY_MAX_BOUND); \
+ RBASIC(h)->flags &= ~RHASH_ARRAY_BOUND_MASK; \
+ RBASIC(h)->flags |= (tmp_n) << RHASH_ARRAY_BOUND_SHIFT; \
+} while (0)
+
+#define RHASH_ARRAY_SIZE_SET(h, n) do { \
+ st_index_t tmp_n = n; \
+ HASH_ASSERT(RHASH_ARRAY_P(h)); \
+ RBASIC(h)->flags &= ~RHASH_ARRAY_SIZE_MASK; \
+ RBASIC(h)->flags |= (tmp_n) << RHASH_ARRAY_SIZE_SHIFT; \
+} while (0)
+
+#define HASH_ARRAY_SIZE_ADD(h, n) do { \
+ HASH_ASSERT(RHASH_ARRAY_P(h)); \
+ RHASH_ARRAY_SIZE_SET((h), RHASH_ARRAY_SIZE(h)+(n)); \
+ hash_verify(h); \
+} while (0)
+
+#define RHASH_ARRAY_SIZE_INC(h) HASH_ARRAY_SIZE_ADD(h, 1)
+#define RHASH_ARRAY_SIZE_DEC(h) do { \
+ HASH_ASSERT(RHASH_ARRAY_P(h)); \
+ RHASH_ARRAY_SIZE_SET((h), RHASH_ARRAY_SIZE(h) - 1); \
+ hash_verify(h); \
+} while (0)
+
+#define RHASH_CLEAR_BITS(h) do { \
+ RBASIC(h)->flags &= ~RHASH_ARRAY_SIZE_MASK; \
+ RBASIC(h)->flags &= ~RHASH_ARRAY_BOUND_MASK; \
+} while (0)
+
+
+static li_table*
+linear_init_table(VALUE hash)
+{
+ li_table *tab;
+
+ tab = (li_table*)rb_transient_heap_alloc(hash, sizeof(li_table));
+ if (tab != NULL) {
+ RHASH_SET_TRANSIENT_FLAG(hash);
+ }
+ else {
+ RHASH_UNSET_TRANSIENT_FLAG(hash);
+ tab = (li_table*)ruby_xmalloc(sizeof(li_table));
+ }
+
+ RHASH_ARRAY_SIZE_SET(hash, 0);
+ RHASH_ARRAY_BOUND_SET(hash, 0);
+ RHASH_ARRAY_SET(hash, tab);
+ return tab;
+}
+
+static unsigned
+find_entry(VALUE hash, st_hash_t hash_value, st_data_t key)
+{
+ unsigned i, bound = RHASH_ARRAY_BOUND(hash);
+
+ /* if table is NULL, then bound also should be 0 */
+
+ for (i = 0; i < bound; i++) {
+ if (PTR_EQUAL(RHASH_ARRAY_REF(hash, i), hash_value, key)) {
+ return i;
+ }
+ }
+ return RHASH_ARRAY_MAX_BOUND;
+}
+
+static inline void
+linear_free_and_clear_table(VALUE hash)
+{
+ li_table *tab = RHASH_ARRAY(hash);
+
+ if (tab) {
+ if (RHASH_TRANSIENT_P(hash)) {
+ RHASH_UNSET_TRANSIENT_FLAG(hash);
+ }
+ else {
+ ruby_xfree(RHASH_ARRAY(hash));
+ }
+ RHASH_CLEAR_BITS(hash);
+ RHASH_ARRAY_SET(hash, NULL);
+ }
+ HASH_ASSERT(RHASH_ARRAY_SIZE(hash) == 0);
+ HASH_ASSERT(RHASH_ARRAY_BOUND(hash) == 0);
+ HASH_ASSERT(RHASH_TRANSIENT_P(hash) == 0);
+}
+
+void st_add_direct_with_hash(st_table *tab, st_data_t key, st_data_t value, st_hash_t hash); /* st.c */
+
+static void
+linear_try_convert_table(VALUE hash)
+{
+ st_table *new_tab;
+ li_table_entry *entry;
+ const unsigned size = RHASH_ARRAY_SIZE(hash);
+ st_index_t i;
+
+ if (!RHASH_ARRAY_P(hash) || size < RHASH_ARRAY_MAX_SIZE) {
+ return;
+ }
+
+ new_tab = st_init_table_with_size(&objhash, size * 2);
+
+ for (i = 0; i < RHASH_ARRAY_MAX_BOUND; i++) {
+ entry = RHASH_ARRAY_REF(hash, i);
+ HASH_ASSERT(entry->hash != 0);
+
+ st_add_direct_with_hash(new_tab, entry->key, entry->record, entry->hash);
+ }
+ linear_free_and_clear_table(hash);
+ RHASH_ST_TABLE_SET(hash, new_tab);
+ return;
+}
+
+static st_table *
+linear_force_convert_table(VALUE hash, const char *file, int line)
+{
+ st_table *new_tab;
+
+ if (RHASH_TABLE_P(hash)) {
+ return RHASH_ST_TABLE(hash);
+ }
+
+ if (RHASH_ARRAY(hash)) {
+ li_table_entry *entry;
+ unsigned i, bound = RHASH_ARRAY_BOUND(hash);
+
+#if RHASH_CONVERT_TABLE_DEBUG
+ rb_obj_info_dump(hash);
+ fprintf(stderr, "force_convert: %s:%d\n", file, line);
+ RB_DEBUG_COUNTER_INC(obj_hash_force_convert);
+#endif
+
+ new_tab = st_init_table_with_size(&objhash, RHASH_ARRAY_SIZE(hash));
+
+ for (i = 0; i < bound; i++) {
+ entry = RHASH_ARRAY_REF(hash, i);
+ if (empty_entry(entry)) continue;
+
+ st_add_direct_with_hash(new_tab, entry->key, entry->record, entry->hash);
+ }
+ linear_free_and_clear_table(hash);
+ }
+ else {
+ new_tab = st_init_table(&objhash);
+ }
+ RHASH_ST_TABLE_SET(hash, new_tab);
+
+ return new_tab;
+}
+
+static li_table *
+hash_ltbl(VALUE hash)
+{
+ if (RHASH_TABLE_EMPTY(hash)) {
+ linear_init_table(hash);
+ }
+ return RHASH_ARRAY(hash);
+}
+
+static int
+linear_compact_table(VALUE hash)
+{
+ const unsigned bound = RHASH_ARRAY_BOUND(hash);
+ const unsigned size = RHASH_ARRAY_SIZE(hash);
+
+ if (size == bound) {
+ return size;
+ }
+ else {
+ unsigned i, j=0;
+ li_table_entry *entries = RHASH_ARRAY_REF(hash, 0);
+
+ for (i=0; i= RHASH_ARRAY_MAX_SIZE) {
+ return 1;
+ }
+ else {
+ if (UNLIKELY(bin >= RHASH_ARRAY_MAX_BOUND)) {
+ bin = linear_compact_table(hash);
+ hash_ltbl(hash);
+ }
+ HASH_ASSERT(bin < RHASH_ARRAY_MAX_BOUND);
+
+ entry = &tab->entries[bin];
+ set_entry(entry, key, val, hash_value);
+ RHASH_ARRAY_BOUND_SET(hash, bin+1);
+ RHASH_ARRAY_SIZE_INC(hash);
+ return 0;
+ }
+}
+
+static int
+linear_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg)
+{
+ if (RHASH_ARRAY_SIZE(hash) > 0) {
+ unsigned i, bound = RHASH_ARRAY_BOUND(hash);
+
+ for (i = 0; i < bound; i++) {
+ enum st_retval retval;
+ li_table_entry *cur_entry = RHASH_ARRAY_REF(hash, i);
+ if (empty_entry(cur_entry)) continue;
+ retval = (*func)(cur_entry->key, cur_entry->record, arg, 0);
+ /* cur_entry is not valid after that */
+
+ switch (retval) {
+ case ST_CONTINUE:
+ break;
+ case ST_CHECK:
+ case ST_STOP:
+ return 0;
+ case ST_DELETE:
+ clear_entry(RHASH_ARRAY_REF(hash, i));
+ RHASH_ARRAY_SIZE_DEC(hash);
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+linear_foreach_check(VALUE hash, int (*func)(ANYARGS), st_data_t arg,
+ st_data_t never)
+{
+ if (RHASH_ARRAY_SIZE(hash) > 0) {
+ unsigned i, ret = 0, bound = RHASH_ARRAY_BOUND(hash);
+ enum st_retval retval;
+ li_table_entry *cur_entry;
+ st_data_t key;
+ st_hash_t hash_value;
+
+ for (i = 0; i < bound; i++) {
+ cur_entry = RHASH_ARRAY_REF(hash, i);
+ if (empty_entry(cur_entry))
+ continue;
+ key = cur_entry->key;
+ hash_value = cur_entry->hash;
+
+ retval = (*func)(key, cur_entry->record, arg, 0);
+ hash_verify(hash);
+
+ cur_entry = RHASH_ARRAY_REF(hash, i);
+
+ switch (retval) {
+ case ST_CHECK: {
+ if (cur_entry->key == never && cur_entry->hash == 0) break;
+ ret = find_entry(hash, hash_value, key);
+ if (ret == RHASH_ARRAY_MAX_BOUND) {
+ retval = (*func)(0, 0, arg, 1);
+ return 2;
+ }
+ }
+ case ST_CONTINUE:
+ break;
+ case ST_STOP:
+ return 0;
+ case ST_DELETE: {
+ if (!empty_entry(cur_entry)) {
+ clear_entry(cur_entry);
+ RHASH_ARRAY_SIZE_DEC(hash);
+ }
+ break;
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+linear_update(VALUE hash, st_data_t key,
+ st_update_callback_func *func, st_data_t arg)
+{
+ int retval, existing;
+ unsigned bin;
+ st_data_t value = 0, old_key;
+ st_hash_t hash_value = do_hash(key);
+
+ if (RHASH_ARRAY_SIZE(hash) > 0) {
+ bin = find_entry(hash, hash_value, key);
+ existing = (bin != RHASH_ARRAY_MAX_BOUND) ? TRUE : FALSE;
+ }
+ else {
+ hash_ltbl(hash); /* allocate ltbl if needed */
+ existing = FALSE;
+ }
+
+ if (existing) {
+ li_table_entry *entry = RHASH_ARRAY_REF(hash, bin);
+ key = entry->key;
+ value = entry->record;
+ }
+ old_key = key;
+ retval = (*func)(&key, &value, arg, existing);
+
+ switch (retval) {
+ case ST_CONTINUE:
+ if (!existing) {
+ if (linear_add_direct_with_hash(hash, key, value, hash_value)) {
+ return -1;
+ }
+ }
+ else {
+ li_table_entry *entry = RHASH_ARRAY_REF(hash, bin);
+ if (old_key != key) {
+ entry->key = key;
+ }
+ entry->record = value;
+ }
+ break;
+ case ST_DELETE:
+ if (existing) {
+ clear_entry(RHASH_ARRAY_REF(hash, bin));
+ RHASH_ARRAY_SIZE_DEC(hash);
+ }
+ break;
+ }
+ return existing;
+}
+
+static int
+linear_insert(VALUE hash, st_data_t key, st_data_t value)
+{
+ unsigned bin = RHASH_ARRAY_BOUND(hash);
+ st_hash_t hash_value = do_hash(key);
+
+ hash_ltbl(hash); /* prepare ltbl */
+
+ bin = find_entry(hash, hash_value, key);
+ if (bin == RHASH_ARRAY_MAX_BOUND) {
+ if (RHASH_ARRAY_SIZE(hash) >= RHASH_ARRAY_MAX_SIZE) {
+ return -1;
+ }
+ else if (bin >= RHASH_ARRAY_MAX_BOUND) {
+ bin = linear_compact_table(hash);
+ hash_ltbl(hash);
+ }
+ HASH_ASSERT(bin < RHASH_ARRAY_MAX_BOUND);
+
+ set_entry(RHASH_ARRAY_REF(hash, bin), key, value, hash_value);
+ RHASH_ARRAY_BOUND_SET(hash, bin+1);
+ RHASH_ARRAY_SIZE_INC(hash);
+ return 0;
+ }
+ else {
+ RHASH_ARRAY_REF(hash, bin)->record = value;
+ return 1;
+ }
+}
+
+static int
+linear_lookup(VALUE hash, st_data_t key, st_data_t *value)
+{
+ st_hash_t hash_value = do_hash(key);
+ unsigned bin = find_entry(hash, hash_value, key);
+
+ if (bin == RHASH_ARRAY_MAX_BOUND) {
+ return 0;
+ }
+ else {
+ HASH_ASSERT(bin < RHASH_ARRAY_MAX_BOUND);
+ if (value != NULL) {
+ *value = RHASH_ARRAY_REF(hash, bin)->record;
+ }
+ return 1;
+ }
+}
+
+static int
+linear_delete(VALUE hash, st_data_t *key, st_data_t *value)
+{
+ unsigned bin;
+ st_hash_t hash_value = do_hash(*key);
+
+
+ bin = find_entry(hash, hash_value, *key);
+
+ if (bin == RHASH_ARRAY_MAX_BOUND) {
+ if (value != 0) *value = 0;
+ return 0;
+ }
+ else {
+ li_table_entry *entry = RHASH_ARRAY_REF(hash, bin);
+ if (value != 0) *value = entry->record;
+ clear_entry(entry);
+ RHASH_ARRAY_SIZE_DEC(hash);
+ return 1;
+ }
+}
+
+static int
+linear_shift(VALUE hash, st_data_t *key, st_data_t *value)
+{
+ if (RHASH_ARRAY_SIZE(hash) > 0) {
+ unsigned i, bound = RHASH_ARRAY_BOUND(hash);
+ li_table_entry *entry, *entries = RHASH_ARRAY(hash)->entries;
+
+ for (i = 0; i < bound; i++) {
+ entry = &entries[i];
+ if (!empty_entry(entry)) {
+ if (value != 0) *value = entry->record;
+ *key = entry->key;
+ clear_entry(entry);
+ RHASH_ARRAY_SIZE_DEC(hash);
+ return 1;
+ }
+ }
+ }
+ if (value != 0) *value = 0;
+ return 0;
+}
+
+static long
+linear_keys(VALUE hash, st_data_t *keys, st_index_t size)
+{
+ unsigned i, bound = RHASH_ARRAY_BOUND(hash);
+ st_data_t *keys_start = keys, *keys_end = keys + size;
+
+ for (i = 0; i < bound; i++) {
+ if (keys == keys_end) {
+ break;
+ }
+ else {
+ li_table_entry *cur_entry = RHASH_ARRAY_REF(hash, i);
+ if (!empty_entry(cur_entry))
+ *keys++ = cur_entry->key;
+ }
+ }
+
+ return keys - keys_start;
+}
+
+static long
+linear_values(VALUE hash, st_data_t *values, st_index_t size)
+{
+ unsigned i, bound = RHASH_ARRAY_BOUND(hash);
+ st_data_t *values_start = values, *values_end = values + size;
+
+ for (i = 0; i < bound; i++) {
+ if (values == values_end) {
+ break;
+ }
+ else {
+ li_table_entry *cur_entry = RHASH_ARRAY_REF(hash, i);
+ if (!empty_entry(cur_entry))
+ *values++ = cur_entry->record;
+ }
+ }
+
+ return values - values_start;
+}
+
+static li_table*
+linear_copy(VALUE hash1, VALUE hash2)
+{
+ li_table *old_tab = RHASH_ARRAY(hash2);
+
+ if (old_tab != NULL) {
+ li_table *new_tab = RHASH_ARRAY(hash1);
+ if (new_tab == NULL) {
+ new_tab = (li_table*) rb_transient_heap_alloc(hash1, sizeof(li_table));
+ if (new_tab != NULL) {
+ RHASH_SET_TRANSIENT_FLAG(hash1);
+ }
+ else {
+ RHASH_UNSET_TRANSIENT_FLAG(hash1);
+ new_tab = (li_table*)ruby_xmalloc(sizeof(li_table));
+ }
+ }
+ *new_tab = *old_tab;
+ RHASH_ARRAY_BOUND_SET(hash1, RHASH_ARRAY_BOUND(hash2));
+ RHASH_ARRAY_SIZE_SET(hash1, RHASH_ARRAY_SIZE(hash2));
+ RHASH_ARRAY_SET(hash1, new_tab);
+
+ rb_gc_writebarrier_remember(hash1);
+ return new_tab;
+ }
+ else {
+ RHASH_ARRAY_BOUND_SET(hash1, RHASH_ARRAY_BOUND(hash2));
+ RHASH_ARRAY_SIZE_SET(hash1, RHASH_ARRAY_SIZE(hash2));
+
+ if (RHASH_TRANSIENT_P(hash1)) {
+ RHASH_UNSET_TRANSIENT_FLAG(hash1);
+ }
+ else if (RHASH_ARRAY(hash1)) {
+ ruby_xfree(RHASH_ARRAY(hash1));
+ }
+
+ RHASH_ARRAY_SET(hash1, NULL);
+
+ rb_gc_writebarrier_remember(hash1);
+ return old_tab;
+ }
+}
+
+static void
+linear_clear(VALUE hash)
+{
+ if (RHASH_ARRAY(hash) != NULL) {
+ RHASH_ARRAY_SIZE_SET(hash, 0);
+ RHASH_ARRAY_BOUND_SET(hash, 0);
+ }
+ else {
+ HASH_ASSERT(RHASH_ARRAY_SIZE(hash) == 0);
+ HASH_ASSERT(RHASH_ARRAY_BOUND(hash) == 0);
+ }
+}
+
+#if USE_TRANSIENT_HEAP
+void
+rb_hash_transient_heap_evacuate(VALUE hash, int promote)
+{
+ if (RHASH_TRANSIENT_P(hash)) {
+ li_table *new_tab;
+ li_table *old_tab = RHASH_ARRAY(hash);
+
+ if (UNLIKELY(old_tab == NULL)) {
+ rb_gc_force_recycle(hash);
+ return;
+ }
+ HASH_ASSERT(old_tab != NULL);
+ if (promote) {
+ promote:
+ new_tab = ruby_xmalloc(sizeof(li_table));
+ RHASH_UNSET_TRANSIENT_FLAG(hash);
+ }
+ else {
+ new_tab = rb_transient_heap_alloc(hash, sizeof(li_table));
+ if (new_tab == NULL) goto promote;
+ }
+ *new_tab = *old_tab;
+ RHASH_ARRAY_SET(hash, new_tab);
+ }
+ hash_verify(hash);
+}
+#endif
+
typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
struct foreach_safe_arg {
@@ -342,6 +1117,27 @@ struct hash_foreach_arg {
VALUE arg;
};
+static int
+hash_linear_foreach_iter(st_data_t key, st_data_t value, st_data_t argp, int error)
+{
+ struct hash_foreach_arg *arg = (struct hash_foreach_arg *)argp;
+ int status;
+
+ if (error) return ST_STOP;
+ status = (*arg->func)((VALUE)key, (VALUE)value, arg->arg);
+ /* TODO: rehash check? rb_raise(rb_eRuntimeError, "rehash occurred during iteration"); */
+
+ switch (status) {
+ case ST_DELETE:
+ return ST_DELETE;
+ case ST_CONTINUE:
+ break;
+ case ST_STOP:
+ return ST_STOP;
+ }
+ return ST_CHECK;
+}
+
static int
hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp, int error)
{
@@ -350,10 +1146,10 @@ hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp, int error)
st_table *tbl;
if (error) return ST_STOP;
- tbl = RHASH(arg->hash)->ntbl;
+ tbl = RHASH_ST_TABLE(arg->hash);
status = (*arg->func)((VALUE)key, (VALUE)value, arg->arg);
- if (RHASH(arg->hash)->ntbl != tbl) {
- rb_raise(rb_eRuntimeError, "rehash occurred during iteration");
+ if (RHASH_ST_TABLE(arg->hash) != tbl) {
+ rb_raise(rb_eRuntimeError, "rehash occurred during iteration");
}
switch (status) {
case ST_DELETE:
@@ -380,12 +1176,32 @@ hash_foreach_ensure(VALUE hash)
return 0;
}
+int
+rb_hash_stlike_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg)
+{
+ if (RHASH_ARRAY_P(hash)) {
+ return linear_foreach(hash, func, arg);
+ }
+ else {
+ return st_foreach(RHASH_ST_TABLE(hash), func, arg);
+ }
+}
+
static VALUE
hash_foreach_call(VALUE arg)
{
VALUE hash = ((struct hash_foreach_arg *)arg)->hash;
- if (st_foreach_check(RHASH(hash)->ntbl, hash_foreach_iter, (st_data_t)arg, (st_data_t)Qundef)) {
- rb_raise(rb_eRuntimeError, "hash modified during iteration");
+ int ret = 0;
+ if (RHASH_ARRAY_P(hash)) {
+ ret = linear_foreach_check(hash, hash_linear_foreach_iter,
+ (st_data_t)arg, (st_data_t)Qundef);
+ }
+ else if (RHASH_TABLE_P(hash)) {
+ ret = st_foreach_check(RHASH_ST_TABLE(hash), hash_foreach_iter,
+ (st_data_t)arg, (st_data_t)Qundef);
+ }
+ if (ret) {
+ rb_raise(rb_eRuntimeError, "ret: %d, hash modified during iteration", ret);
}
return Qnil;
}
@@ -395,13 +1211,14 @@ rb_hash_foreach(VALUE hash, int (*func)(ANYARGS), VALUE farg)
{
struct hash_foreach_arg arg;
- if (!RHASH(hash)->ntbl)
+ if (RHASH_TABLE_EMPTY(hash))
return;
RHASH_ITER_LEV(hash)++;
arg.hash = hash;
arg.func = (rb_foreach_func *)func;
arg.arg = farg;
rb_ensure(hash_foreach_call, (VALUE)&arg, hash_foreach_ensure, hash);
+ hash_verify(hash);
}
static VALUE
@@ -439,7 +1256,7 @@ VALUE
rb_hash_new_compare_by_id(void)
{
VALUE hash = rb_hash_new();
- RHASH(hash)->ntbl = rb_init_identtable();
+ RHASH_ST_TABLE_SET(hash, rb_init_identtable());
return hash;
}
@@ -447,8 +1264,14 @@ MJIT_FUNC_EXPORTED VALUE
rb_hash_new_with_size(st_index_t size)
{
VALUE ret = rb_hash_new();
- if (size)
- RHASH(ret)->ntbl = st_init_table_with_size(&objhash, size);
+ if (size) {
+ if (size <= RHASH_ARRAY_MAX_SIZE) {
+ RHASH_ARRAY_SET(ret, linear_init_table(ret));
+ }
+ else {
+ RHASH_ST_TABLE_SET(ret, st_init_table_with_size(&objhash, size));
+ }
+ }
return ret;
}
@@ -457,8 +1280,12 @@ hash_dup(VALUE hash, VALUE klass, VALUE flags)
{
VALUE ret = hash_alloc_flags(klass, flags,
RHASH_IFNONE(hash));
- if (!RHASH_EMPTY_P(hash))
- RHASH(ret)->ntbl = st_copy(RHASH(hash)->ntbl);
+ if (!RHASH_EMPTY_P(hash)) {
+ if (RHASH_ARRAY_P(hash))
+ linear_copy(ret, hash);
+ else if (RHASH_TABLE_P(hash))
+ RHASH_ST_TABLE_SET(ret, st_copy(RHASH_ST_TABLE(hash)));
+ }
return ret;
}
@@ -479,33 +1306,30 @@ rb_hash_modify_check(VALUE hash)
rb_check_frozen(hash);
}
-static struct st_table *
-hash_tbl(VALUE hash)
+MJIT_FUNC_EXPORTED struct st_table *
+#if RHASH_CONVERT_TABLE_DEBUG
+rb_hash_tbl_raw(VALUE hash, const char *file, int line)
{
- if (!RHASH(hash)->ntbl) {
- RHASH(hash)->ntbl = st_init_table(&objhash);
- }
- return RHASH(hash)->ntbl;
+ return linear_force_convert_table(hash, file, line);
}
-
-struct st_table *
-rb_hash_tbl(VALUE hash)
+#else
+rb_hash_tbl_raw(VALUE hash)
{
- OBJ_WB_UNPROTECT(hash);
- return hash_tbl(hash);
+ return linear_force_convert_table(hash, NULL, 0);
}
+#endif
-MJIT_FUNC_EXPORTED struct st_table *
-rb_hash_tbl_raw(VALUE hash)
+struct st_table *
+rb_hash_tbl(VALUE hash, const char *file, int line)
{
- return hash_tbl(hash);
+ OBJ_WB_UNPROTECT(hash);
+ return RHASH_TBL_RAW(hash);
}
static void
rb_hash_modify(VALUE hash)
{
rb_hash_modify_check(hash);
- hash_tbl(hash);
}
NORETURN(static void no_new_key(void));
@@ -545,6 +1369,22 @@ struct update_arg {
typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
+int
+rb_hash_stlike_update(VALUE hash, st_data_t key, st_update_callback_func func, st_data_t arg)
+{
+ if (RHASH_ARRAY_P(hash)) {
+ int result = linear_update(hash, (st_data_t)key, func, arg);
+ if (result == -1) {
+ linear_try_convert_table(hash);
+ }
+ else {
+ return result;
+ }
+ }
+
+ return st_update(RHASH_ST_TABLE(hash), (st_data_t)key, func, arg);
+}
+
static int
tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg)
{
@@ -558,7 +1398,7 @@ tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg)
arg.new_value = 0;
arg.old_value = Qundef;
- result = st_update(RHASH(hash)->ntbl, (st_data_t)key, func, (st_data_t)&arg);
+ result = rb_hash_stlike_update(hash, key, func, (st_data_t)&arg);
/* write barrier */
if (arg.new_key) RB_OBJ_WRITTEN(hash, arg.old_key, arg.new_key);
@@ -673,12 +1513,15 @@ rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
VALUE hash, tmp;
if (argc == 1) {
- tmp = rb_hash_s_try_convert(Qnil, argv[0]);
+ tmp = rb_hash_s_try_convert(Qnil, argv[0]);
if (!NIL_P(tmp)) {
hash = hash_alloc(klass);
- if (RHASH(tmp)->ntbl) {
- RHASH(hash)->ntbl = st_copy(RHASH(tmp)->ntbl);
+ if (RHASH_ARRAY_P(tmp)) {
+ linear_copy(hash, tmp);
}
+ else {
+ RHASH_ST_TABLE_SET(hash, st_copy(RHASH_ST_TABLE(tmp)));
+ }
return hash;
}
@@ -725,7 +1568,7 @@ rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
hash = hash_alloc(klass);
rb_hash_bulk_insert(argc, argv, hash);
-
+ hash_verify(hash);
return hash;
}
@@ -767,9 +1610,12 @@ struct rehash_arg {
static int
rb_hash_rehash_i(VALUE key, VALUE value, VALUE arg)
{
- st_table *tbl = (st_table *)arg;
-
- st_insert(tbl, (st_data_t)key, (st_data_t)value);
+ if (RHASH_ARRAY_P(arg)) {
+ linear_insert(arg, (st_data_t)key, (st_data_t)value);
+ }
+ else {
+ st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
+ }
return ST_CONTINUE;
}
@@ -803,17 +1649,25 @@ rb_hash_rehash(VALUE hash)
rb_raise(rb_eRuntimeError, "rehash during iteration");
}
rb_hash_modify_check(hash);
- if (!RHASH(hash)->ntbl)
- return hash;
- tmp = hash_alloc(0);
- tbl = st_init_table_with_size(RHASH(hash)->ntbl->type, RHASH(hash)->ntbl->num_entries);
- RHASH(tmp)->ntbl = tbl;
-
- rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tbl);
- st_free_table(RHASH(hash)->ntbl);
- RHASH(hash)->ntbl = tbl;
- RHASH(tmp)->ntbl = 0;
-
+ if (RHASH_ARRAY_P(hash)) {
+ tmp = hash_alloc(0);
+ linear_init_table(tmp);
+ rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
+ linear_free_and_clear_table(hash);
+ linear_copy(hash, tmp);
+ linear_free_and_clear_table(tmp);
+ }
+ else if (RHASH_TABLE_P(hash)) {
+ st_table *old_tab = RHASH_ST_TABLE(hash);
+ tmp = hash_alloc(0);
+ tbl = st_init_table_with_size(old_tab->type, old_tab->num_entries);
+ RHASH_ST_TABLE_SET(tmp, tbl);
+ rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
+ st_free_table(old_tab);
+ RHASH_ST_TABLE_SET(hash, tbl);
+ RHASH_CLEAR(tmp);
+ }
+ hash_verify(hash);
return hash;
}
@@ -850,10 +1704,27 @@ rb_hash_aref(VALUE hash, VALUE key)
{
st_data_t val;
- if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
- return rb_hash_default_value(hash, key);
+ if (RHASH_ARRAY_P(hash) && linear_lookup(hash, key, &val)) {
+ return (VALUE)val;
+ }
+ else if (RHASH_TABLE_P(hash) && st_lookup(RHASH_ST_TABLE(hash), key, &val)) {
+ return (VALUE)val;
+ }
+ hash_verify(hash);
+ return rb_hash_default_value(hash, key);
+}
+
+MJIT_FUNC_EXPORTED int
+rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval)
+{
+ hash_verify(hash);
+
+ if (RHASH_ARRAY_P(hash)) {
+ return linear_lookup(hash, key, pval);
+ }
+ else {
+ return st_lookup(RHASH_ST_TABLE(hash), key, pval);
}
- return (VALUE)val;
}
VALUE
@@ -861,10 +1732,12 @@ rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
{
st_data_t val;
- if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
- return def; /* without Hash#default */
+ if (rb_hash_stlike_lookup(hash, key, &val)) {
+ return (VALUE)val;
+ }
+ else {
+ return def; /* without Hash#default */
}
- return (VALUE)val;
}
VALUE
@@ -916,19 +1789,23 @@ rb_hash_fetch_m(int argc, VALUE *argv, VALUE hash)
if (block_given && argc == 2) {
rb_warn("block supersedes default value argument");
}
- if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
- if (block_given) return rb_yield(key);
- if (argc == 1) {
- VALUE desc = rb_protect(rb_inspect, key, 0);
- if (NIL_P(desc)) {
- desc = rb_any_to_s(key);
- }
- desc = rb_str_ellipsize(desc, 65);
- rb_key_err_raise(rb_sprintf("key not found: %"PRIsVALUE, desc), hash, key);
+ if (RHASH_ARRAY_P(hash) && linear_lookup(hash, key, &val)) {
+ return (VALUE)val;
+ }
+ else if (RHASH_TABLE_P(hash) && st_lookup(RHASH_ST_TABLE(hash), key, &val)) {
+ return (VALUE)val;
+ }
+ if (block_given) return rb_yield(key);
+ if (argc == 1) {
+ VALUE desc = rb_protect(rb_inspect, key, 0);
+ if (NIL_P(desc)) {
+ desc = rb_any_to_s(key);
}
- return argv[1];
+ desc = rb_str_ellipsize(desc, 65);
+ rb_key_err_raise(rb_sprintf("key not found: %"PRIsVALUE, desc), hash, key);
}
- return (VALUE)val;
+ hash_verify(hash);
+ return argv[1];
}
VALUE
@@ -1107,6 +1984,17 @@ rb_hash_index(VALUE hash, VALUE value)
return rb_hash_key(hash, value);
}
+int
+rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval)
+{
+ if (RHASH_ARRAY_P(hash)) {
+ return linear_delete(hash, pkey, pval);
+ }
+ else {
+ return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
+ }
+}
+
/*
* delete a specified entry a given key.
* if there is the corresponding entry, return a value of the entry.
@@ -1117,14 +2005,11 @@ rb_hash_delete_entry(VALUE hash, VALUE key)
{
st_data_t ktmp = (st_data_t)key, val;
- if (!RHASH(hash)->ntbl) {
- return Qundef;
- }
- else if (st_delete(RHASH(hash)->ntbl, &ktmp, &val)) {
- return (VALUE)val;
+ if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
+ return (VALUE)val;
}
else {
- return Qundef;
+ return Qundef;
}
}
@@ -1219,14 +2104,29 @@ rb_hash_shift(VALUE hash)
struct shift_var var;
rb_hash_modify_check(hash);
- if (RHASH(hash)->ntbl) {
+ if (RHASH_ARRAY_P(hash)) {
var.key = Qundef;
if (RHASH_ITER_LEV(hash) == 0) {
- if (st_shift(RHASH(hash)->ntbl, &var.key, &var.val)) {
+ if (linear_shift(hash, &var.key, &var.val)) {
return rb_assoc_new(var.key, var.val);
}
}
else {
+ rb_hash_foreach(hash, shift_i_safe, (VALUE)&var);
+ if (var.key != Qundef) {
+ rb_hash_delete_entry(hash, var.key);
+ return rb_assoc_new(var.key, var.val);
+ }
+ }
+ }
+ if (RHASH_TABLE_P(hash)) {
+ var.key = Qundef;
+ if (RHASH_ITER_LEV(hash) == 0) {
+ if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
+ return rb_assoc_new(var.key, var.val);
+ }
+ }
+ else {
rb_hash_foreach(hash, shift_i_safe, (VALUE)&var);
if (var.key != Qundef) {
rb_hash_delete_entry(hash, var.key);
@@ -1272,8 +2172,9 @@ rb_hash_delete_if(VALUE hash)
{
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify_check(hash);
- if (RHASH(hash)->ntbl)
- rb_hash_foreach(hash, delete_if_i, hash);
+ if (!RHASH_TABLE_EMPTY(hash)) {
+ rb_hash_foreach(hash, delete_if_i, hash);
+ }
return hash;
}
@@ -1296,7 +2197,7 @@ rb_hash_reject_bang(VALUE hash)
n = RHASH_SIZE(hash);
if (!n) return Qnil;
rb_hash_foreach(hash, delete_if_i, hash);
- if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
+ if (n == RHASH_SIZE(hash)) return Qnil;
return hash;
}
@@ -1438,6 +2339,8 @@ select_i(VALUE key, VALUE value, VALUE result)
* call-seq:
* hsh.select {|key, value| block} -> a_hash
* hsh.select -> an_enumerator
+ * hsh.filter {|key, value| block} -> a_hash
+ * hsh.filter -> an_enumerator
*
* Returns a new hash consisting of entries for which the block returns true.
*
@@ -1446,6 +2349,8 @@ select_i(VALUE key, VALUE value, VALUE result)
* h = { "a" => 100, "b" => 200, "c" => 300 }
* h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
* h.select {|k,v| v < 200} #=> {"a" => 100}
+ *
+ * Hash#filter is an alias for Hash#select.
*/
VALUE
@@ -1474,9 +2379,13 @@ keep_if_i(VALUE key, VALUE value, VALUE hash)
* call-seq:
* hsh.select! {| key, value | block } -> hsh or nil
* hsh.select! -> an_enumerator
+ * hsh.filter! {| key, value | block } -> hsh or nil
+ * hsh.filter! -> an_enumerator
*
- * Equivalent to Hash#keep_if
, but returns
- * nil
if no changes were made.
+ * Equivalent to Hash#keep_if, but returns
+ * +nil+ if no changes were made.
+ *
+ * Hash#filter! is an alias for Hash#select!.
*/
VALUE
@@ -1486,11 +2395,10 @@ rb_hash_select_bang(VALUE hash)
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify_check(hash);
- if (!RHASH(hash)->ntbl)
- return Qnil;
- n = RHASH(hash)->ntbl->num_entries;
+ n = RHASH_SIZE(hash);
+ if (!n) return Qnil;
rb_hash_foreach(hash, keep_if_i, hash);
- if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
+ if (n == RHASH_SIZE(hash)) return Qnil;
return hash;
}
@@ -1500,10 +2408,11 @@ rb_hash_select_bang(VALUE hash)
* hsh.keep_if -> an_enumerator
*
* Deletes every key-value pair from hsh for which block
- * evaluates to false.
+ * evaluates to +false+.
*
* If no block is given, an enumerator is returned instead.
*
+ * See also Hash#select!.
*/
VALUE
@@ -1511,8 +2420,9 @@ rb_hash_keep_if(VALUE hash)
{
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify_check(hash);
- if (RHASH(hash)->ntbl)
- rb_hash_foreach(hash, keep_if_i, hash);
+ if (!RHASH_TABLE_EMPTY(hash)) {
+ rb_hash_foreach(hash, keep_if_i, hash);
+ }
return hash;
}
@@ -1537,13 +2447,15 @@ VALUE
rb_hash_clear(VALUE hash)
{
rb_hash_modify_check(hash);
- if (!RHASH(hash)->ntbl)
- return hash;
- if (RHASH(hash)->ntbl->num_entries > 0) {
- if (RHASH_ITER_LEV(hash) > 0)
- rb_hash_foreach(hash, clear_i, 0);
- else
- st_clear(RHASH(hash)->ntbl);
+
+ if (RHASH_ITER_LEV(hash) > 0) {
+ rb_hash_foreach(hash, clear_i, 0);
+ }
+ else if (RHASH_ARRAY_P(hash)) {
+ linear_clear(hash);
+ }
+ else {
+ st_clear(RHASH_ST_TABLE(hash));
}
return hash;
@@ -1618,14 +2530,15 @@ VALUE
rb_hash_aset(VALUE hash, VALUE key, VALUE val)
{
int iter_lev = RHASH_ITER_LEV(hash);
- st_table *tbl = RHASH(hash)->ntbl;
rb_hash_modify(hash);
- if (!tbl) {
+
+ if (RHASH_TABLE_EMPTY(hash)) {
if (iter_lev > 0) no_new_key();
- tbl = hash_tbl(hash);
+ linear_init_table(hash);
}
- if (tbl->type == &identhash || rb_obj_class(key) != rb_cString) {
+
+ if (RHASH_TYPE(hash) == &identhash || rb_obj_class(key) != rb_cString) {
RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset, val);
}
else {
@@ -1646,8 +2559,6 @@ replace_i(VALUE key, VALUE val, VALUE hash)
static VALUE
rb_hash_initialize_copy(VALUE hash, VALUE hash2)
{
- st_table *ntbl;
-
rb_hash_modify_check(hash);
hash2 = to_hash(hash2);
@@ -1655,15 +2566,23 @@ rb_hash_initialize_copy(VALUE hash, VALUE hash2)
if (hash == hash2) return hash;
- ntbl = RHASH(hash)->ntbl;
- if (RHASH(hash2)->ntbl) {
- if (ntbl) st_free_table(ntbl);
- RHASH(hash)->ntbl = st_copy(RHASH(hash2)->ntbl);
- if (RHASH(hash)->ntbl->num_entries)
+ if (RHASH_ARRAY_P(hash2)) {
+ if (RHASH_ARRAY_P(hash)) linear_free_and_clear_table(hash);
+ linear_copy(hash, hash2);
+ if (RHASH_ARRAY_SIZE(hash))
rb_hash_rehash(hash);
}
- else if (ntbl) {
- st_clear(ntbl);
+ else if (RHASH_TABLE_P(hash2)) {
+ if (RHASH_TABLE_P(hash)) st_free_table(RHASH_ST_TABLE(hash));
+ RHASH_ST_TABLE_SET(hash, st_copy(RHASH_ST_TABLE(hash2)));
+ if (RHASH_ST_TABLE(hash)->num_entries)
+ rb_hash_rehash(hash);
+ }
+ else if (RHASH_ARRAY_P(hash)) {
+ linear_clear(hash);
+ }
+ else if (RHASH_TABLE_P(hash)) {
+ st_clear(RHASH_ST_TABLE(hash));
}
COPY_DEFAULT(hash, hash2);
@@ -1686,19 +2605,28 @@ rb_hash_initialize_copy(VALUE hash, VALUE hash2)
static VALUE
rb_hash_replace(VALUE hash, VALUE hash2)
{
- st_table *table2;
-
rb_hash_modify_check(hash);
if (hash == hash2) return hash;
hash2 = to_hash(hash2);
COPY_DEFAULT(hash, hash2);
- table2 = RHASH(hash2)->ntbl;
-
rb_hash_clear(hash);
- if (table2) hash_tbl(hash)->type = table2->type;
- rb_hash_foreach(hash2, replace_i, hash);
+
+ if (RHASH_ARRAY_P(hash)) {
+ if (RHASH_ARRAY_P(hash2)) {
+ linear_copy(hash, hash2);
+ }
+ else {
+ goto st_to_st;
+ }
+ }
+ else {
+ if (RHASH_ARRAY_P(hash2)) linear_force_convert_table(hash2, __FILE__, __LINE__);
+ st_to_st:
+ RHASH_TBL_RAW(hash)->type = RHASH_ST_TABLE(hash2)->type;
+ rb_hash_foreach(hash2, replace_i, hash);
+ }
return hash;
}
@@ -1725,6 +2653,11 @@ rb_hash_size(VALUE hash)
return INT2FIX(RHASH_SIZE(hash));
}
+size_t
+rb_hash_size_num(VALUE hash)
+{
+ return (long)RHASH_SIZE(hash);
+}
/*
* call-seq:
@@ -1922,7 +2855,7 @@ rb_hash_transform_keys_bang(VALUE hash)
{
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify_check(hash);
- if (RHASH(hash)->ntbl) {
+ if (!RHASH_TABLE_EMPTY(hash)) {
long i;
VALUE pairs = rb_hash_flatten(0, NULL, hash);
rb_hash_clear(hash);
@@ -1996,7 +2929,7 @@ rb_hash_transform_values_bang(VALUE hash)
{
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify_check(hash);
- if (RHASH(hash)->ntbl)
+ if (!RHASH_TABLE_EMPTY(hash))
rb_hash_foreach(hash, transform_values_i, hash);
return hash;
}
@@ -2187,12 +3120,20 @@ rb_hash_keys(VALUE hash)
if (size == 0) return keys;
if (ST_DATA_COMPATIBLE_P(VALUE)) {
- st_table *table = RHASH(hash)->ntbl;
+ if (RHASH_ARRAY_P(hash)) {
+ rb_gc_writebarrier_remember(keys);
+ RARRAY_PTR_USE(keys, ptr, {
+ size = linear_keys(hash, ptr, size);
+ });
+ }
+ else if (RHASH_TABLE_P(hash)) {
+ st_table *table = RHASH_ST_TABLE(hash);
- rb_gc_writebarrier_remember(keys);
- RARRAY_PTR_USE(keys, ptr, {
- size = st_keys(table, ptr, size);
- });
+ rb_gc_writebarrier_remember(keys);
+ RARRAY_PTR_USE(keys, ptr, {
+ size = st_keys(table, ptr, size);
+ });
+ }
rb_ary_set_len(keys, size);
}
else {
@@ -2231,12 +3172,20 @@ rb_hash_values(VALUE hash)
if (size == 0) return values;
if (ST_DATA_COMPATIBLE_P(VALUE)) {
- st_table *table = RHASH(hash)->ntbl;
+ if (RHASH_ARRAY_P(hash)) {
+ rb_gc_writebarrier_remember(values);
+ RARRAY_PTR_USE(values, ptr, {
+ size = linear_values(hash, ptr, size);
+ });
+ }
+ else if (RHASH_TABLE_P(hash)) {
+ st_table *table = RHASH_ST_TABLE(hash);
- rb_gc_writebarrier_remember(values);
- RARRAY_PTR_USE(values, ptr, {
- size = st_values(table, ptr, size);
- });
+ rb_gc_writebarrier_remember(values);
+ RARRAY_PTR_USE(values, ptr, {
+ size = st_values(table, ptr, size);
+ });
+ }
rb_ary_set_len(values, size);
}
else {
@@ -2268,9 +3217,10 @@ rb_hash_values(VALUE hash)
MJIT_FUNC_EXPORTED VALUE
rb_hash_has_key(VALUE hash, VALUE key)
{
- if (!RHASH(hash)->ntbl)
- return Qfalse;
- if (st_lookup(RHASH(hash)->ntbl, key, 0)) {
+ if (RHASH_ARRAY_P(hash) && linear_lookup(hash, key, 0)) {
+ return Qtrue;
+ }
+ else if (RHASH_TABLE_P(hash) && st_lookup(RHASH_ST_TABLE(hash), key, 0)) {
return Qtrue;
}
return Qfalse;
@@ -2314,7 +3264,7 @@ rb_hash_has_value(VALUE hash, VALUE val)
struct equal_data {
VALUE result;
- st_table *tbl;
+ VALUE hash;
int eql;
};
@@ -2324,10 +3274,15 @@ eql_i(VALUE key, VALUE val1, VALUE arg)
struct equal_data *data = (struct equal_data *)arg;
st_data_t val2;
- if (!st_lookup(data->tbl, key, &val2)) {
+ if (RHASH_ARRAY_P(data->hash) && !linear_lookup(data->hash, key, &val2)) {
data->result = Qfalse;
return ST_STOP;
}
+ else if (RHASH_TABLE_P(data->hash) && !st_lookup(RHASH_ST_TABLE(data->hash), key, &val2)) {
+ data->result = Qfalse;
+ return ST_STOP;
+ }
+
if (!(data->eql ? rb_eql(val1, (VALUE)val2) : (int)rb_equal(val1, (VALUE)val2))) {
data->result = Qfalse;
return ST_STOP;
@@ -2372,19 +3327,21 @@ hash_equal(VALUE hash1, VALUE hash2, int eql)
}
if (RHASH_SIZE(hash1) != RHASH_SIZE(hash2))
return Qfalse;
- if (!RHASH(hash1)->ntbl || !RHASH(hash2)->ntbl)
- return Qtrue;
- if (RHASH(hash1)->ntbl->type != RHASH(hash2)->ntbl->type)
- return Qfalse;
+ if (!RHASH_TABLE_EMPTY(hash1) && !RHASH_TABLE_EMPTY(hash2)) {
+ if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2))
+ return Qfalse;
+
+ data.hash = hash2;
+ data.eql = eql;
+ return rb_exec_recursive_paired(recursive_eql, hash1, hash2, (VALUE)&data);
+ }
+
#if 0
if (!(rb_equal(RHASH_IFNONE(hash1), RHASH_IFNONE(hash2)) &&
FL_TEST(hash1, HASH_PROC_DEFAULT) == FL_TEST(hash2, HASH_PROC_DEFAULT)))
return Qfalse;
#endif
-
- data.tbl = RHASH(hash2)->ntbl;
- data.eql = eql;
- return rb_exec_recursive_paired(recursive_eql, hash1, hash2, (VALUE)&data);
+ return Qtrue;
}
/*
@@ -2739,7 +3696,8 @@ static VALUE
reset_hash_type(VALUE arg)
{
struct reset_hash_type_arg *p = (struct reset_hash_type_arg *)arg;
- RHASH(p->hash)->ntbl->type = p->orighash;
+ HASH_ASSERT(RHASH_TABLE_P(p->hash));
+ RHASH_ST_TABLE(p->hash)->type = p->orighash;
return Qundef;
}
@@ -2777,7 +3735,10 @@ rb_hash_assoc(VALUE hash, VALUE key)
VALUE args[2];
if (RHASH_EMPTY_P(hash)) return Qnil;
- table = RHASH(hash)->ntbl;
+
+ linear_force_convert_table(hash, __FILE__, __LINE__);
+ HASH_ASSERT(RHASH_TABLE_P(hash));
+ table = RHASH_ST_TABLE(hash);
orighash = table->type;
if (orighash != &identhash) {
@@ -2787,7 +3748,7 @@ rb_hash_assoc(VALUE hash, VALUE key)
assochash.compare = assoc_cmp;
assochash.hash = orighash->hash;
- table->type = &assochash;
+ table->type = &assochash;
args[0] = hash;
args[1] = key;
ensure_arg.hash = hash;
@@ -2954,11 +3915,12 @@ rb_hash_compact(VALUE hash)
static VALUE
rb_hash_compact_bang(VALUE hash)
{
+ st_index_t n;
rb_hash_modify_check(hash);
- if (RHASH(hash)->ntbl) {
- st_index_t n = RHASH(hash)->ntbl->num_entries;
+ n = RHASH_SIZE(hash);
+ if (n) {
rb_hash_foreach(hash, delete_if_nil, hash);
- if (n != RHASH(hash)->ntbl->num_entries)
+ if (n != RHASH_SIZE(hash))
return hash;
}
return Qnil;
@@ -2983,15 +3945,23 @@ rb_hash_compact_bang(VALUE hash)
static VALUE
rb_hash_compare_by_id(VALUE hash)
{
+ VALUE tmp;
st_table *identtable;
+
if (rb_hash_compare_by_id_p(hash)) return hash;
+
rb_hash_modify_check(hash);
+ linear_force_convert_table(hash, __FILE__, __LINE__);
+ HASH_ASSERT(RHASH_TABLE_P(hash));
+ tmp = hash_alloc(0);
identtable = rb_init_identtable_with_size(RHASH_SIZE(hash));
- rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)identtable);
- if (RHASH(hash)->ntbl)
- st_free_table(RHASH(hash)->ntbl);
- RHASH(hash)->ntbl = identtable;
+ RHASH_ST_TABLE_SET(tmp, identtable);
+ rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
+ st_free_table(RHASH_ST_TABLE(hash));
+ RHASH_ST_TABLE_SET(hash, identtable);
+ RHASH_CLEAR(tmp);
+ rb_gc_force_recycle(tmp);
return hash;
}
@@ -3008,19 +3978,19 @@ rb_hash_compare_by_id(VALUE hash)
MJIT_FUNC_EXPORTED VALUE
rb_hash_compare_by_id_p(VALUE hash)
{
- if (!RHASH(hash)->ntbl)
- return Qfalse;
- if (RHASH(hash)->ntbl->type == &identhash) {
+ if (RHASH_TABLE_P(hash) && RHASH_ST_TABLE(hash)->type == &identhash) {
return Qtrue;
}
- return Qfalse;
+ else {
+ return Qfalse;
+ }
}
VALUE
rb_ident_hash_new(void)
{
VALUE hash = rb_hash_new();
- RHASH(hash)->ntbl = st_init_table(&identhash);
+ RHASH_ST_TABLE_SET(hash, st_init_table(&identhash));
return hash;
}
@@ -3282,11 +4252,68 @@ add_new_i(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
int
rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val)
{
- st_table *tbl = rb_hash_tbl_raw(hash);
+ st_table *tbl;
+ int ret = 0;
VALUE args[2];
args[0] = hash;
args[1] = val;
+
+ if (RHASH_ARRAY_P(hash)) {
+ hash_ltbl(hash);
+
+ ret = linear_update(hash, (st_data_t)key, add_new_i, (st_data_t)args);
+ if (ret != -1) {
+ return ret;
+ }
+ linear_try_convert_table(hash);
+ }
+ tbl = RHASH_TBL_RAW(hash);
return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args);
+
+}
+
+static st_data_t
+key_stringify(VALUE key)
+{
+ return (rb_obj_class(key) == rb_cString && !RB_OBJ_FROZEN(key)) ?
+ rb_hash_key_str(key) : key;
+}
+
+static void
+linear_bulk_insert(VALUE hash, long argc, const VALUE *argv)
+{
+ long i;
+ for (i = 0; i < argc; ) {
+ st_data_t k = key_stringify(argv[i++]);
+ st_data_t v = argv[i++];
+ linear_insert(hash, k, v);
+ RB_OBJ_WRITTEN(hash, Qundef, k);
+ RB_OBJ_WRITTEN(hash, Qundef, v);
+ }
+}
+
+MJIT_FUNC_EXPORTED void
+rb_hash_bulk_insert(long argc, const VALUE *argv, VALUE hash)
+{
+ st_index_t size;
+
+ HASH_ASSERT(argc % 2 == 0);
+ if (! argc)
+ return;
+ size = argc / 2;
+ if (RHASH_TABLE_EMPTY(hash)) {
+ if (size <= RHASH_ARRAY_MAX_SIZE)
+ hash_ltbl(hash);
+ else
+ RHASH_TBL_RAW(hash);
+ }
+ if (RHASH_ARRAY_P(hash) &&
+ (RHASH_ARRAY_SIZE(hash) + size <= RHASH_ARRAY_MAX_SIZE)) {
+ linear_bulk_insert(hash, argc, argv);
+ return;
+ }
+
+ rb_hash_bulk_insert_into_st_table(argc, argv, hash);
}
static int path_tainted = -1;
@@ -3468,8 +4495,8 @@ env_delete(VALUE obj, VALUE name)
/*
* call-seq:
- * ENV.delete(name) -> value
- * ENV.delete(name) { |name| } -> value
+ * ENV.delete(name) -> value
+ * ENV.delete(name) { |name| block } -> value
*
* Deletes the environment variable with +name+ and returns the value of the
* variable. If a block is given it will be called when the named environment
@@ -3508,9 +4535,9 @@ rb_f_getenv(VALUE obj, VALUE name)
/*
* :yield: missing_name
* call-seq:
- * ENV.fetch(name) -> value
- * ENV.fetch(name, default) -> value
- * ENV.fetch(name) { |missing_name| ... } -> value
+ * ENV.fetch(name) -> value
+ * ENV.fetch(name, default) -> value
+ * ENV.fetch(name) { |missing_name| block } -> value
*
* Retrieves the environment variable +name+.
*
@@ -3887,8 +4914,8 @@ rb_env_size(VALUE ehash, VALUE args, VALUE eobj)
/*
* call-seq:
- * ENV.each_key { |name| } -> Hash
- * ENV.each_key -> Enumerator
+ * ENV.each_key { |name| block } -> Hash
+ * ENV.each_key -> Enumerator
*
* Yields each environment variable name.
*
@@ -3935,8 +4962,8 @@ env_values(void)
/*
* call-seq:
- * ENV.each_value { |value| } -> Hash
- * ENV.each_value -> Enumerator
+ * ENV.each_value { |value| block } -> Hash
+ * ENV.each_value -> Enumerator
*
* Yields each environment variable +value+.
*
@@ -3958,10 +4985,10 @@ env_each_value(VALUE ehash)
/*
* call-seq:
- * ENV.each { |name, value| } -> Hash
- * ENV.each -> Enumerator
- * ENV.each_pair { |name, value| } -> Hash
- * ENV.each_pair -> Enumerator
+ * ENV.each { |name, value| block } -> Hash
+ * ENV.each -> Enumerator
+ * ENV.each_pair { |name, value| block } -> Hash
+ * ENV.each_pair -> Enumerator
*
* Yields each environment variable +name+ and +value+.
*
@@ -4003,10 +5030,10 @@ env_each_pair(VALUE ehash)
/*
* call-seq:
- * ENV.reject! { |name, value| } -> ENV or nil
- * ENV.reject! -> Enumerator
+ * ENV.reject! { |name, value| block } -> ENV or nil
+ * ENV.reject! -> Enumerator
*
- * Equivalent to ENV#delete_if but returns +nil+ if no changes were made.
+ * Equivalent to ENV.delete_if but returns +nil+ if no changes were made.
*
* Returns an Enumerator if no block was given.
*/
@@ -4037,8 +5064,8 @@ env_reject_bang(VALUE ehash)
/*
* call-seq:
- * ENV.delete_if { |name, value| } -> Hash
- * ENV.delete_if -> Enumerator
+ * ENV.delete_if { |name, value| block } -> Hash
+ * ENV.delete_if -> Enumerator
*
* Deletes every environment variable for which the block evaluates to +true+.
*
@@ -4074,12 +5101,16 @@ env_values_at(int argc, VALUE *argv)
/*
* call-seq:
- * ENV.select { |name, value| } -> Hash
- * ENV.select -> Enumerator
+ * ENV.select { |name, value| block } -> Hash
+ * ENV.select -> Enumerator
+ * ENV.filter { |name, value| block } -> Hash
+ * ENV.filter -> Enumerator
*
* Returns a copy of the environment for entries where the block returns true.
*
* Returns an Enumerator if no block was given.
+ *
+ * ENV.filter is an alias for ENV.select.
*/
static VALUE
env_select(VALUE ehash)
@@ -4107,10 +5138,14 @@ env_select(VALUE ehash)
/*
* call-seq:
- * ENV.select! { |name, value| } -> ENV or nil
- * ENV.select! -> Enumerator
+ * ENV.select! { |name, value| block } -> ENV or nil
+ * ENV.select! -> Enumerator
+ * ENV.filter! { |name, value| block } -> ENV or nil
+ * ENV.filter! -> Enumerator
+ *
+ * Equivalent to ENV.keep_if but returns +nil+ if no changes were made.
*
- * Equivalent to ENV#keep_if but returns +nil+ if no changes were made.
+ * ENV.filter! is an alias for ENV.select!.
*/
static VALUE
env_select_bang(VALUE ehash)
@@ -4139,8 +5174,8 @@ env_select_bang(VALUE ehash)
/*
* call-seq:
- * ENV.keep_if { |name, value| } -> Hash
- * ENV.keep_if -> Enumerator
+ * ENV.keep_if { |name, value| block } -> Hash
+ * ENV.keep_if -> Enumerator
*
* Deletes every environment variable where the block evaluates to +false+.
*
@@ -4527,10 +5562,10 @@ env_to_h(void)
/*
* call-seq:
- * ENV.reject { |name, value| } -> Hash
- * ENV.reject -> Enumerator
+ * ENV.reject { |name, value| block } -> Hash
+ * ENV.reject -> Enumerator
*
- * Same as ENV#delete_if, but works on (and returns) a copy of the
+ * Same as ENV.delete_if, but works on (and returns) a copy of the
* environment.
*/
static VALUE
@@ -4626,8 +5661,8 @@ env_update_i(VALUE key, VALUE val)
/*
* call-seq:
- * ENV.update(hash) -> Hash
- * ENV.update(hash) { |name, old_value, new_value| } -> Hash
+ * ENV.update(hash) -> Hash
+ * ENV.update(hash) { |name, old_value, new_value| block } -> Hash
*
* Adds the contents of +hash+ to the environment variables. If no block is
* specified entries with duplicate keys are overwritten, otherwise the value
diff --git a/ia64.s b/ia64.S
similarity index 100%
rename from ia64.s
rename to ia64.S
diff --git a/include/ruby/intern.h b/include/ruby/intern.h
index 11a97cb6a21993..d60f370a6da95e 100644
--- a/include/ruby/intern.h
+++ b/include/ruby/intern.h
@@ -520,11 +520,12 @@ VALUE rb_hash_delete(VALUE,VALUE);
VALUE rb_hash_set_ifnone(VALUE hash, VALUE ifnone);
typedef VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value);
VALUE rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func);
-struct st_table *rb_hash_tbl(VALUE);
+struct st_table *rb_hash_tbl(VALUE, const char *file, int line);
int rb_path_check(const char*);
int rb_env_path_tainted(void);
VALUE rb_env_clear(void);
VALUE rb_hash_size(VALUE);
+void rb_hash_free(VALUE);
/* io.c */
#define rb_defout rb_stdout
RUBY_EXTERN VALUE rb_fs;
diff --git a/include/ruby/io.h b/include/ruby/io.h
index 6cacd8a7102575..7caca17e3b8578 100644
--- a/include/ruby/io.h
+++ b/include/ruby/io.h
@@ -24,7 +24,6 @@ extern "C" {
#endif
#include
-#include
#include "ruby/encoding.h"
#if defined(HAVE_STDIO_EXT_H)
@@ -32,6 +31,7 @@ extern "C" {
#endif
#include "ruby/config.h"
+#include
#if defined(HAVE_POLL)
# ifdef _AIX
# define reqevents events
diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h
index a05651ac3bad05..58e28d2c9f80a4 100644
--- a/include/ruby/ruby.h
+++ b/include/ruby/ruby.h
@@ -1013,6 +1013,10 @@ struct RString {
((ptrvar) = RSTRING(str)->as.ary, (lenvar) = RSTRING_EMBED_LEN(str)) : \
((ptrvar) = RSTRING(str)->as.heap.ptr, (lenvar) = RSTRING(str)->as.heap.len))
+#ifndef USE_TRANSIENT_HEAP
+#define USE_TRANSIENT_HEAP 1
+#endif
+
enum ruby_rarray_flags {
RARRAY_EMBED_LEN_MAX = 3,
RARRAY_EMBED_FLAG = RUBY_FL_USER1,
@@ -1020,12 +1024,20 @@ enum ruby_rarray_flags {
RARRAY_EMBED_LEN_MASK = (RUBY_FL_USER4|RUBY_FL_USER3),
RARRAY_EMBED_LEN_SHIFT = (RUBY_FL_USHIFT+3),
+#if USE_TRANSIENT_HEAP
+ RARRAY_TRANSIENT_FLAG = RUBY_FL_USER13,
+#define RARRAY_TRANSIENT_FLAG RARRAY_TRANSIENT_FLAG
+#else
+#define RARRAY_TRANSIENT_FLAG 0
+#endif
+
RARRAY_ENUM_END
};
#define RARRAY_EMBED_FLAG (VALUE)RARRAY_EMBED_FLAG
#define RARRAY_EMBED_LEN_MASK (VALUE)RARRAY_EMBED_LEN_MASK
#define RARRAY_EMBED_LEN_MAX RARRAY_EMBED_LEN_MAX
#define RARRAY_EMBED_LEN_SHIFT RARRAY_EMBED_LEN_SHIFT
+
struct RArray {
struct RBasic basic;
union {
@@ -1046,9 +1058,19 @@ struct RArray {
#define RARRAY_LEN(a) rb_array_len(a)
#define RARRAY_LENINT(ary) rb_long2int(RARRAY_LEN(ary))
#define RARRAY_CONST_PTR(a) rb_array_const_ptr(a)
+#define RARRAY_CONST_PTR_TRANSIENT(a) rb_array_const_ptr_transient(a)
-#define RARRAY_PTR_USE_START(a) ((VALUE *)RARRAY_CONST_PTR(a))
-#define RARRAY_PTR_USE_END(a) /* */
+#if USE_TRANSIENT_HEAP
+#define RARRAY_TRANSIENT_P(ary) FL_TEST_RAW((ary), RARRAY_TRANSIENT_FLAG)
+#else
+#define RARRAY_TRANSIENT_P(ary) 0
+#endif
+
+VALUE *rb_ary_ptr_use_start(VALUE ary);
+void rb_ary_ptr_use_end(VALUE ary);
+
+#define RARRAY_PTR_USE_START(a) rb_ary_ptr_use_start(a)
+#define RARRAY_PTR_USE_END(a) rb_ary_ptr_use_end(a)
#define RARRAY_PTR_USE(ary, ptr_name, expr) do { \
const VALUE _ary = (ary); \
@@ -1057,11 +1079,12 @@ struct RArray {
RARRAY_PTR_USE_END(_ary); \
} while (0)
-#define RARRAY_AREF(a, i) (RARRAY_CONST_PTR(a)[i])
+#define RARRAY_AREF(a, i) (RARRAY_CONST_PTR_TRANSIENT(a)[i])
#define RARRAY_ASET(a, i, v) do { \
const VALUE _ary = (a); \
+ const VALUE _v = (v); \
VALUE *ptr = (VALUE *)RARRAY_PTR_USE_START(_ary); \
- RB_OBJ_WRITE(_ary, &ptr[i], (v)); \
+ RB_OBJ_WRITE(_ary, &ptr[i], _v); \
RARRAY_PTR_USE_END(_ary); \
} while (0)
@@ -1079,11 +1102,13 @@ struct RRegexp {
#define RREGEXP_SRC_LEN(r) RSTRING_LEN(RREGEXP(r)->src)
#define RREGEXP_SRC_END(r) RSTRING_END(RREGEXP(r)->src)
-/* RHASH_TBL allocates st_table if not available. */
-#define RHASH_TBL(h) rb_hash_tbl(h)
+/* RHash is defined at internal.h */
+size_t rb_hash_size_num(VALUE hash);
+
+#define RHASH_TBL(h) rb_hash_tbl(h, __FILE__, __LINE__)
#define RHASH_ITER_LEV(h) rb_hash_iter_lev(h)
#define RHASH_IFNONE(h) rb_hash_ifnone(h)
-#define RHASH_SIZE(h) NUM2SIZET(rb_hash_size(h))
+#define RHASH_SIZE(h) rb_hash_size_num(h)
#define RHASH_EMPTY_P(h) (RHASH_SIZE(h) == 0)
#define RHASH_SET_IFNONE(h, ifnone) rb_hash_set_ifnone((VALUE)h, ifnone)
@@ -1689,11 +1714,11 @@ rb_alloc_tmp_buffer2(volatile VALUE *store, long count, size_t elsize)
#else
# define RUBY_ALLOCV_LIMIT 1024
# define RB_ALLOCV(v, n) ((n) < RUBY_ALLOCV_LIMIT ? \
- (RB_GC_GUARD(v) = 0, alloca(n)) : \
+ ((v) = 0, alloca(n)) : \
rb_alloc_tmp_buffer(&(v), (n)))
# define RB_ALLOCV_N(type, v, n) \
((type*)(((size_t)(n) < RUBY_ALLOCV_LIMIT / sizeof(type)) ? \
- (RB_GC_GUARD(v) = 0, alloca((size_t)(n) * sizeof(type))) : \
+ ((v) = 0, alloca((size_t)(n) * sizeof(type))) : \
rb_alloc_tmp_buffer2(&(v), (long)(n), sizeof(type))))
#endif
#define RB_ALLOCV_END(v) rb_free_tmp_buffer(&(v))
@@ -2110,12 +2135,25 @@ rb_array_len(VALUE a)
#endif
static inline const VALUE *
-rb_array_const_ptr(VALUE a)
+rb_array_const_ptr_transient(VALUE a)
{
return FIX_CONST_VALUE_PTR((RBASIC(a)->flags & RARRAY_EMBED_FLAG) ?
RARRAY(a)->as.ary : RARRAY(a)->as.heap.ptr);
}
+static inline const VALUE *
+rb_array_const_ptr(VALUE a)
+{
+#if USE_TRANSIENT_HEAP
+ void rb_ary_detransient(VALUE a);
+
+ if (RARRAY_TRANSIENT_P(a)) {
+ rb_ary_detransient(a);
+ }
+#endif
+ return rb_array_const_ptr_transient(a);
+}
+
#if defined(EXTLIB) && defined(USE_DLN_A_OUT)
/* hook for external modules */
static char *dln_libs_to_be_linked[] = { EXTLIB, 0 };
@@ -2278,10 +2316,10 @@ ERRORFUNC(("variable argument length doesn't match"), int rb_scan_args_length_mi
rb_scan_args_length_mismatch(fmt, varc)))
# if defined(__has_attribute) && __has_attribute(diagnose_if)
-# define rb_scan_args_verify(fmt, varc) 0
+# define rb_scan_args_verify(fmt, varc) (void)0
# elif defined(__GNUC__)
# define rb_scan_args_verify(fmt, varc) \
- __extension__ ({ \
+ (void)__extension__ ({ \
int verify; \
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Warray-bounds\""); \
@@ -2291,7 +2329,7 @@ ERRORFUNC(("variable argument length doesn't match"), int rb_scan_args_length_mi
})
# else
# define rb_scan_args_verify(fmt, varc) \
- rb_scan_args_verify_count(fmt, varc)
+ (void)rb_scan_args_verify_count(fmt, varc)
# endif
ALWAYS_INLINE(static int rb_scan_args_lead_p(const char *fmt));
diff --git a/include/ruby/st.h b/include/ruby/st.h
index ede3ff44567fc0..149e0ebaef3945 100644
--- a/include/ruby/st.h
+++ b/include/ruby/st.h
@@ -143,7 +143,7 @@ CONSTFUNC(st_index_t st_hash_end(st_index_t h));
CONSTFUNC(st_index_t st_hash_start(st_index_t h));
#define st_hash_start(h) ((st_index_t)(h))
-void rb_hash_bulk_insert(long, const VALUE *, VALUE);
+void rb_hash_bulk_insert_into_st_table(long, const VALUE *, VALUE);
RUBY_SYMBOL_EXPORT_END
diff --git a/inits.c b/inits.c
index c9687de516bd52..f730903b5ede75 100644
--- a/inits.c
+++ b/inits.c
@@ -16,6 +16,10 @@
void
rb_call_inits(void)
{
+#if USE_TRANSIENT_HEAP
+ CALL(TransientHeap);
+#endif
+ CALL(vm_postponed_job);
CALL(Method);
CALL(RandomSeedCore);
CALL(sym);
diff --git a/insns.def b/insns.def
index 132ce2f179601b..efd5bb74a268f3 100644
--- a/insns.def
+++ b/insns.def
@@ -380,7 +380,7 @@ concatstrings
(rb_num_t num)
(...)
(VALUE val)
-// attr rb_snum_t sp_inc = 1 - num;
+// attr rb_snum_t sp_inc = 1 - (rb_snum_t)num;
{
val = rb_str_concat_literals(num, STACK_ADDR_FROM_TOP(num));
}
@@ -416,7 +416,7 @@ toregexp
/* This instruction has StringValue(), which is a method call. But it
* seems that path is never covered. */
// attr bool leaf = true; /* yes it is */
-// attr rb_snum_t sp_inc = 1 - cnt;
+// attr rb_snum_t sp_inc = 1 - (rb_snum_t)cnt;
{
const VALUE ary = rb_ary_tmp_new_from_values(0, cnt, STACK_ADDR_FROM_TOP(cnt));
val = rb_reg_new_ary(ary, (int)opt);
@@ -439,7 +439,7 @@ newarray
(rb_num_t num)
(...)
(VALUE val)
-// attr rb_snum_t sp_inc = 1 - num;
+// attr rb_snum_t sp_inc = 1 - (rb_snum_t)num;
{
val = rb_ary_new4(num, STACK_ADDR_FROM_TOP(num));
}
@@ -469,7 +469,7 @@ expandarray
(..., VALUE ary)
(...)
// attr bool leaf = false; /* has rb_check_array_type() */
-// attr rb_snum_t sp_inc = num - 1 + (flag & 1 ? 1 : 0);
+// attr rb_snum_t sp_inc = (rb_snum_t)num - 1 + (flag & 1 ? 1 : 0);
{
vm_expandarray(GET_SP(), ary, num, (int)flag);
}
@@ -503,7 +503,7 @@ newhash
(...)
(VALUE val)
// attr bool leaf = false; /* has rb_hash_key_str() */
-// attr rb_snum_t sp_inc = 1 - num;
+// attr rb_snum_t sp_inc = 1 - (rb_snum_t)num;
{
RUBY_DTRACE_CREATE_HOOK(HASH, num);
@@ -524,7 +524,7 @@ newhashfromarray
{
VM_ASSERT(num * 2 == (rb_num_t)RARRAY_LEN(ary));
hash = rb_hash_new_with_size(num);
- rb_hash_bulk_insert(num * 2, RARRAY_CONST_PTR(ary), hash);
+ rb_hash_bulk_insert(num * 2, RARRAY_CONST_PTR_TRANSIENT(ary), hash);
}
/* put new Range object.(Range.new(low, high, flag)) */
@@ -804,7 +804,7 @@ opt_newarray_max
* necessary. No way to detect such method calls beforehand. We
* cannot but mark it being not leaf. */
// attr bool leaf = false; /* has rb_funcall() */
-// attr rb_snum_t sp_inc = 1 - num;
+// attr rb_snum_t sp_inc = 1 - (rb_snum_t)num;
{
val = vm_opt_newarray_max(num, STACK_ADDR_FROM_TOP(num));
}
@@ -816,7 +816,7 @@ opt_newarray_min
(VALUE val)
/* Same discussion as opt_newarray_max. */
// attr bool leaf = false; /* has rb_funcall() */
-// attr rb_snum_t sp_inc = 1 - num;
+// attr rb_snum_t sp_inc = 1 - (rb_snum_t)num;
{
val = vm_opt_newarray_min(num, STACK_ADDR_FROM_TOP(num));
}
@@ -985,7 +985,7 @@ branchnil
/* push inline-cached value and go to dst if it is valid */
DEFINE_INSN
-getinlinecache
+opt_getinlinecache
(OFFSET dst, IC ic)
()
(VALUE val)
@@ -1001,7 +1001,7 @@ getinlinecache
/* set inline cache */
DEFINE_INSN
-setinlinecache
+opt_setinlinecache
(IC ic)
(VALUE val)
(VALUE val)
diff --git a/internal.h b/internal.h
index 697a1196fa4e65..8f69c6f1dab245 100644
--- a/internal.h
+++ b/internal.h
@@ -54,6 +54,34 @@ extern "C" {
# define WARN_UNUSED_RESULT(x) x
#endif
+#if 0
+#elif defined(NO_SANITIZE)
+# define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(x) \
+ NO_SANITIZE("address", NOINLINE(x))
+#elif defined(NO_SANITIZE_ADDRESS)
+# define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(x) \
+ NO_SANITIZE_ADDRESS(NOINLINE(x))
+#elif defined(NO_ADDRESS_SAFETY_ANALYSIS)
+# define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(x) \
+ NO_ADDRESS_SAFETY_ANALYSIS(NOINLINE(x))
+#else
+# define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(x) x
+#endif
+
+#if defined(NO_SANITIZE) && defined(__GNUC__) &&! defined(__clang__)
+/* GCC warns about unknown sanitizer, which is annoying. */
+#undef NO_SANITIZE
+#define NO_SANITIZE(x, y) \
+ COMPILER_WARNING_PUSH; \
+ COMPILER_WARNING_IGNORED(-Wattributes); \
+ __attribute__((__no_sanitize__(x))) y; \
+ COMPILER_WARNING_POP
+#endif
+
+#ifndef NO_SANITIZE
+# define NO_SANITIZE(x, y) y
+#endif
+
#ifdef HAVE_VALGRIND_MEMCHECK_H
# include
# ifndef VALGRIND_MAKE_MEM_DEFINED
@@ -77,6 +105,64 @@ extern "C" {
# define __has_extension __has_feature
#endif
+#ifndef MJIT_HEADER
+
+#ifdef HAVE_SANITIZER_ASAN_INTERFACE_H
+# include
+#endif
+
+#if !__has_feature(address_sanitizer)
+# define __asan_poison_memory_region(x, y)
+# define __asan_unpoison_memory_region(x, y)
+# define __asan_region_is_poisoned(x, y) 0
+#endif
+
+#ifdef HAVE_SANITIZER_MSAN_INTERFACE_H
+# include
+#endif
+
+#if !__has_feature(memory_sanitizer)
+# define __msan_allocated_memory(x, y)
+# define __msan_poison(x, y)
+# define __msan_unpoison(x, y)
+# define __msan_unpoison_string(x)
+#endif
+
+static inline void
+poison_memory_region(const volatile void *ptr, size_t size)
+{
+ __msan_poison(ptr, size);
+ __asan_poison_memory_region(ptr, size);
+}
+
+static inline void
+poison_object(VALUE obj)
+{
+ struct RVALUE *ptr = (void *)obj;
+ poison_memory_region(ptr, SIZEOF_VALUE);
+}
+
+static inline void
+unpoison_memory_region(const volatile void *ptr, size_t size, bool malloc_p)
+{
+ __asan_unpoison_memory_region(ptr, size);
+ if (malloc_p) {
+ __msan_allocated_memory(ptr, size);
+ }
+ else {
+ __msan_unpoison(ptr, size);
+ }
+}
+
+static inline void
+unpoison_object(VALUE obj, bool newobj_p)
+{
+ struct RVALUE *ptr = (void *)obj;
+ unpoison_memory_region(ptr, SIZEOF_VALUE, newobj_p);
+}
+
+#endif
+
/* Prevent compiler from reordering access */
#define ACCESS_ONCE(type,x) (*((volatile type *)&(x)))
@@ -672,23 +758,99 @@ struct RComplex {
#define RCOMPLEX_SET_REAL(cmp, r) RB_OBJ_WRITE((cmp), &((struct RComplex *)(cmp))->real,(r))
#define RCOMPLEX_SET_IMAG(cmp, i) RB_OBJ_WRITE((cmp), &((struct RComplex *)(cmp))->imag,(i))
+enum ruby_rhash_flags {
+ RHASH_ST_TABLE_FLAG = FL_USER3,
+ RHASH_ARRAY_MAX_SIZE = 8,
+ RHASH_ARRAY_SIZE_MASK = (FL_USER4|FL_USER5|FL_USER6|FL_USER7),
+ RHASH_ARRAY_SIZE_SHIFT = (FL_USHIFT+4),
+ RHASH_ARRAY_BOUND_MASK = (FL_USER8|FL_USER9|FL_USER10|FL_USER11),
+ RHASH_ARRAY_BOUND_SHIFT = (FL_USHIFT+8),
+
+ RHASH_ENUM_END
+};
+
+#define HASH_PROC_DEFAULT FL_USER2
+
+#define RHASH_ARRAY_SIZE_RAW(h) \
+ ((unsigned int)((RBASIC(h)->flags & RHASH_ARRAY_SIZE_MASK) >> RHASH_ARRAY_SIZE_SHIFT))
+
+int rb_hash_array_p(VALUE hash);
+struct li_table *rb_hash_array(VALUE hash);
+st_table *rb_hash_st_table(VALUE hash);
+void rb_hash_st_table_set(VALUE hash, st_table *st);
+
+#if 0 /* for debug */
+#define RHASH_ARRAY_P(hash) rb_hash_array_p(hash)
+#define RHASH_ARRAY(h) rb_hash_array(h)
+#define RHASH_ST_TABLE(h) rb_hash_st_table(h)
+#else
+#define RHASH_ARRAY_P(hash) (!FL_TEST_RAW((hash), RHASH_ST_TABLE_FLAG))
+#define RHASH_ARRAY(hash) (RHASH(hash)->as.li)
+#define RHASH_ST_TABLE(hash) (RHASH(hash)->as.st)
+#endif
+
+#define RHASH(obj) (R_CAST(RHash)(obj))
+#define RHASH_ST_SIZE(h) (RHASH_ST_TABLE(h)->num_entries)
+#define RHASH_TABLE_P(h) (!RHASH_ARRAY_P(h))
+#define RHASH_CLEAR(h) (FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG), RHASH(h)->as.li = NULL)
+
+#define RHASH_ARRAY_SIZE_MASK (VALUE)RHASH_ARRAY_SIZE_MASK
+#define RHASH_ARRAY_SIZE_SHIFT RHASH_ARRAY_SIZE_SHIFT
+#define RHASH_ARRAY_BOUND_MASK (VALUE)RHASH_ARRAY_BOUND_MASK
+#define RHASH_ARRAY_BOUND_SHIFT RHASH_ARRAY_BOUND_SHIFT
+
+#if USE_TRANSIENT_HEAP
+#define RHASH_TRANSIENT_FLAG FL_USER14
+#define RHASH_TRANSIENT_P(hash) FL_TEST_RAW((hash), RHASH_TRANSIENT_FLAG)
+#define RHASH_SET_TRANSIENT_FLAG(h) FL_SET_RAW(h, RHASH_TRANSIENT_FLAG)
+#define RHASH_UNSET_TRANSIENT_FLAG(h) FL_UNSET_RAW(h, RHASH_TRANSIENT_FLAG)
+#else
+#define RHASH_TRANSIENT_P(hash) 0
+#define RHASH_SET_TRANSIENT_FLAG(h) ((void)0)
+#define RHASH_UNSET_TRANSIENT_FLAG(h) ((void)0)
+#endif
+
+#define RHASH_ARRAY_MAX_SIZE 8
+#define RHASH_ARRAY_MAX_BOUND RHASH_ARRAY_MAX_SIZE
+
+typedef struct li_table_entry {
+ VALUE hash;
+ VALUE key;
+ VALUE record;
+} li_table_entry;
+
+typedef struct li_table {
+ li_table_entry entries[RHASH_ARRAY_MAX_SIZE];
+} li_table;
+
+/*
+ * RHASH_ARRAY_P(h):
+ * * as.li == NULL or
+ * as.li points li_table.
+ * * as.li is allocated by transient heap or xmalloc.
+ *
+ * !RHASH_ARRAY_P(h):
+ * * as.st points st_table.
+ */
struct RHash {
struct RBasic basic;
- struct st_table *ntbl; /* possibly 0 */
+ union {
+ struct st_table *st;
+ struct li_table *li; /* possibly 0 */
+ } as;
int iter_lev;
const VALUE ifnone;
};
-#define RHASH(obj) (R_CAST(RHash)(obj))
-
#ifdef RHASH_ITER_LEV
-#undef RHASH_ITER_LEV
-#undef RHASH_IFNONE
-#undef RHASH_SIZE
-#define RHASH_ITER_LEV(h) (RHASH(h)->iter_lev)
-#define RHASH_IFNONE(h) (RHASH(h)->ifnone)
-#define RHASH_SIZE(h) (RHASH(h)->ntbl ? RHASH(h)->ntbl->num_entries : (st_index_t)0)
-#endif
+# undef RHASH_ITER_LEV
+# undef RHASH_IFNONE
+# undef RHASH_SIZE
+
+# define RHASH_ITER_LEV(h) (RHASH(h)->iter_lev)
+# define RHASH_IFNONE(h) (RHASH(h)->ifnone)
+# define RHASH_SIZE(h) (RHASH_ARRAY_P(h) ? RHASH_ARRAY_SIZE_RAW(h) : RHASH_ST_SIZE(h))
+#endif /* #ifdef RHASH_ITER_LEV */
/* missing/setproctitle.c */
#ifndef HAVE_SETPROCTITLE
@@ -698,14 +860,26 @@ extern void ruby_init_setproctitle(int argc, char *argv[]);
#define RSTRUCT_EMBED_LEN_MAX RSTRUCT_EMBED_LEN_MAX
#define RSTRUCT_EMBED_LEN_MASK RSTRUCT_EMBED_LEN_MASK
#define RSTRUCT_EMBED_LEN_SHIFT RSTRUCT_EMBED_LEN_SHIFT
+
enum {
RSTRUCT_EMBED_LEN_MAX = 3,
RSTRUCT_EMBED_LEN_MASK = (RUBY_FL_USER2|RUBY_FL_USER1),
RSTRUCT_EMBED_LEN_SHIFT = (RUBY_FL_USHIFT+1),
+ RSTRUCT_TRANSIENT_FLAG = FL_USER3,
RSTRUCT_ENUM_END
};
+#if USE_TRANSIENT_HEAP
+#define RSTRUCT_TRANSIENT_P(st) FL_TEST_RAW((obj), RSTRUCT_TRANSIENT_FLAG)
+#define RSTRUCT_TRANSIENT_SET(st) FL_SET_RAW((st), RSTRUCT_TRANSIENT_FLAG)
+#define RSTRUCT_TRANSIENT_UNSET(st) FL_UNSET_RAW((st), RSTRUCT_TRANSIENT_FLAG)
+#else
+#define RSTRUCT_TRANSIENT_P(st) 0
+#define RSTRUCT_TRANSIENT_SET(st) ((void)0)
+#define RSTRUCT_TRANSIENT_UNSET(st) ((void)0)
+#endif
+
struct RStruct {
struct RBasic basic;
union {
@@ -746,6 +920,13 @@ rb_struct_const_ptr(VALUE st)
RSTRUCT(st)->as.ary : RSTRUCT(st)->as.heap.ptr);
}
+static inline const VALUE *
+rb_struct_const_heap_ptr(VALUE st)
+{
+ /* TODO: check embed on debug mode */
+ return RSTRUCT(st)->as.heap.ptr;
+}
+
/* class.c */
struct rb_deprecated_classext_struct {
@@ -1073,6 +1254,37 @@ VALUE rb_gvar_set(struct rb_global_entry *, VALUE);
VALUE rb_gvar_defined(struct rb_global_entry *);
/* array.c */
+
+#ifndef ARRAY_DEBUG
+#define ARRAY_DEBUG 0
+#endif
+
+#ifdef ARRAY_DEBUG
+#define RARRAY_PTR_IN_USE_FLAG FL_USER14
+#define ARY_PTR_USING_P(ary) FL_TEST_RAW((ary), RARRAY_PTR_IN_USE_FLAG)
+
+#else
+
+/* disable debug function */
+#undef RARRAY_PTR_USE_START
+#undef RARRAY_PTR_USE_END
+#define RARRAY_PTR_USE_START(a) ((VALUE *)RARRAY_CONST_PTR_TRANSIENT(a))
+#define RARRAY_PTR_USE_END(a)
+#define ARY_PTR_USING_P(ary) 0
+
+#endif
+
+#if USE_TRANSIENT_HEAP
+#define RARY_TRANSIENT_SET(ary) FL_SET_RAW((ary), RARRAY_TRANSIENT_FLAG);
+#define RARY_TRANSIENT_UNSET(ary) FL_UNSET_RAW((ary), RARRAY_TRANSIENT_FLAG);
+#else
+#undef RARRAY_TRANSIENT_P
+#define RARRAY_TRANSIENT_P(a) 0
+#define RARY_TRANSIENT_SET(ary) ((void)0)
+#define RARY_TRANSIENT_UNSET(ary) ((void)0)
+#endif
+
+
VALUE rb_ary_last(int, const VALUE *, VALUE);
void rb_ary_set_len(VALUE, long);
void rb_ary_delete_same(VALUE, VALUE);
@@ -1100,7 +1312,7 @@ static inline VALUE
rb_ary_entry_internal(VALUE ary, long offset)
{
long len = RARRAY_LEN(ary);
- const VALUE *ptr = RARRAY_CONST_PTR(ary);
+ const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(ary);
if (len == 0) return Qnil;
if (offset < 0) {
offset += len;
@@ -1173,12 +1385,13 @@ VALUE rb_complex_plus(VALUE, VALUE);
VALUE rb_complex_mul(VALUE, VALUE);
VALUE rb_complex_abs(VALUE x);
VALUE rb_complex_sqrt(VALUE x);
-VALUE rb_dbl_complex_polar(double abs, double ang);
+VALUE rb_dbl_complex_polar_pi(double abs, double ang);
VALUE rb_complex_pow(VALUE self, VALUE other);
+struct rb_thread_struct;
/* cont.c */
VALUE rb_obj_is_fiber(VALUE);
-void rb_fiber_reset_root_local_storage(VALUE);
+void rb_fiber_reset_root_local_storage(struct rb_thread_struct *);
void ruby_register_rollback_func_for_ensure(VALUE (*ensure_func)(ANYARGS), VALUE (*rollback_func)(ANYARGS));
/* debug.c */
@@ -1337,8 +1550,18 @@ RUBY_SYMBOL_EXPORT_END
rb_wb_unprotected_newobj_of(klass, flags))
#define NEWOBJ_OF(obj,type,klass,flags) RB_NEWOBJ_OF(obj,type,klass,flags)
+void *rb_aligned_malloc(size_t, size_t);
+void rb_aligned_free(void *);
+
/* hash.c */
+#if RHASH_CONVERT_TABLE_DEBUG
+struct st_table *rb_hash_tbl_raw(VALUE hash, const char *file, int line);
+#define RHASH_TBL_RAW(h) rb_hash_tbl_raw(h, __FILE__, __LINE__)
+#else
struct st_table *rb_hash_tbl_raw(VALUE hash);
+#define RHASH_TBL_RAW(h) rb_hash_tbl_raw(h)
+#endif
+
VALUE rb_hash_new_with_size(st_index_t size);
RUBY_SYMBOL_EXPORT_BEGIN
VALUE rb_hash_new_compare_by_id(void);
@@ -1353,14 +1576,17 @@ st_table *rb_init_identtable_with_size(st_index_t size);
VALUE rb_hash_compare_by_id_p(VALUE hash);
VALUE rb_to_hash_type(VALUE obj);
VALUE rb_hash_key_str(VALUE);
-
-#define RHASH_TBL_RAW(h) rb_hash_tbl_raw(h)
VALUE rb_hash_keys(VALUE hash);
VALUE rb_hash_values(VALUE hash);
VALUE rb_hash_rehash(VALUE hash);
int rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val);
-#define HASH_PROC_DEFAULT FL_USER2
VALUE rb_hash_set_pair(VALUE hash, VALUE pair);
+void rb_hash_bulk_insert(long, const VALUE *, VALUE);
+
+int rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval);
+int rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval);
+int rb_hash_stlike_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg);
+int rb_hash_stlike_update(VALUE hash, st_data_t key, st_update_callback_func func, st_data_t arg);
/* inits.c */
void rb_call_inits(void);
@@ -1408,10 +1634,12 @@ VALUE rb_math_sqrt(VALUE);
extern int mjit_enabled;
VALUE mjit_pause(int wait_p);
VALUE mjit_resume(void);
+void mjit_finish(int close_handle_p);
#else
#define mjit_enabled 0
static inline VALUE mjit_pause(int wait_p){ return Qnil; } /* unreachable */
static inline VALUE mjit_resume(void){ return Qnil; } /* unreachable */
+static inline void mjit_finish(int close_handle_p){}
#endif
/* newline.c */
@@ -1529,6 +1757,7 @@ rb_num_negative_int_p(VALUE num)
VALUE rb_float_abs(VALUE flt);
VALUE rb_float_equal(VALUE x, VALUE y);
VALUE rb_float_eql(VALUE x, VALUE y);
+VALUE rb_flo_div_flo(VALUE x, VALUE y);
#if USE_FLONUM
#define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n)))
@@ -1899,6 +2128,16 @@ extern rb_encoding OnigEncodingUTF_8;
#endif
/* variable.c */
+#if USE_TRANSIENT_HEAP
+#define ROBJECT_TRANSIENT_FLAG FL_USER13
+#define ROBJ_TRANSIENT_P(obj) FL_TEST_RAW((obj), ROBJECT_TRANSIENT_FLAG)
+#define ROBJ_TRANSIENT_SET(obj) FL_SET_RAW((obj), ROBJECT_TRANSIENT_FLAG)
+#define ROBJ_TRANSIENT_UNSET(obj) FL_UNSET_RAW((obj), ROBJECT_TRANSIENT_FLAG)
+#else
+#define ROBJ_TRANSIENT_P(obj) 0
+#define ROBJ_TRANSIENT_SET(obj) ((void)0)
+#define ROBJ_TRANSIENT_UNSET(obj) ((void)0)
+#endif
void rb_gc_mark_global_tbl(void);
size_t rb_generic_ivar_memsize(VALUE);
VALUE rb_search_class_path(VALUE);
@@ -1983,6 +2222,9 @@ const char *rb_objspace_data_type_name(VALUE obj);
/* Temporary. This API will be removed (renamed). */
VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd);
+/* array.c (export) */
+void rb_ary_detransient(VALUE a);
+
/* bignum.c (export) */
VALUE rb_big_mul_normal(VALUE x, VALUE y);
VALUE rb_big_mul_balance(VALUE x, VALUE y);
@@ -2024,6 +2266,13 @@ NORETURN(void rb_unexpected_type(VALUE,int));
((t) == RUBY_T_DATA && RTYPEDDATA_P(v)) ? \
rb_unexpected_type((VALUE)(v), (t)) : (void)0)
+static inline int
+rb_typeddata_is_instance_of_inline(VALUE obj, const rb_data_type_t *data_type)
+{
+ return RB_TYPE_P(obj, T_DATA) && RTYPEDDATA_P(obj) && (RTYPEDDATA_TYPE(obj) == data_type);
+}
+#define rb_typeddata_is_instance_of rb_typeddata_is_instance_of_inline
+
/* file.c (export) */
#if defined HAVE_READLINK && defined RUBY_ENCODING_H
VALUE rb_readlink(VALUE path, rb_encoding *enc);
diff --git a/io.c b/io.c
index 32629165cf0d8a..ac35e1ee4fa9f8 100644
--- a/io.c
+++ b/io.c
@@ -135,6 +135,15 @@ off_t __syscall(quad_t number, ...);
#define rename(f, t) rb_w32_urename((f), (t))
#endif
+#if defined(_WIN32)
+# define RUBY_PIPE_NONBLOCK_DEFAULT (0)
+#elif defined(O_NONBLOCK)
+ /* disabled for [Bug #15356] (Rack::Deflater + rails) failure: */
+# define RUBY_PIPE_NONBLOCK_DEFAULT (0)
+#else /* any platforms where O_NONBLOCK does not exist? */
+# define RUBY_PIPE_NONBLOCK_DEFAULT (0)
+#endif
+
VALUE rb_cIO;
VALUE rb_eEOFError;
VALUE rb_eIOError;
@@ -185,14 +194,22 @@ static rb_atomic_t max_file_descriptor = NOFILE;
void
rb_update_max_fd(int fd)
{
- struct stat buf;
rb_atomic_t afd = (rb_atomic_t)fd;
rb_atomic_t max_fd = max_file_descriptor;
+ int err;
if (afd <= max_fd)
return;
- if (fstat(fd, &buf) != 0 && errno == EBADF) {
+#if defined(HAVE_FCNTL) && defined(F_GETFL)
+ err = fcntl(fd, F_GETFL) == -1;
+#else
+ {
+ struct stat buf;
+ err = fstat(fd, &buf) != 0;
+ }
+#endif
+ if (err && errno == EBADF) {
rb_bug("rb_update_max_fd: invalid fd (%d) given.", fd);
}
@@ -217,7 +234,7 @@ rb_maygvl_fd_fix_cloexec(int fd)
flags2 = flags | FD_CLOEXEC; /* Set CLOEXEC for non-standard file descriptors: 3, 4, 5, ... */
if (flags != flags2) {
ret = fcntl(fd, F_SETFD, flags2);
- if (ret == -1) {
+ if (ret != 0) {
rb_bug("rb_maygvl_fd_fix_cloexec: fcntl(%d, F_SETFD, %d) failed: %s", fd, flags2, strerror(errno));
}
}
@@ -261,7 +278,7 @@ rb_cloexec_open(const char *pathname, int flags, mode_t mode)
flags |= O_NOINHERIT;
#endif
ret = open(pathname, flags, mode);
- if (ret == -1) return -1;
+ if (ret < 0) return ret;
if (ret <= 2 || o_cloexec_state == 0) {
rb_maygvl_fd_fix_cloexec(ret);
}
@@ -310,12 +327,30 @@ rb_cloexec_dup2(int oldfd, int newfd)
#else
ret = dup2(oldfd, newfd);
#endif
- if (ret == -1) return -1;
+ if (ret < 0) return ret;
}
rb_maygvl_fd_fix_cloexec(ret);
return ret;
}
+static int
+rb_fd_set_nonblock(int fd)
+{
+#ifdef _WIN32
+ return rb_w32_set_nonblock(fd);
+#elif defined(F_GETFL)
+ int oflags = fcntl(fd, F_GETFL);
+
+ if (oflags == -1)
+ return -1;
+ if (oflags & O_NONBLOCK)
+ return 0;
+ oflags |= O_NONBLOCK;
+ return fcntl(fd, F_SETFL, oflags);
+#endif
+ return 0;
+}
+
int
rb_cloexec_pipe(int fildes[2])
{
@@ -324,7 +359,7 @@ rb_cloexec_pipe(int fildes[2])
#if defined(HAVE_PIPE2)
static int try_pipe2 = 1;
if (try_pipe2) {
- ret = pipe2(fildes, O_CLOEXEC);
+ ret = pipe2(fildes, O_CLOEXEC | RUBY_PIPE_NONBLOCK_DEFAULT);
if (ret != -1)
return ret;
/* pipe2 is available since Linux 2.6.27, glibc 2.9. */
@@ -339,7 +374,7 @@ rb_cloexec_pipe(int fildes[2])
#else
ret = pipe(fildes);
#endif
- if (ret == -1) return -1;
+ if (ret < 0) return ret;
#ifdef __CYGWIN__
if (ret == 0 && fildes[1] == -1) {
close(fildes[0]);
@@ -350,6 +385,10 @@ rb_cloexec_pipe(int fildes[2])
#endif
rb_maygvl_fd_fix_cloexec(fildes[0]);
rb_maygvl_fd_fix_cloexec(fildes[1]);
+ if (RUBY_PIPE_NONBLOCK_DEFAULT) {
+ rb_fd_set_nonblock(fildes[0]);
+ rb_fd_set_nonblock(fildes[1]);
+ }
return ret;
}
@@ -382,7 +421,7 @@ rb_cloexec_fcntl_dupfd(int fd, int minfd)
ret = fcntl(fd, F_DUPFD, minfd);
#elif defined(HAVE_DUP)
ret = dup(fd);
- if (ret != -1 && ret < minfd) {
+ if (ret >= 0 && ret < minfd) {
const int prev_fd = ret;
ret = rb_cloexec_fcntl_dupfd(fd, minfd);
close(prev_fd);
@@ -391,7 +430,7 @@ rb_cloexec_fcntl_dupfd(int fd, int minfd)
#else
# error "dup() or fcntl(F_DUPFD) must be supported."
#endif
- if (ret == -1) return -1;
+ if (ret < 0) return ret;
rb_maygvl_fd_fix_cloexec(ret);
return ret;
}
@@ -615,7 +654,8 @@ static void
io_fd_check_closed(int fd)
{
if (fd < 0) {
- rb_raise(rb_eIOError, closed_stream);
+ rb_thread_check_ints(); /* check for ruby_error_stream_closed */
+ rb_raise(rb_eIOError, closed_stream);
}
}
@@ -920,6 +960,7 @@ io_alloc(VALUE klass)
struct io_internal_read_struct {
int fd;
+ int nonblock;
void *buf;
size_t capa;
};
@@ -938,11 +979,24 @@ struct io_internal_writev_struct {
};
#endif
+static int nogvl_wait_for_single_fd(int fd, short events);
static VALUE
internal_read_func(void *ptr)
{
struct io_internal_read_struct *iis = ptr;
- return read(iis->fd, iis->buf, iis->capa);
+ ssize_t r;
+retry:
+ r = read(iis->fd, iis->buf, iis->capa);
+ if (r < 0 && !iis->nonblock) {
+ int e = errno;
+ if (e == EAGAIN || e == EWOULDBLOCK) {
+ if (nogvl_wait_for_single_fd(iis->fd, RB_WAITFD_IN) != -1) {
+ goto retry;
+ }
+ errno = e;
+ }
+ }
+ return r;
}
#if defined __APPLE__
@@ -980,7 +1034,9 @@ static ssize_t
rb_read_internal(int fd, void *buf, size_t count)
{
struct io_internal_read_struct iis;
+
iis.fd = fd;
+ iis.nonblock = 0;
iis.buf = buf;
iis.capa = count;
@@ -1255,8 +1311,8 @@ io_binwrite_string(VALUE arg)
r = rb_writev_internal(fptr->fd, iov, 2);
- if (r == -1)
- return -1;
+ if (r < 0)
+ return r;
if (fptr->wbuf.len <= r) {
r -= fptr->wbuf.len;
@@ -1487,7 +1543,7 @@ io_write(VALUE io, VALUE str, int nosync)
rb_io_check_writable(fptr);
n = io_fwrite(str, fptr, nosync);
- if (n == -1L) rb_sys_fail_path(fptr->pathv);
+ if (n < 0L) rb_sys_fail_path(fptr->pathv);
return LONG2FIX(n);
}
@@ -1674,7 +1730,7 @@ io_writev(int argc, VALUE *argv, VALUE io)
/* sync at last item */
n = io_fwrite(rb_obj_as_string(argv[i]), fptr, (i < argc-1));
}
- if (n == -1L) rb_sys_fail_path(fptr->pathv);
+ if (n < 0L) rb_sys_fail_path(fptr->pathv);
total = rb_fix_plus(LONG2FIX(n), total);
}
@@ -1973,6 +2029,16 @@ rb_io_rewind(VALUE io)
return INT2FIX(0);
}
+static int
+fptr_wait_readable(rb_io_t *fptr)
+{
+ int ret = rb_io_wait_readable(fptr->fd);
+
+ if (ret)
+ rb_io_check_closed(fptr);
+ return ret;
+}
+
static int
io_fillbuf(rb_io_t *fptr)
{
@@ -1993,7 +2059,7 @@ io_fillbuf(rb_io_t *fptr)
r = rb_read_internal(fptr->fd, fptr->rbuf.ptr, fptr->rbuf.capa);
}
if (r < 0) {
- if (rb_io_wait_readable(fptr->fd))
+ if (fptr_wait_readable(fptr))
goto retry;
{
int e = errno;
@@ -2340,7 +2406,7 @@ io_bufread(char *ptr, long len, rb_io_t *fptr)
c = rb_read_internal(fptr->fd, ptr+offset, n);
if (c == 0) break;
if (c < 0) {
- if (rb_io_wait_readable(fptr->fd))
+ if (fptr_wait_readable(fptr))
goto again;
return -1;
}
@@ -2526,7 +2592,7 @@ fill_cbuf(rb_io_t *fptr, int ec_flags)
if (res == econv_source_buffer_empty) {
if (fptr->rbuf.len == 0) {
READ_CHECK(fptr);
- if (io_fillbuf(fptr) == -1) {
+ if (io_fillbuf(fptr) < 0) {
if (!fptr->readconv) {
return MORE_CHAR_FINISHED;
}
@@ -2696,41 +2762,23 @@ read_all(rb_io_t *fptr, long siz, VALUE str)
void
rb_io_set_nonblock(rb_io_t *fptr)
{
-#ifdef _WIN32
- if (rb_w32_set_nonblock(fptr->fd) != 0) {
+ if (rb_fd_set_nonblock(fptr->fd) != 0) {
rb_sys_fail_path(fptr->pathv);
}
-#else
- int oflags;
-#ifdef F_GETFL
- oflags = fcntl(fptr->fd, F_GETFL);
- if (oflags == -1) {
- rb_sys_fail_path(fptr->pathv);
- }
-#else
- oflags = 0;
-#endif
- if ((oflags & O_NONBLOCK) == 0) {
- oflags |= O_NONBLOCK;
- if (fcntl(fptr->fd, F_SETFL, oflags) == -1) {
- rb_sys_fail_path(fptr->pathv);
- }
- }
-#endif
}
-struct read_internal_arg {
- int fd;
- char *str_ptr;
- long len;
-};
-
static VALUE
read_internal_call(VALUE arg)
{
- struct read_internal_arg *p = (struct read_internal_arg *)arg;
- p->len = rb_read_internal(p->fd, p->str_ptr, p->len);
- return Qundef;
+ struct io_internal_read_struct *iis = (struct io_internal_read_struct *)arg;
+
+ return rb_thread_io_blocking_region(internal_read_func, iis, iis->fd);
+}
+
+static long
+read_internal_locktmp(VALUE str, struct io_internal_read_struct *iis)
+{
+ return (long)rb_str_locktmp_ensure(str, read_internal_call, (VALUE)iis);
}
static int
@@ -2749,7 +2797,7 @@ io_getpartial(int argc, VALUE *argv, VALUE io, VALUE opts, int nonblock)
rb_io_t *fptr;
VALUE length, str;
long n, len;
- struct read_internal_arg arg;
+ struct io_internal_read_struct iis;
int shrinkable;
rb_scan_args(argc, argv, "11", &length, &str);
@@ -2776,14 +2824,14 @@ io_getpartial(int argc, VALUE *argv, VALUE io, VALUE opts, int nonblock)
rb_io_set_nonblock(fptr);
}
io_setstrbuf(&str, len);
- arg.fd = fptr->fd;
- arg.str_ptr = RSTRING_PTR(str);
- arg.len = len;
- rb_str_locktmp_ensure(str, read_internal_call, (VALUE)&arg);
- n = arg.len;
+ iis.fd = fptr->fd;
+ iis.nonblock = nonblock;
+ iis.buf = RSTRING_PTR(str);
+ iis.capa = len;
+ n = read_internal_locktmp(str, &iis);
if (n < 0) {
int e = errno;
- if (!nonblock && rb_io_wait_readable(fptr->fd))
+ if (!nonblock && fptr_wait_readable(fptr))
goto again;
if (nonblock && (e == EWOULDBLOCK || e == EAGAIN)) {
if (no_exception_p(opts))
@@ -2890,7 +2938,7 @@ io_read_nonblock(VALUE io, VALUE length, VALUE str, VALUE ex)
{
rb_io_t *fptr;
long n, len;
- struct read_internal_arg arg;
+ struct io_internal_read_struct iis;
int shrinkable;
if ((len = NUM2LONG(length)) < 0) {
@@ -2909,11 +2957,11 @@ io_read_nonblock(VALUE io, VALUE length, VALUE str, VALUE ex)
if (n <= 0) {
rb_io_set_nonblock(fptr);
shrinkable |= io_setstrbuf(&str, len);
- arg.fd = fptr->fd;
- arg.str_ptr = RSTRING_PTR(str);
- arg.len = len;
- rb_str_locktmp_ensure(str, read_internal_call, (VALUE)&arg);
- n = arg.len;
+ iis.fd = fptr->fd;
+ iis.nonblock = 1;
+ iis.buf = RSTRING_PTR(str);
+ iis.capa = len;
+ n = read_internal_locktmp(str, &iis);
if (n < 0) {
int e = errno;
if ((e == EWOULDBLOCK || e == EAGAIN)) {
@@ -2954,7 +3002,7 @@ io_write_nonblock(VALUE io, VALUE str, VALUE ex)
rb_io_set_nonblock(fptr);
n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
- if (n == -1) {
+ if (n < 0) {
int e = errno;
if (e == EWOULDBLOCK || e == EAGAIN) {
if (ex == Qfalse) {
@@ -4212,8 +4260,18 @@ rb_io_ungetbyte(VALUE io, VALUE b)
rb_io_check_byte_readable(fptr);
if (NIL_P(b)) return Qnil;
if (FIXNUM_P(b)) {
- char cc = FIX2INT(b);
- b = rb_str_new(&cc, 1);
+ int i = FIX2INT(b);
+ if (0 <= i && i <= UCHAR_MAX) {
+ unsigned char cc = i & 0xFF;
+ b = rb_str_new((const char *)&cc, 1);
+ }
+ else {
+ rb_raise(rb_eRangeError,
+ "integer %d too big to convert into `unsigned char'", i);
+ }
+ }
+ else if (RB_TYPE_P(b, T_BIGNUM)) {
+ rb_raise(rb_eRangeError, "bignum too big to convert into `unsigned char'");
}
else {
SafeStringValue(b);
@@ -4385,7 +4443,7 @@ rb_io_set_close_on_exec(VALUE io, VALUE arg)
if ((ret & FD_CLOEXEC) != flag) {
ret = (ret & ~FD_CLOEXEC) | flag;
ret = fcntl(fd, F_SETFD, ret);
- if (ret == -1) rb_sys_fail_path(fptr->pathv);
+ if (ret != 0) rb_sys_fail_path(fptr->pathv);
}
}
@@ -4397,7 +4455,7 @@ rb_io_set_close_on_exec(VALUE io, VALUE arg)
if ((ret & FD_CLOEXEC) != flag) {
ret = (ret & ~FD_CLOEXEC) | flag;
ret = fcntl(fd, F_SETFD, ret);
- if (ret == -1) rb_sys_fail_path(fptr->pathv);
+ if (ret != 0) rb_sys_fail_path(fptr->pathv);
}
}
return Qnil;
@@ -4527,7 +4585,8 @@ static void free_io_buffer(rb_io_buffer_t *buf);
static void clear_codeconv(rb_io_t *fptr);
static void
-fptr_finalize_flush(rb_io_t *fptr, int noraise, int keepgvl)
+fptr_finalize_flush(rb_io_t *fptr, int noraise, int keepgvl,
+ struct list_head *busy)
{
VALUE err = Qnil;
int fd = fptr->fd;
@@ -4559,6 +4618,14 @@ fptr_finalize_flush(rb_io_t *fptr, int noraise, int keepgvl)
fptr->stdio_file = 0;
fptr->mode &= ~(FMODE_READABLE|FMODE_WRITABLE);
+ /*
+ * ensure waiting_fd users do not hit EBADF, wait for them
+ * to exit before we call close().
+ */
+ if (busy) {
+ do rb_thread_schedule(); while (!list_empty(busy));
+ }
+
if (IS_PREP_STDIO(fptr) || fd <= 2) {
/* need to keep FILE objects of stdin, stdout and stderr */
}
@@ -4591,7 +4658,7 @@ fptr_finalize_flush(rb_io_t *fptr, int noraise, int keepgvl)
static void
fptr_finalize(rb_io_t *fptr, int noraise)
{
- fptr_finalize_flush(fptr, noraise, FALSE);
+ fptr_finalize_flush(fptr, noraise, FALSE, 0);
free_io_buffer(&fptr->rbuf);
free_io_buffer(&fptr->wbuf);
clear_codeconv(fptr);
@@ -4716,8 +4783,8 @@ io_close_fptr(VALUE io)
if (fptr->fd < 0) return 0;
if (rb_notify_fd_close(fptr->fd, &busy)) {
- fptr_finalize_flush(fptr, FALSE, KEEPGVL); /* calls close(fptr->fd) */
- do rb_thread_schedule(); while (!list_empty(&busy));
+ /* calls close(fptr->fd): */
+ fptr_finalize_flush(fptr, FALSE, KEEPGVL, &busy);
}
rb_io_fptr_cleanup(fptr, FALSE);
return fptr;
@@ -4987,7 +5054,7 @@ rb_io_sysseek(int argc, VALUE *argv, VALUE io)
}
errno = 0;
pos = lseek(fptr->fd, pos, whence);
- if (pos == -1 && errno) rb_sys_fail_path(fptr->pathv);
+ if (pos < 0 && errno) rb_sys_fail_path(fptr->pathv);
return OFFT2NUM(pos);
}
@@ -5027,7 +5094,7 @@ rb_io_syswrite(VALUE io, VALUE str)
tmp = rb_str_tmp_frozen_acquire(str);
RSTRING_GETMEM(tmp, ptr, len);
n = rb_write_internal(fptr->fd, ptr, len);
- if (n == -1) rb_sys_fail_path(fptr->pathv);
+ if (n < 0) rb_sys_fail_path(fptr->pathv);
rb_str_tmp_frozen_release(str, tmp);
return LONG2FIX(n);
@@ -5059,7 +5126,7 @@ rb_io_sysread(int argc, VALUE *argv, VALUE io)
VALUE len, str;
rb_io_t *fptr;
long n, ilen;
- struct read_internal_arg arg;
+ struct io_internal_read_struct iis;
int shrinkable;
rb_scan_args(argc, argv, "11", &len, &str);
@@ -5087,14 +5154,13 @@ rb_io_sysread(int argc, VALUE *argv, VALUE io)
rb_io_check_closed(fptr);
io_setstrbuf(&str, ilen);
- rb_str_locktmp(str);
- arg.fd = fptr->fd;
- arg.str_ptr = RSTRING_PTR(str);
- arg.len = ilen;
- rb_ensure(read_internal_call, (VALUE)&arg, rb_str_unlocktmp, str);
- n = arg.len;
+ iis.fd = fptr->fd;
+ iis.nonblock = 1; /* for historical reasons, maybe (see above) */
+ iis.buf = RSTRING_PTR(str);
+ iis.capa = ilen;
+ n = read_internal_locktmp(str, &iis);
- if (n == -1) {
+ if (n < 0) {
rb_sys_fail_path(fptr->pathv);
}
io_set_read_length(str, n, shrinkable);
@@ -5179,7 +5245,7 @@ rb_io_pread(int argc, VALUE *argv, VALUE io)
rb_str_locktmp(str);
n = (ssize_t)rb_ensure(pread_internal_call, (VALUE)&arg, rb_str_unlocktmp, str);
- if (n == -1) {
+ if (n < 0) {
rb_sys_fail_path(fptr->pathv);
}
io_set_read_length(str, n, shrinkable);
@@ -5245,7 +5311,7 @@ rb_io_pwrite(VALUE io, VALUE str, VALUE offset)
arg.count = (size_t)RSTRING_LEN(tmp);
n = (ssize_t)rb_thread_io_blocking_region(internal_pwrite_func, &arg, fptr->fd);
- if (n == -1) rb_sys_fail_path(fptr->pathv);
+ if (n < 0) rb_sys_fail_path(fptr->pathv);
rb_str_tmp_frozen_release(str, tmp);
return SSIZET2NUM(n);
@@ -6292,7 +6358,7 @@ rb_pipe(int *pipes)
{
int ret;
ret = rb_cloexec_pipe(pipes);
- if (ret == -1) {
+ if (ret < 0) {
if (rb_gc_for_fd(errno)) {
ret = rb_cloexec_pipe(pipes);
}
@@ -6369,9 +6435,9 @@ linux_get_maxfd(void)
char buf[4096], *p, *np, *e;
ssize_t ss;
fd = rb_cloexec_open("/proc/self/status", O_RDONLY|O_NOCTTY, 0);
- if (fd == -1) return -1;
+ if (fd < 0) return fd;
ss = read(fd, buf, sizeof(buf));
- if (ss == -1) goto err;
+ if (ss < 0) goto err;
p = buf;
e = buf + ss;
while ((int)sizeof("FDSize:\t0\n")-1 <= e-p &&
@@ -6390,7 +6456,7 @@ linux_get_maxfd(void)
err:
close(fd);
- return -1;
+ return (int)ss;
}
#endif
@@ -6560,7 +6626,7 @@ pipe_open(VALUE execarg_obj, const char *modestr, int fmode,
# if defined(HAVE_SPAWNVE)
if (eargp->envp_str) envp = (char **)RSTRING_PTR(eargp->envp_str);
# endif
- while ((pid = DO_SPAWN(cmd, args, envp)) == -1) {
+ while ((pid = DO_SPAWN(cmd, args, envp)) < 0) {
/* exec failed */
switch (e = errno) {
case EAGAIN:
@@ -6593,7 +6659,7 @@ pipe_open(VALUE execarg_obj, const char *modestr, int fmode,
}
/* parent */
- if (pid == -1) {
+ if (pid < 0) {
# if defined(HAVE_WORKING_FORK)
e = errno;
# endif
@@ -8193,7 +8259,7 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io)
oflags = fcntl(fd, F_GETFL);
if (oflags == -1) rb_sys_fail(0);
#else
- if (fstat(fd, &st) == -1) rb_sys_fail(0);
+ if (fstat(fd, &st) < 0) rb_sys_fail(0);
#endif
rb_update_max_fd(fd);
#if defined(HAVE_FCNTL) && defined(F_GETFL)
@@ -10163,7 +10229,7 @@ rb_io_s_pipe(int argc, VALUE *argv, VALUE klass)
VALUE ret;
argc = rb_scan_args(argc, argv, "02:", &v1, &v2, &opt);
- if (rb_pipe(pipes) == -1)
+ if (rb_pipe(pipes) < 0)
rb_sys_fail(0);
args[0] = klass;
@@ -10637,11 +10703,11 @@ struct copy_stream_struct {
int src_fd;
int dst_fd;
- int close_src;
- int close_dst;
+ unsigned close_src : 1;
+ unsigned close_dst : 1;
+ int error_no;
off_t total;
const char *syserr;
- int error_no;
const char *notimp;
VALUE th;
};
@@ -10707,6 +10773,7 @@ nogvl_wait_for_single_fd(int fd, short events)
return poll(&fds, 1, -1);
}
#else /* !USE_POLL */
+# include "vm_core.h"
# define IOWAIT_SYSCALL "select"
static int
nogvl_wait_for_single_fd(int fd, short events)
@@ -10725,7 +10792,7 @@ nogvl_wait_for_single_fd(int fd, short events)
ret = rb_fd_select(fd + 1, 0, &fds, 0, 0);
break;
default:
- assert(0 && "not supported yet, should never get here");
+ VM_UNREACHABLE(nogvl_wait_for_single_fd);
}
rb_fd_term(&fds);
@@ -10745,12 +10812,12 @@ maygvl_copy_stream_wait_read(int has_gvl, struct copy_stream_struct *stp)
else {
ret = nogvl_wait_for_single_fd(stp->src_fd, RB_WAITFD_IN);
}
- } while (ret == -1 && maygvl_copy_stream_continue_p(has_gvl, stp));
+ } while (ret < 0 && maygvl_copy_stream_continue_p(has_gvl, stp));
- if (ret == -1) {
+ if (ret < 0) {
stp->syserr = IOWAIT_SYSCALL;
stp->error_no = errno;
- return -1;
+ return ret;
}
return 0;
}
@@ -10762,12 +10829,12 @@ nogvl_copy_stream_wait_write(struct copy_stream_struct *stp)
do {
ret = nogvl_wait_for_single_fd(stp->dst_fd, RB_WAITFD_OUT);
- } while (ret == -1 && maygvl_copy_stream_continue_p(0, stp));
+ } while (ret < 0 && maygvl_copy_stream_continue_p(0, stp));
- if (ret == -1) {
+ if (ret < 0) {
stp->syserr = IOWAIT_SYSCALL;
stp->error_no = errno;
- return -1;
+ return ret;
}
return 0;
}
@@ -10787,30 +10854,31 @@ simple_copy_file_range(int in_fd, off_t *in_offset, int out_fd, off_t *out_offse
static int
nogvl_copy_file_range(struct copy_stream_struct *stp)
{
- struct stat src_stat, dst_stat;
+ struct stat sb;
ssize_t ss;
+ off_t src_size;
int ret;
-
off_t copy_length, src_offset, *src_offset_ptr;
- ret = fstat(stp->src_fd, &src_stat);
- if (ret == -1) {
+ ret = fstat(stp->src_fd, &sb);
+ if (ret < 0) {
stp->syserr = "fstat";
stp->error_no = errno;
- return -1;
+ return ret;
}
- if (!S_ISREG(src_stat.st_mode))
+ if (!S_ISREG(sb.st_mode))
return 0;
- ret = fstat(stp->dst_fd, &dst_stat);
- if (ret == -1) {
+ src_size = sb.st_size;
+ ret = fstat(stp->dst_fd, &sb);
+ if (ret < 0) {
stp->syserr = "fstat";
stp->error_no = errno;
- return -1;
+ return ret;
}
src_offset = stp->src_offset;
- if (src_offset != (off_t)-1) {
+ if (src_offset >= (off_t)0) {
src_offset_ptr = &src_offset;
}
else {
@@ -10818,20 +10886,20 @@ nogvl_copy_file_range(struct copy_stream_struct *stp)
}
copy_length = stp->copy_length;
- if (copy_length == (off_t)-1) {
- if (src_offset == (off_t)-1) {
+ if (copy_length < (off_t)0) {
+ if (src_offset < (off_t)0) {
off_t current_offset;
errno = 0;
current_offset = lseek(stp->src_fd, 0, SEEK_CUR);
- if (current_offset == (off_t)-1 && errno) {
+ if (current_offset < (off_t)0 && errno) {
stp->syserr = "lseek";
stp->error_no = errno;
- return -1;
+ return (int)current_offset;
}
- copy_length = src_stat.st_size - current_offset;
+ copy_length = src_size - current_offset;
}
else {
- copy_length = src_stat.st_size - src_offset;
+ copy_length = src_size - src_offset;
}
}
@@ -10850,7 +10918,7 @@ nogvl_copy_file_range(struct copy_stream_struct *stp)
goto retry_copy_file_range;
}
}
- if (ss == -1) {
+ if (ss < 0) {
if (maygvl_copy_stream_continue_p(0, stp)) {
goto retry_copy_file_range;
}
@@ -10869,8 +10937,10 @@ nogvl_copy_file_range(struct copy_stream_struct *stp)
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
- if (nogvl_copy_stream_wait_write(stp) == -1)
- return -1;
+ {
+ int ret = nogvl_copy_stream_wait_write(stp);
+ if (ret < 0) return ret;
+ }
goto retry_copy_file_range;
case EBADF:
{
@@ -10885,7 +10955,7 @@ nogvl_copy_file_range(struct copy_stream_struct *stp)
}
stp->syserr = "copy_file_range";
stp->error_no = errno;
- return -1;
+ return (int)ss;
}
return 1;
}
@@ -10924,7 +10994,7 @@ simple_sendfile(int out_fd, int in_fd, off_t *offset, off_t count)
# else
r = sendfile(in_fd, out_fd, pos, (size_t)count, NULL, &sbytes, 0);
# endif
- if (r != 0 && sbytes == 0) return -1;
+ if (r != 0 && sbytes == 0) return r;
if (offset) {
*offset += sbytes;
}
@@ -10942,51 +11012,52 @@ simple_sendfile(int out_fd, int in_fd, off_t *offset, off_t count)
static int
nogvl_copy_stream_sendfile(struct copy_stream_struct *stp)
{
- struct stat src_stat, dst_stat;
+ struct stat sb;
ssize_t ss;
int ret;
-
+ off_t src_size;
off_t copy_length;
off_t src_offset;
int use_pread;
- ret = fstat(stp->src_fd, &src_stat);
- if (ret == -1) {
+ ret = fstat(stp->src_fd, &sb);
+ if (ret < 0) {
stp->syserr = "fstat";
stp->error_no = errno;
- return -1;
+ return ret;
}
- if (!S_ISREG(src_stat.st_mode))
+ if (!S_ISREG(sb.st_mode))
return 0;
- ret = fstat(stp->dst_fd, &dst_stat);
- if (ret == -1) {
+ src_size = sb.st_size;
+ ret = fstat(stp->dst_fd, &sb);
+ if (ret < 0) {
stp->syserr = "fstat";
stp->error_no = errno;
- return -1;
+ return ret;
}
#ifndef __linux__
- if ((dst_stat.st_mode & S_IFMT) != S_IFSOCK)
+ if ((sb.st_mode & S_IFMT) != S_IFSOCK)
return 0;
#endif
src_offset = stp->src_offset;
- use_pread = src_offset != (off_t)-1;
+ use_pread = src_offset >= (off_t)0;
copy_length = stp->copy_length;
- if (copy_length == (off_t)-1) {
+ if (copy_length < (off_t)0) {
if (use_pread)
- copy_length = src_stat.st_size - src_offset;
+ copy_length = src_size - src_offset;
else {
off_t cur;
errno = 0;
cur = lseek(stp->src_fd, 0, SEEK_CUR);
- if (cur == (off_t)-1 && errno) {
+ if (cur < (off_t)0 && errno) {
stp->syserr = "lseek";
stp->error_no = errno;
- return -1;
+ return (int)cur;
}
- copy_length = src_stat.st_size - cur;
+ copy_length = src_size - cur;
}
}
@@ -11010,7 +11081,7 @@ nogvl_copy_stream_sendfile(struct copy_stream_struct *stp)
goto retry_sendfile;
}
}
- if (ss == -1) {
+ if (ss < 0) {
if (maygvl_copy_stream_continue_p(0, stp))
goto retry_sendfile;
switch (errno) {
@@ -11023,24 +11094,27 @@ nogvl_copy_stream_sendfile(struct copy_stream_struct *stp)
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
+ {
+ int ret;
#ifndef __linux__
- /*
- * Linux requires stp->src_fd to be a mmap-able (regular) file,
- * select() reports regular files to always be "ready", so
- * there is no need to select() on it.
- * Other OSes may have the same limitation for sendfile() which
- * allow us to bypass maygvl_copy_stream_wait_read()...
- */
- if (maygvl_copy_stream_wait_read(0, stp) == -1)
- return -1;
-#endif
- if (nogvl_copy_stream_wait_write(stp) == -1)
- return -1;
+ /*
+ * Linux requires stp->src_fd to be a mmap-able (regular) file,
+ * select() reports regular files to always be "ready", so
+ * there is no need to select() on it.
+ * Other OSes may have the same limitation for sendfile() which
+ * allow us to bypass maygvl_copy_stream_wait_read()...
+ */
+ ret = maygvl_copy_stream_wait_read(0, stp);
+ if (ret < 0) return ret;
+#endif
+ ret = nogvl_copy_stream_wait_write(stp);
+ if (ret < 0) return ret;
+ }
goto retry_sendfile;
}
stp->syserr = "sendfile";
stp->error_no = errno;
- return -1;
+ return (int)ss;
}
return 1;
}
@@ -11060,7 +11134,7 @@ maygvl_copy_stream_read(int has_gvl, struct copy_stream_struct *stp, char *buf,
{
ssize_t ss;
retry_read:
- if (offset == (off_t)-1) {
+ if (offset < (off_t)0) {
ss = maygvl_read(has_gvl, stp->src_fd, buf, len);
}
else {
@@ -11074,7 +11148,7 @@ maygvl_copy_stream_read(int has_gvl, struct copy_stream_struct *stp, char *buf,
if (ss == 0) {
return 0;
}
- if (ss == -1) {
+ if (ss < 0) {
if (maygvl_copy_stream_continue_p(has_gvl, stp))
goto retry_read;
switch (errno) {
@@ -11082,18 +11156,19 @@ maygvl_copy_stream_read(int has_gvl, struct copy_stream_struct *stp, char *buf,
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
- if (maygvl_copy_stream_wait_read(has_gvl, stp) == -1)
- return -1;
+ {
+ int ret = maygvl_copy_stream_wait_read(has_gvl, stp);
+ if (ret < 0) return ret;
+ }
goto retry_read;
#ifdef ENOSYS
case ENOSYS:
stp->notimp = "pread";
- return -1;
+ return ss;
#endif
}
- stp->syserr = offset == (off_t)-1 ? "read" : "pread";
+ stp->syserr = offset < (off_t)0 ? "read" : "pread";
stp->error_no = errno;
- return -1;
}
return ss;
}
@@ -11105,17 +11180,17 @@ nogvl_copy_stream_write(struct copy_stream_struct *stp, char *buf, size_t len)
int off = 0;
while (len) {
ss = write(stp->dst_fd, buf+off, len);
- if (ss == -1) {
- if (maygvl_copy_stream_continue_p(0, stp))
- continue;
+ if (ss < 0) {
+ if (maygvl_copy_stream_continue_p(0, stp))
+ continue;
if (errno == EAGAIN || errno == EWOULDBLOCK) {
- if (nogvl_copy_stream_wait_write(stp) == -1)
- return -1;
+ int ret = nogvl_copy_stream_wait_write(stp);
+ if (ret < 0) return ret;
continue;
}
stp->syserr = "write";
stp->error_no = errno;
- return -1;
+ return (int)ss;
}
off += (int)ss;
len -= (int)ss;
@@ -11137,15 +11212,15 @@ nogvl_copy_stream_read_write(struct copy_stream_struct *stp)
int use_pread;
copy_length = stp->copy_length;
- use_eof = copy_length == (off_t)-1;
+ use_eof = copy_length < (off_t)0;
src_offset = stp->src_offset;
- use_pread = src_offset != (off_t)-1;
+ use_pread = src_offset >= (off_t)0;
if (use_pread && stp->close_src) {
off_t r;
errno = 0;
r = lseek(stp->src_fd, src_offset, SEEK_SET);
- if (r == (off_t)-1 && errno) {
+ if (r < (off_t)0 && errno) {
stp->syserr = "lseek";
stp->error_no = errno;
return;
@@ -11220,7 +11295,7 @@ copy_stream_fallback_body(VALUE arg)
off_t off = stp->src_offset;
ID read_method = id_readpartial;
- if (stp->src_fd == -1) {
+ if (stp->src_fd < 0) {
if (!rb_respond_to(stp->src, read_method)) {
read_method = id_read;
}
@@ -11229,7 +11304,7 @@ copy_stream_fallback_body(VALUE arg)
while (1) {
long numwrote;
long l;
- if (stp->copy_length == (off_t)-1) {
+ if (stp->copy_length < (off_t)0) {
l = buflen;
}
else {
@@ -11239,7 +11314,7 @@ copy_stream_fallback_body(VALUE arg)
}
l = buflen < rest ? buflen : (long)rest;
}
- if (stp->src_fd == -1) {
+ if (stp->src_fd < 0) {
VALUE rc = rb_funcall(stp->src, read_method, 2, INT2FIX(l), buf);
if (read_method == id_read && NIL_P(rc))
@@ -11250,11 +11325,11 @@ copy_stream_fallback_body(VALUE arg)
rb_str_resize(buf, buflen);
ss = maygvl_copy_stream_read(1, stp, RSTRING_PTR(buf), l, off);
rb_str_resize(buf, ss > 0 ? ss : 0);
- if (ss == -1)
+ if (ss < 0)
return Qnil;
if (ss == 0)
rb_eof_error();
- if (off != (off_t)-1)
+ if (off >= (off_t)0)
off += ss;
}
n = rb_io_write(stp->dst, buf);
@@ -11272,7 +11347,7 @@ copy_stream_fallback_body(VALUE arg)
static VALUE
copy_stream_fallback(struct copy_stream_struct *stp)
{
- if (stp->src_fd == -1 && stp->src_offset != (off_t)-1) {
+ if (stp->src_fd < 0 && stp->src_offset >= (off_t)0) {
rb_raise(rb_eArgError, "cannot specify src_offset for non-IO");
}
rb_rescue2(copy_stream_fallback_body, (VALUE)stp,
@@ -11362,10 +11437,10 @@ copy_stream_body(VALUE arg)
if (dst_fptr)
io_ascii8bit_binmode(dst_fptr);
- if (stp->src_offset == (off_t)-1 && src_fptr && src_fptr->rbuf.len) {
+ if (stp->src_offset < (off_t)0 && src_fptr && src_fptr->rbuf.len) {
size_t len = src_fptr->rbuf.len;
VALUE str;
- if (stp->copy_length != (off_t)-1 && stp->copy_length < (off_t)len) {
+ if (stp->copy_length >= (off_t)0 && stp->copy_length < (off_t)len) {
len = (size_t)stp->copy_length;
}
str = rb_str_buf_new(len);
@@ -11379,7 +11454,7 @@ copy_stream_body(VALUE arg)
rb_io_write(dst_io, str);
rb_str_resize(str, 0);
stp->total += len;
- if (stp->copy_length != (off_t)-1)
+ if (stp->copy_length >= (off_t)0)
stp->copy_length -= len;
}
@@ -11390,7 +11465,7 @@ copy_stream_body(VALUE arg)
if (stp->copy_length == 0)
return Qnil;
- if (src_fd == -1 || dst_fd == -1) {
+ if (src_fd < 0 || dst_fd < 0) {
return copy_stream_fallback(stp);
}
diff --git a/iseq.c b/iseq.c
index 164a1f477cfdb1..0b5f7c14e47b21 100644
--- a/iseq.c
+++ b/iseq.c
@@ -111,6 +111,11 @@ rb_iseq_free(const rb_iseq_t *iseq)
compile_data_free(ISEQ_COMPILE_DATA(iseq));
ruby_xfree(body);
}
+
+ if (iseq->local_hooks) {
+ rb_hook_list_free(iseq->local_hooks);
+ }
+
RUBY_FREE_LEAVE("iseq");
}
@@ -247,6 +252,9 @@ rb_iseq_mark(const rb_iseq_t *iseq)
}
}
+ if (iseq->local_hooks) {
+ rb_hook_list_mark(iseq->local_hooks);
+ }
if (FL_TEST(iseq, ISEQ_NOT_LOADED_YET)) {
rb_gc_mark(iseq->aux.loader.obj);
@@ -371,7 +379,7 @@ rb_iseq_pathobj_set(const rb_iseq_t *iseq, VALUE path, VALUE realpath)
}
static rb_iseq_location_t *
-iseq_location_setup(rb_iseq_t *iseq, VALUE name, VALUE path, VALUE realpath, VALUE first_lineno, const rb_code_location_t *code_location)
+iseq_location_setup(rb_iseq_t *iseq, VALUE name, VALUE path, VALUE realpath, VALUE first_lineno, const rb_code_location_t *code_location, const int node_id)
{
rb_iseq_location_t *loc = &iseq->body->location;
@@ -380,6 +388,7 @@ iseq_location_setup(rb_iseq_t *iseq, VALUE name, VALUE path, VALUE realpath, VAL
RB_OBJ_WRITE(iseq, &loc->base_label, name);
loc->first_lineno = first_lineno;
if (code_location) {
+ loc->node_id = node_id;
loc->code_location = *code_location;
}
else {
@@ -420,7 +429,7 @@ set_relation(rb_iseq_t *iseq, const rb_iseq_t *piseq)
static VALUE
prepare_iseq_build(rb_iseq_t *iseq,
- VALUE name, VALUE path, VALUE realpath, VALUE first_lineno, const rb_code_location_t *code_location,
+ VALUE name, VALUE path, VALUE realpath, VALUE first_lineno, const rb_code_location_t *code_location, const int node_id,
const rb_iseq_t *parent, enum iseq_type type,
const rb_compile_option_t *option)
{
@@ -435,7 +444,7 @@ prepare_iseq_build(rb_iseq_t *iseq,
set_relation(iseq, parent);
name = rb_fstring(name);
- iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location);
+ iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
if (iseq != body->local_iseq) {
RB_OBJ_WRITE(iseq, &body->location.base_label, body->local_iseq->body->location.label);
}
@@ -510,9 +519,9 @@ rb_iseq_insns_info_decode_positions(const struct rb_iseq_constant_body *body)
void
rb_iseq_init_trace(rb_iseq_t *iseq)
{
- iseq->aux.trace_events = 0;
- if (ruby_vm_event_enabled_flags & ISEQ_TRACE_EVENTS) {
- rb_iseq_trace_set(iseq, ruby_vm_event_enabled_flags & ISEQ_TRACE_EVENTS);
+ iseq->aux.global_trace_events = 0;
+ if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
+ rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
}
}
@@ -704,7 +713,7 @@ rb_iseq_new_with_opt(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE rea
new_opt = option ? *option : COMPILE_OPTION_DEFAULT;
if (ast && ast->compile_option) rb_iseq_make_compile_option(&new_opt, ast->compile_option);
- prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, parent, type, &new_opt);
+ prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, node ? nd_node_id(node) : -1, parent, type, &new_opt);
rb_iseq_compile_node(iseq, node);
finish_iseq_build(iseq);
@@ -721,7 +730,7 @@ rb_iseq_new_ifunc(const struct vm_ifunc *ifunc, VALUE name, VALUE path, VALUE re
rb_iseq_t *iseq = iseq_alloc();
if (!option) option = &COMPILE_OPTION_DEFAULT;
- prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, parent, type, option);
+ prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, type, option);
rb_iseq_compile_ifunc(iseq, ifunc);
finish_iseq_build(iseq);
@@ -780,7 +789,7 @@ iseq_load(VALUE data, const rb_iseq_t *parent, VALUE opt)
rb_iseq_t *iseq = iseq_alloc();
VALUE magic, version1, version2, format_type, misc;
- VALUE name, path, realpath, first_lineno, code_location;
+ VALUE name, path, realpath, first_lineno, code_location, node_id;
VALUE type, body, locals, params, exception;
st_data_t iseq_type;
@@ -821,6 +830,8 @@ iseq_load(VALUE data, const rb_iseq_t *parent, VALUE opt)
rb_raise(rb_eTypeError, "unsupport type: :%"PRIsVALUE, rb_sym2str(type));
}
+ node_id = rb_hash_aref(misc, ID2SYM(rb_intern("node_id")));
+
code_location = rb_hash_aref(misc, ID2SYM(rb_intern("code_location")));
if (RB_TYPE_P(code_location, T_ARRAY) && RARRAY_LEN(code_location) == 4) {
tmp_loc.beg_pos.lineno = NUM2INT(rb_ary_entry(code_location, 0));
@@ -831,7 +842,7 @@ iseq_load(VALUE data, const rb_iseq_t *parent, VALUE opt)
make_compile_option(&option, opt);
option.peephole_optimization = FALSE; /* because peephole optimization can modify original iseq */
- prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc,
+ prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc, NUM2INT(node_id),
parent, (enum iseq_type)iseq_type, &option);
rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
@@ -1665,7 +1676,7 @@ rb_iseq_clear_event_flags(const rb_iseq_t *iseq, size_t pos, rb_event_flag_t res
struct iseq_insn_info_entry *entry = (struct iseq_insn_info_entry *)get_insn_info(iseq, pos);
if (entry) {
entry->events &= ~reset;
- if (!(entry->events & iseq->aux.trace_events)) {
+ if (!(entry->events & iseq->aux.global_trace_events)) {
void rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos);
rb_iseq_trace_flag_cleared(iseq, pos);
}
@@ -1936,8 +1947,7 @@ rb_iseq_disasm_insn(VALUE ret, const VALUE *code, size_t pos,
events & RUBY_EVENT_B_CALL ? "Bc" : "",
events & RUBY_EVENT_B_RETURN ? "Br" : "",
events & RUBY_EVENT_COVERAGE_LINE ? "Cli" : "",
- events & RUBY_EVENT_COVERAGE_BRANCH ? "Cbr" : ""
- );
+ events & RUBY_EVENT_COVERAGE_BRANCH ? "Cbr" : "");
}
}
@@ -2134,47 +2144,6 @@ rb_iseq_disasm(const rb_iseq_t *iseq)
return rb_iseq_disasm_recursive(iseq, rb_str_new(0, 0));
}
-static VALUE
-rb_iseq_all_children(const rb_iseq_t *iseq)
-{
- unsigned int i;
- VALUE *code = rb_iseq_original_iseq(iseq);
- VALUE all_children = rb_obj_hide(rb_ident_hash_new());
- VALUE child;
- const struct rb_iseq_constant_body *const body = iseq->body;
-
- if (body->catch_table) {
- for (i = 0; i < body->catch_table->size; i++) {
- const struct iseq_catch_table_entry *entry = &body->catch_table->entries[i];
- child = (VALUE)entry->iseq;
- if (child) {
- rb_hash_aset(all_children, child, Qtrue);
- }
- }
- }
- for (i=0; iiseq_size;) {
- VALUE insn = code[i];
- int len = insn_len(insn);
- const char *types = insn_op_types(insn);
- int j;
-
- for (j=0; types[j]; j++) {
- switch (types[j]) {
- case TS_ISEQ:
- child = code[i+j+1];
- if (child) {
- rb_hash_aset(all_children, child, Qtrue);
- }
- break;
- default:
- break;
- }
- }
- i += len;
- }
- return all_children;
-}
-
/*
* call-seq:
* iseq.disasm -> str
@@ -2200,10 +2169,58 @@ iseqw_disasm(VALUE self)
}
static int
-iseqw_each_child_i(VALUE key, VALUE value, VALUE dummy)
+iseq_iterate_children(const rb_iseq_t *iseq, void (*iter_func)(const rb_iseq_t *child_iseq, void *data), void *data)
{
- rb_yield(iseqw_new((const rb_iseq_t *)key));
- return ST_CONTINUE;
+ unsigned int i;
+ VALUE *code = rb_iseq_original_iseq(iseq);
+ const struct rb_iseq_constant_body *const body = iseq->body;
+ const rb_iseq_t *child;
+ VALUE all_children = rb_obj_hide(rb_ident_hash_new());
+
+ if (body->catch_table) {
+ for (i = 0; i < body->catch_table->size; i++) {
+ const struct iseq_catch_table_entry *entry = &body->catch_table->entries[i];
+ child = entry->iseq;
+ if (child) {
+ if (rb_hash_aref(all_children, (VALUE)child) == Qnil) {
+ rb_hash_aset(all_children, (VALUE)child, Qtrue);
+ (*iter_func)(child, data);
+ }
+ }
+ }
+ }
+
+ for (i=0; iiseq_size;) {
+ VALUE insn = code[i];
+ int len = insn_len(insn);
+ const char *types = insn_op_types(insn);
+ int j;
+
+ for (j=0; types[j]; j++) {
+ switch (types[j]) {
+ case TS_ISEQ:
+ child = (const rb_iseq_t *)code[i+j+1];
+ if (child) {
+ if (rb_hash_aref(all_children, (VALUE)child) == Qnil) {
+ rb_hash_aset(all_children, (VALUE)child, Qtrue);
+ (*iter_func)(child, data);
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ i += len;
+ }
+
+ return (int)RHASH_SIZE(all_children);
+}
+
+static void
+yield_each_children(const rb_iseq_t *child_iseq, void *data)
+{
+ rb_yield(iseqw_new(child_iseq));
}
/*
@@ -2218,8 +2235,7 @@ static VALUE
iseqw_each_child(VALUE self)
{
const rb_iseq_t *iseq = iseqw_check(self);
- VALUE all_children = rb_iseq_all_children(iseq);
- rb_hash_foreach(all_children, iseqw_each_child_i, Qnil);
+ iseq_iterate_children(iseq, yield_each_children, NULL);
return self;
}
@@ -2304,14 +2320,17 @@ iseqw_s_of(VALUE klass, VALUE body)
rb_secure(1);
if (rb_obj_is_proc(body)) {
- iseq = vm_proc_iseq(body);
+ iseq = vm_proc_iseq(body);
- if (!rb_obj_is_iseq((VALUE)iseq)) {
- iseq = NULL;
- }
+ if (!rb_obj_is_iseq((VALUE)iseq)) {
+ iseq = NULL;
+ }
}
- else {
- iseq = rb_method_iseq(body);
+ else if (rb_obj_is_method(body)) {
+ iseq = rb_method_iseq(body);
+ }
+ else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
+ return body;
}
return iseq ? iseqw_new(iseq) : Qnil;
@@ -2730,6 +2749,7 @@ iseq_data_to_ary(const rb_iseq_t *iseq)
rb_hash_aset(misc, ID2SYM(rb_intern("arg_size")), INT2FIX(iseq_body->param.size));
rb_hash_aset(misc, ID2SYM(rb_intern("local_size")), INT2FIX(iseq_body->local_table_size));
rb_hash_aset(misc, ID2SYM(rb_intern("stack_max")), INT2FIX(iseq_body->stack_max));
+ rb_hash_aset(misc, ID2SYM(rb_intern("node_id")), INT2FIX(iseq_body->location.node_id));
rb_hash_aset(misc, ID2SYM(rb_intern("code_location")),
rb_ary_new_from_args(4,
INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
@@ -2962,12 +2982,131 @@ rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos)
encoded_iseq_trace_instrument(&iseq_encoded[pos], 0);
}
+static int
+iseq_add_local_tracepoint(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line)
+{
+ unsigned int pc;
+ int n = 0;
+ const struct rb_iseq_constant_body *const body = iseq->body;
+ VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
+
+ VM_ASSERT((iseq->flags & ISEQ_USE_COMPILE_DATA) == 0);
+
+ for (pc=0; pciseq_size;) {
+ const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pc);
+ rb_event_flag_t pc_events = entry->events;
+ rb_event_flag_t target_events = turnon_events;
+ unsigned int line = (int)entry->line_no;
+
+ if (target_line == 0 || target_line == line) {
+ /* ok */
+ }
+ else {
+ target_events &= ~RUBY_EVENT_LINE;
+ }
+
+ if (pc_events & target_events) {
+ n++;
+ }
+ pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.global_trace_events));
+ }
+
+ if (n > 0) {
+ if (iseq->local_hooks == NULL) {
+ ((rb_iseq_t *)iseq)->local_hooks = RB_ZALLOC(rb_hook_list_t);
+ }
+ rb_hook_list_connect_tracepoint((VALUE)iseq, iseq->local_hooks, tpval, target_line);
+ }
+
+ return n;
+}
+
+struct trace_set_local_events_struct {
+ rb_event_flag_t turnon_events;
+ VALUE tpval;
+ unsigned int target_line;
+ int n;
+};
+
+static void
+iseq_add_local_tracepoint_i(const rb_iseq_t *iseq, void *p)
+{
+ struct trace_set_local_events_struct *data = (struct trace_set_local_events_struct *)p;
+ data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line);
+ iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
+}
+
+int
+rb_iseq_add_local_tracepoint_recursively(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line)
+{
+ struct trace_set_local_events_struct data;
+ data.turnon_events = turnon_events;
+ data.tpval = tpval;
+ data.target_line = target_line;
+ data.n = 0;
+
+ iseq_add_local_tracepoint_i(iseq, (void *)&data);
+ if (0) rb_funcall(Qnil, rb_intern("puts"), 1, rb_iseq_disasm(iseq)); /* for debug */
+ return data.n;
+}
+
+static int
+iseq_remove_local_tracepoint(const rb_iseq_t *iseq, VALUE tpval)
+{
+ int n = 0;
+
+ if (iseq->local_hooks) {
+ unsigned int pc;
+ const struct rb_iseq_constant_body *const body = iseq->body;
+ VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
+ rb_event_flag_t local_events = 0;
+
+ rb_hook_list_remove_tracepoint(iseq->local_hooks, tpval);
+ local_events = iseq->local_hooks->events;
+
+ if (local_events == 0) {
+ if (iseq->local_hooks->running == 0) {
+ rb_hook_list_free(iseq->local_hooks);
+ }
+ ((rb_iseq_t *)iseq)->local_hooks = NULL;
+ }
+
+ for (pc = 0; pciseq_size;) {
+ rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
+ pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.global_trace_events));
+ }
+ }
+ return n;
+}
+
+struct trace_clear_local_events_struct {
+ VALUE tpval;
+ int n;
+};
+
+static void
+iseq_remove_local_tracepoint_i(const rb_iseq_t *iseq, void *p)
+{
+ struct trace_clear_local_events_struct *data = (struct trace_clear_local_events_struct *)p;
+ data->n += iseq_remove_local_tracepoint(iseq, data->tpval);
+ iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
+}
+
+int
+rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval)
+{
+ struct trace_clear_local_events_struct data;
+ data.tpval = tpval;
+ data.n = 0;
+
+ iseq_remove_local_tracepoint_i(iseq, (void *)&data);
+ return data.n;
+}
+
void
rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events)
{
- VM_ASSERT((turnon_events & ~ISEQ_TRACE_EVENTS) == 0);
-
- if (iseq->aux.trace_events == turnon_events) {
+ if (iseq->aux.global_trace_events == turnon_events) {
return;
}
if (iseq->flags & ISEQ_USE_COMPILE_DATA) {
@@ -2975,16 +3114,18 @@ rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events)
return;
}
else {
- unsigned int i;
+ unsigned int pc;
const struct rb_iseq_constant_body *const body = iseq->body;
VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
- ((rb_iseq_t *)iseq)->aux.trace_events = turnon_events;
-
- for (i=0; iiseq_size;) {
- rb_event_flag_t events = rb_iseq_event_flags(iseq, i);
- i += encoded_iseq_trace_instrument(&iseq_encoded[i], events & turnon_events);
+ rb_event_flag_t enabled_events;
+ rb_event_flag_t local_events = iseq->local_hooks ? iseq->local_hooks->events : 0;
+ ((rb_iseq_t *)iseq)->aux.global_trace_events = turnon_events;
+ enabled_events = turnon_events | local_events;
+
+ for (pc=0; pciseq_size;) {
+ rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
+ pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events);
}
- /* clear for debugging: ISEQ_ORIGINAL_ISEQ_CLEAR(iseq); */
}
}
@@ -3009,7 +3150,7 @@ rb_iseq_trace_set_all(rb_event_flag_t turnon_events)
}
/* This is exported since Ruby 2.5 but not internally used for now. If you're going to use this, please
- update `ruby_vm_event_enabled_flags` and set `mjit_call_p = FALSE` as well to cancel MJIT code. */
+ update `ruby_vm_event_enabled_global_flags` and set `mjit_call_p = FALSE` as well to cancel MJIT code. */
void
rb_iseq_trace_on_all(void)
{
diff --git a/iseq.h b/iseq.h
index 370e16cf442794..abf93ff594f9b4 100644
--- a/iseq.h
+++ b/iseq.h
@@ -148,6 +148,8 @@ void rb_ibf_load_iseq_complete(rb_iseq_t *iseq);
const rb_iseq_t *rb_iseq_ibf_load(VALUE str);
VALUE rb_iseq_ibf_load_extra_data(VALUE str);
void rb_iseq_init_trace(rb_iseq_t *iseq);
+int rb_iseq_add_local_tracepoint_recursively(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line);
+int rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval);
#if VM_INSN_INFO_TABLE_IMPL == 2
unsigned int *rb_iseq_insns_info_decode_positions(const struct rb_iseq_constant_body *body);
diff --git a/lib/bundler.gemspec b/lib/bundler.gemspec
new file mode 100644
index 00000000000000..2b2bb412d501ea
--- /dev/null
+++ b/lib/bundler.gemspec
@@ -0,0 +1,64 @@
+# coding: utf-8
+# frozen_string_literal: true
+
+begin
+ require File.expand_path("../lib/bundler/version", __FILE__)
+rescue LoadError
+ # for Ruby core repository
+ require File.expand_path("../bundler/version", __FILE__)
+end
+
+require "shellwords"
+
+Gem::Specification.new do |s|
+ s.name = "bundler"
+ s.version = Bundler::VERSION
+ s.license = "MIT"
+ s.authors = [
+ "André Arko", "Samuel Giddins", "Colby Swandale", "Hiroshi Shibata",
+ "David RodrÃguez", "Grey Baker", "Stephanie Morillo", "Chris Morris", "James Wen", "Tim Moore",
+ "André Medeiros", "Jessica Lynn Suttles", "Terence Lee", "Carl Lerche",
+ "Yehuda Katz"
+ ]
+ s.email = ["team@bundler.io"]
+ s.homepage = "http://bundler.io"
+ s.summary = "The best way to manage your application's dependencies"
+ s.description = "Bundler manages an application's dependencies through its entire life, across many machines, systematically and repeatably"
+
+ if s.respond_to?(:metadata=)
+ s.metadata = {
+ "bug_tracker_uri" => "http://github.com/bundler/bundler/issues",
+ "changelog_uri" => "https://github.com/bundler/bundler/blob/master/CHANGELOG.md",
+ "homepage_uri" => "https://bundler.io/",
+ "source_code_uri" => "http://github.com/bundler/bundler/",
+ }
+ end
+
+ if s.version >= Gem::Version.new("2.a".dup)
+ s.required_ruby_version = ">= 2.3.0"
+ s.required_rubygems_version = ">= 2.5.0"
+ else
+ s.required_ruby_version = ">= 1.8.7"
+ s.required_rubygems_version = ">= 1.3.6"
+ end
+
+ s.add_development_dependency "automatiek", "~> 0.1.0"
+ s.add_development_dependency "mustache", "0.99.6"
+ s.add_development_dependency "rake", "~> 10.0"
+ s.add_development_dependency "rdiscount", "~> 2.2"
+ s.add_development_dependency "ronn", "~> 0.7.3"
+ s.add_development_dependency "rspec", "~> 3.6"
+
+ # s.files = `git ls-files -z`.split("\x0").select {|f| f.match(%r{^(lib|exe)/}) }
+ # we don't check in man pages, but we need to ship them because
+ # we use them to generate the long-form help for each command.
+ # s.files += Dir.glob("man/**/*")
+ # Include the CHANGELOG.md, LICENSE.md, README.md manually
+ # s.files += %w[CHANGELOG.md LICENSE.md README.md]
+ # include the gemspec itself because warbler breaks w/o it
+ s.files += %w[bundler.gemspec]
+
+ s.bindir = "exe"
+ s.executables = %w[bundle bundler]
+ s.require_paths = ["lib"]
+end
diff --git a/lib/bundler.rb b/lib/bundler.rb
new file mode 100644
index 00000000000000..cf3a289df2b40c
--- /dev/null
+++ b/lib/bundler.rb
@@ -0,0 +1,567 @@
+# frozen_string_literal: true
+
+require "bundler/compatibility_guard"
+
+require "bundler/vendored_fileutils"
+require "pathname"
+require "rbconfig"
+require "thread"
+
+require "bundler/errors"
+require "bundler/environment_preserver"
+require "bundler/plugin"
+require "bundler/rubygems_ext"
+require "bundler/rubygems_integration"
+require "bundler/version"
+require "bundler/constants"
+require "bundler/current_ruby"
+require "bundler/build_metadata"
+
+module Bundler
+ environment_preserver = EnvironmentPreserver.new(ENV, EnvironmentPreserver::BUNDLER_KEYS)
+ ORIGINAL_ENV = environment_preserver.restore
+ ENV.replace(environment_preserver.backup)
+ SUDO_MUTEX = Mutex.new
+
+ autoload :Definition, "bundler/definition"
+ autoload :Dependency, "bundler/dependency"
+ autoload :DepProxy, "bundler/dep_proxy"
+ autoload :Deprecate, "bundler/deprecate"
+ autoload :Dsl, "bundler/dsl"
+ autoload :EndpointSpecification, "bundler/endpoint_specification"
+ autoload :Env, "bundler/env"
+ autoload :Fetcher, "bundler/fetcher"
+ autoload :FeatureFlag, "bundler/feature_flag"
+ autoload :GemHelper, "bundler/gem_helper"
+ autoload :GemHelpers, "bundler/gem_helpers"
+ autoload :GemRemoteFetcher, "bundler/gem_remote_fetcher"
+ autoload :GemVersionPromoter, "bundler/gem_version_promoter"
+ autoload :Graph, "bundler/graph"
+ autoload :Index, "bundler/index"
+ autoload :Injector, "bundler/injector"
+ autoload :Installer, "bundler/installer"
+ autoload :LazySpecification, "bundler/lazy_specification"
+ autoload :LockfileParser, "bundler/lockfile_parser"
+ autoload :MatchPlatform, "bundler/match_platform"
+ autoload :ProcessLock, "bundler/process_lock"
+ autoload :RemoteSpecification, "bundler/remote_specification"
+ autoload :Resolver, "bundler/resolver"
+ autoload :Retry, "bundler/retry"
+ autoload :RubyDsl, "bundler/ruby_dsl"
+ autoload :RubyGemsGemInstaller, "bundler/rubygems_gem_installer"
+ autoload :RubyVersion, "bundler/ruby_version"
+ autoload :Runtime, "bundler/runtime"
+ autoload :Settings, "bundler/settings"
+ autoload :SharedHelpers, "bundler/shared_helpers"
+ autoload :Source, "bundler/source"
+ autoload :SourceList, "bundler/source_list"
+ autoload :SpecSet, "bundler/spec_set"
+ autoload :StubSpecification, "bundler/stub_specification"
+ autoload :UI, "bundler/ui"
+ autoload :URICredentialsFilter, "bundler/uri_credentials_filter"
+ autoload :VersionRanges, "bundler/version_ranges"
+
+ class << self
+ def configure
+ @configured ||= configure_gem_home_and_path
+ end
+
+ def ui
+ (defined?(@ui) && @ui) || (self.ui = UI::Silent.new)
+ end
+
+ def ui=(ui)
+ Bundler.rubygems.ui = ui ? UI::RGProxy.new(ui) : nil
+ @ui = ui
+ end
+
+ # Returns absolute path of where gems are installed on the filesystem.
+ def bundle_path
+ @bundle_path ||= Pathname.new(configured_bundle_path.path).expand_path(root)
+ end
+
+ def configured_bundle_path
+ @configured_bundle_path ||= settings.path.tap(&:validate!)
+ end
+
+ # Returns absolute location of where binstubs are installed to.
+ def bin_path
+ @bin_path ||= begin
+ path = settings[:bin] || "bin"
+ path = Pathname.new(path).expand_path(root).expand_path
+ SharedHelpers.filesystem_access(path) {|p| FileUtils.mkdir_p(p) }
+ path
+ end
+ end
+
+ def setup(*groups)
+ # Return if all groups are already loaded
+ return @setup if defined?(@setup) && @setup
+
+ definition.validate_runtime!
+
+ SharedHelpers.print_major_deprecations!
+
+ if groups.empty?
+ # Load all groups, but only once
+ @setup = load.setup
+ else
+ load.setup(*groups)
+ end
+ end
+
+ def require(*groups)
+ setup(*groups).require(*groups)
+ end
+
+ def load
+ @load ||= Runtime.new(root, definition)
+ end
+
+ def environment
+ SharedHelpers.major_deprecation 3, "Bundler.environment has been removed in favor of Bundler.load"
+ load
+ end
+
+ # Returns an instance of Bundler::Definition for given Gemfile and lockfile
+ #
+ # @param unlock [Hash, Boolean, nil] Gems that have been requested
+ # to be updated or true if all gems should be updated
+ # @return [Bundler::Definition]
+ def definition(unlock = nil)
+ @definition = nil if unlock
+ @definition ||= begin
+ configure
+ Definition.build(default_gemfile, default_lockfile, unlock)
+ end
+ end
+
+ def frozen_bundle?
+ frozen = settings[:deployment]
+ frozen ||= settings[:frozen] unless feature_flag.deployment_means_frozen?
+ frozen
+ end
+
+ def locked_gems
+ @locked_gems ||=
+ if defined?(@definition) && @definition
+ definition.locked_gems
+ elsif Bundler.default_lockfile.file?
+ lock = Bundler.read_file(Bundler.default_lockfile)
+ LockfileParser.new(lock)
+ end
+ end
+
+ def ruby_scope
+ "#{Bundler.rubygems.ruby_engine}/#{Bundler.rubygems.config_map[:ruby_version]}"
+ end
+
+ def user_home
+ @user_home ||= begin
+ home = Bundler.rubygems.user_home
+ bundle_home = home ? File.join(home, ".bundle") : nil
+
+ warning = if home.nil?
+ "Your home directory is not set."
+ elsif !File.directory?(home)
+ "`#{home}` is not a directory."
+ elsif !File.writable?(home) && (!File.directory?(bundle_home) || !File.writable?(bundle_home))
+ "`#{home}` is not writable."
+ end
+
+ if warning
+ Kernel.send(:require, "etc")
+ user_home = tmp_home_path(Etc.getlogin, warning)
+ Bundler.ui.warn "#{warning}\nBundler will use `#{user_home}' as your home directory temporarily.\n"
+ user_home
+ else
+ Pathname.new(home)
+ end
+ end
+ end
+
+ def tmp_home_path(login, warning)
+ login ||= "unknown"
+ Kernel.send(:require, "tmpdir")
+ path = Pathname.new(Dir.tmpdir).join("bundler", "home")
+ SharedHelpers.filesystem_access(path) do |tmp_home_path|
+ unless tmp_home_path.exist?
+ tmp_home_path.mkpath
+ tmp_home_path.chmod(0o777)
+ end
+ tmp_home_path.join(login).tap(&:mkpath)
+ end
+ rescue RuntimeError => e
+ raise e.exception("#{warning}\nBundler also failed to create a temporary home directory at `#{path}':\n#{e}")
+ end
+
+ def user_bundle_path(dir = "home")
+ env_var, fallback = case dir
+ when "home"
+ ["BUNDLE_USER_HOME", Pathname.new(user_home).join(".bundle")]
+ when "cache"
+ ["BUNDLE_USER_CACHE", user_bundle_path.join("cache")]
+ when "config"
+ ["BUNDLE_USER_CONFIG", user_bundle_path.join("config")]
+ when "plugin"
+ ["BUNDLE_USER_PLUGIN", user_bundle_path.join("plugin")]
+ else
+ raise BundlerError, "Unknown user path requested: #{dir}"
+ end
+ # `fallback` will already be a Pathname, but Pathname.new() is
+ # idempotent so it's OK
+ Pathname.new(ENV.fetch(env_var, fallback))
+ end
+
+ def user_cache
+ user_bundle_path("cache")
+ end
+
+ def home
+ bundle_path.join("bundler")
+ end
+
+ def install_path
+ home.join("gems")
+ end
+
+ def specs_path
+ bundle_path.join("specifications")
+ end
+
+ def root
+ @root ||= begin
+ SharedHelpers.root
+ rescue GemfileNotFound
+ bundle_dir = default_bundle_dir
+ raise GemfileNotFound, "Could not locate Gemfile or .bundle/ directory" unless bundle_dir
+ Pathname.new(File.expand_path("..", bundle_dir))
+ end
+ end
+
+ def app_config_path
+ if app_config = ENV["BUNDLE_APP_CONFIG"]
+ Pathname.new(app_config).expand_path(root)
+ else
+ root.join(".bundle")
+ end
+ end
+
+ def app_cache(custom_path = nil)
+ path = custom_path || root
+ Pathname.new(path).join(settings.app_cache_path)
+ end
+
+ def tmp(name = Process.pid.to_s)
+ Kernel.send(:require, "tmpdir")
+ Pathname.new(Dir.mktmpdir(["bundler", name]))
+ end
+
+ def rm_rf(path)
+ FileUtils.remove_entry_secure(path) if path && File.exist?(path)
+ rescue ArgumentError
+ message = < e
+ raise MarshalError, "#{e.class}: #{e.message}"
+ end
+
+ def load_gemspec(file, validate = false)
+ @gemspec_cache ||= {}
+ key = File.expand_path(file)
+ @gemspec_cache[key] ||= load_gemspec_uncached(file, validate)
+ # Protect against caching side-effected gemspecs by returning a
+ # new instance each time.
+ @gemspec_cache[key].dup if @gemspec_cache[key]
+ end
+
+ def load_gemspec_uncached(file, validate = false)
+ path = Pathname.new(file)
+ contents = read_file(file)
+ spec = if contents.start_with?("---") # YAML header
+ eval_yaml_gemspec(path, contents)
+ else
+ # Eval the gemspec from its parent directory, because some gemspecs
+ # depend on "./" relative paths.
+ SharedHelpers.chdir(path.dirname.to_s) do
+ eval_gemspec(path, contents)
+ end
+ end
+ return unless spec
+ spec.loaded_from = path.expand_path.to_s
+ Bundler.rubygems.validate(spec) if validate
+ spec
+ end
+
+ def clear_gemspec_cache
+ @gemspec_cache = {}
+ end
+
+ def git_present?
+ return @git_present if defined?(@git_present)
+ @git_present = Bundler.which("git") || Bundler.which("git.exe")
+ end
+
+ def feature_flag
+ @feature_flag ||= FeatureFlag.new(VERSION)
+ end
+
+ def reset!
+ reset_paths!
+ Plugin.reset!
+ reset_rubygems!
+ end
+
+ def reset_paths!
+ @bin_path = nil
+ @bundler_major_version = nil
+ @bundle_path = nil
+ @configured = nil
+ @configured_bundle_path = nil
+ @definition = nil
+ @load = nil
+ @locked_gems = nil
+ @root = nil
+ @settings = nil
+ @setup = nil
+ @user_home = nil
+ end
+
+ def reset_rubygems!
+ return unless defined?(@rubygems) && @rubygems
+ rubygems.undo_replacements
+ rubygems.reset
+ @rubygems = nil
+ end
+
+ private
+
+ def eval_yaml_gemspec(path, contents)
+ Kernel.send(:require, "bundler/psyched_yaml")
+
+ # If the YAML is invalid, Syck raises an ArgumentError, and Psych
+ # raises a Psych::SyntaxError. See psyched_yaml.rb for more info.
+ Gem::Specification.from_yaml(contents)
+ rescue YamlLibrarySyntaxError, ArgumentError, Gem::EndOfYAMLException, Gem::Exception
+ eval_gemspec(path, contents)
+ end
+
+ def eval_gemspec(path, contents)
+ eval(contents, TOPLEVEL_BINDING.dup, path.expand_path.to_s)
+ rescue ScriptError, StandardError => e
+ msg = "There was an error while loading `#{path.basename}`: #{e.message}"
+
+ if e.is_a?(LoadError) && RUBY_VERSION >= "1.9"
+ msg += "\nDoes it try to require a relative path? That's been removed in Ruby 1.9"
+ end
+
+ raise GemspecError, Dsl::DSLError.new(msg, path, e.backtrace, contents)
+ end
+
+ def configure_gem_home_and_path
+ configure_gem_path
+ configure_gem_home
+ bundle_path
+ end
+
+ def configure_gem_path(env = ENV)
+ blank_home = env["GEM_HOME"].nil? || env["GEM_HOME"].empty?
+ if !use_system_gems?
+ # this needs to be empty string to cause
+ # PathSupport.split_gem_path to only load up the
+ # Bundler --path setting as the GEM_PATH.
+ env["GEM_PATH"] = ""
+ elsif blank_home
+ possibles = [Bundler.rubygems.gem_dir, Bundler.rubygems.gem_path]
+ paths = possibles.flatten.compact.uniq.reject(&:empty?)
+ env["GEM_PATH"] = paths.join(File::PATH_SEPARATOR)
+ end
+ end
+
+ def configure_gem_home
+ Bundler::SharedHelpers.set_env "GEM_HOME", File.expand_path(bundle_path, root)
+ Bundler.rubygems.clear_paths
+ end
+
+ # @param env [Hash]
+ def with_env(env)
+ backup = ENV.to_hash
+ ENV.replace(env)
+ yield
+ ensure
+ ENV.replace(backup)
+ end
+ end
+end
diff --git a/lib/bundler/build_metadata.rb b/lib/bundler/build_metadata.rb
new file mode 100644
index 00000000000000..33f91e91622a57
--- /dev/null
+++ b/lib/bundler/build_metadata.rb
@@ -0,0 +1,51 @@
+# frozen_string_literal: true
+
+module Bundler
+ # Represents metadata from when the Bundler gem was built.
+ module BuildMetadata
+ # begin ivars
+ @release = false
+ # end ivars
+
+ # A hash representation of the build metadata.
+ def self.to_h
+ {
+ "Built At" => built_at,
+ "Git SHA" => git_commit_sha,
+ "Released Version" => release?,
+ }
+ end
+
+ # A string representing the date the bundler gem was built.
+ def self.built_at
+ @built_at ||= Time.now.utc.strftime("%Y-%m-%d").freeze
+ end
+
+ # The SHA for the git commit the bundler gem was built from.
+ def self.git_commit_sha
+ return @git_commit_sha if @git_commit_sha
+
+ # If Bundler has been installed without its .git directory and without a
+ # commit instance variable then we can't determine its commits SHA.
+ git_dir = File.join(File.expand_path("../../..", __FILE__), ".git")
+ if File.directory?(git_dir)
+ return @git_commit_sha = Dir.chdir(git_dir) { `git rev-parse --short HEAD`.strip.freeze }
+ end
+
+ # If Bundler is a submodule in RubyGems, get the submodule commit
+ git_sub_dir = File.join(File.expand_path("../../../..", __FILE__), ".git")
+ if File.directory?(git_sub_dir)
+ return @git_commit_sha = Dir.chdir(git_sub_dir) do
+ `git ls-tree --abbrev=8 HEAD bundler`.split(/\s/).fetch(2, "").strip.freeze
+ end
+ end
+
+ @git_commit_sha ||= "unknown"
+ end
+
+ # Whether this is an official release build of Bundler.
+ def self.release?
+ @release
+ end
+ end
+end
diff --git a/lib/bundler/capistrano.rb b/lib/bundler/capistrano.rb
new file mode 100644
index 00000000000000..40e2e5dbe8b9e8
--- /dev/null
+++ b/lib/bundler/capistrano.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+require "bundler/shared_helpers"
+Bundler::SharedHelpers.major_deprecation 3,
+ "The Bundler task for Capistrano. Please use http://github.com/capistrano/bundler"
+
+# Capistrano task for Bundler.
+#
+# Add "require 'bundler/capistrano'" in your Capistrano deploy.rb, and
+# Bundler will be activated after each new deployment.
+require "bundler/deployment"
+require "capistrano/version"
+
+if defined?(Capistrano::Version) && Gem::Version.new(Capistrano::Version).release >= Gem::Version.new("3.0")
+ raise "For Capistrano 3.x integration, please use http://github.com/capistrano/bundler"
+end
+
+Capistrano::Configuration.instance(:must_exist).load do
+ before "deploy:finalize_update", "bundle:install"
+ Bundler::Deployment.define_task(self, :task, :except => { :no_release => true })
+ set :rake, lambda { "#{fetch(:bundle_cmd, "bundle")} exec rake" }
+end
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb
new file mode 100644
index 00000000000000..3efe1936130c81
--- /dev/null
+++ b/lib/bundler/cli.rb
@@ -0,0 +1,790 @@
+# frozen_string_literal: true
+
+require "bundler"
+require "bundler/vendored_thor"
+
+module Bundler
+ class CLI < Thor
+ require "bundler/cli/common"
+
+ package_name "Bundler"
+
+ AUTO_INSTALL_CMDS = %w[show binstubs outdated exec open console licenses clean].freeze
+ PARSEABLE_COMMANDS = %w[
+ check config help exec platform show version
+ ].freeze
+
+ def self.start(*)
+ super
+ rescue Exception => e
+ Bundler.ui = UI::Shell.new
+ raise e
+ ensure
+ Bundler::SharedHelpers.print_major_deprecations!
+ end
+
+ def self.dispatch(*)
+ super do |i|
+ i.send(:print_command)
+ i.send(:warn_on_outdated_bundler)
+ end
+ end
+
+ def initialize(*args)
+ super
+
+ custom_gemfile = options[:gemfile] || Bundler.settings[:gemfile]
+ if custom_gemfile && !custom_gemfile.empty?
+ Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", File.expand_path(custom_gemfile)
+ Bundler.reset_paths!
+ end
+
+ Bundler.settings.set_command_option_if_given :retry, options[:retry]
+
+ current_cmd = args.last[:current_command].name
+ auto_install if AUTO_INSTALL_CMDS.include?(current_cmd)
+ rescue UnknownArgumentError => e
+ raise InvalidOption, e.message
+ ensure
+ self.options ||= {}
+ unprinted_warnings = Bundler.ui.unprinted_warnings
+ Bundler.ui = UI::Shell.new(options)
+ Bundler.ui.level = "debug" if options["verbose"]
+ unprinted_warnings.each {|w| Bundler.ui.warn(w) }
+
+ if ENV["RUBYGEMS_GEMDEPS"] && !ENV["RUBYGEMS_GEMDEPS"].empty?
+ Bundler.ui.warn(
+ "The RUBYGEMS_GEMDEPS environment variable is set. This enables RubyGems' " \
+ "experimental Gemfile mode, which may conflict with Bundler and cause unexpected errors. " \
+ "To remove this warning, unset RUBYGEMS_GEMDEPS.", :wrap => true
+ )
+ end
+ end
+
+ def self.deprecated_option(*args, &blk)
+ return if Bundler.feature_flag.forget_cli_options?
+ method_option(*args, &blk)
+ end
+
+ check_unknown_options!(:except => [:config, :exec])
+ stop_on_unknown_option! :exec
+
+ desc "cli_help", "Prints a summary of bundler commands", :hide => true
+ def cli_help
+ version
+ Bundler.ui.info "\n"
+
+ primary_commands = ["install", "update",
+ Bundler.feature_flag.cache_command_is_package? ? "cache" : "package",
+ "exec", "config", "help"]
+
+ list = self.class.printable_commands(true)
+ by_name = list.group_by {|name, _message| name.match(/^bundle (\w+)/)[1] }
+ utilities = by_name.keys.sort - primary_commands
+ primary_commands.map! {|name| (by_name[name] || raise("no primary command #{name}")).first }
+ utilities.map! {|name| by_name[name].first }
+
+ shell.say "Bundler commands:\n\n"
+
+ shell.say " Primary commands:\n"
+ shell.print_table(primary_commands, :indent => 4, :truncate => true)
+ shell.say
+ shell.say " Utilities:\n"
+ shell.print_table(utilities, :indent => 4, :truncate => true)
+ shell.say
+ self.class.send(:class_options_help, shell)
+ end
+ default_task(Bundler.feature_flag.default_cli_command)
+
+ class_option "no-color", :type => :boolean, :desc => "Disable colorization in output"
+ class_option "retry", :type => :numeric, :aliases => "-r", :banner => "NUM",
+ :desc => "Specify the number of times you wish to attempt network commands"
+ class_option "verbose", :type => :boolean, :desc => "Enable verbose output mode", :aliases => "-V"
+
+ def help(cli = nil)
+ case cli
+ when "gemfile" then command = "gemfile"
+ when nil then command = "bundle"
+ else command = "bundle-#{cli}"
+ end
+
+ man_path = File.expand_path("../../../man", __FILE__)
+ man_pages = Hash[Dir.glob(File.join(man_path, "*")).grep(/.*\.\d*\Z/).collect do |f|
+ [File.basename(f, ".*"), f]
+ end]
+
+ if man_pages.include?(command)
+ if Bundler.which("man") && man_path !~ %r{^file:/.+!/META-INF/jruby.home/.+}
+ Kernel.exec "man #{man_pages[command]}"
+ else
+ puts File.read("#{man_path}/#{File.basename(man_pages[command])}.txt")
+ end
+ elsif command_path = Bundler.which("bundler-#{cli}")
+ Kernel.exec(command_path, "--help")
+ else
+ super
+ end
+ end
+
+ def self.handle_no_command_error(command, has_namespace = $thor_runner)
+ if Bundler.feature_flag.plugins? && Bundler::Plugin.command?(command)
+ return Bundler::Plugin.exec_command(command, ARGV[1..-1])
+ end
+
+ return super unless command_path = Bundler.which("bundler-#{command}")
+
+ Kernel.exec(command_path, *ARGV[1..-1])
+ end
+
+ desc "init [OPTIONS]", "Generates a Gemfile into the current working directory"
+ long_desc <<-D
+ Init generates a default Gemfile in the current working directory. When adding a
+ Gemfile to a gem with a gemspec, the --gemspec option will automatically add each
+ dependency listed in the gemspec file to the newly created Gemfile.
+ D
+ deprecated_option "gemspec", :type => :string, :banner => "Use the specified .gemspec to create the Gemfile"
+ def init
+ require "bundler/cli/init"
+ Init.new(options.dup).run
+ end
+
+ desc "check [OPTIONS]", "Checks if the dependencies listed in Gemfile are satisfied by currently installed gems"
+ long_desc <<-D
+ Check searches the local machine for each of the gems requested in the Gemfile. If
+ all gems are found, Bundler prints a success message and exits with a status of 0.
+ If not, the first missing gem is listed and Bundler exits status 1.
+ D
+ method_option "dry-run", :type => :boolean, :default => false, :banner =>
+ "Lock the Gemfile"
+ method_option "gemfile", :type => :string, :banner =>
+ "Use the specified gemfile instead of Gemfile"
+ method_option "path", :type => :string, :banner =>
+ "Specify a different path than the system default ($BUNDLE_PATH or $GEM_HOME).#{" Bundler will remember this value for future installs on this machine" unless Bundler.feature_flag.forget_cli_options?}"
+ map "c" => "check"
+ def check
+ require "bundler/cli/check"
+ Check.new(options).run
+ end
+
+ desc "remove [GEM [GEM ...]]", "Removes gems from the Gemfile"
+ long_desc <<-D
+ Removes the given gems from the Gemfile while ensuring that the resulting Gemfile is still valid. If the gem is not found, Bundler prints a error message and if gem could not be removed due to any reason Bundler will display a warning.
+ D
+ method_option "install", :type => :boolean, :banner =>
+ "Runs 'bundle install' after removing the gems from the Gemfile"
+ def remove(*gems)
+ require "bundler/cli/remove"
+ Remove.new(gems, options).run
+ end
+
+ desc "install [OPTIONS]", "Install the current environment to the system"
+ long_desc <<-D
+ Install will install all of the gems in the current bundle, making them available
+ for use. In a freshly checked out repository, this command will give you the same
+ gem versions as the last person who updated the Gemfile and ran `bundle update`.
+
+ Passing [DIR] to install (e.g. vendor) will cause the unpacked gems to be installed
+ into the [DIR] directory rather than into system gems.
+
+ If the bundle has already been installed, bundler will tell you so and then exit.
+ D
+ deprecated_option "binstubs", :type => :string, :lazy_default => "bin", :banner =>
+ "Generate bin stubs for bundled gems to ./bin"
+ deprecated_option "clean", :type => :boolean, :banner =>
+ "Run bundle clean automatically after install"
+ deprecated_option "deployment", :type => :boolean, :banner =>
+ "Install using defaults tuned for deployment environments"
+ deprecated_option "frozen", :type => :boolean, :banner =>
+ "Do not allow the Gemfile.lock to be updated after this install"
+ method_option "full-index", :type => :boolean, :banner =>
+ "Fall back to using the single-file index of all gems"
+ method_option "gemfile", :type => :string, :banner =>
+ "Use the specified gemfile instead of Gemfile"
+ method_option "jobs", :aliases => "-j", :type => :numeric, :banner =>
+ "Specify the number of jobs to run in parallel"
+ method_option "local", :type => :boolean, :banner =>
+ "Do not attempt to fetch gems remotely and use the gem cache instead"
+ deprecated_option "no-cache", :type => :boolean, :banner =>
+ "Don't update the existing gem cache."
+ method_option "redownload", :type => :boolean, :aliases => "--force", :banner =>
+ "Force downloading every gem."
+ deprecated_option "no-prune", :type => :boolean, :banner =>
+ "Don't remove stale gems from the cache."
+ deprecated_option "path", :type => :string, :banner =>
+ "Specify a different path than the system default ($BUNDLE_PATH or $GEM_HOME). Bundler will remember this value for future installs on this machine"
+ method_option "quiet", :type => :boolean, :banner =>
+ "Only output warnings and errors."
+ deprecated_option "shebang", :type => :string, :banner =>
+ "Specify a different shebang executable name than the default (usually 'ruby')"
+ method_option "standalone", :type => :array, :lazy_default => [], :banner =>
+ "Make a bundle that can work without the Bundler runtime"
+ deprecated_option "system", :type => :boolean, :banner =>
+ "Install to the system location ($BUNDLE_PATH or $GEM_HOME) even if the bundle was previously installed somewhere else for this application"
+ method_option "trust-policy", :alias => "P", :type => :string, :banner =>
+ "Gem trust policy (like gem install -P). Must be one of " +
+ Bundler.rubygems.security_policy_keys.join("|")
+ deprecated_option "without", :type => :array, :banner =>
+ "Exclude gems that are part of the specified named group."
+ deprecated_option "with", :type => :array, :banner =>
+ "Include gems that are part of the specified named group."
+ map "i" => "install"
+ def install
+ SharedHelpers.major_deprecation(3, "The `--force` option has been renamed to `--redownload`") if ARGV.include?("--force")
+ require "bundler/cli/install"
+ Bundler.settings.temporary(:no_install => false) do
+ Install.new(options.dup).run
+ end
+ end
+
+ desc "update [OPTIONS]", "Update the current environment"
+ long_desc <<-D
+ Update will install the newest versions of the gems listed in the Gemfile. Use
+ update when you have changed the Gemfile, or if you want to get the newest
+ possible versions of the gems in the bundle.
+ D
+ method_option "full-index", :type => :boolean, :banner =>
+ "Fall back to using the single-file index of all gems"
+ method_option "gemfile", :type => :string, :banner =>
+ "Use the specified gemfile instead of Gemfile"
+ method_option "group", :aliases => "-g", :type => :array, :banner =>
+ "Update a specific group"
+ method_option "jobs", :aliases => "-j", :type => :numeric, :banner =>
+ "Specify the number of jobs to run in parallel"
+ method_option "local", :type => :boolean, :banner =>
+ "Do not attempt to fetch gems remotely and use the gem cache instead"
+ method_option "quiet", :type => :boolean, :banner =>
+ "Only output warnings and errors."
+ method_option "source", :type => :array, :banner =>
+ "Update a specific source (and all gems associated with it)"
+ method_option "redownload", :type => :boolean, :aliases => "--force", :banner =>
+ "Force downloading every gem."
+ method_option "ruby", :type => :boolean, :banner =>
+ "Update ruby specified in Gemfile.lock"
+ method_option "bundler", :type => :string, :lazy_default => "> 0.a", :banner =>
+ "Update the locked version of bundler"
+ method_option "patch", :type => :boolean, :banner =>
+ "Prefer updating only to next patch version"
+ method_option "minor", :type => :boolean, :banner =>
+ "Prefer updating only to next minor version"
+ method_option "major", :type => :boolean, :banner =>
+ "Prefer updating to next major version (default)"
+ method_option "strict", :type => :boolean, :banner =>
+ "Do not allow any gem to be updated past latest --patch | --minor | --major"
+ method_option "conservative", :type => :boolean, :banner =>
+ "Use bundle install conservative update behavior and do not allow shared dependencies to be updated."
+ method_option "all", :type => :boolean, :banner =>
+ "Update everything."
+ def update(*gems)
+ SharedHelpers.major_deprecation(3, "The `--force` option has been renamed to `--redownload`") if ARGV.include?("--force")
+ require "bundler/cli/update"
+ Update.new(options, gems).run
+ end
+
+ desc "show GEM [OPTIONS]", "Shows all gems that are part of the bundle, or the path to a given gem"
+ long_desc <<-D
+ Show lists the names and versions of all gems that are required by your Gemfile.
+ Calling show with [GEM] will list the exact location of that gem on your machine.
+ D
+ method_option "paths", :type => :boolean,
+ :banner => "List the paths of all gems that are required by your Gemfile."
+ method_option "outdated", :type => :boolean,
+ :banner => "Show verbose output including whether gems are outdated."
+ def show(gem_name = nil)
+ if ARGV[0] == "show"
+ rest = ARGV[1..-1]
+
+ new_command = rest.find {|arg| !arg.start_with?("--") } ? "info" : "list"
+
+ new_arguments = rest.map do |arg|
+ next arg if arg != "--paths"
+ next "--path" if new_command == "info"
+ end
+
+ old_argv = ARGV.join(" ")
+ new_argv = [new_command, *new_arguments.compact].join(" ")
+
+ Bundler::SharedHelpers.major_deprecation(3, "use `bundle #{new_argv}` instead of `bundle #{old_argv}`")
+ end
+ require "bundler/cli/show"
+ Show.new(options, gem_name).run
+ end
+ # TODO: 2.0 remove `bundle show`
+
+ if Bundler.feature_flag.list_command?
+ desc "list", "List all gems in the bundle"
+ method_option "name-only", :type => :boolean, :banner => "print only the gem names"
+ method_option "only-group", :type => :string, :banner => "print gems from a particular group"
+ method_option "without-group", :type => :string, :banner => "print all gems expect from a group"
+ method_option "paths", :type => :boolean, :banner => "print the path to each gem in the bundle"
+ def list
+ require "bundler/cli/list"
+ List.new(options).run
+ end
+
+ map %w[ls] => "list"
+ else
+ map %w[list] => "show"
+ end
+
+ desc "info GEM [OPTIONS]", "Show information for the given gem"
+ method_option "path", :type => :boolean, :banner => "Print full path to gem"
+ def info(gem_name)
+ require "bundler/cli/info"
+ Info.new(options, gem_name).run
+ end
+
+ desc "binstubs GEM [OPTIONS]", "Install the binstubs of the listed gem"
+ long_desc <<-D
+ Generate binstubs for executables in [GEM]. Binstubs are put into bin,
+ or the --binstubs directory if one has been set. Calling binstubs with [GEM [GEM]]
+ will create binstubs for all given gems.
+ D
+ method_option "force", :type => :boolean, :default => false, :banner =>
+ "Overwrite existing binstubs if they exist"
+ method_option "path", :type => :string, :lazy_default => "bin", :banner =>
+ "Binstub destination directory (default bin)"
+ method_option "shebang", :type => :string, :banner =>
+ "Specify a different shebang executable name than the default (usually 'ruby')"
+ method_option "standalone", :type => :boolean, :banner =>
+ "Make binstubs that can work without the Bundler runtime"
+ method_option "all", :type => :boolean, :banner =>
+ "Install binstubs for all gems"
+ def binstubs(*gems)
+ require "bundler/cli/binstubs"
+ Binstubs.new(options, gems).run
+ end
+
+ desc "add GEM VERSION", "Add gem to Gemfile and run bundle install"
+ long_desc <<-D
+ Adds the specified gem to Gemfile (if valid) and run 'bundle install' in one step.
+ D
+ method_option "version", :aliases => "-v", :type => :string
+ method_option "group", :aliases => "-g", :type => :string
+ method_option "source", :aliases => "-s", :type => :string
+ method_option "skip-install", :type => :boolean, :banner =>
+ "Adds gem to the Gemfile but does not install it"
+ method_option "optimistic", :type => :boolean, :banner => "Adds optimistic declaration of version to gem"
+ method_option "strict", :type => :boolean, :banner => "Adds strict declaration of version to gem"
+ def add(*gems)
+ require "bundler/cli/add"
+ Add.new(options.dup, gems).run
+ end
+
+ desc "outdated GEM [OPTIONS]", "List installed gems with newer versions available"
+ long_desc <<-D
+ Outdated lists the names and versions of gems that have a newer version available
+ in the given source. Calling outdated with [GEM [GEM]] will only check for newer
+ versions of the given gems. Prerelease gems are ignored by default. If your gems
+ are up to date, Bundler will exit with a status of 0. Otherwise, it will exit 1.
+
+ For more information on patch level options (--major, --minor, --patch,
+ --update-strict) see documentation on the same options on the update command.
+ D
+ method_option "group", :type => :string, :banner => "List gems from a specific group"
+ method_option "groups", :type => :boolean, :banner => "List gems organized by groups"
+ method_option "local", :type => :boolean, :banner =>
+ "Do not attempt to fetch gems remotely and use the gem cache instead"
+ method_option "pre", :type => :boolean, :banner => "Check for newer pre-release gems"
+ method_option "source", :type => :array, :banner => "Check against a specific source"
+ method_option "strict", :type => :boolean, :banner =>
+ "Only list newer versions allowed by your Gemfile requirements"
+ method_option "update-strict", :type => :boolean, :banner =>
+ "Strict conservative resolution, do not allow any gem to be updated past latest --patch | --minor | --major"
+ method_option "minor", :type => :boolean, :banner => "Prefer updating only to next minor version"
+ method_option "major", :type => :boolean, :banner => "Prefer updating to next major version (default)"
+ method_option "patch", :type => :boolean, :banner => "Prefer updating only to next patch version"
+ method_option "filter-major", :type => :boolean, :banner => "Only list major newer versions"
+ method_option "filter-minor", :type => :boolean, :banner => "Only list minor newer versions"
+ method_option "filter-patch", :type => :boolean, :banner => "Only list patch newer versions"
+ method_option "parseable", :aliases => "--porcelain", :type => :boolean, :banner =>
+ "Use minimal formatting for more parseable output"
+ method_option "only-explicit", :type => :boolean, :banner =>
+ "Only list gems specified in your Gemfile, not their dependencies"
+ def outdated(*gems)
+ require "bundler/cli/outdated"
+ Outdated.new(options, gems).run
+ end
+
+ if Bundler.feature_flag.cache_command_is_package?
+ map %w[cache] => :package
+ else
+ desc "cache [OPTIONS]", "Cache all the gems to vendor/cache", :hide => true
+ unless Bundler.feature_flag.cache_command_is_package?
+ method_option "all", :type => :boolean,
+ :banner => "Include all sources (including path and git)."
+ end
+ method_option "all-platforms", :type => :boolean, :banner => "Include gems for all platforms present in the lockfile, not only the current one"
+ method_option "no-prune", :type => :boolean, :banner => "Don't remove stale gems from the cache."
+ def cache
+ require "bundler/cli/cache"
+ Cache.new(options).run
+ end
+ end
+
+ desc "#{Bundler.feature_flag.cache_command_is_package? ? :cache : :package} [OPTIONS]", "Locks and then caches all of the gems into vendor/cache"
+ unless Bundler.feature_flag.cache_command_is_package?
+ method_option "all", :type => :boolean,
+ :banner => "Include all sources (including path and git)."
+ end
+ method_option "all-platforms", :type => :boolean, :banner => "Include gems for all platforms present in the lockfile, not only the current one"
+ method_option "cache-path", :type => :string, :banner =>
+ "Specify a different cache path than the default (vendor/cache)."
+ method_option "gemfile", :type => :string, :banner => "Use the specified gemfile instead of Gemfile"
+ method_option "no-install", :type => :boolean, :banner => "Don't install the gems, only the package."
+ method_option "no-prune", :type => :boolean, :banner => "Don't remove stale gems from the cache."
+ method_option "path", :type => :string, :banner =>
+ "Specify a different path than the system default ($BUNDLE_PATH or $GEM_HOME). Bundler will remember this value for future installs on this machine"
+ method_option "quiet", :type => :boolean, :banner => "Only output warnings and errors."
+ method_option "frozen", :type => :boolean, :banner =>
+ "Do not allow the Gemfile.lock to be updated after this package operation's install"
+ long_desc <<-D
+ The package command will copy the .gem files for every gem in the bundle into the
+ directory ./vendor/cache. If you then check that directory into your source
+ control repository, others who check out your source will be able to install the
+ bundle without having to download any additional gems.
+ D
+ def package
+ require "bundler/cli/package"
+ Package.new(options).run
+ end
+ map %w[pack] => :package
+
+ desc "exec [OPTIONS]", "Run the command in context of the bundle"
+ method_option :keep_file_descriptors, :type => :boolean, :default => false
+ method_option :gemfile, :type => :string, :required => false
+ long_desc <<-D
+ Exec runs a command, providing it access to the gems in the bundle. While using
+ bundle exec you can require and call the bundled gems as if they were installed
+ into the system wide RubyGems repository.
+ D
+ map "e" => "exec"
+ def exec(*args)
+ require "bundler/cli/exec"
+ Exec.new(options, args).run
+ end
+
+ desc "config NAME [VALUE]", "Retrieve or set a configuration value"
+ long_desc <<-D
+ Retrieves or sets a configuration value. If only one parameter is provided, retrieve the value. If two parameters are provided, replace the
+ existing value with the newly provided one.
+
+ By default, setting a configuration value sets it for all projects
+ on the machine.
+
+ If a global setting is superceded by local configuration, this command
+ will show the current value, as well as any superceded values and
+ where they were specified.
+ D
+ method_option "parseable", :type => :boolean, :banner => "Use minimal formatting for more parseable output"
+ def config(*args)
+ require "bundler/cli/config"
+ Config.new(options, args, self).run
+ end
+
+ desc "open GEM", "Opens the source directory of the given bundled gem"
+ def open(name)
+ require "bundler/cli/open"
+ Open.new(options, name).run
+ end
+
+ if Bundler.feature_flag.console_command?
+ desc "console [GROUP]", "Opens an IRB session with the bundle pre-loaded"
+ def console(group = nil)
+ require "bundler/cli/console"
+ Console.new(options, group).run
+ end
+ end
+
+ desc "version", "Prints the bundler's version information"
+ def version
+ cli_help = current_command.name == "cli_help"
+ if cli_help || ARGV.include?("version")
+ build_info = " (#{BuildMetadata.built_at} commit #{BuildMetadata.git_commit_sha})"
+ end
+
+ if !cli_help && Bundler.feature_flag.print_only_version_number?
+ Bundler.ui.info "#{Bundler::VERSION}#{build_info}"
+ else
+ Bundler.ui.info "Bundler version #{Bundler::VERSION}#{build_info}"
+ end
+ end
+ map %w[-v --version] => :version
+
+ desc "licenses", "Prints the license of all gems in the bundle"
+ def licenses
+ Bundler.load.specs.sort_by {|s| s.license.to_s }.reverse_each do |s|
+ gem_name = s.name
+ license = s.license || s.licenses
+
+ if license.empty?
+ Bundler.ui.warn "#{gem_name}: Unknown"
+ else
+ Bundler.ui.info "#{gem_name}: #{license}"
+ end
+ end
+ end
+
+ if Bundler.feature_flag.viz_command?
+ desc "viz [OPTIONS]", "Generates a visual dependency graph", :hide => true
+ long_desc <<-D
+ Viz generates a PNG file of the current Gemfile as a dependency graph.
+ Viz requires the ruby-graphviz gem (and its dependencies).
+ The associated gems must also be installed via 'bundle install'.
+ D
+ method_option :file, :type => :string, :default => "gem_graph", :aliases => "-f", :desc => "The name to use for the generated file. see format option"
+ method_option :format, :type => :string, :default => "png", :aliases => "-F", :desc => "This is output format option. Supported format is png, jpg, svg, dot ..."
+ method_option :requirements, :type => :boolean, :default => false, :aliases => "-R", :desc => "Set to show the version of each required dependency."
+ method_option :version, :type => :boolean, :default => false, :aliases => "-v", :desc => "Set to show each gem version."
+ method_option :without, :type => :array, :default => [], :aliases => "-W", :banner => "GROUP[ GROUP...]", :desc => "Exclude gems that are part of the specified named group."
+ def viz
+ SharedHelpers.major_deprecation 3, "The `viz` command has been moved to the `bundle-viz` gem, see https://github.com/bundler/bundler-viz"
+ require "bundler/cli/viz"
+ Viz.new(options.dup).run
+ end
+ end
+
+ old_gem = instance_method(:gem)
+
+ desc "gem NAME [OPTIONS]", "Creates a skeleton for creating a rubygem"
+ method_option :exe, :type => :boolean, :default => false, :aliases => ["--bin", "-b"], :desc => "Generate a binary executable for your library."
+ method_option :coc, :type => :boolean, :desc => "Generate a code of conduct file. Set a default with `bundle config gem.coc true`."
+ method_option :edit, :type => :string, :aliases => "-e", :required => false, :banner => "EDITOR",
+ :lazy_default => [ENV["BUNDLER_EDITOR"], ENV["VISUAL"], ENV["EDITOR"]].find {|e| !e.nil? && !e.empty? },
+ :desc => "Open generated gemspec in the specified editor (defaults to $EDITOR or $BUNDLER_EDITOR)"
+ method_option :ext, :type => :boolean, :default => false, :desc => "Generate the boilerplate for C extension code"
+ method_option :mit, :type => :boolean, :desc => "Generate an MIT license file. Set a default with `bundle config gem.mit true`."
+ method_option :test, :type => :string, :lazy_default => "rspec", :aliases => "-t", :banner => "rspec",
+ :desc => "Generate a test directory for your library, either rspec or minitest. Set a default with `bundle config gem.test rspec`."
+ def gem(name)
+ end
+
+ commands["gem"].tap do |gem_command|
+ def gem_command.run(instance, args = [])
+ arity = 1 # name
+
+ require "bundler/cli/gem"
+ cmd_args = args + [instance]
+ cmd_args.unshift(instance.options)
+
+ cmd = begin
+ Gem.new(*cmd_args)
+ rescue ArgumentError => e
+ instance.class.handle_argument_error(self, e, args, arity)
+ end
+
+ cmd.run
+ end
+ end
+
+ undef_method(:gem)
+ define_method(:gem, old_gem)
+ private :gem
+
+ def self.source_root
+ File.expand_path(File.join(File.dirname(__FILE__), "templates"))
+ end
+
+ desc "clean [OPTIONS]", "Cleans up unused gems in your bundler directory", :hide => true
+ method_option "dry-run", :type => :boolean, :default => false, :banner =>
+ "Only print out changes, do not clean gems"
+ method_option "force", :type => :boolean, :default => false, :banner =>
+ "Forces clean even if --path is not set"
+ def clean
+ require "bundler/cli/clean"
+ Clean.new(options.dup).run
+ end
+
+ desc "platform [OPTIONS]", "Displays platform compatibility information"
+ method_option "ruby", :type => :boolean, :default => false, :banner =>
+ "only display ruby related platform information"
+ def platform
+ require "bundler/cli/platform"
+ Platform.new(options).run
+ end
+
+ desc "inject GEM VERSION", "Add the named gem, with version requirements, to the resolved Gemfile", :hide => true
+ method_option "source", :type => :string, :banner =>
+ "Install gem from the given source"
+ method_option "group", :type => :string, :banner =>
+ "Install gem into a bundler group"
+ def inject(name, version)
+ SharedHelpers.major_deprecation 3, "The `inject` command has been replaced by the `add` command"
+ require "bundler/cli/inject"
+ Inject.new(options.dup, name, version).run
+ end
+
+ desc "lock", "Creates a lockfile without installing"
+ method_option "update", :type => :array, :lazy_default => true, :banner =>
+ "ignore the existing lockfile, update all gems by default, or update list of given gems"
+ method_option "local", :type => :boolean, :default => false, :banner =>
+ "do not attempt to fetch remote gemspecs and use the local gem cache only"
+ method_option "print", :type => :boolean, :default => false, :banner =>
+ "print the lockfile to STDOUT instead of writing to the file system"
+ method_option "lockfile", :type => :string, :default => nil, :banner =>
+ "the path the lockfile should be written to"
+ method_option "full-index", :type => :boolean, :default => false, :banner =>
+ "Fall back to using the single-file index of all gems"
+ method_option "add-platform", :type => :array, :default => [], :banner =>
+ "Add a new platform to the lockfile"
+ method_option "remove-platform", :type => :array, :default => [], :banner =>
+ "Remove a platform from the lockfile"
+ method_option "patch", :type => :boolean, :banner =>
+ "If updating, prefer updating only to next patch version"
+ method_option "minor", :type => :boolean, :banner =>
+ "If updating, prefer updating only to next minor version"
+ method_option "major", :type => :boolean, :banner =>
+ "If updating, prefer updating to next major version (default)"
+ method_option "strict", :type => :boolean, :banner =>
+ "If updating, do not allow any gem to be updated past latest --patch | --minor | --major"
+ method_option "conservative", :type => :boolean, :banner =>
+ "If updating, use bundle install conservative update behavior and do not allow shared dependencies to be updated"
+ def lock
+ require "bundler/cli/lock"
+ Lock.new(options).run
+ end
+
+ desc "env", "Print information about the environment Bundler is running under"
+ def env
+ Env.write($stdout)
+ end
+
+ desc "doctor [OPTIONS]", "Checks the bundle for common problems"
+ long_desc <<-D
+ Doctor scans the OS dependencies of each of the gems requested in the Gemfile. If
+ missing dependencies are detected, Bundler prints them and exits status 1.
+ Otherwise, Bundler prints a success message and exits with a status of 0.
+ D
+ method_option "gemfile", :type => :string, :banner =>
+ "Use the specified gemfile instead of Gemfile"
+ method_option "quiet", :type => :boolean, :banner =>
+ "Only output warnings and errors."
+ def doctor
+ require "bundler/cli/doctor"
+ Doctor.new(options).run
+ end
+
+ desc "issue", "Learn how to report an issue in Bundler"
+ def issue
+ require "bundler/cli/issue"
+ Issue.new.run
+ end
+
+ desc "pristine [GEMS...]", "Restores installed gems to pristine condition"
+ long_desc <<-D
+ Restores installed gems to pristine condition from files located in the
+ gem cache. Gems installed from a git repository will be issued `git
+ checkout --force`.
+ D
+ def pristine(*gems)
+ require "bundler/cli/pristine"
+ Pristine.new(gems).run
+ end
+
+ if Bundler.feature_flag.plugins?
+ require "bundler/cli/plugin"
+ desc "plugin", "Manage the bundler plugins"
+ subcommand "plugin", Plugin
+ end
+
+ # Reformat the arguments passed to bundle that include a --help flag
+ # into the corresponding `bundle help #{command}` call
+ def self.reformatted_help_args(args)
+ bundler_commands = all_commands.keys
+ help_flags = %w[--help -h]
+ exec_commands = %w[e ex exe exec]
+ help_used = args.index {|a| help_flags.include? a }
+ exec_used = args.index {|a| exec_commands.include? a }
+ command = args.find {|a| bundler_commands.include? a }
+ if exec_used && help_used
+ if exec_used + help_used == 1
+ %w[help exec]
+ else
+ args
+ end
+ elsif help_used
+ args = args.dup
+ args.delete_at(help_used)
+ ["help", command || args].flatten.compact
+ else
+ args
+ end
+ end
+
+ private
+
+ # Automatically invoke `bundle install` and resume if
+ # Bundler.settings[:auto_install] exists. This is set through config cmd
+ # `bundle config auto_install 1`.
+ #
+ # Note that this method `nil`s out the global Definition object, so it
+ # should be called first, before you instantiate anything like an
+ # `Installer` that'll keep a reference to the old one instead.
+ def auto_install
+ return unless Bundler.settings[:auto_install]
+
+ begin
+ Bundler.definition.specs
+ rescue GemNotFound
+ Bundler.ui.info "Automatically installing missing gems."
+ Bundler.reset!
+ invoke :install, []
+ Bundler.reset!
+ end
+ end
+
+ def current_command
+ _, _, config = @_initializer
+ config[:current_command]
+ end
+
+ def print_command
+ return unless Bundler.ui.debug?
+ cmd = current_command
+ command_name = cmd.name
+ return if PARSEABLE_COMMANDS.include?(command_name)
+ command = ["bundle", command_name] + args
+ options_to_print = options.dup
+ options_to_print.delete_if do |k, v|
+ next unless o = cmd.options[k]
+ o.default == v
+ end
+ command << Thor::Options.to_switches(options_to_print.sort_by(&:first)).strip
+ command.reject!(&:empty?)
+ Bundler.ui.info "Running `#{command * " "}` with bundler #{Bundler::VERSION}"
+ end
+
+ def warn_on_outdated_bundler
+ return if Bundler.settings[:disable_version_check]
+
+ command_name = current_command.name
+ return if PARSEABLE_COMMANDS.include?(command_name)
+
+ return unless SharedHelpers.md5_available?
+
+ latest = Fetcher::CompactIndex.
+ new(nil, Source::Rubygems::Remote.new(URI("https://rubygems.org")), nil).
+ send(:compact_index_client).
+ instance_variable_get(:@cache).
+ dependencies("bundler").
+ map {|d| Gem::Version.new(d.first) }.
+ max
+ return unless latest
+
+ current = Gem::Version.new(VERSION)
+ return if current >= latest
+ latest_installed = Bundler.rubygems.find_name("bundler").map(&:version).max
+
+ installation = "To install the latest version, run `gem install bundler#{" --pre" if latest.prerelease?}`"
+ if latest_installed && latest_installed > current
+ suggestion = "To update to the most recent installed version (#{latest_installed}), run `bundle update --bundler`"
+ suggestion = "#{installation}\n#{suggestion}" if latest_installed < latest
+ else
+ suggestion = installation
+ end
+
+ Bundler.ui.warn "The latest bundler is #{latest}, but you are currently running #{current}.\n#{suggestion}"
+ rescue RuntimeError
+ nil
+ end
+ end
+end
diff --git a/lib/bundler/cli/add.rb b/lib/bundler/cli/add.rb
new file mode 100644
index 00000000000000..9709e71be0456a
--- /dev/null
+++ b/lib/bundler/cli/add.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Add
+ def initialize(options, gems)
+ @gems = gems
+ @options = options
+ @options[:group] = @options[:group].split(",").map(&:strip) if !@options[:group].nil? && !@options[:group].empty?
+ end
+
+ def run
+ raise InvalidOption, "You can not specify `--strict` and `--optimistic` at the same time." if @options[:strict] && @options[:optimistic]
+
+ # raise error when no gems are specified
+ raise InvalidOption, "Please specify gems to add." if @gems.empty?
+
+ version = @options[:version].nil? ? nil : @options[:version].split(",").map(&:strip)
+
+ unless version.nil?
+ version.each do |v|
+ raise InvalidOption, "Invalid gem requirement pattern '#{v}'" unless Gem::Requirement::PATTERN =~ v.to_s
+ end
+ end
+
+ dependencies = @gems.map {|g| Bundler::Dependency.new(g, version, @options) }
+
+ Injector.inject(dependencies,
+ :conservative_versioning => @options[:version].nil?, # Perform conservative versioning only when version is not specified
+ :optimistic => @options[:optimistic],
+ :strict => @options[:strict])
+
+ Installer.install(Bundler.root, Bundler.definition) unless @options["skip-install"]
+ end
+ end
+end
diff --git a/lib/bundler/cli/binstubs.rb b/lib/bundler/cli/binstubs.rb
new file mode 100644
index 00000000000000..266396eedc67e0
--- /dev/null
+++ b/lib/bundler/cli/binstubs.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Binstubs
+ attr_reader :options, :gems
+ def initialize(options, gems)
+ @options = options
+ @gems = gems
+ end
+
+ def run
+ Bundler.definition.validate_runtime!
+ path_option = options["path"]
+ path_option = nil if path_option && path_option.empty?
+ Bundler.settings.set_command_option :bin, path_option if options["path"]
+ Bundler.settings.set_command_option_if_given :shebang, options["shebang"]
+ installer = Installer.new(Bundler.root, Bundler.definition)
+
+ installer_opts = { :force => options[:force], :binstubs_cmd => true }
+
+ if options[:all]
+ raise InvalidOption, "Cannot specify --all with specific gems" unless gems.empty?
+ @gems = Bundler.definition.specs.map(&:name)
+ installer_opts.delete(:binstubs_cmd)
+ elsif gems.empty?
+ Bundler.ui.error "`bundle binstubs` needs at least one gem to run."
+ exit 1
+ end
+
+ gems.each do |gem_name|
+ spec = Bundler.definition.specs.find {|s| s.name == gem_name }
+ unless spec
+ raise GemNotFound, Bundler::CLI::Common.gem_not_found_message(
+ gem_name, Bundler.definition.specs
+ )
+ end
+
+ if options[:standalone]
+ next Bundler.ui.warn("Sorry, Bundler can only be run via RubyGems.") if gem_name == "bundler"
+ Bundler.settings.temporary(:path => (Bundler.settings[:path] || Bundler.root)) do
+ installer.generate_standalone_bundler_executable_stubs(spec)
+ end
+ else
+ installer.generate_bundler_executable_stubs(spec, installer_opts)
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/cli/cache.rb b/lib/bundler/cli/cache.rb
new file mode 100644
index 00000000000000..9d2ba87d3408d6
--- /dev/null
+++ b/lib/bundler/cli/cache.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Cache
+ attr_reader :options
+ def initialize(options)
+ @options = options
+ end
+
+ def run
+ Bundler.definition.validate_runtime!
+ Bundler.definition.resolve_with_cache!
+ setup_cache_all
+ Bundler.settings.set_command_option_if_given :cache_all_platforms, options["all-platforms"]
+ Bundler.load.cache
+ Bundler.settings.set_command_option_if_given :no_prune, options["no-prune"]
+ Bundler.load.lock
+ rescue GemNotFound => e
+ Bundler.ui.error(e.message)
+ Bundler.ui.warn "Run `bundle install` to install missing gems."
+ exit 1
+ end
+
+ private
+
+ def setup_cache_all
+ Bundler.settings.set_command_option_if_given :cache_all, options[:all]
+
+ if Bundler.definition.has_local_dependencies? && !Bundler.feature_flag.cache_all?
+ Bundler.ui.warn "Your Gemfile contains path and git dependencies. If you want " \
+ "to package them as well, please pass the --all flag. This will be the default " \
+ "on Bundler 2.0."
+ end
+ end
+ end
+end
diff --git a/lib/bundler/cli/check.rb b/lib/bundler/cli/check.rb
new file mode 100644
index 00000000000000..19c0aaea06ebbc
--- /dev/null
+++ b/lib/bundler/cli/check.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Check
+ attr_reader :options
+
+ def initialize(options)
+ @options = options
+ end
+
+ def run
+ Bundler.settings.set_command_option_if_given :path, options[:path]
+
+ begin
+ definition = Bundler.definition
+ definition.validate_runtime!
+ not_installed = definition.missing_specs
+ rescue GemNotFound, VersionConflict
+ Bundler.ui.error "Bundler can't satisfy your Gemfile's dependencies."
+ Bundler.ui.warn "Install missing gems with `bundle install`."
+ exit 1
+ end
+
+ if not_installed.any?
+ Bundler.ui.error "The following gems are missing"
+ not_installed.each {|s| Bundler.ui.error " * #{s.name} (#{s.version})" }
+ Bundler.ui.warn "Install missing gems with `bundle install`"
+ exit 1
+ elsif !Bundler.default_lockfile.file? && Bundler.frozen_bundle?
+ Bundler.ui.error "This bundle has been frozen, but there is no #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)} present"
+ exit 1
+ else
+ Bundler.load.lock(:preserve_unknown_sections => true) unless options[:"dry-run"]
+ Bundler.ui.info "The Gemfile's dependencies are satisfied"
+ end
+ end
+ end
+end
diff --git a/lib/bundler/cli/clean.rb b/lib/bundler/cli/clean.rb
new file mode 100644
index 00000000000000..4a407fbae7ec98
--- /dev/null
+++ b/lib/bundler/cli/clean.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Clean
+ attr_reader :options
+
+ def initialize(options)
+ @options = options
+ end
+
+ def run
+ require_path_or_force unless options[:"dry-run"]
+ Bundler.load.clean(options[:"dry-run"])
+ end
+
+ protected
+
+ def require_path_or_force
+ return unless Bundler.use_system_gems? && !options[:force]
+ raise InvalidOption, "Cleaning all the gems on your system is dangerous! " \
+ "If you're sure you want to remove every system gem not in this " \
+ "bundle, run `bundle clean --force`."
+ end
+ end
+end
diff --git a/lib/bundler/cli/common.rb b/lib/bundler/cli/common.rb
new file mode 100644
index 00000000000000..9d40ee9dfd1b81
--- /dev/null
+++ b/lib/bundler/cli/common.rb
@@ -0,0 +1,102 @@
+# frozen_string_literal: true
+
+module Bundler
+ module CLI::Common
+ def self.output_post_install_messages(messages)
+ return if Bundler.settings["ignore_messages"]
+ messages.to_a.each do |name, msg|
+ print_post_install_message(name, msg) unless Bundler.settings["ignore_messages.#{name}"]
+ end
+ end
+
+ def self.print_post_install_message(name, msg)
+ Bundler.ui.confirm "Post-install message from #{name}:"
+ Bundler.ui.info msg
+ end
+
+ def self.output_without_groups_message
+ return if Bundler.settings[:without].empty?
+ Bundler.ui.confirm without_groups_message
+ end
+
+ def self.without_groups_message
+ groups = Bundler.settings[:without]
+ group_list = [groups[0...-1].join(", "), groups[-1..-1]].
+ reject {|s| s.to_s.empty? }.join(" and ")
+ group_str = (groups.size == 1) ? "group" : "groups"
+ "Gems in the #{group_str} #{group_list} were not installed."
+ end
+
+ def self.select_spec(name, regex_match = nil)
+ specs = []
+ regexp = Regexp.new(name) if regex_match
+
+ Bundler.definition.specs.each do |spec|
+ return spec if spec.name == name
+ specs << spec if regexp && spec.name =~ regexp
+ end
+
+ case specs.count
+ when 0
+ raise GemNotFound, gem_not_found_message(name, Bundler.definition.dependencies)
+ when 1
+ specs.first
+ else
+ ask_for_spec_from(specs)
+ end
+ rescue RegexpError
+ raise GemNotFound, gem_not_found_message(name, Bundler.definition.dependencies)
+ end
+
+ def self.ask_for_spec_from(specs)
+ if !$stdout.tty? && ENV["BUNDLE_SPEC_RUN"].nil?
+ raise GemNotFound, gem_not_found_message(name, Bundler.definition.dependencies)
+ end
+
+ specs.each_with_index do |spec, index|
+ Bundler.ui.info "#{index.succ} : #{spec.name}", true
+ end
+ Bundler.ui.info "0 : - exit -", true
+
+ num = Bundler.ui.ask("> ").to_i
+ num > 0 ? specs[num - 1] : nil
+ end
+
+ def self.gem_not_found_message(missing_gem_name, alternatives)
+ require "bundler/similarity_detector"
+ message = "Could not find gem '#{missing_gem_name}'."
+ alternate_names = alternatives.map {|a| a.respond_to?(:name) ? a.name : a }
+ suggestions = SimilarityDetector.new(alternate_names).similar_word_list(missing_gem_name)
+ message += "\nDid you mean #{suggestions}?" if suggestions
+ message
+ end
+
+ def self.ensure_all_gems_in_lockfile!(names, locked_gems = Bundler.locked_gems)
+ locked_names = locked_gems.specs.map(&:name)
+ names.-(locked_names).each do |g|
+ raise GemNotFound, gem_not_found_message(g, locked_names)
+ end
+ end
+
+ def self.configure_gem_version_promoter(definition, options)
+ patch_level = patch_level_options(options)
+ raise InvalidOption, "Provide only one of the following options: #{patch_level.join(", ")}" unless patch_level.length <= 1
+ definition.gem_version_promoter.tap do |gvp|
+ gvp.level = patch_level.first || :major
+ gvp.strict = options[:strict] || options["update-strict"]
+ end
+ end
+
+ def self.patch_level_options(options)
+ [:major, :minor, :patch].select {|v| options.keys.include?(v.to_s) }
+ end
+
+ def self.clean_after_install?
+ clean = Bundler.settings[:clean]
+ return clean unless clean.nil?
+ clean ||= Bundler.feature_flag.auto_clean_without_path? && Bundler.settings[:path].nil?
+ clean &&= !Bundler.use_system_gems?
+ clean
+ end
+ end
+end
diff --git a/lib/bundler/cli/config.rb b/lib/bundler/cli/config.rb
new file mode 100644
index 00000000000000..12f71ea8fea984
--- /dev/null
+++ b/lib/bundler/cli/config.rb
@@ -0,0 +1,119 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Config
+ attr_reader :name, :options, :scope, :thor
+ attr_accessor :args
+
+ def initialize(options, args, thor)
+ @options = options
+ @args = args
+ @thor = thor
+ @name = peek = args.shift
+ @scope = "global"
+ return unless peek && peek.start_with?("--")
+ @name = args.shift
+ @scope = peek[2..-1]
+ end
+
+ def run
+ unless name
+ confirm_all
+ return
+ end
+
+ unless valid_scope?(scope)
+ Bundler.ui.error "Invalid scope --#{scope} given. Please use --local or --global."
+ exit 1
+ end
+
+ if scope == "delete"
+ Bundler.settings.set_local(name, nil)
+ Bundler.settings.set_global(name, nil)
+ return
+ end
+
+ if args.empty?
+ if options[:parseable]
+ if value = Bundler.settings[name]
+ Bundler.ui.info("#{name}=#{value}")
+ end
+ return
+ end
+
+ confirm(name)
+ return
+ end
+
+ Bundler.ui.info(message) if message
+ Bundler.settings.send("set_#{scope}", name, new_value)
+ end
+
+ private
+
+ def confirm_all
+ if @options[:parseable]
+ thor.with_padding do
+ Bundler.settings.all.each do |setting|
+ val = Bundler.settings[setting]
+ Bundler.ui.info "#{setting}=#{val}"
+ end
+ end
+ else
+ Bundler.ui.confirm "Settings are listed in order of priority. The top value will be used.\n"
+ Bundler.settings.all.each do |setting|
+ Bundler.ui.confirm "#{setting}"
+ show_pretty_values_for(setting)
+ Bundler.ui.confirm ""
+ end
+ end
+ end
+
+ def confirm(name)
+ Bundler.ui.confirm "Settings for `#{name}` in order of priority. The top value will be used"
+ show_pretty_values_for(name)
+ end
+
+ def new_value
+ pathname = Pathname.new(args.join(" "))
+ if name.start_with?("local.") && pathname.directory?
+ pathname.expand_path.to_s
+ else
+ args.join(" ")
+ end
+ end
+
+ def message
+ locations = Bundler.settings.locations(name)
+ if @options[:parseable]
+ "#{name}=#{new_value}" if new_value
+ elsif scope == "global"
+ if locations[:local]
+ "Your application has set #{name} to #{locations[:local].inspect}. " \
+ "This will override the global value you are currently setting"
+ elsif locations[:env]
+ "You have a bundler environment variable for #{name} set to " \
+ "#{locations[:env].inspect}. This will take precedence over the global value you are setting"
+ elsif locations[:global] && locations[:global] != args.join(" ")
+ "You are replacing the current global value of #{name}, which is currently " \
+ "#{locations[:global].inspect}"
+ end
+ elsif scope == "local" && locations[:local] != args.join(" ")
+ "You are replacing the current local value of #{name}, which is currently " \
+ "#{locations[:local].inspect}"
+ end
+ end
+
+ def show_pretty_values_for(setting)
+ thor.with_padding do
+ Bundler.settings.pretty_values_for(setting).each do |line|
+ Bundler.ui.info line
+ end
+ end
+ end
+
+ def valid_scope?(scope)
+ %w[delete local global].include?(scope)
+ end
+ end
+end
diff --git a/lib/bundler/cli/console.rb b/lib/bundler/cli/console.rb
new file mode 100644
index 00000000000000..d45f30cdcf2ac1
--- /dev/null
+++ b/lib/bundler/cli/console.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Console
+ attr_reader :options, :group
+ def initialize(options, group)
+ @options = options
+ @group = group
+ end
+
+ def run
+ Bundler::SharedHelpers.major_deprecation 3, "bundle console will be replaced " \
+ "by `bin/console` generated by `bundle gem `"
+
+ group ? Bundler.require(:default, *(group.split.map!(&:to_sym))) : Bundler.require
+ ARGV.clear
+
+ console = get_console(Bundler.settings[:console] || "irb")
+ console.start
+ end
+
+ def get_console(name)
+ require name
+ get_constant(name)
+ rescue LoadError
+ Bundler.ui.error "Couldn't load console #{name}, falling back to irb"
+ require "irb"
+ get_constant("irb")
+ end
+
+ def get_constant(name)
+ const_name = {
+ "pry" => :Pry,
+ "ripl" => :Ripl,
+ "irb" => :IRB,
+ }[name]
+ Object.const_get(const_name)
+ rescue NameError
+ Bundler.ui.error "Could not find constant #{const_name}"
+ exit 1
+ end
+ end
+end
diff --git a/lib/bundler/cli/doctor.rb b/lib/bundler/cli/doctor.rb
new file mode 100644
index 00000000000000..3e0898ff8a8fb9
--- /dev/null
+++ b/lib/bundler/cli/doctor.rb
@@ -0,0 +1,140 @@
+# frozen_string_literal: true
+
+require "rbconfig"
+
+module Bundler
+ class CLI::Doctor
+ DARWIN_REGEX = /\s+(.+) \(compatibility /
+ LDD_REGEX = /\t\S+ => (\S+) \(\S+\)/
+
+ attr_reader :options
+
+ def initialize(options)
+ @options = options
+ end
+
+ def otool_available?
+ Bundler.which("otool")
+ end
+
+ def ldd_available?
+ Bundler.which("ldd")
+ end
+
+ def dylibs_darwin(path)
+ output = `/usr/bin/otool -L "#{path}"`.chomp
+ dylibs = output.split("\n")[1..-1].map {|l| l.match(DARWIN_REGEX).captures[0] }.uniq
+ # ignore @rpath and friends
+ dylibs.reject {|dylib| dylib.start_with? "@" }
+ end
+
+ def dylibs_ldd(path)
+ output = `/usr/bin/ldd "#{path}"`.chomp
+ output.split("\n").map do |l|
+ match = l.match(LDD_REGEX)
+ next if match.nil?
+ match.captures[0]
+ end.compact
+ end
+
+ def dylibs(path)
+ case RbConfig::CONFIG["host_os"]
+ when /darwin/
+ return [] unless otool_available?
+ dylibs_darwin(path)
+ when /(linux|solaris|bsd)/
+ return [] unless ldd_available?
+ dylibs_ldd(path)
+ else # Windows, etc.
+ Bundler.ui.warn("Dynamic library check not supported on this platform.")
+ []
+ end
+ end
+
+ def bundles_for_gem(spec)
+ Dir.glob("#{spec.full_gem_path}/**/*.bundle")
+ end
+
+ def check!
+ require "bundler/cli/check"
+ Bundler::CLI::Check.new({}).run
+ end
+
+ def run
+ Bundler.ui.level = "error" if options[:quiet]
+ Bundler.settings.validate!
+ check!
+
+ definition = Bundler.definition
+ broken_links = {}
+
+ definition.specs.each do |spec|
+ bundles_for_gem(spec).each do |bundle|
+ bad_paths = dylibs(bundle).select {|f| !File.exist?(f) }
+ if bad_paths.any?
+ broken_links[spec] ||= []
+ broken_links[spec].concat(bad_paths)
+ end
+ end
+ end
+
+ permissions_valid = check_home_permissions
+
+ if broken_links.any?
+ message = "The following gems are missing OS dependencies:"
+ broken_links.map do |spec, paths|
+ paths.uniq.map do |path|
+ "\n * #{spec.name}: #{path}"
+ end
+ end.flatten.sort.each {|m| message += m }
+ raise ProductionError, message
+ elsif !permissions_valid
+ Bundler.ui.info "No issues found with the installed bundle"
+ end
+ end
+
+ private
+
+ def check_home_permissions
+ require "find"
+ files_not_readable_or_writable = []
+ files_not_rw_and_owned_by_different_user = []
+ files_not_owned_by_current_user_but_still_rw = []
+ Find.find(Bundler.home.to_s).each do |f|
+ if !File.writable?(f) || !File.readable?(f)
+ if File.stat(f).uid != Process.uid
+ files_not_rw_and_owned_by_different_user << f
+ else
+ files_not_readable_or_writable << f
+ end
+ elsif File.stat(f).uid != Process.uid
+ files_not_owned_by_current_user_but_still_rw << f
+ end
+ end
+
+ ok = true
+ if files_not_owned_by_current_user_but_still_rw.any?
+ Bundler.ui.warn "Files exist in the Bundler home that are owned by another " \
+ "user, but are still readable/writable. These files are:\n - #{files_not_owned_by_current_user_but_still_rw.join("\n - ")}"
+
+ ok = false
+ end
+
+ if files_not_rw_and_owned_by_different_user.any?
+ Bundler.ui.warn "Files exist in the Bundler home that are owned by another " \
+ "user, and are not readable/writable. These files are:\n - #{files_not_rw_and_owned_by_different_user.join("\n - ")}"
+
+ ok = false
+ end
+
+ if files_not_readable_or_writable.any?
+ Bundler.ui.warn "Files exist in the Bundler home that are not " \
+ "readable/writable by the current user. These files are:\n - #{files_not_readable_or_writable.join("\n - ")}"
+
+ ok = false
+ end
+
+ ok
+ end
+ end
+end
diff --git a/lib/bundler/cli/exec.rb b/lib/bundler/cli/exec.rb
new file mode 100644
index 00000000000000..c29d6323070cc7
--- /dev/null
+++ b/lib/bundler/cli/exec.rb
@@ -0,0 +1,105 @@
+# frozen_string_literal: true
+
+require "bundler/current_ruby"
+
+module Bundler
+ class CLI::Exec
+ attr_reader :options, :args, :cmd
+
+ TRAPPED_SIGNALS = %w[INT].freeze
+
+ def initialize(options, args)
+ @options = options
+ @cmd = args.shift
+ @args = args
+
+ if Bundler.current_ruby.ruby_2? && !Bundler.current_ruby.jruby?
+ @args << { :close_others => !options.keep_file_descriptors? }
+ elsif options.keep_file_descriptors?
+ Bundler.ui.warn "Ruby version #{RUBY_VERSION} defaults to keeping non-standard file descriptors on Kernel#exec."
+ end
+ end
+
+ def run
+ validate_cmd!
+ SharedHelpers.set_bundle_environment
+ if bin_path = Bundler.which(cmd)
+ if !Bundler.settings[:disable_exec_load] && ruby_shebang?(bin_path)
+ return kernel_load(bin_path, *args)
+ end
+ # First, try to exec directly to something in PATH
+ if Bundler.current_ruby.jruby_18?
+ kernel_exec(bin_path, *args)
+ else
+ kernel_exec([bin_path, cmd], *args)
+ end
+ else
+ # exec using the given command
+ kernel_exec(cmd, *args)
+ end
+ end
+
+ private
+
+ def validate_cmd!
+ return unless cmd.nil?
+ Bundler.ui.error "bundler: exec needs a command to run"
+ exit 128
+ end
+
+ def kernel_exec(*args)
+ ui = Bundler.ui
+ Bundler.ui = nil
+ Kernel.exec(*args)
+ rescue Errno::EACCES, Errno::ENOEXEC
+ Bundler.ui = ui
+ Bundler.ui.error "bundler: not executable: #{cmd}"
+ exit 126
+ rescue Errno::ENOENT
+ Bundler.ui = ui
+ Bundler.ui.error "bundler: command not found: #{cmd}"
+ Bundler.ui.warn "Install missing gem executables with `bundle install`"
+ exit 127
+ end
+
+ def kernel_load(file, *args)
+ args.pop if args.last.is_a?(Hash)
+ ARGV.replace(args)
+ $0 = file
+ Process.setproctitle(process_title(file, args)) if Process.respond_to?(:setproctitle)
+ ui = Bundler.ui
+ Bundler.ui = nil
+ require "bundler/setup"
+ TRAPPED_SIGNALS.each {|s| trap(s, "DEFAULT") }
+ Kernel.load(file)
+ rescue SystemExit, SignalException
+ raise
+ rescue Exception => e # rubocop:disable Lint/RescueException
+ Bundler.ui = ui
+ Bundler.ui.error "bundler: failed to load command: #{cmd} (#{file})"
+ backtrace = e.backtrace ? e.backtrace.take_while {|bt| !bt.start_with?(__FILE__) } : []
+ abort "#{e.class}: #{e.message}\n #{backtrace.join("\n ")}"
+ end
+
+ def process_title(file, args)
+ "#{file} #{args.join(" ")}".strip
+ end
+
+ def ruby_shebang?(file)
+ possibilities = [
+ "#!/usr/bin/env ruby\n",
+ "#!/usr/bin/env jruby\n",
+ "#!/usr/bin/env truffleruby\n",
+ "#!#{Gem.ruby}\n",
+ ]
+
+ if File.zero?(file)
+ Bundler.ui.warn "#{file} is empty"
+ return false
+ end
+
+ first_line = File.open(file, "rb") {|f| f.read(possibilities.map(&:size).max) }
+ possibilities.any? {|shebang| first_line.start_with?(shebang) }
+ end
+ end
+end
diff --git a/lib/bundler/cli/gem.rb b/lib/bundler/cli/gem.rb
new file mode 100644
index 00000000000000..58e2f8a3fdbfdb
--- /dev/null
+++ b/lib/bundler/cli/gem.rb
@@ -0,0 +1,252 @@
+# frozen_string_literal: true
+
+require "pathname"
+
+module Bundler
+ class CLI
+ Bundler.require_thor_actions
+ include Thor::Actions
+ end
+
+ class CLI::Gem
+ TEST_FRAMEWORK_VERSIONS = {
+ "rspec" => "3.0",
+ "minitest" => "5.0"
+ }.freeze
+
+ attr_reader :options, :gem_name, :thor, :name, :target
+
+ def initialize(options, gem_name, thor)
+ @options = options
+ @gem_name = resolve_name(gem_name)
+
+ @thor = thor
+ thor.behavior = :invoke
+ thor.destination_root = nil
+
+ @name = @gem_name
+ @target = SharedHelpers.pwd.join(gem_name)
+
+ validate_ext_name if options[:ext]
+ end
+
+ def run
+ Bundler.ui.confirm "Creating gem '#{name}'..."
+
+ underscored_name = name.tr("-", "_")
+ namespaced_path = name.tr("-", "/")
+ constant_name = name.gsub(/-[_-]*(?![_-]|$)/) { "::" }.gsub(/([_-]+|(::)|^)(.|$)/) { $2.to_s + $3.upcase }
+ constant_array = constant_name.split("::")
+
+ git_installed = Bundler.git_present?
+
+ git_author_name = git_installed ? `git config user.name`.chomp : ""
+ github_username = git_installed ? `git config github.user`.chomp : ""
+ git_user_email = git_installed ? `git config user.email`.chomp : ""
+
+ config = {
+ :name => name,
+ :underscored_name => underscored_name,
+ :namespaced_path => namespaced_path,
+ :makefile_path => "#{underscored_name}/#{underscored_name}",
+ :constant_name => constant_name,
+ :constant_array => constant_array,
+ :author => git_author_name.empty? ? "TODO: Write your name" : git_author_name,
+ :email => git_user_email.empty? ? "TODO: Write your email address" : git_user_email,
+ :test => options[:test],
+ :ext => options[:ext],
+ :exe => options[:exe],
+ :bundler_version => bundler_dependency_version,
+ :github_username => github_username.empty? ? "[USERNAME]" : github_username
+ }
+ ensure_safe_gem_name(name, constant_array)
+
+ templates = {
+ "Gemfile.tt" => "Gemfile",
+ "lib/newgem.rb.tt" => "lib/#{namespaced_path}.rb",
+ "lib/newgem/version.rb.tt" => "lib/#{namespaced_path}/version.rb",
+ "newgem.gemspec.tt" => "#{name}.gemspec",
+ "Rakefile.tt" => "Rakefile",
+ "README.md.tt" => "README.md",
+ "bin/console.tt" => "bin/console",
+ "bin/setup.tt" => "bin/setup"
+ }
+
+ executables = %w[
+ bin/console
+ bin/setup
+ ]
+
+ templates.merge!("gitignore.tt" => ".gitignore") if Bundler.git_present?
+
+ if test_framework = ask_and_set_test_framework
+ config[:test] = test_framework
+ config[:test_framework_version] = TEST_FRAMEWORK_VERSIONS[test_framework]
+
+ templates.merge!("travis.yml.tt" => ".travis.yml")
+
+ case test_framework
+ when "rspec"
+ templates.merge!(
+ "rspec.tt" => ".rspec",
+ "spec/spec_helper.rb.tt" => "spec/spec_helper.rb",
+ "spec/newgem_spec.rb.tt" => "spec/#{namespaced_path}_spec.rb"
+ )
+ when "minitest"
+ templates.merge!(
+ "test/test_helper.rb.tt" => "test/test_helper.rb",
+ "test/newgem_test.rb.tt" => "test/#{namespaced_path}_test.rb"
+ )
+ end
+ end
+
+ config[:test_task] = config[:test] == "minitest" ? "test" : "spec"
+
+ if ask_and_set(:mit, "Do you want to license your code permissively under the MIT license?",
+ "This means that any other developer or company will be legally allowed to use your code " \
+ "for free as long as they admit you created it. You can read more about the MIT license " \
+ "at https://choosealicense.com/licenses/mit.")
+ config[:mit] = true
+ Bundler.ui.info "MIT License enabled in config"
+ templates.merge!("LICENSE.txt.tt" => "LICENSE.txt")
+ end
+
+ if ask_and_set(:coc, "Do you want to include a code of conduct in gems you generate?",
+ "Codes of conduct can increase contributions to your project by contributors who " \
+ "prefer collaborative, safe spaces. You can read more about the code of conduct at " \
+ "contributor-covenant.org. Having a code of conduct means agreeing to the responsibility " \
+ "of enforcing it, so be sure that you are prepared to do that. Be sure that your email " \
+ "address is specified as a contact in the generated code of conduct so that people know " \
+ "who to contact in case of a violation. For suggestions about " \
+ "how to enforce codes of conduct, see https://bit.ly/coc-enforcement.")
+ config[:coc] = true
+ Bundler.ui.info "Code of conduct enabled in config"
+ templates.merge!("CODE_OF_CONDUCT.md.tt" => "CODE_OF_CONDUCT.md")
+ end
+
+ templates.merge!("exe/newgem.tt" => "exe/#{name}") if config[:exe]
+
+ if options[:ext]
+ templates.merge!(
+ "ext/newgem/extconf.rb.tt" => "ext/#{name}/extconf.rb",
+ "ext/newgem/newgem.h.tt" => "ext/#{name}/#{underscored_name}.h",
+ "ext/newgem/newgem.c.tt" => "ext/#{name}/#{underscored_name}.c"
+ )
+ end
+
+ templates.each do |src, dst|
+ destination = target.join(dst)
+ SharedHelpers.filesystem_access(destination) do
+ thor.template("newgem/#{src}", destination, config)
+ end
+ end
+
+ executables.each do |file|
+ SharedHelpers.filesystem_access(target.join(file)) do |path|
+ executable = (path.stat.mode | 0o111)
+ path.chmod(executable)
+ end
+ end
+
+ if Bundler.git_present?
+ Bundler.ui.info "Initializing git repo in #{target}"
+ Dir.chdir(target) do
+ `git init`
+ `git add .`
+ end
+ end
+
+ # Open gemspec in editor
+ open_editor(options["edit"], target.join("#{name}.gemspec")) if options[:edit]
+
+ Bundler.ui.info "Gem '#{name}' was successfully created. " \
+ "For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html"
+ rescue Errno::EEXIST => e
+ raise GenericSystemCallError.new(e, "There was a conflict while creating the new gem.")
+ end
+
+ private
+
+ def resolve_name(name)
+ SharedHelpers.pwd.join(name).basename.to_s
+ end
+
+ def ask_and_set(key, header, message)
+ choice = options[key]
+ choice = Bundler.settings["gem.#{key}"] if choice.nil?
+
+ if choice.nil?
+ Bundler.ui.confirm header
+ choice = Bundler.ui.yes? "#{message} y/(n):"
+ Bundler.settings.set_global("gem.#{key}", choice)
+ end
+
+ choice
+ end
+
+ def validate_ext_name
+ return unless gem_name.index("-")
+
+ Bundler.ui.error "You have specified a gem name which does not conform to the \n" \
+ "naming guidelines for C extensions. For more information, \n" \
+ "see the 'Extension Naming' section at the following URL:\n" \
+ "http://guides.rubygems.org/gems-with-extensions/\n"
+ exit 1
+ end
+
+ def ask_and_set_test_framework
+ test_framework = options[:test] || Bundler.settings["gem.test"]
+
+ if test_framework.nil?
+ Bundler.ui.confirm "Do you want to generate tests with your gem?"
+ result = Bundler.ui.ask "Type 'rspec' or 'minitest' to generate those test files now and " \
+ "in the future. rspec/minitest/(none):"
+ if result =~ /rspec|minitest/
+ test_framework = result
+ else
+ test_framework = false
+ end
+ end
+
+ if Bundler.settings["gem.test"].nil?
+ Bundler.settings.set_global("gem.test", test_framework)
+ end
+
+ test_framework
+ end
+
+ def bundler_dependency_version
+ v = Gem::Version.new(Bundler::VERSION)
+ req = v.segments[0..1]
+ req << "a" if v.prerelease?
+ req.join(".")
+ end
+
+ def ensure_safe_gem_name(name, constant_array)
+ if name =~ /^\d/
+ Bundler.ui.error "Invalid gem name #{name} Please give a name which does not start with numbers."
+ exit 1
+ end
+
+ constant_name = constant_array.join("::")
+
+ existing_constant = constant_array.inject(Object) do |c, s|
+ defined = begin
+ c.const_defined?(s)
+ rescue NameError
+ Bundler.ui.error "Invalid gem name #{name} -- `#{constant_name}` is an invalid constant name"
+ exit 1
+ end
+ (defined && c.const_get(s)) || break
+ end
+
+ return unless existing_constant
+ Bundler.ui.error "Invalid gem name #{name} constant #{constant_name} is already in use. Please choose another gem name."
+ exit 1
+ end
+
+ def open_editor(editor, file)
+ thor.run(%(#{editor} "#{file}"))
+ end
+ end
+end
diff --git a/lib/bundler/cli/info.rb b/lib/bundler/cli/info.rb
new file mode 100644
index 00000000000000..958b5250677575
--- /dev/null
+++ b/lib/bundler/cli/info.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Info
+ attr_reader :gem_name, :options
+ def initialize(options, gem_name)
+ @options = options
+ @gem_name = gem_name
+ end
+
+ def run
+ spec = spec_for_gem(gem_name)
+
+ spec_not_found(gem_name) unless spec
+ return print_gem_path(spec) if @options[:path]
+ print_gem_info(spec)
+ end
+
+ private
+
+ def spec_for_gem(gem_name)
+ spec = Bundler.definition.specs.find {|s| s.name == gem_name }
+ spec || default_gem_spec(gem_name)
+ end
+
+ def default_gem_spec(gem_name)
+ return unless Gem::Specification.respond_to?(:find_all_by_name)
+ gem_spec = Gem::Specification.find_all_by_name(gem_name).last
+ return gem_spec if gem_spec && gem_spec.respond_to?(:default_gem?) && gem_spec.default_gem?
+ end
+
+ def spec_not_found(gem_name)
+ raise GemNotFound, Bundler::CLI::Common.gem_not_found_message(gem_name, Bundler.definition.dependencies)
+ end
+
+ def print_gem_path(spec)
+ Bundler.ui.info spec.full_gem_path
+ end
+
+ def print_gem_info(spec)
+ gem_info = String.new
+ gem_info << " * #{spec.name} (#{spec.version}#{spec.git_version})\n"
+ gem_info << "\tSummary: #{spec.summary}\n" if spec.summary
+ gem_info << "\tHomepage: #{spec.homepage}\n" if spec.homepage
+ gem_info << "\tPath: #{spec.full_gem_path}\n"
+ gem_info << "\tDefault Gem: yes" if spec.respond_to?(:default_gem?) && spec.default_gem?
+ Bundler.ui.info gem_info
+ end
+ end
+end
diff --git a/lib/bundler/cli/init.rb b/lib/bundler/cli/init.rb
new file mode 100644
index 00000000000000..40df7972693654
--- /dev/null
+++ b/lib/bundler/cli/init.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Init
+ attr_reader :options
+ def initialize(options)
+ @options = options
+ end
+
+ def run
+ if File.exist?(gemfile)
+ Bundler.ui.error "#{gemfile} already exists at #{File.expand_path(gemfile)}"
+ exit 1
+ end
+
+ unless File.writable?(Dir.pwd)
+ Bundler.ui.error "Can not create #{gemfile} as the current directory is not writable."
+ exit 1
+ end
+
+ if options[:gemspec]
+ gemspec = File.expand_path(options[:gemspec])
+ unless File.exist?(gemspec)
+ Bundler.ui.error "Gem specification #{gemspec} doesn't exist"
+ exit 1
+ end
+
+ spec = Bundler.load_gemspec_uncached(gemspec)
+
+ File.open(gemfile, "wb") do |file|
+ file << "# Generated from #{gemspec}\n"
+ file << spec.to_gemfile
+ end
+ else
+ FileUtils.cp(File.expand_path("../../templates/#{gemfile}", __FILE__), gemfile)
+ end
+
+ puts "Writing new #{gemfile} to #{SharedHelpers.pwd}/#{gemfile}"
+ end
+
+ private
+
+ def gemfile
+ @gemfile ||= Bundler.feature_flag.init_gems_rb? ? "gems.rb" : "Gemfile"
+ end
+ end
+end
diff --git a/lib/bundler/cli/inject.rb b/lib/bundler/cli/inject.rb
new file mode 100644
index 00000000000000..b00675d34883d4
--- /dev/null
+++ b/lib/bundler/cli/inject.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Inject
+ attr_reader :options, :name, :version, :group, :source, :gems
+ def initialize(options, name, version)
+ @options = options
+ @name = name
+ @version = version || last_version_number
+ @group = options[:group].split(",") unless options[:group].nil?
+ @source = options[:source]
+ @gems = []
+ end
+
+ def run
+ # The required arguments allow Thor to give useful feedback when the arguments
+ # are incorrect. This adds those first two arguments onto the list as a whole.
+ gems.unshift(source).unshift(group).unshift(version).unshift(name)
+
+ # Build an array of Dependency objects out of the arguments
+ deps = []
+ # when `inject` support addition of more than one gem, then this loop will
+ # help. Currently this loop is running once.
+ gems.each_slice(4) do |gem_name, gem_version, gem_group, gem_source|
+ ops = Gem::Requirement::OPS.map {|key, _val| key }
+ has_op = ops.any? {|op| gem_version.start_with? op }
+ gem_version = "~> #{gem_version}" unless has_op
+ deps << Bundler::Dependency.new(gem_name, gem_version, "group" => gem_group, "source" => gem_source)
+ end
+
+ added = Injector.inject(deps, options)
+
+ if added.any?
+ Bundler.ui.confirm "Added to Gemfile:"
+ Bundler.ui.confirm(added.map do |d|
+ name = "'#{d.name}'"
+ requirement = ", '#{d.requirement}'"
+ group = ", :group => #{d.groups.inspect}" if d.groups != Array(:default)
+ source = ", :source => '#{d.source}'" unless d.source.nil?
+ %(gem #{name}#{requirement}#{group}#{source})
+ end.join("\n"))
+ else
+ Bundler.ui.confirm "All gems were already present in the Gemfile"
+ end
+ end
+
+ private
+
+ def last_version_number
+ definition = Bundler.definition(true)
+ definition.resolve_remotely!
+ specs = definition.index[name].sort_by(&:version)
+ unless options[:pre]
+ specs.delete_if {|b| b.respond_to?(:version) && b.version.prerelease? }
+ end
+ spec = specs.last
+ spec.version.to_s
+ end
+ end
+end
diff --git a/lib/bundler/cli/install.rb b/lib/bundler/cli/install.rb
new file mode 100644
index 00000000000000..55e90ead0e1677
--- /dev/null
+++ b/lib/bundler/cli/install.rb
@@ -0,0 +1,217 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Install
+ attr_reader :options
+ def initialize(options)
+ @options = options
+ end
+
+ def run
+ Bundler.ui.level = "error" if options[:quiet]
+
+ warn_if_root
+
+ normalize_groups
+
+ Bundler::SharedHelpers.set_env "RB_USER_INSTALL", "1" if Bundler::FREEBSD
+
+ # Disable color in deployment mode
+ Bundler.ui.shell = Thor::Shell::Basic.new if options[:deployment]
+
+ check_for_options_conflicts
+
+ check_trust_policy
+
+ if options[:deployment] || options[:frozen] || Bundler.frozen_bundle?
+ unless Bundler.default_lockfile.exist?
+ flag = "--deployment flag" if options[:deployment]
+ flag ||= "--frozen flag" if options[:frozen]
+ flag ||= "deployment setting"
+ raise ProductionError, "The #{flag} requires a #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)}. Please make " \
+ "sure you have checked your #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)} into version control " \
+ "before deploying."
+ end
+
+ options[:local] = true if Bundler.app_cache.exist?
+
+ if Bundler.feature_flag.deployment_means_frozen?
+ Bundler.settings.set_command_option :deployment, true
+ else
+ Bundler.settings.set_command_option :frozen, true
+ end
+ end
+
+ # When install is called with --no-deployment, disable deployment mode
+ if options[:deployment] == false
+ Bundler.settings.set_command_option :frozen, nil
+ options[:system] = true
+ end
+
+ normalize_settings
+
+ Bundler::Fetcher.disable_endpoint = options["full-index"]
+
+ if options["binstubs"]
+ Bundler::SharedHelpers.major_deprecation 3,
+ "The --binstubs option will be removed in favor of `bundle binstubs`"
+ end
+
+ Plugin.gemfile_install(Bundler.default_gemfile) if Bundler.feature_flag.plugins?
+
+ definition = Bundler.definition
+ definition.validate_runtime!
+
+ installer = Installer.install(Bundler.root, definition, options)
+ Bundler.load.cache if Bundler.app_cache.exist? && !options["no-cache"] && !Bundler.frozen_bundle?
+
+ Bundler.ui.confirm "Bundle complete! #{dependencies_count_for(definition)}, #{gems_installed_for(definition)}."
+ Bundler::CLI::Common.output_without_groups_message
+
+ if Bundler.use_system_gems?
+ Bundler.ui.confirm "Use `bundle info [gemname]` to see where a bundled gem is installed."
+ else
+ relative_path = Bundler.configured_bundle_path.base_path_relative_to_pwd
+ Bundler.ui.confirm "Bundled gems are installed into `#{relative_path}`"
+ end
+
+ Bundler::CLI::Common.output_post_install_messages installer.post_install_messages
+
+ warn_ambiguous_gems
+
+ if CLI::Common.clean_after_install?
+ require "bundler/cli/clean"
+ Bundler::CLI::Clean.new(options).run
+ end
+ rescue GemNotFound, VersionConflict => e
+ if options[:local] && Bundler.app_cache.exist?
+ Bundler.ui.warn "Some gems seem to be missing from your #{Bundler.settings.app_cache_path} directory."
+ end
+
+ unless Bundler.definition.has_rubygems_remotes?
+ Bundler.ui.warn <<-WARN, :wrap => true
+ Your Gemfile has no gem server sources. If you need gems that are \
+ not already on your machine, add a line like this to your Gemfile:
+ source 'https://rubygems.org'
+ WARN
+ end
+ raise e
+ rescue Gem::InvalidSpecificationException => e
+ Bundler.ui.warn "You have one or more invalid gemspecs that need to be fixed."
+ raise e
+ end
+
+ private
+
+ def warn_if_root
+ return if Bundler.settings[:silence_root_warning] || Bundler::WINDOWS || !Process.uid.zero?
+ Bundler.ui.warn "Don't run Bundler as root. Bundler can ask for sudo " \
+ "if it is needed, and installing your bundle as root will break this " \
+ "application for all non-root users on this machine.", :wrap => true
+ end
+
+ def dependencies_count_for(definition)
+ count = definition.dependencies.count
+ "#{count} Gemfile #{count == 1 ? "dependency" : "dependencies"}"
+ end
+
+ def gems_installed_for(definition)
+ count = definition.specs.count
+ "#{count} #{count == 1 ? "gem" : "gems"} now installed"
+ end
+
+ def check_for_group_conflicts_in_cli_options
+ conflicting_groups = Array(options[:without]) & Array(options[:with])
+ return if conflicting_groups.empty?
+ raise InvalidOption, "You can't list a group in both with and without." \
+ " The offending groups are: #{conflicting_groups.join(", ")}."
+ end
+
+ def check_for_options_conflicts
+ if (options[:path] || options[:deployment]) && options[:system]
+ error_message = String.new
+ error_message << "You have specified both --path as well as --system. Please choose only one option.\n" if options[:path]
+ error_message << "You have specified both --deployment as well as --system. Please choose only one option.\n" if options[:deployment]
+ raise InvalidOption.new(error_message)
+ end
+ end
+
+ def check_trust_policy
+ trust_policy = options["trust-policy"]
+ unless Bundler.rubygems.security_policies.keys.unshift(nil).include?(trust_policy)
+ raise InvalidOption, "RubyGems doesn't know about trust policy '#{trust_policy}'. " \
+ "The known policies are: #{Bundler.rubygems.security_policies.keys.join(", ")}."
+ end
+ Bundler.settings.set_command_option_if_given :"trust-policy", trust_policy
+ end
+
+ def normalize_groups
+ options[:with] &&= options[:with].join(":").tr(" ", ":").split(":")
+ options[:without] &&= options[:without].join(":").tr(" ", ":").split(":")
+
+ check_for_group_conflicts_in_cli_options
+
+ Bundler.settings.set_command_option :with, nil if options[:with] == []
+ Bundler.settings.set_command_option :without, nil if options[:without] == []
+
+ with = options.fetch(:with, [])
+ with |= Bundler.settings[:with].map(&:to_s)
+ with -= options[:without] if options[:without]
+
+ without = options.fetch(:without, [])
+ without |= Bundler.settings[:without].map(&:to_s)
+ without -= options[:with] if options[:with]
+
+ options[:with] = with
+ options[:without] = without
+ end
+
+ def normalize_settings
+ Bundler.settings.set_command_option :path, nil if options[:system]
+ Bundler.settings.temporary(:path_relative_to_cwd => false) do
+ Bundler.settings.set_command_option :path, "vendor/bundle" if options[:deployment]
+ end
+ Bundler.settings.set_command_option_if_given :path, options[:path]
+ Bundler.settings.temporary(:path_relative_to_cwd => false) do
+ Bundler.settings.set_command_option :path, "bundle" if options["standalone"] && Bundler.settings[:path].nil?
+ end
+
+ bin_option = options["binstubs"]
+ bin_option = nil if bin_option && bin_option.empty?
+ Bundler.settings.set_command_option :bin, bin_option if options["binstubs"]
+
+ Bundler.settings.set_command_option_if_given :shebang, options["shebang"]
+
+ Bundler.settings.set_command_option_if_given :jobs, options["jobs"]
+
+ Bundler.settings.set_command_option_if_given :no_prune, options["no-prune"]
+
+ Bundler.settings.set_command_option_if_given :no_install, options["no-install"]
+
+ Bundler.settings.set_command_option_if_given :clean, options["clean"]
+
+ unless Bundler.settings[:without] == options[:without] && Bundler.settings[:with] == options[:with]
+ # need to nil them out first to get around validation for backwards compatibility
+ Bundler.settings.set_command_option :without, nil
+ Bundler.settings.set_command_option :with, nil
+ Bundler.settings.set_command_option :without, options[:without] - options[:with]
+ Bundler.settings.set_command_option :with, options[:with]
+ end
+
+ options[:force] = options[:redownload]
+ end
+
+ def warn_ambiguous_gems
+ Installer.ambiguous_gems.to_a.each do |name, installed_from_uri, *also_found_in_uris|
+ Bundler.ui.error "Warning: the gem '#{name}' was found in multiple sources."
+ Bundler.ui.error "Installed from: #{installed_from_uri}"
+ Bundler.ui.error "Also found in:"
+ also_found_in_uris.each {|uri| Bundler.ui.error " * #{uri}" }
+ Bundler.ui.error "You should add a source requirement to restrict this gem to your preferred source."
+ Bundler.ui.error "For example:"
+ Bundler.ui.error " gem '#{name}', :source => '#{installed_from_uri}'"
+ Bundler.ui.error "Then uninstall the gem '#{name}' (or delete all bundled gems) and then install again."
+ end
+ end
+ end
+end
diff --git a/lib/bundler/cli/issue.rb b/lib/bundler/cli/issue.rb
new file mode 100644
index 00000000000000..91f827ea99cc7e
--- /dev/null
+++ b/lib/bundler/cli/issue.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+require "rbconfig"
+
+module Bundler
+ class CLI::Issue
+ def run
+ Bundler.ui.info <<-EOS.gsub(/^ {8}/, "")
+ Did you find an issue with Bundler? Before filing a new issue,
+ be sure to check out these resources:
+
+ 1. Check out our troubleshooting guide for quick fixes to common issues:
+ https://github.com/bundler/bundler/blob/master/doc/TROUBLESHOOTING.md
+
+ 2. Instructions for common Bundler uses can be found on the documentation
+ site: http://bundler.io/
+
+ 3. Information about each Bundler command can be found in the Bundler
+ man pages: http://bundler.io/man/bundle.1.html
+
+ Hopefully the troubleshooting steps above resolved your problem! If things
+ still aren't working the way you expect them to, please let us know so
+ that we can diagnose and help fix the problem you're having. Please
+ view the Filing Issues guide for more information:
+ https://github.com/bundler/bundler/blob/master/doc/contributing/ISSUES.md
+
+ EOS
+
+ Bundler.ui.info Bundler::Env.report
+
+ Bundler.ui.info "\n## Bundle Doctor"
+ doctor
+ end
+
+ def doctor
+ require "bundler/cli/doctor"
+ Bundler::CLI::Doctor.new({}).run
+ end
+ end
+end
diff --git a/lib/bundler/cli/list.rb b/lib/bundler/cli/list.rb
new file mode 100644
index 00000000000000..d1799196e783f6
--- /dev/null
+++ b/lib/bundler/cli/list.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::List
+ def initialize(options)
+ @options = options
+ end
+
+ def run
+ raise InvalidOption, "The `--only-group` and `--without-group` options cannot be used together" if @options["only-group"] && @options["without-group"]
+
+ raise InvalidOption, "The `--name-only` and `--paths` options cannot be used together" if @options["name-only"] && @options[:paths]
+
+ specs = if @options["only-group"] || @options["without-group"]
+ filtered_specs_by_groups
+ else
+ Bundler.load.specs
+ end.reject {|s| s.name == "bundler" }.sort_by(&:name)
+
+ return Bundler.ui.info "No gems in the Gemfile" if specs.empty?
+
+ return specs.each {|s| Bundler.ui.info s.name } if @options["name-only"]
+ return specs.each {|s| Bundler.ui.info s.full_gem_path } if @options["paths"]
+
+ Bundler.ui.info "Gems included by the bundle:"
+
+ specs.each {|s| Bundler.ui.info " * #{s.name} (#{s.version}#{s.git_version})" }
+
+ Bundler.ui.info "Use `bundle info` to print more detailed information about a gem"
+ end
+
+ private
+
+ def verify_group_exists(groups)
+ raise InvalidOption, "`#{@options["without-group"]}` group could not be found." if @options["without-group"] && !groups.include?(@options["without-group"].to_sym)
+
+ raise InvalidOption, "`#{@options["only-group"]}` group could not be found." if @options["only-group"] && !groups.include?(@options["only-group"].to_sym)
+ end
+
+ def filtered_specs_by_groups
+ definition = Bundler.definition
+ groups = definition.groups
+
+ verify_group_exists(groups)
+
+ show_groups =
+ if @options["without-group"]
+ groups.reject {|g| g == @options["without-group"].to_sym }
+ elsif @options["only-group"]
+ groups.select {|g| g == @options["only-group"].to_sym }
+ else
+ groups
+ end.map(&:to_sym)
+
+ definition.specs_for(show_groups)
+ end
+ end
+end
diff --git a/lib/bundler/cli/lock.rb b/lib/bundler/cli/lock.rb
new file mode 100644
index 00000000000000..7dd078b1ef9544
--- /dev/null
+++ b/lib/bundler/cli/lock.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Lock
+ attr_reader :options
+
+ def initialize(options)
+ @options = options
+ end
+
+ def run
+ unless Bundler.default_gemfile
+ Bundler.ui.error "Unable to find a Gemfile to lock"
+ exit 1
+ end
+
+ print = options[:print]
+ ui = Bundler.ui
+ Bundler.ui = UI::Silent.new if print
+
+ Bundler::Fetcher.disable_endpoint = options["full-index"]
+
+ update = options[:update]
+ if update.is_a?(Array) # unlocking specific gems
+ Bundler::CLI::Common.ensure_all_gems_in_lockfile!(update)
+ update = { :gems => update, :lock_shared_dependencies => options[:conservative] }
+ end
+ definition = Bundler.definition(update)
+
+ Bundler::CLI::Common.configure_gem_version_promoter(Bundler.definition, options) if options[:update]
+
+ options["remove-platform"].each do |platform|
+ definition.remove_platform(platform)
+ end
+
+ options["add-platform"].each do |platform_string|
+ platform = Gem::Platform.new(platform_string)
+ if platform.to_s == "unknown"
+ Bundler.ui.warn "The platform `#{platform_string}` is unknown to RubyGems " \
+ "and adding it will likely lead to resolution errors"
+ end
+ definition.add_platform(platform)
+ end
+
+ if definition.platforms.empty?
+ raise InvalidOption, "Removing all platforms from the bundle is not allowed"
+ end
+
+ definition.resolve_remotely! unless options[:local]
+
+ if print
+ puts definition.to_lock
+ else
+ file = options[:lockfile]
+ file = file ? File.expand_path(file) : Bundler.default_lockfile
+ puts "Writing lockfile to #{file}"
+ definition.lock(file)
+ end
+
+ Bundler.ui = ui
+ end
+ end
+end
diff --git a/lib/bundler/cli/open.rb b/lib/bundler/cli/open.rb
new file mode 100644
index 00000000000000..552fe6f128ba4c
--- /dev/null
+++ b/lib/bundler/cli/open.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+require "shellwords"
+
+module Bundler
+ class CLI::Open
+ attr_reader :options, :name
+ def initialize(options, name)
+ @options = options
+ @name = name
+ end
+
+ def run
+ editor = [ENV["BUNDLER_EDITOR"], ENV["VISUAL"], ENV["EDITOR"]].find {|e| !e.nil? && !e.empty? }
+ return Bundler.ui.info("To open a bundled gem, set $EDITOR or $BUNDLER_EDITOR") unless editor
+ return unless spec = Bundler::CLI::Common.select_spec(name, :regex_match)
+ path = spec.full_gem_path
+ Dir.chdir(path) do
+ command = Shellwords.split(editor) + [path]
+ Bundler.with_original_env do
+ system(*command)
+ end || Bundler.ui.info("Could not run '#{command.join(" ")}'")
+ end
+ end
+ end
+end
diff --git a/lib/bundler/cli/outdated.rb b/lib/bundler/cli/outdated.rb
new file mode 100644
index 00000000000000..2ca90293db0b74
--- /dev/null
+++ b/lib/bundler/cli/outdated.rb
@@ -0,0 +1,266 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Outdated
+ attr_reader :options, :gems
+
+ def initialize(options, gems)
+ @options = options
+ @gems = gems
+ end
+
+ def run
+ check_for_deployment_mode
+
+ sources = Array(options[:source])
+
+ gems.each do |gem_name|
+ Bundler::CLI::Common.select_spec(gem_name)
+ end
+
+ Bundler.definition.validate_runtime!
+ current_specs = Bundler.ui.silence { Bundler.definition.resolve }
+ current_dependencies = {}
+ Bundler.ui.silence do
+ Bundler.load.dependencies.each do |dep|
+ current_dependencies[dep.name] = dep
+ end
+ end
+
+ definition = if gems.empty? && sources.empty?
+ # We're doing a full update
+ Bundler.definition(true)
+ else
+ Bundler.definition(:gems => gems, :sources => sources)
+ end
+
+ Bundler::CLI::Common.configure_gem_version_promoter(
+ Bundler.definition,
+ options
+ )
+
+ # the patch level options imply strict is also true. It wouldn't make
+ # sense otherwise.
+ strict = options[:strict] ||
+ Bundler::CLI::Common.patch_level_options(options).any?
+
+ filter_options_patch = options.keys &
+ %w[filter-major filter-minor filter-patch]
+
+ definition_resolution = proc do
+ options[:local] ? definition.resolve_with_cache! : definition.resolve_remotely!
+ end
+
+ if options[:parseable]
+ Bundler.ui.silence(&definition_resolution)
+ else
+ definition_resolution.call
+ end
+
+ Bundler.ui.info ""
+ outdated_gems_by_groups = {}
+ outdated_gems_list = []
+
+ # Loop through the current specs
+ gemfile_specs, dependency_specs = current_specs.partition do |spec|
+ current_dependencies.key? spec.name
+ end
+
+ specs = if options["only-explicit"]
+ gemfile_specs
+ else
+ gemfile_specs + dependency_specs
+ end
+
+ specs.sort_by(&:name).each do |current_spec|
+ next if !gems.empty? && !gems.include?(current_spec.name)
+
+ dependency = current_dependencies[current_spec.name]
+ active_spec = retrieve_active_spec(strict, definition, current_spec)
+
+ next if active_spec.nil?
+ if filter_options_patch.any?
+ update_present = update_present_via_semver_portions(current_spec, active_spec, options)
+ next unless update_present
+ end
+
+ gem_outdated = Gem::Version.new(active_spec.version) > Gem::Version.new(current_spec.version)
+ next unless gem_outdated || (current_spec.git_version != active_spec.git_version)
+ groups = nil
+ if dependency && !options[:parseable]
+ groups = dependency.groups.join(", ")
+ end
+
+ outdated_gems_list << { :active_spec => active_spec,
+ :current_spec => current_spec,
+ :dependency => dependency,
+ :groups => groups }
+
+ outdated_gems_by_groups[groups] ||= []
+ outdated_gems_by_groups[groups] << { :active_spec => active_spec,
+ :current_spec => current_spec,
+ :dependency => dependency,
+ :groups => groups }
+ end
+
+ if outdated_gems_list.empty?
+ display_nothing_outdated_message(filter_options_patch)
+ else
+ unless options[:parseable]
+ if options[:pre]
+ Bundler.ui.info "Outdated gems included in the bundle (including " \
+ "pre-releases):"
+ else
+ Bundler.ui.info "Outdated gems included in the bundle:"
+ end
+ end
+
+ options_include_groups = [:group, :groups].select do |v|
+ options.keys.include?(v.to_s)
+ end
+
+ if options_include_groups.any?
+ ordered_groups = outdated_gems_by_groups.keys.compact.sort
+ [nil, ordered_groups].flatten.each do |groups|
+ gems = outdated_gems_by_groups[groups]
+ contains_group = if groups
+ groups.split(",").include?(options[:group])
+ else
+ options[:group] == "group"
+ end
+
+ next if (!options[:groups] && !contains_group) || gems.nil?
+
+ unless options[:parseable]
+ if groups
+ Bundler.ui.info "===== Group #{groups} ====="
+ else
+ Bundler.ui.info "===== Without group ====="
+ end
+ end
+
+ gems.each do |gem|
+ print_gem(
+ gem[:current_spec],
+ gem[:active_spec],
+ gem[:dependency],
+ groups,
+ options_include_groups.any?
+ )
+ end
+ end
+ else
+ outdated_gems_list.each do |gem|
+ print_gem(
+ gem[:current_spec],
+ gem[:active_spec],
+ gem[:dependency],
+ gem[:groups],
+ options_include_groups.any?
+ )
+ end
+ end
+
+ exit 1
+ end
+ end
+
+ private
+
+ def retrieve_active_spec(strict, definition, current_spec)
+ if strict
+ active_spec = definition.find_resolved_spec(current_spec)
+ else
+ active_specs = definition.find_indexed_specs(current_spec)
+ if !current_spec.version.prerelease? && !options[:pre] && active_specs.size > 1
+ active_specs.delete_if {|b| b.respond_to?(:version) && b.version.prerelease? }
+ end
+ active_spec = active_specs.last
+ end
+
+ active_spec
+ end
+
+ def display_nothing_outdated_message(filter_options_patch)
+ unless options[:parseable]
+ if filter_options_patch.any?
+ display = filter_options_patch.map do |o|
+ o.sub("filter-", "")
+ end.join(" or ")
+
+ Bundler.ui.info "No #{display} updates to display.\n"
+ else
+ Bundler.ui.info "Bundle up to date!\n"
+ end
+ end
+ end
+
+ def print_gem(current_spec, active_spec, dependency, groups, options_include_groups)
+ spec_version = "#{active_spec.version}#{active_spec.git_version}"
+ spec_version += " (from #{active_spec.loaded_from})" if Bundler.ui.debug? && active_spec.loaded_from
+ current_version = "#{current_spec.version}#{current_spec.git_version}"
+
+ if dependency && dependency.specific?
+ dependency_version = %(, requested #{dependency.requirement})
+ end
+
+ spec_outdated_info = "#{active_spec.name} (newest #{spec_version}, " \
+ "installed #{current_version}#{dependency_version})"
+
+ output_message = if options[:parseable]
+ spec_outdated_info.to_s
+ elsif options_include_groups || !groups
+ " * #{spec_outdated_info}"
+ else
+ " * #{spec_outdated_info} in groups \"#{groups}\""
+ end
+
+ Bundler.ui.info output_message.rstrip
+ end
+
+ def check_for_deployment_mode
+ return unless Bundler.frozen_bundle?
+ suggested_command = if Bundler.settings.locations("frozen")[:global]
+ "bundle config --delete frozen"
+ elsif Bundler.settings.locations("deployment").keys.&([:global, :local]).any?
+ "bundle config --delete deployment"
+ else
+ "bundle install --no-deployment"
+ end
+ raise ProductionError, "You are trying to check outdated gems in " \
+ "deployment mode. Run `bundle outdated` elsewhere.\n" \
+ "\nIf this is a development machine, remove the " \
+ "#{Bundler.default_gemfile} freeze" \
+ "\nby running `#{suggested_command}`."
+ end
+
+ def update_present_via_semver_portions(current_spec, active_spec, options)
+ current_major = current_spec.version.segments.first
+ active_major = active_spec.version.segments.first
+
+ update_present = false
+ update_present = active_major > current_major if options["filter-major"]
+
+ if !update_present && (options["filter-minor"] || options["filter-patch"]) && current_major == active_major
+ current_minor = get_version_semver_portion_value(current_spec, 1)
+ active_minor = get_version_semver_portion_value(active_spec, 1)
+
+ update_present = active_minor > current_minor if options["filter-minor"]
+
+ if !update_present && options["filter-patch"] && current_minor == active_minor
+ current_patch = get_version_semver_portion_value(current_spec, 2)
+ active_patch = get_version_semver_portion_value(active_spec, 2)
+
+ update_present = active_patch > current_patch
+ end
+ end
+
+ update_present
+ end
+
+ def get_version_semver_portion_value(spec, version_portion_index)
+ version_section = spec.version.segments[version_portion_index, 1]
+ version_section.nil? ? 0 : (version_section.first || 0)
+ end
+ end
+end
diff --git a/lib/bundler/cli/package.rb b/lib/bundler/cli/package.rb
new file mode 100644
index 00000000000000..2dcd0e1e29c31e
--- /dev/null
+++ b/lib/bundler/cli/package.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Package
+ attr_reader :options
+
+ def initialize(options)
+ @options = options
+ end
+
+ def run
+ Bundler.ui.level = "error" if options[:quiet]
+ Bundler.settings.set_command_option_if_given :path, options[:path]
+ Bundler.settings.set_command_option_if_given :cache_all_platforms, options["all-platforms"]
+ Bundler.settings.set_command_option_if_given :cache_path, options["cache-path"]
+
+ setup_cache_all
+ install
+
+ # TODO: move cache contents here now that all bundles are locked
+ custom_path = Bundler.settings[:path] if options[:path]
+ Bundler.load.cache(custom_path)
+ end
+
+ private
+
+ def install
+ require "bundler/cli/install"
+ options = self.options.dup
+ if Bundler.settings[:cache_all_platforms]
+ options["local"] = false
+ options["update"] = true
+ end
+ Bundler::CLI::Install.new(options).run
+ end
+
+ def setup_cache_all
+ all = options.fetch(:all, Bundler.feature_flag.cache_command_is_package? || nil)
+
+ Bundler.settings.set_command_option_if_given :cache_all, all
+
+ if Bundler.definition.has_local_dependencies? && !Bundler.feature_flag.cache_all?
+ Bundler.ui.warn "Your Gemfile contains path and git dependencies. If you want " \
+ "to package them as well, please pass the --all flag. This will be the default " \
+ "on Bundler 2.0."
+ end
+ end
+ end
+end
diff --git a/lib/bundler/cli/platform.rb b/lib/bundler/cli/platform.rb
new file mode 100644
index 00000000000000..e97cad49a4c052
--- /dev/null
+++ b/lib/bundler/cli/platform.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Platform
+ attr_reader :options
+ def initialize(options)
+ @options = options
+ end
+
+ def run
+ platforms, ruby_version = Bundler.ui.silence do
+ locked_ruby_version = Bundler.locked_gems && Bundler.locked_gems.ruby_version
+ gemfile_ruby_version = Bundler.definition.ruby_version && Bundler.definition.ruby_version.single_version_string
+ [Bundler.definition.platforms.map {|p| "* #{p}" },
+ locked_ruby_version || gemfile_ruby_version]
+ end
+ output = []
+
+ if options[:ruby]
+ if ruby_version
+ output << ruby_version
+ else
+ output << "No ruby version specified"
+ end
+ else
+ output << "Your platform is: #{RUBY_PLATFORM}"
+ output << "Your app has gems that work on these platforms:\n#{platforms.join("\n")}"
+
+ if ruby_version
+ output << "Your Gemfile specifies a Ruby version requirement:\n* #{ruby_version}"
+
+ begin
+ Bundler.definition.validate_runtime!
+ output << "Your current platform satisfies the Ruby version requirement."
+ rescue RubyVersionMismatch => e
+ output << e.message
+ end
+ else
+ output << "Your Gemfile does not specify a Ruby version requirement."
+ end
+ end
+
+ Bundler.ui.info output.join("\n\n")
+ end
+ end
+end
diff --git a/lib/bundler/cli/plugin.rb b/lib/bundler/cli/plugin.rb
new file mode 100644
index 00000000000000..5488a9f28db95f
--- /dev/null
+++ b/lib/bundler/cli/plugin.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+require "bundler/vendored_thor"
+module Bundler
+ class CLI::Plugin < Thor
+ desc "install PLUGINS", "Install the plugin from the source"
+ long_desc <<-D
+ Install plugins either from the rubygems source provided (with --source option) or from a git source provided with (--git option). If no sources are provided, it uses Gem.sources
+ D
+ method_option "source", :type => :string, :default => nil, :banner =>
+ "URL of the RubyGems source to fetch the plugin from"
+ method_option "version", :type => :string, :default => nil, :banner =>
+ "The version of the plugin to fetch"
+ method_option "git", :type => :string, :default => nil, :banner =>
+ "URL of the git repo to fetch from"
+ method_option "branch", :type => :string, :default => nil, :banner =>
+ "The git branch to checkout"
+ method_option "ref", :type => :string, :default => nil, :banner =>
+ "The git revision to check out"
+ def install(*plugins)
+ Bundler::Plugin.install(plugins, options)
+ end
+ end
+end
diff --git a/lib/bundler/cli/pristine.rb b/lib/bundler/cli/pristine.rb
new file mode 100644
index 00000000000000..4a411a83fc701a
--- /dev/null
+++ b/lib/bundler/cli/pristine.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Pristine
+ def initialize(gems)
+ @gems = gems
+ end
+
+ def run
+ CLI::Common.ensure_all_gems_in_lockfile!(@gems)
+ definition = Bundler.definition
+ definition.validate_runtime!
+ installer = Bundler::Installer.new(Bundler.root, definition)
+
+ Bundler.load.specs.each do |spec|
+ next if spec.name == "bundler" # Source::Rubygems doesn't install bundler
+ next if !@gems.empty? && !@gems.include?(spec.name)
+
+ gem_name = "#{spec.name} (#{spec.version}#{spec.git_version})"
+ gem_name += " (#{spec.platform})" if !spec.platform.nil? && spec.platform != Gem::Platform::RUBY
+
+ case source = spec.source
+ when Source::Rubygems
+ cached_gem = spec.cache_file
+ unless File.exist?(cached_gem)
+ Bundler.ui.error("Failed to pristine #{gem_name}. Cached gem #{cached_gem} does not exist.")
+ next
+ end
+
+ FileUtils.rm_rf spec.full_gem_path
+ when Source::Git
+ source.remote!
+ if extension_cache_path = source.extension_cache_path(spec)
+ FileUtils.rm_rf extension_cache_path
+ end
+ FileUtils.rm_rf spec.extension_dir if spec.respond_to?(:extension_dir)
+ FileUtils.rm_rf spec.full_gem_path
+ else
+ Bundler.ui.warn("Cannot pristine #{gem_name}. Gem is sourced from local path.")
+ next
+ end
+
+ Bundler::GemInstaller.new(spec, installer, false, 0, true).install_from_spec
+ end
+ end
+ end
+end
diff --git a/lib/bundler/cli/remove.rb b/lib/bundler/cli/remove.rb
new file mode 100644
index 00000000000000..cd6a2cec281e52
--- /dev/null
+++ b/lib/bundler/cli/remove.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Remove
+ def initialize(gems, options)
+ @gems = gems
+ @options = options
+ end
+
+ def run
+ raise InvalidOption, "Please specify gems to remove." if @gems.empty?
+
+ Injector.remove(@gems, {})
+
+ Installer.install(Bundler.root, Bundler.definition) if @options["install"]
+ end
+ end
+end
diff --git a/lib/bundler/cli/show.rb b/lib/bundler/cli/show.rb
new file mode 100644
index 00000000000000..61756801b2f121
--- /dev/null
+++ b/lib/bundler/cli/show.rb
@@ -0,0 +1,75 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Show
+ attr_reader :options, :gem_name, :latest_specs
+ def initialize(options, gem_name)
+ @options = options
+ @gem_name = gem_name
+ @verbose = options[:verbose] || options[:outdated]
+ @latest_specs = fetch_latest_specs if @verbose
+ end
+
+ def run
+ Bundler.ui.silence do
+ Bundler.definition.validate_runtime!
+ Bundler.load.lock
+ end
+
+ if gem_name
+ if gem_name == "bundler"
+ path = File.expand_path("../../../..", __FILE__)
+ else
+ spec = Bundler::CLI::Common.select_spec(gem_name, :regex_match)
+ return unless spec
+ path = spec.full_gem_path
+ unless File.directory?(path)
+ Bundler.ui.warn "The gem #{gem_name} has been deleted. It was installed at:"
+ end
+ end
+ return Bundler.ui.info(path)
+ end
+
+ if options[:paths]
+ Bundler.load.specs.sort_by(&:name).map do |s|
+ Bundler.ui.info s.full_gem_path
+ end
+ else
+ Bundler.ui.info "Gems included by the bundle:"
+ Bundler.load.specs.sort_by(&:name).each do |s|
+ desc = " * #{s.name} (#{s.version}#{s.git_version})"
+ if @verbose
+ latest = latest_specs.find {|l| l.name == s.name }
+ Bundler.ui.info <<-END.gsub(/^ +/, "")
+ #{desc}
+ \tSummary: #{s.summary || "No description available."}
+ \tHomepage: #{s.homepage || "No website available."}
+ \tStatus: #{outdated?(s, latest) ? "Outdated - #{s.version} < #{latest.version}" : "Up to date"}
+ END
+ else
+ Bundler.ui.info desc
+ end
+ end
+ end
+ end
+
+ private
+
+ def fetch_latest_specs
+ definition = Bundler.definition(true)
+ if options[:outdated]
+ Bundler.ui.info "Fetching remote specs for outdated check...\n\n"
+ Bundler.ui.silence { definition.resolve_remotely! }
+ else
+ definition.resolve_with_cache!
+ end
+ Bundler.reset!
+ definition.specs
+ end
+
+ def outdated?(current, latest)
+ return false unless latest
+ Gem::Version.new(current.version) < Gem::Version.new(latest.version)
+ end
+ end
+end
diff --git a/lib/bundler/cli/update.rb b/lib/bundler/cli/update.rb
new file mode 100644
index 00000000000000..bf300a843717a8
--- /dev/null
+++ b/lib/bundler/cli/update.rb
@@ -0,0 +1,91 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Update
+ attr_reader :options, :gems
+ def initialize(options, gems)
+ @options = options
+ @gems = gems
+ end
+
+ def run
+ Bundler.ui.level = "error" if options[:quiet]
+
+ Plugin.gemfile_install(Bundler.default_gemfile) if Bundler.feature_flag.plugins?
+
+ sources = Array(options[:source])
+ groups = Array(options[:group]).map(&:to_sym)
+
+ full_update = gems.empty? && sources.empty? && groups.empty? && !options[:ruby] && !options[:bundler]
+
+ if full_update && !options[:all]
+ if Bundler.feature_flag.update_requires_all_flag?
+ raise InvalidOption, "To update everything, pass the `--all` flag."
+ end
+ SharedHelpers.major_deprecation 3, "Pass --all to `bundle update` to update everything"
+ elsif !full_update && options[:all]
+ raise InvalidOption, "Cannot specify --all along with specific options."
+ end
+
+ if full_update
+ # We're doing a full update
+ Bundler.definition(true)
+ else
+ unless Bundler.default_lockfile.exist?
+ raise GemfileLockNotFound, "This Bundle hasn't been installed yet. " \
+ "Run `bundle install` to update and install the bundled gems."
+ end
+ Bundler::CLI::Common.ensure_all_gems_in_lockfile!(gems)
+
+ if groups.any?
+ deps = Bundler.definition.dependencies.select {|d| (d.groups & groups).any? }
+ gems.concat(deps.map(&:name))
+ end
+
+ Bundler.definition(:gems => gems, :sources => sources, :ruby => options[:ruby],
+ :lock_shared_dependencies => options[:conservative],
+ :bundler => options[:bundler])
+ end
+
+ Bundler::CLI::Common.configure_gem_version_promoter(Bundler.definition, options)
+
+ Bundler::Fetcher.disable_endpoint = options["full-index"]
+
+ opts = options.dup
+ opts["update"] = true
+ opts["local"] = options[:local]
+
+ Bundler.settings.set_command_option_if_given :jobs, opts["jobs"]
+
+ Bundler.definition.validate_runtime!
+ installer = Installer.install Bundler.root, Bundler.definition, opts
+ Bundler.load.cache if Bundler.app_cache.exist?
+
+ if CLI::Common.clean_after_install?
+ require "bundler/cli/clean"
+ Bundler::CLI::Clean.new(options).run
+ end
+
+ if locked_gems = Bundler.definition.locked_gems
+ gems.each do |name|
+ locked_version = locked_gems.specs.find {|s| s.name == name }
+ locked_version &&= locked_version.version
+ next unless locked_version
+ new_version = Bundler.definition.specs[name].first
+ new_version &&= new_version.version
+ if !new_version
+ Bundler.ui.warn "Bundler attempted to update #{name} but it was removed from the bundle"
+ elsif new_version < locked_version
+ Bundler.ui.warn "Note: #{name} version regressed from #{locked_version} to #{new_version}"
+ elsif new_version == locked_version
+ Bundler.ui.warn "Bundler attempted to update #{name} but its version stayed the same"
+ end
+ end
+ end
+
+ Bundler.ui.confirm "Bundle updated!"
+ Bundler::CLI::Common.output_without_groups_message
+ Bundler::CLI::Common.output_post_install_messages installer.post_install_messages
+ end
+ end
+end
diff --git a/lib/bundler/cli/viz.rb b/lib/bundler/cli/viz.rb
new file mode 100644
index 00000000000000..644f9b25cf704b
--- /dev/null
+++ b/lib/bundler/cli/viz.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CLI::Viz
+ attr_reader :options, :gem_name
+ def initialize(options)
+ @options = options
+ end
+
+ def run
+ # make sure we get the right `graphviz`. There is also a `graphviz`
+ # gem we're not built to support
+ gem "ruby-graphviz"
+ require "graphviz"
+
+ options[:without] = options[:without].join(":").tr(" ", ":").split(":")
+ output_file = File.expand_path(options[:file])
+
+ graph = Graph.new(Bundler.load, output_file, options[:version], options[:requirements], options[:format], options[:without])
+ graph.viz
+ rescue LoadError => e
+ Bundler.ui.error e.inspect
+ Bundler.ui.warn "Make sure you have the graphviz ruby gem. You can install it with:"
+ Bundler.ui.warn "`gem install ruby-graphviz`"
+ rescue StandardError => e
+ raise unless e.message =~ /GraphViz not installed or dot not in PATH/
+ Bundler.ui.error e.message
+ Bundler.ui.warn "Please install GraphViz. On a Mac with Homebrew, you can run `brew install graphviz`."
+ end
+ end
+end
diff --git a/lib/bundler/compact_index_client.rb b/lib/bundler/compact_index_client.rb
new file mode 100644
index 00000000000000..6c241ca07a0cbf
--- /dev/null
+++ b/lib/bundler/compact_index_client.rb
@@ -0,0 +1,109 @@
+# frozen_string_literal: true
+
+require "pathname"
+require "set"
+
+module Bundler
+ class CompactIndexClient
+ DEBUG_MUTEX = Mutex.new
+ def self.debug
+ return unless ENV["DEBUG_COMPACT_INDEX"]
+ DEBUG_MUTEX.synchronize { warn("[#{self}] #{yield}") }
+ end
+
+ class Error < StandardError; end
+
+ require "bundler/compact_index_client/cache"
+ require "bundler/compact_index_client/updater"
+
+ attr_reader :directory
+
+ # @return [Lambda] A lambda that takes an array of inputs and a block, and
+ # maps the inputs with the block in parallel.
+ #
+ attr_accessor :in_parallel
+
+ def initialize(directory, fetcher)
+ @directory = Pathname.new(directory)
+ @updater = Updater.new(fetcher)
+ @cache = Cache.new(@directory)
+ @endpoints = Set.new
+ @info_checksums_by_name = {}
+ @parsed_checksums = false
+ @mutex = Mutex.new
+ @in_parallel = lambda do |inputs, &blk|
+ inputs.map(&blk)
+ end
+ end
+
+ def names
+ Bundler::CompactIndexClient.debug { "/names" }
+ update(@cache.names_path, "names")
+ @cache.names
+ end
+
+ def versions
+ Bundler::CompactIndexClient.debug { "/versions" }
+ update(@cache.versions_path, "versions")
+ versions, @info_checksums_by_name = @cache.versions
+ versions
+ end
+
+ def dependencies(names)
+ Bundler::CompactIndexClient.debug { "dependencies(#{names})" }
+ in_parallel.call(names) do |name|
+ update_info(name)
+ @cache.dependencies(name).map {|d| d.unshift(name) }
+ end.flatten(1)
+ end
+
+ def spec(name, version, platform = nil)
+ Bundler::CompactIndexClient.debug { "spec(name = #{name}, version = #{version}, platform = #{platform})" }
+ update_info(name)
+ @cache.specific_dependency(name, version, platform)
+ end
+
+ def update_and_parse_checksums!
+ Bundler::CompactIndexClient.debug { "update_and_parse_checksums!" }
+ return @info_checksums_by_name if @parsed_checksums
+ update(@cache.versions_path, "versions")
+ @info_checksums_by_name = @cache.checksums
+ @parsed_checksums = true
+ end
+
+ private
+
+ def update(local_path, remote_path)
+ Bundler::CompactIndexClient.debug { "update(#{local_path}, #{remote_path})" }
+ unless synchronize { @endpoints.add?(remote_path) }
+ Bundler::CompactIndexClient.debug { "already fetched #{remote_path}" }
+ return
+ end
+ @updater.update(local_path, url(remote_path))
+ end
+
+ def update_info(name)
+ Bundler::CompactIndexClient.debug { "update_info(#{name})" }
+ path = @cache.info_path(name)
+ checksum = @updater.checksum_for_file(path)
+ unless existing = @info_checksums_by_name[name]
+ Bundler::CompactIndexClient.debug { "skipping updating info for #{name} since it is missing from versions" }
+ return
+ end
+ if checksum == existing
+ Bundler::CompactIndexClient.debug { "skipping updating info for #{name} since the versions checksum matches the local checksum" }
+ return
+ end
+ Bundler::CompactIndexClient.debug { "updating info for #{name} since the versions checksum #{existing} != the local checksum #{checksum}" }
+ update(path, "info/#{name}")
+ end
+
+ def url(path)
+ path
+ end
+
+ def synchronize
+ @mutex.synchronize { yield }
+ end
+ end
+end
diff --git a/lib/bundler/compact_index_client/cache.rb b/lib/bundler/compact_index_client/cache.rb
new file mode 100644
index 00000000000000..f6105d3bb3ad7b
--- /dev/null
+++ b/lib/bundler/compact_index_client/cache.rb
@@ -0,0 +1,118 @@
+# frozen_string_literal: true
+
+module Bundler
+ class CompactIndexClient
+ class Cache
+ attr_reader :directory
+
+ def initialize(directory)
+ @directory = Pathname.new(directory).expand_path
+ info_roots.each do |dir|
+ SharedHelpers.filesystem_access(dir) do
+ FileUtils.mkdir_p(dir)
+ end
+ end
+ end
+
+ def names
+ lines(names_path)
+ end
+
+ def names_path
+ directory.join("names")
+ end
+
+ def versions
+ versions_by_name = Hash.new {|hash, key| hash[key] = [] }
+ info_checksums_by_name = {}
+
+ lines(versions_path).each do |line|
+ name, versions_string, info_checksum = line.split(" ", 3)
+ info_checksums_by_name[name] = info_checksum || ""
+ versions_string.split(",").each do |version|
+ if version.start_with?("-")
+ version = version[1..-1].split("-", 2).unshift(name)
+ versions_by_name[name].delete(version)
+ else
+ version = version.split("-", 2).unshift(name)
+ versions_by_name[name] << version
+ end
+ end
+ end
+
+ [versions_by_name, info_checksums_by_name]
+ end
+
+ def versions_path
+ directory.join("versions")
+ end
+
+ def checksums
+ checksums = {}
+
+ lines(versions_path).each do |line|
+ name, _, checksum = line.split(" ", 3)
+ checksums[name] = checksum
+ end
+
+ checksums
+ end
+
+ def dependencies(name)
+ lines(info_path(name)).map do |line|
+ parse_gem(line)
+ end
+ end
+
+ def info_path(name)
+ name = name.to_s
+ if name =~ /[^a-z0-9_-]/
+ name += "-#{SharedHelpers.digest(:MD5).hexdigest(name).downcase}"
+ info_roots.last.join(name)
+ else
+ info_roots.first.join(name)
+ end
+ end
+
+ def specific_dependency(name, version, platform)
+ pattern = [version, platform].compact.join("-")
+ return nil if pattern.empty?
+
+ gem_lines = info_path(name).read
+ gem_line = gem_lines[/^#{Regexp.escape(pattern)}\b.*/, 0]
+ gem_line ? parse_gem(gem_line) : nil
+ end
+
+ private
+
+ def lines(path)
+ return [] unless path.file?
+ lines = SharedHelpers.filesystem_access(path, :read, &:read).split("\n")
+ header = lines.index("---")
+ header ? lines[header + 1..-1] : lines
+ end
+
+ def parse_gem(string)
+ version_and_platform, rest = string.split(" ", 2)
+ version, platform = version_and_platform.split("-", 2)
+ dependencies, requirements = rest.split("|", 2).map {|s| s.split(",") } if rest
+ dependencies = dependencies ? dependencies.map {|d| parse_dependency(d) } : []
+ requirements = requirements ? requirements.map {|r| parse_dependency(r) } : []
+ [version, platform, dependencies, requirements]
+ end
+
+ def parse_dependency(string)
+ dependency = string.split(":")
+ dependency[-1] = dependency[-1].split("&") if dependency.size > 1
+ dependency
+ end
+
+ def info_roots
+ [
+ directory.join("info"),
+ directory.join("info-special-characters"),
+ ]
+ end
+ end
+ end
+end
diff --git a/lib/bundler/compact_index_client/updater.rb b/lib/bundler/compact_index_client/updater.rb
new file mode 100644
index 00000000000000..4d6eb80044650c
--- /dev/null
+++ b/lib/bundler/compact_index_client/updater.rb
@@ -0,0 +1,116 @@
+# frozen_string_literal: true
+
+require "bundler/vendored_fileutils"
+require "stringio"
+require "zlib"
+
+module Bundler
+ class CompactIndexClient
+ class Updater
+ class MisMatchedChecksumError < Error
+ def initialize(path, server_checksum, local_checksum)
+ @path = path
+ @server_checksum = server_checksum
+ @local_checksum = local_checksum
+ end
+
+ def message
+ "The checksum of /#{@path} does not match the checksum provided by the server! Something is wrong " \
+ "(local checksum is #{@local_checksum.inspect}, was expecting #{@server_checksum.inspect})."
+ end
+ end
+
+ def initialize(fetcher)
+ @fetcher = fetcher
+ require "tmpdir"
+ end
+
+ def update(local_path, remote_path, retrying = nil)
+ headers = {}
+
+ Dir.mktmpdir("bundler-compact-index-") do |local_temp_dir|
+ local_temp_path = Pathname.new(local_temp_dir).join(local_path.basename)
+
+ # first try to fetch any new bytes on the existing file
+ if retrying.nil? && local_path.file?
+ SharedHelpers.filesystem_access(local_temp_path) do
+ FileUtils.cp local_path, local_temp_path
+ end
+ headers["If-None-Match"] = etag_for(local_temp_path)
+ headers["Range"] =
+ if local_temp_path.size.nonzero?
+ # Subtract a byte to ensure the range won't be empty.
+ # Avoids 416 (Range Not Satisfiable) responses.
+ "bytes=#{local_temp_path.size - 1}-"
+ else
+ "bytes=#{local_temp_path.size}-"
+ end
+ else
+ # Fastly ignores Range when Accept-Encoding: gzip is set
+ headers["Accept-Encoding"] = "gzip"
+ end
+
+ response = @fetcher.call(remote_path, headers)
+ return nil if response.is_a?(Net::HTTPNotModified)
+
+ content = response.body
+ if response["Content-Encoding"] == "gzip"
+ content = Zlib::GzipReader.new(StringIO.new(content)).read
+ end
+
+ SharedHelpers.filesystem_access(local_temp_path) do
+ if response.is_a?(Net::HTTPPartialContent) && local_temp_path.size.nonzero?
+ local_temp_path.open("a") {|f| f << slice_body(content, 1..-1) }
+ else
+ local_temp_path.open("w") {|f| f << content }
+ end
+ end
+
+ response_etag = (response["ETag"] || "").gsub(%r{\AW/}, "")
+ if etag_for(local_temp_path) == response_etag
+ SharedHelpers.filesystem_access(local_path) do
+ FileUtils.mv(local_temp_path, local_path)
+ end
+ return nil
+ end
+
+ if retrying
+ raise MisMatchedChecksumError.new(remote_path, response_etag, etag_for(local_temp_path))
+ end
+
+ update(local_path, remote_path, :retrying)
+ end
+ rescue Errno::EACCES
+ raise Bundler::PermissionError,
+ "Bundler does not have write access to create a temp directory " \
+ "within #{Dir.tmpdir}. Bundler must have write access to your " \
+ "systems temp directory to function properly. "
+ rescue Zlib::GzipFile::Error
+ raise Bundler::HTTPError
+ end
+
+ def etag_for(path)
+ sum = checksum_for_file(path)
+ sum ? %("#{sum}") : nil
+ end
+
+ def slice_body(body, range)
+ if body.respond_to?(:byteslice)
+ body.byteslice(range)
+ else # pre-1.9.3
+ body.unpack("@#{range.first}a#{range.end + 1}").first
+ end
+ end
+
+ def checksum_for_file(path)
+ return nil unless path.file?
+ # This must use IO.read instead of Digest.file().hexdigest
+ # because we need to preserve \n line endings on windows when calculating
+ # the checksum
+ SharedHelpers.filesystem_access(path, :read) do
+ SharedHelpers.digest(:MD5).hexdigest(IO.read(path))
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/compatibility_guard.rb b/lib/bundler/compatibility_guard.rb
new file mode 100644
index 00000000000000..750a1db04fbb37
--- /dev/null
+++ b/lib/bundler/compatibility_guard.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: false
+
+require "rubygems"
+require "bundler/version"
+
+if Bundler::VERSION.split(".").first.to_i >= 2
+ if Gem::Version.new(Object::RUBY_VERSION.dup) < Gem::Version.new("2.3")
+ abort "Bundler 2 requires Ruby 2.3 or later. Either install bundler 1 or update to a supported Ruby version."
+ end
+
+ if Gem::Version.new(Gem::VERSION.dup) < Gem::Version.new("2.5")
+ abort "Bundler 2 requires RubyGems 2.5 or later. Either install bundler 1 or update to a supported RubyGems version."
+ end
+end
diff --git a/lib/bundler/constants.rb b/lib/bundler/constants.rb
new file mode 100644
index 00000000000000..2e4ebb37ee606d
--- /dev/null
+++ b/lib/bundler/constants.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+module Bundler
+ WINDOWS = RbConfig::CONFIG["host_os"] =~ /(msdos|mswin|djgpp|mingw)/
+ FREEBSD = RbConfig::CONFIG["host_os"] =~ /bsd/
+ NULL = WINDOWS ? "NUL" : "/dev/null"
+end
diff --git a/lib/bundler/current_ruby.rb b/lib/bundler/current_ruby.rb
new file mode 100644
index 00000000000000..d5efaad6c52baf
--- /dev/null
+++ b/lib/bundler/current_ruby.rb
@@ -0,0 +1,93 @@
+# frozen_string_literal: true
+
+module Bundler
+ # Returns current version of Ruby
+ #
+ # @return [CurrentRuby] Current version of Ruby
+ def self.current_ruby
+ @current_ruby ||= CurrentRuby.new
+ end
+
+ class CurrentRuby
+ KNOWN_MINOR_VERSIONS = %w[
+ 1.8
+ 1.9
+ 2.0
+ 2.1
+ 2.2
+ 2.3
+ 2.4
+ 2.5
+ 2.6
+ ].freeze
+
+ KNOWN_MAJOR_VERSIONS = KNOWN_MINOR_VERSIONS.map {|v| v.split(".", 2).first }.uniq.freeze
+
+ KNOWN_PLATFORMS = %w[
+ jruby
+ maglev
+ mingw
+ mri
+ mswin
+ mswin64
+ rbx
+ ruby
+ truffleruby
+ x64_mingw
+ ].freeze
+
+ def ruby?
+ !mswin? && (!defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby" ||
+ RUBY_ENGINE == "rbx" || RUBY_ENGINE == "maglev" || RUBY_ENGINE == "truffleruby")
+ end
+
+ def mri?
+ !mswin? && (!defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby")
+ end
+
+ def rbx?
+ ruby? && defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx"
+ end
+
+ def jruby?
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
+ end
+
+ def maglev?
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "maglev"
+ end
+
+ def truffleruby?
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "truffleruby"
+ end
+
+ def mswin?
+ Bundler::WINDOWS
+ end
+
+ def mswin64?
+ Bundler::WINDOWS && Bundler.local_platform != Gem::Platform::RUBY && Bundler.local_platform.os == "mswin64" && Bundler.local_platform.cpu == "x64"
+ end
+
+ def mingw?
+ Bundler::WINDOWS && Bundler.local_platform != Gem::Platform::RUBY && Bundler.local_platform.os == "mingw32" && Bundler.local_platform.cpu != "x64"
+ end
+
+ def x64_mingw?
+ Bundler::WINDOWS && Bundler.local_platform != Gem::Platform::RUBY && Bundler.local_platform.os == "mingw32" && Bundler.local_platform.cpu == "x64"
+ end
+
+ (KNOWN_MINOR_VERSIONS + KNOWN_MAJOR_VERSIONS).each do |version|
+ trimmed_version = version.tr(".", "")
+ define_method(:"on_#{trimmed_version}?") do
+ RUBY_VERSION.start_with?("#{version}.")
+ end
+
+ KNOWN_PLATFORMS.each do |platform|
+ define_method(:"#{platform}_#{trimmed_version}?") do
+ send(:"#{platform}?") && send(:"on_#{trimmed_version}?")
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb
new file mode 100644
index 00000000000000..c5e94c712301c7
--- /dev/null
+++ b/lib/bundler/definition.rb
@@ -0,0 +1,993 @@
+# frozen_string_literal: true
+
+require "bundler/lockfile_parser"
+require "set"
+
+module Bundler
+ class Definition
+ include GemHelpers
+
+ attr_reader(
+ :dependencies,
+ :locked_deps,
+ :locked_gems,
+ :platforms,
+ :requires,
+ :ruby_version,
+ :lockfile,
+ :gemfiles
+ )
+
+ # Given a gemfile and lockfile creates a Bundler definition
+ #
+ # @param gemfile [Pathname] Path to Gemfile
+ # @param lockfile [Pathname,nil] Path to Gemfile.lock
+ # @param unlock [Hash, Boolean, nil] Gems that have been requested
+ # to be updated or true if all gems should be updated
+ # @return [Bundler::Definition]
+ def self.build(gemfile, lockfile, unlock)
+ unlock ||= {}
+ gemfile = Pathname.new(gemfile).expand_path
+
+ raise GemfileNotFound, "#{gemfile} not found" unless gemfile.file?
+
+ Dsl.evaluate(gemfile, lockfile, unlock)
+ end
+
+ #
+ # How does the new system work?
+ #
+ # * Load information from Gemfile and Lockfile
+ # * Invalidate stale locked specs
+ # * All specs from stale source are stale
+ # * All specs that are reachable only through a stale
+ # dependency are stale.
+ # * If all fresh dependencies are satisfied by the locked
+ # specs, then we can try to resolve locally.
+ #
+ # @param lockfile [Pathname] Path to Gemfile.lock
+ # @param dependencies [Array(Bundler::Dependency)] array of dependencies from Gemfile
+ # @param sources [Bundler::SourceList]
+ # @param unlock [Hash, Boolean, nil] Gems that have been requested
+ # to be updated or true if all gems should be updated
+ # @param ruby_version [Bundler::RubyVersion, nil] Requested Ruby Version
+ # @param optional_groups [Array(String)] A list of optional groups
+ def initialize(lockfile, dependencies, sources, unlock, ruby_version = nil, optional_groups = [], gemfiles = [])
+ if [true, false].include?(unlock)
+ @unlocking_bundler = false
+ @unlocking = unlock
+ else
+ unlock = unlock.dup
+ @unlocking_bundler = unlock.delete(:bundler)
+ unlock.delete_if {|_k, v| Array(v).empty? }
+ @unlocking = !unlock.empty?
+ end
+
+ @dependencies = dependencies
+ @sources = sources
+ @unlock = unlock
+ @optional_groups = optional_groups
+ @remote = false
+ @specs = nil
+ @ruby_version = ruby_version
+ @gemfiles = gemfiles
+
+ @lockfile = lockfile
+ @lockfile_contents = String.new
+ @locked_bundler_version = nil
+ @locked_ruby_version = nil
+ @locked_specs_incomplete_for_platform = false
+
+ if lockfile && File.exist?(lockfile)
+ @lockfile_contents = Bundler.read_file(lockfile)
+ @locked_gems = LockfileParser.new(@lockfile_contents)
+ @locked_platforms = @locked_gems.platforms
+ @platforms = @locked_platforms.dup
+ @locked_bundler_version = @locked_gems.bundler_version
+ @locked_ruby_version = @locked_gems.ruby_version
+
+ if unlock != true
+ @locked_deps = @locked_gems.dependencies
+ @locked_specs = SpecSet.new(@locked_gems.specs)
+ @locked_sources = @locked_gems.sources
+ else
+ @unlock = {}
+ @locked_deps = {}
+ @locked_specs = SpecSet.new([])
+ @locked_sources = []
+ end
+ else
+ @unlock = {}
+ @platforms = []
+ @locked_gems = nil
+ @locked_deps = {}
+ @locked_specs = SpecSet.new([])
+ @locked_sources = []
+ @locked_platforms = []
+ end
+
+ @unlock[:gems] ||= []
+ @unlock[:sources] ||= []
+ @unlock[:ruby] ||= if @ruby_version && locked_ruby_version_object
+ @ruby_version.diff(locked_ruby_version_object)
+ end
+ @unlocking ||= @unlock[:ruby] ||= (!@locked_ruby_version ^ !@ruby_version)
+
+ add_current_platform unless Bundler.frozen_bundle?
+
+ converge_path_sources_to_gemspec_sources
+ @path_changes = converge_paths
+ @source_changes = converge_sources
+
+ unless @unlock[:lock_shared_dependencies]
+ eager_unlock = expand_dependencies(@unlock[:gems], true)
+ @unlock[:gems] = @locked_specs.for(eager_unlock, [], false, false, false).map(&:name)
+ end
+
+ @dependency_changes = converge_dependencies
+ @local_changes = converge_locals
+
+ @requires = compute_requires
+ end
+
+ def gem_version_promoter
+ @gem_version_promoter ||= begin
+ locked_specs =
+ if unlocking? && @locked_specs.empty? && !@lockfile_contents.empty?
+ # Definition uses an empty set of locked_specs to indicate all gems
+ # are unlocked, but GemVersionPromoter needs the locked_specs
+ # for conservative comparison.
+ Bundler::SpecSet.new(@locked_gems.specs)
+ else
+ @locked_specs
+ end
+ GemVersionPromoter.new(locked_specs, @unlock[:gems])
+ end
+ end
+
+ def resolve_with_cache!
+ raise "Specs already loaded" if @specs
+ sources.cached!
+ specs
+ end
+
+ def resolve_remotely!
+ raise "Specs already loaded" if @specs
+ @remote = true
+ sources.remote!
+ specs
+ end
+
+ # For given dependency list returns a SpecSet with Gemspec of all the required
+ # dependencies.
+ # 1. The method first resolves the dependencies specified in Gemfile
+ # 2. After that it tries and fetches gemspec of resolved dependencies
+ #
+ # @return [Bundler::SpecSet]
+ def specs
+ @specs ||= begin
+ begin
+ specs = resolve.materialize(Bundler.settings[:cache_all_platforms] ? dependencies : requested_dependencies)
+ rescue GemNotFound => e # Handle yanked gem
+ gem_name, gem_version = extract_gem_info(e)
+ locked_gem = @locked_specs[gem_name].last
+ raise if locked_gem.nil? || locked_gem.version.to_s != gem_version || !@remote
+ raise GemNotFound, "Your bundle is locked to #{locked_gem}, but that version could not " \
+ "be found in any of the sources listed in your Gemfile. If you haven't changed sources, " \
+ "that means the author of #{locked_gem} has removed it. You'll need to update your bundle " \
+ "to a version other than #{locked_gem} that hasn't been removed in order to install."
+ end
+ unless specs["bundler"].any?
+ bundler = sources.metadata_source.specs.search(Gem::Dependency.new("bundler", VERSION)).last
+ specs["bundler"] = bundler
+ end
+
+ specs
+ end
+ end
+
+ def new_specs
+ specs - @locked_specs
+ end
+
+ def removed_specs
+ @locked_specs - specs
+ end
+
+ def new_platform?
+ @new_platform
+ end
+
+ def missing_specs
+ missing = []
+ resolve.materialize(requested_dependencies, missing)
+ missing
+ end
+
+ def missing_specs?
+ missing = missing_specs
+ return false if missing.empty?
+ Bundler.ui.debug "The definition is missing #{missing.map(&:full_name)}"
+ true
+ rescue BundlerError => e
+ @index = nil
+ @resolve = nil
+ @specs = nil
+ @gem_version_promoter = nil
+
+ Bundler.ui.debug "The definition is missing dependencies, failed to resolve & materialize locally (#{e})"
+ true
+ end
+
+ def requested_specs
+ @requested_specs ||= begin
+ groups = requested_groups
+ groups.map!(&:to_sym)
+ specs_for(groups)
+ end
+ end
+
+ def current_dependencies
+ dependencies.select(&:should_include?)
+ end
+
+ def specs_for(groups)
+ deps = dependencies.select {|d| (d.groups & groups).any? }
+ deps.delete_if {|d| !d.should_include? }
+ specs.for(expand_dependencies(deps))
+ end
+
+ # Resolve all the dependencies specified in Gemfile. It ensures that
+ # dependencies that have been already resolved via locked file and are fresh
+ # are reused when resolving dependencies
+ #
+ # @return [SpecSet] resolved dependencies
+ def resolve
+ @resolve ||= begin
+ last_resolve = converge_locked_specs
+ resolve =
+ if Bundler.frozen_bundle?
+ Bundler.ui.debug "Frozen, using resolution from the lockfile"
+ last_resolve
+ elsif !unlocking? && nothing_changed?
+ Bundler.ui.debug("Found no changes, using resolution from the lockfile")
+ last_resolve
+ else
+ # Run a resolve against the locally available gems
+ Bundler.ui.debug("Found changes from the lockfile, re-resolving dependencies because #{change_reason}")
+ last_resolve.merge Resolver.resolve(expanded_dependencies, index, source_requirements, last_resolve, gem_version_promoter, additional_base_requirements_for_resolve, platforms)
+ end
+
+ # filter out gems that _can_ be installed on multiple platforms, but don't need
+ # to be
+ resolve.for(expand_dependencies(dependencies, true), [], false, false, false)
+ end
+ end
+
+ def index
+ @index ||= Index.build do |idx|
+ dependency_names = @dependencies.map(&:name)
+
+ sources.all_sources.each do |source|
+ source.dependency_names = dependency_names - pinned_spec_names(source)
+ idx.add_source source.specs
+ dependency_names.concat(source.unmet_deps).uniq!
+ end
+
+ double_check_for_index(idx, dependency_names)
+ end
+ end
+
+ # Suppose the gem Foo depends on the gem Bar. Foo exists in Source A. Bar has some versions that exist in both
+ # sources A and B. At this point, the API request will have found all the versions of Bar in source A,
+ # but will not have found any versions of Bar from source B, which is a problem if the requested version
+ # of Foo specifically depends on a version of Bar that is only found in source B. This ensures that for
+ # each spec we found, we add all possible versions from all sources to the index.
+ def double_check_for_index(idx, dependency_names)
+ pinned_names = pinned_spec_names
+ loop do
+ idxcount = idx.size
+
+ names = :names # do this so we only have to traverse to get dependency_names from the index once
+ unmet_dependency_names = lambda do
+ return names unless names == :names
+ new_names = sources.all_sources.map(&:dependency_names_to_double_check)
+ return names = nil if new_names.compact!
+ names = new_names.flatten(1).concat(dependency_names)
+ names.uniq!
+ names -= pinned_names
+ names
+ end
+
+ sources.all_sources.each do |source|
+ source.double_check_for(unmet_dependency_names)
+ end
+
+ break if idxcount == idx.size
+ end
+ end
+ private :double_check_for_index
+
+ def has_rubygems_remotes?
+ sources.rubygems_sources.any? {|s| s.remotes.any? }
+ end
+
+ def has_local_dependencies?
+ !sources.path_sources.empty? || !sources.git_sources.empty?
+ end
+
+ def spec_git_paths
+ sources.git_sources.map {|s| s.path.to_s }
+ end
+
+ def groups
+ dependencies.map(&:groups).flatten.uniq
+ end
+
+ def lock(file, preserve_unknown_sections = false)
+ contents = to_lock
+
+ # Convert to \r\n if the existing lock has them
+ # i.e., Windows with `git config core.autocrlf=true`
+ contents.gsub!(/\n/, "\r\n") if @lockfile_contents.match("\r\n")
+
+ if @locked_bundler_version && Bundler.feature_flag.lockfile_upgrade_warning?
+ locked_major = @locked_bundler_version.segments.first
+ current_major = Gem::Version.create(Bundler::VERSION).segments.first
+
+ if updating_major = locked_major < current_major
+ Bundler.ui.warn "Warning: the lockfile is being updated to Bundler #{current_major}, " \
+ "after which you will be unable to return to Bundler #{@locked_bundler_version.segments.first}."
+ end
+ end
+
+ preserve_unknown_sections ||= !updating_major && (Bundler.frozen_bundle? || !(unlocking? || @unlocking_bundler))
+
+ return if file && File.exist?(file) && lockfiles_equal?(@lockfile_contents, contents, preserve_unknown_sections)
+
+ if Bundler.frozen_bundle?
+ Bundler.ui.error "Cannot write a changed lockfile while frozen."
+ return
+ end
+
+ SharedHelpers.filesystem_access(file) do |p|
+ File.open(p, "wb") {|f| f.puts(contents) }
+ end
+ end
+
+ def locked_bundler_version
+ if @locked_bundler_version && @locked_bundler_version < Gem::Version.new(Bundler::VERSION)
+ new_version = Bundler::VERSION
+ end
+
+ new_version || @locked_bundler_version || Bundler::VERSION
+ end
+
+ def locked_ruby_version
+ return unless ruby_version
+ if @unlock[:ruby] || !@locked_ruby_version
+ Bundler::RubyVersion.system
+ else
+ @locked_ruby_version
+ end
+ end
+
+ def locked_ruby_version_object
+ return unless @locked_ruby_version
+ @locked_ruby_version_object ||= begin
+ unless version = RubyVersion.from_string(@locked_ruby_version)
+ raise LockfileError, "The Ruby version #{@locked_ruby_version} from " \
+ "#{@lockfile} could not be parsed. " \
+ "Try running bundle update --ruby to resolve this."
+ end
+ version
+ end
+ end
+
+ def to_lock
+ require "bundler/lockfile_generator"
+ LockfileGenerator.generate(self)
+ end
+
+ def ensure_equivalent_gemfile_and_lockfile(explicit_flag = false)
+ msg = String.new
+ msg << "You are trying to install in deployment mode after changing\n" \
+ "your Gemfile. Run `bundle install` elsewhere and add the\n" \
+ "updated #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)} to version control."
+
+ unless explicit_flag
+ suggested_command = if Bundler.settings.locations("frozen")[:global]
+ "bundle config --delete frozen"
+ elsif Bundler.settings.locations("deployment").keys.&([:global, :local]).any?
+ "bundle config --delete deployment"
+ else
+ "bundle install --no-deployment"
+ end
+ msg << "\n\nIf this is a development machine, remove the #{Bundler.default_gemfile} " \
+ "freeze \nby running `#{suggested_command}`."
+ end
+
+ added = []
+ deleted = []
+ changed = []
+
+ new_platforms = @platforms - @locked_platforms
+ deleted_platforms = @locked_platforms - @platforms
+ added.concat new_platforms.map {|p| "* platform: #{p}" }
+ deleted.concat deleted_platforms.map {|p| "* platform: #{p}" }
+
+ gemfile_sources = sources.lock_sources
+
+ new_sources = gemfile_sources - @locked_sources
+ deleted_sources = @locked_sources - gemfile_sources
+
+ new_deps = @dependencies - @locked_deps.values
+ deleted_deps = @locked_deps.values - @dependencies
+
+ # Check if it is possible that the source is only changed thing
+ if (new_deps.empty? && deleted_deps.empty?) && (!new_sources.empty? && !deleted_sources.empty?)
+ new_sources.reject! {|source| (source.path? && source.path.exist?) || equivalent_rubygems_remotes?(source) }
+ deleted_sources.reject! {|source| (source.path? && source.path.exist?) || equivalent_rubygems_remotes?(source) }
+ end
+
+ if @locked_sources != gemfile_sources
+ if new_sources.any?
+ added.concat new_sources.map {|source| "* source: #{source}" }
+ end
+
+ if deleted_sources.any?
+ deleted.concat deleted_sources.map {|source| "* source: #{source}" }
+ end
+ end
+
+ added.concat new_deps.map {|d| "* #{pretty_dep(d)}" } if new_deps.any?
+ if deleted_deps.any?
+ deleted.concat deleted_deps.map {|d| "* #{pretty_dep(d)}" }
+ end
+
+ both_sources = Hash.new {|h, k| h[k] = [] }
+ @dependencies.each {|d| both_sources[d.name][0] = d }
+ @locked_deps.each {|name, d| both_sources[name][1] = d.source }
+
+ both_sources.each do |name, (dep, lock_source)|
+ next unless (dep.nil? && !lock_source.nil?) || (!dep.nil? && !lock_source.nil? && !lock_source.can_lock?(dep))
+ gemfile_source_name = (dep && dep.source) || "no specified source"
+ lockfile_source_name = lock_source || "no specified source"
+ changed << "* #{name} from `#{gemfile_source_name}` to `#{lockfile_source_name}`"
+ end
+
+ reason = change_reason
+ msg << "\n\n#{reason.split(", ").map(&:capitalize).join("\n")}" unless reason.strip.empty?
+ msg << "\n\nYou have added to the Gemfile:\n" << added.join("\n") if added.any?
+ msg << "\n\nYou have deleted from the Gemfile:\n" << deleted.join("\n") if deleted.any?
+ msg << "\n\nYou have changed in the Gemfile:\n" << changed.join("\n") if changed.any?
+ msg << "\n"
+
+ raise ProductionError, msg if added.any? || deleted.any? || changed.any? || !nothing_changed?
+ end
+
+ def validate_runtime!
+ validate_ruby!
+ validate_platforms!
+ end
+
+ def validate_ruby!
+ return unless ruby_version
+
+ if diff = ruby_version.diff(Bundler::RubyVersion.system)
+ problem, expected, actual = diff
+
+ msg = case problem
+ when :engine
+ "Your Ruby engine is #{actual}, but your Gemfile specified #{expected}"
+ when :version
+ "Your Ruby version is #{actual}, but your Gemfile specified #{expected}"
+ when :engine_version
+ "Your #{Bundler::RubyVersion.system.engine} version is #{actual}, but your Gemfile specified #{ruby_version.engine} #{expected}"
+ when :patchlevel
+ if !expected.is_a?(String)
+ "The Ruby patchlevel in your Gemfile must be a string"
+ else
+ "Your Ruby patchlevel is #{actual}, but your Gemfile specified #{expected}"
+ end
+ end
+
+ raise RubyVersionMismatch, msg
+ end
+ end
+
+ def validate_platforms!
+ return if @platforms.any? do |bundle_platform|
+ Bundler.rubygems.platforms.any? do |local_platform|
+ MatchPlatform.platforms_match?(bundle_platform, local_platform)
+ end
+ end
+
+ raise ProductionError, "Your bundle only supports platforms #{@platforms.map(&:to_s)} " \
+ "but your local platforms are #{Bundler.rubygems.platforms.map(&:to_s)}, and " \
+ "there's no compatible match between those two lists."
+ end
+
+ def add_platform(platform)
+ @new_platform ||= !@platforms.include?(platform)
+ @platforms |= [platform]
+ end
+
+ def remove_platform(platform)
+ return if @platforms.delete(Gem::Platform.new(platform))
+ raise InvalidOption, "Unable to remove the platform `#{platform}` since the only platforms are #{@platforms.join ", "}"
+ end
+
+ def add_current_platform
+ current_platform = Bundler.local_platform
+ add_platform(current_platform) if Bundler.feature_flag.specific_platform?
+ add_platform(generic(current_platform))
+ end
+
+ def find_resolved_spec(current_spec)
+ specs.find_by_name_and_platform(current_spec.name, current_spec.platform)
+ end
+
+ def find_indexed_specs(current_spec)
+ index[current_spec.name].select {|spec| spec.match_platform(current_spec.platform) }.sort_by(&:version)
+ end
+
+ attr_reader :sources
+ private :sources
+
+ def nothing_changed?
+ !@source_changes && !@dependency_changes && !@new_platform && !@path_changes && !@local_changes && !@locked_specs_incomplete_for_platform
+ end
+
+ def unlocking?
+ @unlocking
+ end
+
+ private
+
+ def change_reason
+ if unlocking?
+ unlock_reason = @unlock.reject {|_k, v| Array(v).empty? }.map do |k, v|
+ if v == true
+ k.to_s
+ else
+ v = Array(v)
+ "#{k}: (#{v.join(", ")})"
+ end
+ end.join(", ")
+ return "bundler is unlocking #{unlock_reason}"
+ end
+ [
+ [@source_changes, "the list of sources changed"],
+ [@dependency_changes, "the dependencies in your gemfile changed"],
+ [@new_platform, "you added a new platform to your gemfile"],
+ [@path_changes, "the gemspecs for path gems changed"],
+ [@local_changes, "the gemspecs for git local gems changed"],
+ [@locked_specs_incomplete_for_platform, "the lockfile does not have all gems needed for the current platform"],
+ ].select(&:first).map(&:last).join(", ")
+ end
+
+ def pretty_dep(dep, source = false)
+ SharedHelpers.pretty_dependency(dep, source)
+ end
+
+ # Check if the specs of the given source changed
+ # according to the locked source.
+ def specs_changed?(source)
+ locked = @locked_sources.find {|s| s == source }
+
+ !locked || dependencies_for_source_changed?(source, locked) || specs_for_source_changed?(source)
+ end
+
+ def dependencies_for_source_changed?(source, locked_source = source)
+ deps_for_source = @dependencies.select {|s| s.source == source }
+ locked_deps_for_source = @locked_deps.values.select {|dep| dep.source == locked_source }
+
+ Set.new(deps_for_source) != Set.new(locked_deps_for_source)
+ end
+
+ def specs_for_source_changed?(source)
+ locked_index = Index.new
+ locked_index.use(@locked_specs.select {|s| source.can_lock?(s) })
+
+ # order here matters, since Index#== is checking source.specs.include?(locked_index)
+ locked_index != source.specs
+ rescue PathError, GitError => e
+ Bundler.ui.debug "Assuming that #{source} has not changed since fetching its specs errored (#{e})"
+ false
+ end
+
+ # Get all locals and override their matching sources.
+ # Return true if any of the locals changed (for example,
+ # they point to a new revision) or depend on new specs.
+ def converge_locals
+ locals = []
+
+ Bundler.settings.local_overrides.map do |k, v|
+ spec = @dependencies.find {|s| s.name == k }
+ source = spec && spec.source
+ if source && source.respond_to?(:local_override!)
+ source.unlock! if @unlock[:gems].include?(spec.name)
+ locals << [source, source.local_override!(v)]
+ end
+ end
+
+ sources_with_changes = locals.select do |source, changed|
+ changed || specs_changed?(source)
+ end.map(&:first)
+ !sources_with_changes.each {|source| @unlock[:sources] << source.name }.empty?
+ end
+
+ def converge_paths
+ sources.path_sources.any? do |source|
+ specs_changed?(source)
+ end
+ end
+
+ def converge_path_source_to_gemspec_source(source)
+ return source unless source.instance_of?(Source::Path)
+ gemspec_source = sources.path_sources.find {|s| s.is_a?(Source::Gemspec) && s.as_path_source == source }
+ gemspec_source || source
+ end
+
+ def converge_path_sources_to_gemspec_sources
+ @locked_sources.map! do |source|
+ converge_path_source_to_gemspec_source(source)
+ end
+ @locked_specs.each do |spec|
+ spec.source &&= converge_path_source_to_gemspec_source(spec.source)
+ end
+ @locked_deps.each do |_, dep|
+ dep.source &&= converge_path_source_to_gemspec_source(dep.source)
+ end
+ end
+
+ def converge_rubygems_sources
+ return false if Bundler.feature_flag.lockfile_uses_separate_rubygems_sources?
+
+ changes = false
+
+ # Get the RubyGems sources from the Gemfile.lock
+ locked_gem_sources = @locked_sources.select {|s| s.is_a?(Source::Rubygems) }
+ # Get the RubyGems remotes from the Gemfile
+ actual_remotes = sources.rubygems_remotes
+
+ # If there is a RubyGems source in both
+ if !locked_gem_sources.empty? && !actual_remotes.empty?
+ locked_gem_sources.each do |locked_gem|
+ # Merge the remotes from the Gemfile into the Gemfile.lock
+ changes |= locked_gem.replace_remotes(actual_remotes, Bundler.settings[:allow_deployment_source_credential_changes])
+ end
+ end
+
+ changes
+ end
+
+ def converge_sources
+ changes = false
+
+ changes |= converge_rubygems_sources
+
+ # Replace the sources from the Gemfile with the sources from the Gemfile.lock,
+ # if they exist in the Gemfile.lock and are `==`. If you can't find an equivalent
+ # source in the Gemfile.lock, use the one from the Gemfile.
+ changes |= sources.replace_sources!(@locked_sources)
+
+ sources.all_sources.each do |source|
+ # If the source is unlockable and the current command allows an unlock of
+ # the source (for example, you are doing a `bundle update ` of a git-pinned
+ # gem), unlock it. For git sources, this means to unlock the revision, which
+ # will cause the `ref` used to be the most recent for the branch (or master) if
+ # an explicit `ref` is not used.
+ if source.respond_to?(:unlock!) && @unlock[:sources].include?(source.name)
+ source.unlock!
+ changes = true
+ end
+ end
+
+ changes
+ end
+
+ def converge_dependencies
+ frozen = Bundler.frozen_bundle?
+ (@dependencies + @locked_deps.values).each do |dep|
+ locked_source = @locked_deps[dep.name]
+ # This is to make sure that if bundler is installing in deployment mode and
+ # after locked_source and sources don't match, we still use locked_source.
+ if frozen && !locked_source.nil? &&
+ locked_source.respond_to?(:source) && locked_source.source.instance_of?(Source::Path) && locked_source.source.path.exist?
+ dep.source = locked_source.source
+ elsif dep.source
+ dep.source = sources.get(dep.source)
+ end
+ if dep.source.is_a?(Source::Gemspec)
+ dep.platforms.concat(@platforms.map {|p| Dependency::REVERSE_PLATFORM_MAP[p] }.flatten(1)).uniq!
+ end
+ end
+
+ changes = false
+ # We want to know if all match, but don't want to check all entries
+ # This means we need to return false if any dependency doesn't match
+ # the lock or doesn't exist in the lock.
+ @dependencies.each do |dependency|
+ unless locked_dep = @locked_deps[dependency.name]
+ changes = true
+ next
+ end
+
+ # Gem::Dependency#== matches Gem::Dependency#type. As the lockfile
+ # doesn't carry a notion of the dependency type, if you use
+ # add_development_dependency in a gemspec that's loaded with the gemspec
+ # directive, the lockfile dependencies and resolved dependencies end up
+ # with a mismatch on #type. Work around that by setting the type on the
+ # dep from the lockfile.
+ locked_dep.instance_variable_set(:@type, dependency.type)
+
+ # We already know the name matches from the hash lookup
+ # so we only need to check the requirement now
+ changes ||= dependency.requirement != locked_dep.requirement
+ end
+
+ changes
+ end
+
+ # Remove elements from the locked specs that are expired. This will most
+ # commonly happen if the Gemfile has changed since the lockfile was last
+ # generated
+ def converge_locked_specs
+ deps = []
+
+ # Build a list of dependencies that are the same in the Gemfile
+ # and Gemfile.lock. If the Gemfile modified a dependency, but
+ # the gem in the Gemfile.lock still satisfies it, this is fine
+ # too.
+ @dependencies.each do |dep|
+ locked_dep = @locked_deps[dep.name]
+
+ # If the locked_dep doesn't match the dependency we're looking for then we ignore the locked_dep
+ locked_dep = nil unless locked_dep == dep
+
+ if in_locked_deps?(dep, locked_dep) || satisfies_locked_spec?(dep)
+ deps << dep
+ elsif dep.source.is_a?(Source::Path) && dep.current_platform? && (!locked_dep || dep.source != locked_dep.source)
+ @locked_specs.each do |s|
+ @unlock[:gems] << s.name if s.source == dep.source
+ end
+
+ dep.source.unlock! if dep.source.respond_to?(:unlock!)
+ dep.source.specs.each {|s| @unlock[:gems] << s.name }
+ end
+ end
+
+ unlock_source_unlocks_spec = Bundler.feature_flag.unlock_source_unlocks_spec?
+
+ converged = []
+ @locked_specs.each do |s|
+ # Replace the locked dependency's source with the equivalent source from the Gemfile
+ dep = @dependencies.find {|d| s.satisfies?(d) }
+ s.source = (dep && dep.source) || sources.get(s.source)
+
+ # Don't add a spec to the list if its source is expired. For example,
+ # if you change a Git gem to RubyGems.
+ next if s.source.nil?
+ next if @unlock[:sources].include?(s.source.name)
+
+ # XXX This is a backwards-compatibility fix to preserve the ability to
+ # unlock a single gem by passing its name via `--source`. See issue #3759
+ # TODO: delete in Bundler 2
+ next if unlock_source_unlocks_spec && @unlock[:sources].include?(s.name)
+
+ # If the spec is from a path source and it doesn't exist anymore
+ # then we unlock it.
+
+ # Path sources have special logic
+ if s.source.instance_of?(Source::Path) || s.source.instance_of?(Source::Gemspec)
+ other_sources_specs = begin
+ s.source.specs
+ rescue PathError, GitError
+ # if we won't need the source (according to the lockfile),
+ # don't error if the path/git source isn't available
+ next if @locked_specs.
+ for(requested_dependencies, [], false, true, false).
+ none? {|locked_spec| locked_spec.source == s.source }
+
+ raise
+ end
+
+ other = other_sources_specs[s].first
+
+ # If the spec is no longer in the path source, unlock it. This
+ # commonly happens if the version changed in the gemspec
+ next unless other
+
+ deps2 = other.dependencies.select {|d| d.type != :development }
+ runtime_dependencies = s.dependencies.select {|d| d.type != :development }
+ # If the dependencies of the path source have changed, unlock it
+ next unless runtime_dependencies.sort == deps2.sort
+ end
+
+ converged << s
+ end
+
+ resolve = SpecSet.new(converged)
+ expanded_deps = expand_dependencies(deps, true)
+ @locked_specs_incomplete_for_platform = !resolve.for(expanded_deps, @unlock[:gems], true, true)
+ resolve = resolve.for(expanded_deps, @unlock[:gems], false, false, false)
+ diff = nil
+
+ # Now, we unlock any sources that do not have anymore gems pinned to it
+ sources.all_sources.each do |source|
+ next unless source.respond_to?(:unlock!)
+
+ unless resolve.any? {|s| s.source == source }
+ diff ||= @locked_specs.to_a - resolve.to_a
+ source.unlock! if diff.any? {|s| s.source == source }
+ end
+ end
+
+ resolve
+ end
+
+ def in_locked_deps?(dep, locked_dep)
+ # Because the lockfile can't link a dep to a specific remote, we need to
+ # treat sources as equivalent anytime the locked dep has all the remotes
+ # that the Gemfile dep does.
+ locked_dep && locked_dep.source && dep.source && locked_dep.source.include?(dep.source)
+ end
+
+ def satisfies_locked_spec?(dep)
+ @locked_specs[dep].any? {|s| s.satisfies?(dep) && (!dep.source || s.source.include?(dep.source)) }
+ end
+
+ # This list of dependencies is only used in #resolve, so it's OK to add
+ # the metadata dependencies here
+ def expanded_dependencies
+ @expanded_dependencies ||= begin
+ expand_dependencies(dependencies + metadata_dependencies, @remote)
+ end
+ end
+
+ def metadata_dependencies
+ @metadata_dependencies ||= begin
+ ruby_versions = concat_ruby_version_requirements(@ruby_version)
+ if ruby_versions.empty? || !@ruby_version.exact?
+ concat_ruby_version_requirements(RubyVersion.system)
+ concat_ruby_version_requirements(locked_ruby_version_object) unless @unlock[:ruby]
+ end
+ [
+ Dependency.new("ruby\0", ruby_versions),
+ Dependency.new("rubygems\0", Gem::VERSION),
+ ]
+ end
+ end
+
+ def concat_ruby_version_requirements(ruby_version, ruby_versions = [])
+ return ruby_versions unless ruby_version
+ if ruby_version.patchlevel
+ ruby_versions << ruby_version.to_gem_version_with_patchlevel
+ else
+ ruby_versions.concat(ruby_version.versions.map do |version|
+ requirement = Gem::Requirement.new(version)
+ if requirement.exact?
+ "~> #{version}.0"
+ else
+ requirement
+ end
+ end)
+ end
+ end
+
+ def expand_dependencies(dependencies, remote = false)
+ sorted_platforms = Resolver.sort_platforms(@platforms)
+ deps = []
+ dependencies.each do |dep|
+ dep = Dependency.new(dep, ">= 0") unless dep.respond_to?(:name)
+ next if !remote && !dep.current_platform?
+ platforms = dep.gem_platforms(sorted_platforms)
+ if platforms.empty? && !Bundler.settings[:disable_platform_warnings]
+ mapped_platforms = dep.platforms.map {|p| Dependency::PLATFORM_MAP[p] }
+ Bundler.ui.warn \
+ "The dependency #{dep} will be unused by any of the platforms Bundler is installing for. " \
+ "Bundler is installing for #{@platforms.join ", "} but the dependency " \
+ "is only for #{mapped_platforms.join ", "}. " \
+ "To add those platforms to the bundle, " \
+ "run `bundle lock --add-platform #{mapped_platforms.join " "}`."
+ end
+ platforms.each do |p|
+ deps << DepProxy.new(dep, p) if remote || p == generic_local_platform
+ end
+ end
+ deps
+ end
+
+ def requested_dependencies
+ groups = requested_groups
+ groups.map!(&:to_sym)
+ dependencies.reject {|d| !d.should_include? || (d.groups & groups).empty? }
+ end
+
+ def source_requirements
+ # Load all specs from remote sources
+ index
+
+ # Record the specs available in each gem's source, so that those
+ # specs will be available later when the resolver knows where to
+ # look for that gemspec (or its dependencies)
+ default = sources.default_source
+ source_requirements = { :default => default }
+ default = nil unless Bundler.feature_flag.lockfile_uses_separate_rubygems_sources?
+ dependencies.each do |dep|
+ next unless source = dep.source || default
+ source_requirements[dep.name] = source
+ end
+ metadata_dependencies.each do |dep|
+ source_requirements[dep.name] = sources.metadata_source
+ end
+ source_requirements["bundler"] = sources.metadata_source # needs to come last to override
+ source_requirements
+ end
+
+ def pinned_spec_names(skip = nil)
+ pinned_names = []
+ default = Bundler.feature_flag.lockfile_uses_separate_rubygems_sources? && sources.default_source
+ @dependencies.each do |dep|
+ next unless dep_source = dep.source || default
+ next if dep_source == skip
+ pinned_names << dep.name
+ end
+ pinned_names
+ end
+
+ def requested_groups
+ groups - Bundler.settings[:without] - @optional_groups + Bundler.settings[:with]
+ end
+
+ def lockfiles_equal?(current, proposed, preserve_unknown_sections)
+ if preserve_unknown_sections
+ sections_to_ignore = LockfileParser.sections_to_ignore(@locked_bundler_version)
+ sections_to_ignore += LockfileParser.unknown_sections_in_lockfile(current)
+ sections_to_ignore += LockfileParser::ENVIRONMENT_VERSION_SECTIONS
+ pattern = /#{Regexp.union(sections_to_ignore)}\n(\s{2,}.*\n)+/
+ whitespace_cleanup = /\n{2,}/
+ current = current.gsub(pattern, "\n").gsub(whitespace_cleanup, "\n\n").strip
+ proposed = proposed.gsub(pattern, "\n").gsub(whitespace_cleanup, "\n\n").strip
+ end
+ current == proposed
+ end
+
+ def extract_gem_info(error)
+ # This method will extract the error message like "Could not find foo-1.2.3 in any of the sources"
+ # to an array. The first element will be the gem name (e.g. foo), the second will be the version number.
+ error.message.scan(/Could not find (\w+)-(\d+(?:\.\d+)+)/).flatten
+ end
+
+ def compute_requires
+ dependencies.reduce({}) do |requires, dep|
+ next requires unless dep.should_include?
+ requires[dep.name] = Array(dep.autorequire || dep.name).map do |file|
+ # Allow `require: true` as an alias for `require: `
+ file == true ? dep.name : file
+ end
+ requires
+ end
+ end
+
+ def additional_base_requirements_for_resolve
+ return [] unless @locked_gems && Bundler.feature_flag.only_update_to_newer_versions?
+ dependencies_by_name = dependencies.inject({}) {|memo, dep| memo.update(dep.name => dep) }
+ @locked_gems.specs.reduce({}) do |requirements, locked_spec|
+ name = locked_spec.name
+ next requirements if @locked_gems.dependencies[name] != dependencies_by_name[name]
+ dep = Gem::Dependency.new(name, ">= #{locked_spec.version}")
+ requirements[name] = DepProxy.new(dep, locked_spec.platform)
+ requirements
+ end.values
+ end
+
+ def equivalent_rubygems_remotes?(source)
+ return false unless source.is_a?(Source::Rubygems)
+
+ Bundler.settings[:allow_deployment_source_credential_changes] && source.equivalent_remotes?(sources.rubygems_remotes)
+ end
+ end
+end
diff --git a/lib/bundler/dep_proxy.rb b/lib/bundler/dep_proxy.rb
new file mode 100644
index 00000000000000..6c32179ac188af
--- /dev/null
+++ b/lib/bundler/dep_proxy.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+module Bundler
+ class DepProxy
+ attr_reader :__platform, :dep
+
+ def initialize(dep, platform)
+ @dep = dep
+ @__platform = platform
+ end
+
+ def hash
+ @hash ||= [dep, __platform].hash
+ end
+
+ def ==(other)
+ return false if other.class != self.class
+ dep == other.dep && __platform == other.__platform
+ end
+
+ alias_method :eql?, :==
+
+ def type
+ @dep.type
+ end
+
+ def name
+ @dep.name
+ end
+
+ def requirement
+ @dep.requirement
+ end
+
+ def to_s
+ s = name.dup
+ s << " (#{requirement})" unless requirement == Gem::Requirement.default
+ s << " #{__platform}" unless __platform == Gem::Platform::RUBY
+ s
+ end
+
+ private
+
+ def method_missing(*args, &blk)
+ @dep.send(*args, &blk)
+ end
+ end
+end
diff --git a/lib/bundler/dependency.rb b/lib/bundler/dependency.rb
new file mode 100644
index 00000000000000..8840ad6a9ca691
--- /dev/null
+++ b/lib/bundler/dependency.rb
@@ -0,0 +1,139 @@
+# frozen_string_literal: true
+
+require "rubygems/dependency"
+require "bundler/shared_helpers"
+require "bundler/rubygems_ext"
+
+module Bundler
+ class Dependency < Gem::Dependency
+ attr_reader :autorequire
+ attr_reader :groups, :platforms, :gemfile
+
+ PLATFORM_MAP = {
+ :ruby => Gem::Platform::RUBY,
+ :ruby_18 => Gem::Platform::RUBY,
+ :ruby_19 => Gem::Platform::RUBY,
+ :ruby_20 => Gem::Platform::RUBY,
+ :ruby_21 => Gem::Platform::RUBY,
+ :ruby_22 => Gem::Platform::RUBY,
+ :ruby_23 => Gem::Platform::RUBY,
+ :ruby_24 => Gem::Platform::RUBY,
+ :ruby_25 => Gem::Platform::RUBY,
+ :mri => Gem::Platform::RUBY,
+ :mri_18 => Gem::Platform::RUBY,
+ :mri_19 => Gem::Platform::RUBY,
+ :mri_20 => Gem::Platform::RUBY,
+ :mri_21 => Gem::Platform::RUBY,
+ :mri_22 => Gem::Platform::RUBY,
+ :mri_23 => Gem::Platform::RUBY,
+ :mri_24 => Gem::Platform::RUBY,
+ :mri_25 => Gem::Platform::RUBY,
+ :rbx => Gem::Platform::RUBY,
+ :truffleruby => Gem::Platform::RUBY,
+ :jruby => Gem::Platform::JAVA,
+ :jruby_18 => Gem::Platform::JAVA,
+ :jruby_19 => Gem::Platform::JAVA,
+ :mswin => Gem::Platform::MSWIN,
+ :mswin_18 => Gem::Platform::MSWIN,
+ :mswin_19 => Gem::Platform::MSWIN,
+ :mswin_20 => Gem::Platform::MSWIN,
+ :mswin_21 => Gem::Platform::MSWIN,
+ :mswin_22 => Gem::Platform::MSWIN,
+ :mswin_23 => Gem::Platform::MSWIN,
+ :mswin_24 => Gem::Platform::MSWIN,
+ :mswin_25 => Gem::Platform::MSWIN,
+ :mswin64 => Gem::Platform::MSWIN64,
+ :mswin64_19 => Gem::Platform::MSWIN64,
+ :mswin64_20 => Gem::Platform::MSWIN64,
+ :mswin64_21 => Gem::Platform::MSWIN64,
+ :mswin64_22 => Gem::Platform::MSWIN64,
+ :mswin64_23 => Gem::Platform::MSWIN64,
+ :mswin64_24 => Gem::Platform::MSWIN64,
+ :mswin64_25 => Gem::Platform::MSWIN64,
+ :mingw => Gem::Platform::MINGW,
+ :mingw_18 => Gem::Platform::MINGW,
+ :mingw_19 => Gem::Platform::MINGW,
+ :mingw_20 => Gem::Platform::MINGW,
+ :mingw_21 => Gem::Platform::MINGW,
+ :mingw_22 => Gem::Platform::MINGW,
+ :mingw_23 => Gem::Platform::MINGW,
+ :mingw_24 => Gem::Platform::MINGW,
+ :mingw_25 => Gem::Platform::MINGW,
+ :x64_mingw => Gem::Platform::X64_MINGW,
+ :x64_mingw_20 => Gem::Platform::X64_MINGW,
+ :x64_mingw_21 => Gem::Platform::X64_MINGW,
+ :x64_mingw_22 => Gem::Platform::X64_MINGW,
+ :x64_mingw_23 => Gem::Platform::X64_MINGW,
+ :x64_mingw_24 => Gem::Platform::X64_MINGW,
+ :x64_mingw_25 => Gem::Platform::X64_MINGW,
+ }.freeze
+
+ REVERSE_PLATFORM_MAP = {}.tap do |reverse_platform_map|
+ PLATFORM_MAP.each do |key, value|
+ reverse_platform_map[value] ||= []
+ reverse_platform_map[value] << key
+ end
+
+ reverse_platform_map.each {|_, platforms| platforms.freeze }
+ end.freeze
+
+ def initialize(name, version, options = {}, &blk)
+ type = options["type"] || :runtime
+ super(name, version, type)
+
+ @autorequire = nil
+ @groups = Array(options["group"] || :default).map(&:to_sym)
+ @source = options["source"]
+ @platforms = Array(options["platforms"])
+ @env = options["env"]
+ @should_include = options.fetch("should_include", true)
+ @gemfile = options["gemfile"]
+
+ @autorequire = Array(options["require"] || []) if options.key?("require")
+ end
+
+ # Returns the platforms this dependency is valid for, in the same order as
+ # passed in the `valid_platforms` parameter
+ def gem_platforms(valid_platforms)
+ return valid_platforms if @platforms.empty?
+
+ @gem_platforms ||= @platforms.map {|pl| PLATFORM_MAP[pl] }.compact.uniq
+
+ valid_platforms & @gem_platforms
+ end
+
+ def should_include?
+ @should_include && current_env? && current_platform?
+ end
+
+ def current_env?
+ return true unless @env
+ if @env.is_a?(Hash)
+ @env.all? do |key, val|
+ ENV[key.to_s] && (val.is_a?(String) ? ENV[key.to_s] == val : ENV[key.to_s] =~ val)
+ end
+ else
+ ENV[@env.to_s]
+ end
+ end
+
+ def current_platform?
+ return true if @platforms.empty?
+ @platforms.any? do |p|
+ Bundler.current_ruby.send("#{p}?")
+ end
+ end
+
+ def to_lock
+ out = super
+ out << "!" if source
+ out << "\n"
+ end
+
+ def specific?
+ super
+ rescue NoMethodError
+ requirement != ">= 0"
+ end
+ end
+end
diff --git a/lib/bundler/deployment.rb b/lib/bundler/deployment.rb
new file mode 100644
index 00000000000000..4c8f48d4056246
--- /dev/null
+++ b/lib/bundler/deployment.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+require "bundler/shared_helpers"
+Bundler::SharedHelpers.major_deprecation 3, "Bundler no longer integrates with " \
+ "Capistrano, but Capistrano provides its own integration with " \
+ "Bundler via the capistrano-bundler gem. Use it instead."
+
+module Bundler
+ class Deployment
+ def self.define_task(context, task_method = :task, opts = {})
+ if defined?(Capistrano) && context.is_a?(Capistrano::Configuration)
+ context_name = "capistrano"
+ role_default = "{:except => {:no_release => true}}"
+ error_type = ::Capistrano::CommandError
+ else
+ context_name = "vlad"
+ role_default = "[:app]"
+ error_type = ::Rake::CommandFailedError
+ end
+
+ roles = context.fetch(:bundle_roles, false)
+ opts[:roles] = roles if roles
+
+ context.send :namespace, :bundle do
+ send :desc, <<-DESC
+ Install the current Bundler environment. By default, gems will be \
+ installed to the shared/bundle path. Gems in the development and \
+ test group will not be installed. The install command is executed \
+ with the --deployment and --quiet flags. If the bundle cmd cannot \
+ be found then you can override the bundle_cmd variable to specify \
+ which one it should use. The base path to the app is fetched from \
+ the :latest_release variable. Set it for custom deploy layouts.
+
+ You can override any of these defaults by setting the variables shown below.
+
+ N.B. bundle_roles must be defined before you require 'bundler/#{context_name}' \
+ in your deploy.rb file.
+
+ set :bundle_gemfile, "Gemfile"
+ set :bundle_dir, File.join(fetch(:shared_path), 'bundle')
+ set :bundle_flags, "--deployment --quiet"
+ set :bundle_without, [:development, :test]
+ set :bundle_with, [:mysql]
+ set :bundle_cmd, "bundle" # e.g. "/opt/ruby/bin/bundle"
+ set :bundle_roles, #{role_default} # e.g. [:app, :batch]
+ DESC
+ send task_method, :install, opts do
+ bundle_cmd = context.fetch(:bundle_cmd, "bundle")
+ bundle_flags = context.fetch(:bundle_flags, "--deployment --quiet")
+ bundle_dir = context.fetch(:bundle_dir, File.join(context.fetch(:shared_path), "bundle"))
+ bundle_gemfile = context.fetch(:bundle_gemfile, "Gemfile")
+ bundle_without = [*context.fetch(:bundle_without, [:development, :test])].compact
+ bundle_with = [*context.fetch(:bundle_with, [])].compact
+ app_path = context.fetch(:latest_release)
+ if app_path.to_s.empty?
+ raise error_type.new("Cannot detect current release path - make sure you have deployed at least once.")
+ end
+ args = ["--gemfile #{File.join(app_path, bundle_gemfile)}"]
+ args << "--path #{bundle_dir}" unless bundle_dir.to_s.empty?
+ args << bundle_flags.to_s
+ args << "--without #{bundle_without.join(" ")}" unless bundle_without.empty?
+ args << "--with #{bundle_with.join(" ")}" unless bundle_with.empty?
+
+ run "cd #{app_path} && #{bundle_cmd} install #{args.join(" ")}"
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/deprecate.rb b/lib/bundler/deprecate.rb
new file mode 100644
index 00000000000000..f59533630e99f9
--- /dev/null
+++ b/lib/bundler/deprecate.rb
@@ -0,0 +1,44 @@
+# frozen_string_literal: true
+
+begin
+ require "rubygems/deprecate"
+rescue LoadError
+ # it's fine if it doesn't exist on the current RubyGems...
+ nil
+end
+
+module Bundler
+ # If Bundler::Deprecate is an autoload constant, we need to define it
+ if defined?(Bundler::Deprecate) && !autoload?(:Deprecate)
+ # nothing to do!
+ elsif defined? ::Deprecate
+ Deprecate = ::Deprecate
+ elsif defined? Gem::Deprecate
+ Deprecate = Gem::Deprecate
+ else
+ class Deprecate
+ end
+ end
+
+ unless Deprecate.respond_to?(:skip_during)
+ def Deprecate.skip_during
+ original = skip
+ self.skip = true
+ yield
+ ensure
+ self.skip = original
+ end
+ end
+
+ unless Deprecate.respond_to?(:skip)
+ def Deprecate.skip
+ @skip ||= false
+ end
+ end
+
+ unless Deprecate.respond_to?(:skip=)
+ def Deprecate.skip=(skip)
+ @skip = skip
+ end
+ end
+end
diff --git a/lib/bundler/dsl.rb b/lib/bundler/dsl.rb
new file mode 100644
index 00000000000000..90ac073c36d264
--- /dev/null
+++ b/lib/bundler/dsl.rb
@@ -0,0 +1,615 @@
+# frozen_string_literal: true
+
+require "bundler/dependency"
+require "bundler/ruby_dsl"
+
+module Bundler
+ class Dsl
+ include RubyDsl
+
+ def self.evaluate(gemfile, lockfile, unlock)
+ builder = new
+ builder.eval_gemfile(gemfile)
+ builder.to_definition(lockfile, unlock)
+ end
+
+ VALID_PLATFORMS = Bundler::Dependency::PLATFORM_MAP.keys.freeze
+
+ VALID_KEYS = %w[group groups git path glob name branch ref tag require submodules
+ platform platforms type source install_if gemfile].freeze
+
+ attr_reader :gemspecs
+ attr_accessor :dependencies
+
+ def initialize
+ @source = nil
+ @sources = SourceList.new
+ @git_sources = {}
+ @dependencies = []
+ @groups = []
+ @install_conditionals = []
+ @optional_groups = []
+ @platforms = []
+ @env = nil
+ @ruby_version = nil
+ @gemspecs = []
+ @gemfile = nil
+ @gemfiles = []
+ add_git_sources
+ end
+
+ def eval_gemfile(gemfile, contents = nil)
+ expanded_gemfile_path = Pathname.new(gemfile).expand_path(@gemfile && @gemfile.parent)
+ original_gemfile = @gemfile
+ @gemfile = expanded_gemfile_path
+ @gemfiles << expanded_gemfile_path
+ contents ||= Bundler.read_file(@gemfile.to_s)
+ instance_eval(contents.dup.untaint, gemfile.to_s, 1)
+ rescue Exception => e
+ message = "There was an error " \
+ "#{e.is_a?(GemfileEvalError) ? "evaluating" : "parsing"} " \
+ "`#{File.basename gemfile.to_s}`: #{e.message}"
+
+ raise DSLError.new(message, gemfile, e.backtrace, contents)
+ ensure
+ @gemfile = original_gemfile
+ end
+
+ def gemspec(opts = nil)
+ opts ||= {}
+ path = opts[:path] || "."
+ glob = opts[:glob]
+ name = opts[:name]
+ development_group = opts[:development_group] || :development
+ expanded_path = gemfile_root.join(path)
+
+ gemspecs = Dir[File.join(expanded_path, "{,*}.gemspec")].map {|g| Bundler.load_gemspec(g) }.compact
+ gemspecs.reject! {|s| s.name != name } if name
+ Index.sort_specs(gemspecs)
+ specs_by_name_and_version = gemspecs.group_by {|s| [s.name, s.version] }
+
+ case specs_by_name_and_version.size
+ when 1
+ specs = specs_by_name_and_version.values.first
+ spec = specs.find {|s| s.match_platform(Bundler.local_platform) } || specs.first
+
+ @gemspecs << spec
+
+ gem_platforms = Bundler::Dependency::REVERSE_PLATFORM_MAP[Bundler::GemHelpers.generic_local_platform]
+ gem spec.name, :name => spec.name, :path => path, :glob => glob, :platforms => gem_platforms
+
+ group(development_group) do
+ spec.development_dependencies.each do |dep|
+ gem dep.name, *(dep.requirement.as_list + [:type => :development])
+ end
+ end
+ when 0
+ raise InvalidOption, "There are no gemspecs at #{expanded_path}"
+ else
+ raise InvalidOption, "There are multiple gemspecs at #{expanded_path}. " \
+ "Please use the :name option to specify which one should be used"
+ end
+ end
+
+ def gem(name, *args)
+ options = args.last.is_a?(Hash) ? args.pop.dup : {}
+ options["gemfile"] = @gemfile
+ version = args || [">= 0"]
+
+ normalize_options(name, version, options)
+
+ dep = Dependency.new(name, version, options)
+
+ # if there's already a dependency with this name we try to prefer one
+ if current = @dependencies.find {|d| d.name == dep.name }
+ deleted_dep = @dependencies.delete(current) if current.type == :development
+
+ if current.requirement != dep.requirement
+ unless deleted_dep
+ return if dep.type == :development
+
+ update_prompt = ""
+
+ if File.basename(@gemfile) == Injector::INJECTED_GEMS
+ if dep.requirements_list.include?(">= 0") && !current.requirements_list.include?(">= 0")
+ update_prompt = ". Gem already added"
+ else
+ update_prompt = ". If you want to update the gem version, run `bundle update #{current.name}`"
+
+ update_prompt += ". You may also need to change the version requirement specified in the Gemfile if it's too restrictive." unless current.requirements_list.include?(">= 0")
+ end
+ end
+
+ raise GemfileError, "You cannot specify the same gem twice with different version requirements.\n" \
+ "You specified: #{current.name} (#{current.requirement}) and #{dep.name} (#{dep.requirement})" \
+ "#{update_prompt}"
+ end
+
+ else
+ Bundler.ui.warn "Your Gemfile lists the gem #{current.name} (#{current.requirement}) more than once.\n" \
+ "You should probably keep only one of them.\n" \
+ "Remove any duplicate entries and specify the gem only once (per group).\n" \
+ "While it's not a problem now, it could cause errors if you change the version of one of them later."
+ end
+
+ if current.source != dep.source
+ unless deleted_dep
+ return if dep.type == :development
+ raise GemfileError, "You cannot specify the same gem twice coming from different sources.\n" \
+ "You specified that #{dep.name} (#{dep.requirement}) should come from " \
+ "#{current.source || "an unspecified source"} and #{dep.source}\n"
+ end
+ end
+ end
+
+ @dependencies << dep
+ end
+
+ def source(source, *args, &blk)
+ options = args.last.is_a?(Hash) ? args.pop.dup : {}
+ options = normalize_hash(options)
+ source = normalize_source(source)
+
+ if options.key?("type")
+ options["type"] = options["type"].to_s
+ unless Plugin.source?(options["type"])
+ raise InvalidOption, "No plugin sources available for #{options["type"]}"
+ end
+
+ unless block_given?
+ raise InvalidOption, "You need to pass a block to #source with :type option"
+ end
+
+ source_opts = options.merge("uri" => source)
+ with_source(@sources.add_plugin_source(options["type"], source_opts), &blk)
+ elsif block_given?
+ with_source(@sources.add_rubygems_source("remotes" => source), &blk)
+ else
+ check_primary_source_safety(@sources)
+ @sources.global_rubygems_source = source
+ end
+ end
+
+ def git_source(name, &block)
+ unless block_given?
+ raise InvalidOption, "You need to pass a block to #git_source"
+ end
+
+ if valid_keys.include?(name.to_s)
+ raise InvalidOption, "You cannot use #{name} as a git source. It " \
+ "is a reserved key. Reserved keys are: #{valid_keys.join(", ")}"
+ end
+
+ @git_sources[name.to_s] = block
+ end
+
+ def path(path, options = {}, &blk)
+ unless block_given?
+ msg = "You can no longer specify a path source by itself. Instead, \n" \
+ "either use the :path option on a gem, or specify the gems that \n" \
+ "bundler should find in the path source by passing a block to \n" \
+ "the path method, like: \n\n" \
+ " path 'dir/containing/rails' do\n" \
+ " gem 'rails'\n" \
+ " end\n\n"
+
+ raise DeprecatedError, msg if Bundler.feature_flag.disable_multisource?
+ SharedHelpers.major_deprecation(3, msg.strip)
+ end
+
+ source_options = normalize_hash(options).merge(
+ "path" => Pathname.new(path),
+ "root_path" => gemfile_root,
+ "gemspec" => gemspecs.find {|g| g.name == options["name"] }
+ )
+ source = @sources.add_path_source(source_options)
+ with_source(source, &blk)
+ end
+
+ def git(uri, options = {}, &blk)
+ unless block_given?
+ msg = "You can no longer specify a git source by itself. Instead, \n" \
+ "either use the :git option on a gem, or specify the gems that \n" \
+ "bundler should find in the git source by passing a block to \n" \
+ "the git method, like: \n\n" \
+ " git 'git://github.com/rails/rails.git' do\n" \
+ " gem 'rails'\n" \
+ " end"
+ raise DeprecatedError, msg
+ end
+
+ with_source(@sources.add_git_source(normalize_hash(options).merge("uri" => uri)), &blk)
+ end
+
+ def github(repo, options = {})
+ raise ArgumentError, "GitHub sources require a block" unless block_given?
+ raise DeprecatedError, "The #github method has been removed" if Bundler.feature_flag.skip_default_git_sources?
+ github_uri = @git_sources["github"].call(repo)
+ git_options = normalize_hash(options).merge("uri" => github_uri)
+ git_source = @sources.add_git_source(git_options)
+ with_source(git_source) { yield }
+ end
+
+ def to_definition(lockfile, unlock)
+ Definition.new(lockfile, @dependencies, @sources, unlock, @ruby_version, @optional_groups, @gemfiles)
+ end
+
+ def group(*args, &blk)
+ options = args.last.is_a?(Hash) ? args.pop.dup : {}
+ normalize_group_options(options, args)
+
+ @groups.concat args
+
+ if options["optional"]
+ optional_groups = args - @optional_groups
+ @optional_groups.concat optional_groups
+ end
+
+ yield
+ ensure
+ args.each { @groups.pop }
+ end
+
+ def install_if(*args)
+ @install_conditionals.concat args
+ yield
+ ensure
+ args.each { @install_conditionals.pop }
+ end
+
+ def platforms(*platforms)
+ @platforms.concat platforms
+ yield
+ ensure
+ platforms.each { @platforms.pop }
+ end
+ alias_method :platform, :platforms
+
+ def env(name)
+ old = @env
+ @env = name
+ yield
+ ensure
+ @env = old
+ end
+
+ def plugin(*args)
+ # Pass on
+ end
+
+ def method_missing(name, *args)
+ raise GemfileError, "Undefined local variable or method `#{name}' for Gemfile"
+ end
+
+ private
+
+ def add_git_sources
+ return if Bundler.feature_flag.skip_default_git_sources?
+
+ git_source(:github) do |repo_name|
+ warn_deprecated_git_source(:github, <<-'RUBY'.strip, 'Change any "reponame" :github sources to "username/reponame".')
+"https://github.com/#{repo_name}.git"
+ RUBY
+ # It would be better to use https instead of the git protocol, but this
+ # can break deployment of existing locked bundles when switching between
+ # different versions of Bundler. The change will be made in 2.0, which
+ # does not guarantee compatibility with the 1.x series.
+ #
+ # See https://github.com/bundler/bundler/pull/2569 for discussion
+ #
+ # This can be overridden by adding this code to your Gemfiles:
+ #
+ # git_source(:github) do |repo_name|
+ # repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
+ # "https://github.com/#{repo_name}.git"
+ # end
+ repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
+ # TODO: 2.0 upgrade this setting to the default
+ if Bundler.settings["github.https"]
+ Bundler::SharedHelpers.major_deprecation 3, "The `github.https` setting will be removed"
+ "https://github.com/#{repo_name}.git"
+ else
+ "git://github.com/#{repo_name}.git"
+ end
+ end
+
+ # TODO: 2.0 remove this deprecated git source
+ git_source(:gist) do |repo_name|
+ warn_deprecated_git_source(:gist, '"https://gist.github.com/#{repo_name}.git"')
+
+ "https://gist.github.com/#{repo_name}.git"
+ end
+
+ # TODO: 2.0 remove this deprecated git source
+ git_source(:bitbucket) do |repo_name|
+ warn_deprecated_git_source(:bitbucket, <<-'RUBY'.strip)
+user_name, repo_name = repo_name.split("/")
+repo_name ||= user_name
+"https://#{user_name}@bitbucket.org/#{user_name}/#{repo_name}.git"
+ RUBY
+
+ user_name, repo_name = repo_name.split("/")
+ repo_name ||= user_name
+ "https://#{user_name}@bitbucket.org/#{user_name}/#{repo_name}.git"
+ end
+ end
+
+ def with_source(source)
+ old_source = @source
+ if block_given?
+ @source = source
+ yield
+ end
+ source
+ ensure
+ @source = old_source
+ end
+
+ def normalize_hash(opts)
+ opts.keys.each do |k|
+ opts[k.to_s] = opts.delete(k) unless k.is_a?(String)
+ end
+ opts
+ end
+
+ def valid_keys
+ @valid_keys ||= VALID_KEYS
+ end
+
+ def normalize_options(name, version, opts)
+ if name.is_a?(Symbol)
+ raise GemfileError, %(You need to specify gem names as Strings. Use 'gem "#{name}"' instead)
+ end
+ if name =~ /\s/
+ raise GemfileError, %('#{name}' is not a valid gem name because it contains whitespace)
+ end
+ if name.empty?
+ raise GemfileError, %(an empty gem name is not valid)
+ end
+
+ normalize_hash(opts)
+
+ git_names = @git_sources.keys.map(&:to_s)
+ validate_keys("gem '#{name}'", opts, valid_keys + git_names)
+
+ groups = @groups.dup
+ opts["group"] = opts.delete("groups") || opts["group"]
+ groups.concat Array(opts.delete("group"))
+ groups = [:default] if groups.empty?
+
+ install_if = @install_conditionals.dup
+ install_if.concat Array(opts.delete("install_if"))
+ install_if = install_if.reduce(true) do |memo, val|
+ memo && (val.respond_to?(:call) ? val.call : val)
+ end
+
+ platforms = @platforms.dup
+ opts["platforms"] = opts["platform"] || opts["platforms"]
+ platforms.concat Array(opts.delete("platforms"))
+ platforms.map!(&:to_sym)
+ platforms.each do |p|
+ next if VALID_PLATFORMS.include?(p)
+ raise GemfileError, "`#{p}` is not a valid platform. The available options are: #{VALID_PLATFORMS.inspect}"
+ end
+
+ # Save sources passed in a key
+ if opts.key?("source")
+ source = normalize_source(opts["source"])
+ opts["source"] = @sources.add_rubygems_source("remotes" => source)
+ end
+
+ git_name = (git_names & opts.keys).last
+ if @git_sources[git_name]
+ opts["git"] = @git_sources[git_name].call(opts[git_name])
+ end
+
+ %w[git path].each do |type|
+ next unless param = opts[type]
+ if version.first && version.first =~ /^\s*=?\s*(\d[^\s]*)\s*$/
+ options = opts.merge("name" => name, "version" => $1)
+ else
+ options = opts.dup
+ end
+ source = send(type, param, options) {}
+ opts["source"] = source
+ end
+
+ opts["source"] ||= @source
+ opts["env"] ||= @env
+ opts["platforms"] = platforms.dup
+ opts["group"] = groups
+ opts["should_include"] = install_if
+ end
+
+ def normalize_group_options(opts, groups)
+ normalize_hash(opts)
+
+ groups = groups.map {|group| ":#{group}" }.join(", ")
+ validate_keys("group #{groups}", opts, %w[optional])
+
+ opts["optional"] ||= false
+ end
+
+ def validate_keys(command, opts, valid_keys)
+ invalid_keys = opts.keys - valid_keys
+
+ git_source = opts.keys & @git_sources.keys.map(&:to_s)
+ if opts["branch"] && !(opts["git"] || opts["github"] || git_source.any?)
+ raise GemfileError, %(The `branch` option for `#{command}` is not allowed. Only gems with a git source can specify a branch)
+ end
+
+ return true unless invalid_keys.any?
+
+ message = String.new
+ message << "You passed #{invalid_keys.map {|k| ":" + k }.join(", ")} "
+ message << if invalid_keys.size > 1
+ "as options for #{command}, but they are invalid."
+ else
+ "as an option for #{command}, but it is invalid."
+ end
+
+ message << " Valid options are: #{valid_keys.join(", ")}."
+ message << " You may be able to resolve this by upgrading Bundler to the newest version."
+ raise InvalidOption, message
+ end
+
+ def normalize_source(source)
+ case source
+ when :gemcutter, :rubygems, :rubyforge
+ Bundler::SharedHelpers.major_deprecation 3, "The source :#{source} is deprecated because HTTP " \
+ "requests are insecure.\nPlease change your source to 'https://" \
+ "rubygems.org' if possible, or 'http://rubygems.org' if not."
+ "http://rubygems.org"
+ when String
+ source
+ else
+ raise GemfileError, "Unknown source '#{source}'"
+ end
+ end
+
+ def check_primary_source_safety(source_list)
+ return if source_list.rubygems_primary_remotes.empty? && source_list.global_rubygems_source.nil?
+
+ if Bundler.feature_flag.disable_multisource?
+ msg = "This Gemfile contains multiple primary sources. " \
+ "Each source after the first must include a block to indicate which gems " \
+ "should come from that source"
+ unless Bundler.feature_flag.bundler_3_mode?
+ msg += ". To downgrade this error to a warning, run " \
+ "`bundle config --delete disable_multisource`"
+ end
+ raise GemfileEvalError, msg
+ else
+ Bundler::SharedHelpers.major_deprecation 3, "Your Gemfile contains multiple primary sources. " \
+ "Using `source` more than once without a block is a security risk, and " \
+ "may result in installing unexpected gems. To resolve this warning, use " \
+ "a block to indicate which gems should come from the secondary source. " \
+ "To upgrade this warning to an error, run `bundle config " \
+ "disable_multisource true`."
+ end
+ end
+
+ def warn_deprecated_git_source(name, replacement, additional_message = nil)
+ # TODO: 2.0 remove deprecation
+ additional_message &&= " #{additional_message}"
+ replacement = if replacement.count("\n").zero?
+ "{|repo_name| #{replacement} }"
+ else
+ "do |repo_name|\n#{replacement.to_s.gsub(/^/, " ")}\n end"
+ end
+
+ Bundler::SharedHelpers.major_deprecation 3, <<-EOS
+The :#{name} git source is deprecated, and will be removed in Bundler 3.0.#{additional_message} Add this code to the top of your Gemfile to ensure it continues to work:
+
+ git_source(:#{name}) #{replacement}
+
+ EOS
+ end
+
+ class DSLError < GemfileError
+ # @return [String] the description that should be presented to the user.
+ #
+ attr_reader :description
+
+ # @return [String] the path of the dsl file that raised the exception.
+ #
+ attr_reader :dsl_path
+
+ # @return [Exception] the backtrace of the exception raised by the
+ # evaluation of the dsl file.
+ #
+ attr_reader :backtrace
+
+ # @param [Exception] backtrace @see backtrace
+ # @param [String] dsl_path @see dsl_path
+ #
+ def initialize(description, dsl_path, backtrace, contents = nil)
+ @status_code = $!.respond_to?(:status_code) && $!.status_code
+
+ @description = description
+ @dsl_path = dsl_path
+ @backtrace = backtrace
+ @contents = contents
+ end
+
+ def status_code
+ @status_code || super
+ end
+
+ # @return [String] the contents of the DSL that cause the exception to
+ # be raised.
+ #
+ def contents
+ @contents ||= begin
+ dsl_path && File.exist?(dsl_path) && File.read(dsl_path)
+ end
+ end
+
+ # The message of the exception reports the content of podspec for the
+ # line that generated the original exception.
+ #
+ # @example Output
+ #
+ # Invalid podspec at `RestKit.podspec` - undefined method
+ # `exclude_header_search_paths=' for #
+ #
+ # from spec-repos/master/RestKit/0.9.3/RestKit.podspec:36
+ # -------------------------------------------
+ # # because it would break: #import
+ # > ns.exclude_header_search_paths = 'Code/RestKit.h'
+ # end
+ # -------------------------------------------
+ #
+ # @return [String] the message of the exception.
+ #
+ def to_s
+ @to_s ||= begin
+ trace_line, description = parse_line_number_from_description
+
+ m = String.new("\n[!] ")
+ m << description
+ m << ". Bundler cannot continue.\n"
+
+ return m unless backtrace && dsl_path && contents
+
+ trace_line = backtrace.find {|l| l.include?(dsl_path.to_s) } || trace_line
+ return m unless trace_line
+ line_numer = trace_line.split(":")[1].to_i - 1
+ return m unless line_numer
+
+ lines = contents.lines.to_a
+ indent = " # "
+ indicator = indent.tr("#", ">")
+ first_line = line_numer.zero?
+ last_line = (line_numer == (lines.count - 1))
+
+ m << "\n"
+ m << "#{indent}from #{trace_line.gsub(/:in.*$/, "")}\n"
+ m << "#{indent}-------------------------------------------\n"
+ m << "#{indent}#{lines[line_numer - 1]}" unless first_line
+ m << "#{indicator}#{lines[line_numer]}"
+ m << "#{indent}#{lines[line_numer + 1]}" unless last_line
+ m << "\n" unless m.end_with?("\n")
+ m << "#{indent}-------------------------------------------\n"
+ end
+ end
+
+ private
+
+ def parse_line_number_from_description
+ description = self.description
+ if dsl_path && description =~ /((#{Regexp.quote File.expand_path(dsl_path)}|#{Regexp.quote dsl_path.to_s}):\d+)/
+ trace_line = Regexp.last_match[1]
+ description = description.sub(/#{Regexp.quote trace_line}:\s*/, "").sub("\n", " - ")
+ end
+ [trace_line, description]
+ end
+ end
+
+ def gemfile_root
+ @gemfile ||= Bundler.default_gemfile
+ @gemfile.dirname
+ end
+ end
+end
diff --git a/lib/bundler/endpoint_specification.rb b/lib/bundler/endpoint_specification.rb
new file mode 100644
index 00000000000000..9a00b64e0ede93
--- /dev/null
+++ b/lib/bundler/endpoint_specification.rb
@@ -0,0 +1,141 @@
+# frozen_string_literal: true
+
+module Bundler
+ # used for Creating Specifications from the Gemcutter Endpoint
+ class EndpointSpecification < Gem::Specification
+ ILLFORMED_MESSAGE = 'Ill-formed requirement ["# e
+ raise GemspecError, "There was an error parsing the metadata for the gem #{name} (#{version}): #{e.class}\n#{e}\nThe metadata was #{data.inspect}"
+ end
+
+ def build_dependency(name, requirements)
+ Gem::Dependency.new(name, requirements)
+ rescue ArgumentError => e
+ raise unless e.message.include?(ILLFORMED_MESSAGE)
+ puts # we shouldn't print the error message on the "fetching info" status line
+ raise GemspecError,
+ "Unfortunately, the gem #{name} (#{version}) has an invalid " \
+ "gemspec.\nPlease ask the gem author to yank the bad version to fix " \
+ "this issue. For more information, see http://bit.ly/syck-defaultkey."
+ end
+ end
+end
diff --git a/lib/bundler/env.rb b/lib/bundler/env.rb
new file mode 100644
index 00000000000000..51738139fac815
--- /dev/null
+++ b/lib/bundler/env.rb
@@ -0,0 +1,155 @@
+# frozen_string_literal: true
+
+require "bundler/rubygems_integration"
+require "bundler/source/git/git_proxy"
+
+module Bundler
+ class Env
+ def self.write(io)
+ io.write report
+ end
+
+ def self.report(options = {})
+ print_gemfile = options.delete(:print_gemfile) { true }
+ print_gemspecs = options.delete(:print_gemspecs) { true }
+
+ out = String.new
+ append_formatted_table("Environment", environment, out)
+ append_formatted_table("Bundler Build Metadata", BuildMetadata.to_h, out)
+
+ unless Bundler.settings.all.empty?
+ out << "\n## Bundler settings\n\n```\n"
+ Bundler.settings.all.each do |setting|
+ out << setting << "\n"
+ Bundler.settings.pretty_values_for(setting).each do |line|
+ out << " " << line << "\n"
+ end
+ end
+ out << "```\n"
+ end
+
+ return out unless SharedHelpers.in_bundle?
+
+ if print_gemfile
+ gemfiles = [Bundler.default_gemfile]
+ begin
+ gemfiles = Bundler.definition.gemfiles
+ rescue GemfileNotFound
+ nil
+ end
+
+ out << "\n## Gemfile\n"
+ gemfiles.each do |gemfile|
+ out << "\n### #{Pathname.new(gemfile).relative_path_from(SharedHelpers.pwd)}\n\n"
+ out << "```ruby\n" << read_file(gemfile).chomp << "\n```\n"
+ end
+
+ out << "\n### #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)}\n\n"
+ out << "```\n" << read_file(Bundler.default_lockfile).chomp << "\n```\n"
+ end
+
+ if print_gemspecs
+ dsl = Dsl.new.tap {|d| d.eval_gemfile(Bundler.default_gemfile) }
+ out << "\n## Gemspecs\n" unless dsl.gemspecs.empty?
+ dsl.gemspecs.each do |gs|
+ out << "\n### #{File.basename(gs.loaded_from)}"
+ out << "\n\n```ruby\n" << read_file(gs.loaded_from).chomp << "\n```\n"
+ end
+ end
+
+ out
+ end
+
+ def self.read_file(filename)
+ Bundler.read_file(filename.to_s).strip
+ rescue Errno::ENOENT
+ ""
+ rescue RuntimeError => e
+ "#{e.class}: #{e.message}"
+ end
+
+ def self.ruby_version
+ str = String.new("#{RUBY_VERSION}")
+ if RUBY_VERSION < "1.9"
+ str << " (#{RUBY_RELEASE_DATE}"
+ str << " patchlevel #{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
+ str << ") [#{RUBY_PLATFORM}]"
+ else
+ str << "p#{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
+ str << " (#{RUBY_RELEASE_DATE} revision #{RUBY_REVISION}) [#{RUBY_PLATFORM}]"
+ end
+ end
+
+ def self.git_version
+ Bundler::Source::Git::GitProxy.new(nil, nil, nil).full_version
+ rescue Bundler::Source::Git::GitNotInstalledError
+ "not installed"
+ end
+
+ def self.version_of(script)
+ return "not installed" unless Bundler.which(script)
+ `#{script} --version`.chomp
+ end
+
+ def self.chruby_version
+ return "not installed" unless Bundler.which("chruby-exec")
+ `chruby-exec -- chruby --version`.
+ sub(/.*^chruby: (#{Gem::Version::VERSION_PATTERN}).*/m, '\1')
+ end
+
+ def self.environment
+ out = []
+
+ out << ["Bundler", Bundler::VERSION]
+ out << [" Platforms", Gem.platforms.join(", ")]
+ out << ["Ruby", ruby_version]
+ out << [" Full Path", Gem.ruby]
+ out << [" Config Dir", Pathname.new(Gem::ConfigFile::SYSTEM_WIDE_CONFIG_FILE).dirname]
+ out << ["RubyGems", Gem::VERSION]
+ out << [" Gem Home", ENV.fetch("GEM_HOME") { Gem.dir }]
+ out << [" Gem Path", ENV.fetch("GEM_PATH") { Gem.path.join(File::PATH_SEPARATOR) }]
+ out << [" User Path", Gem.user_dir]
+ out << [" Bin Dir", Gem.bindir]
+ if defined?(OpenSSL)
+ out << ["OpenSSL"]
+ out << [" Compiled", OpenSSL::OPENSSL_VERSION] if defined?(OpenSSL::OPENSSL_VERSION)
+ out << [" Loaded", OpenSSL::OPENSSL_LIBRARY_VERSION] if defined?(OpenSSL::OPENSSL_LIBRARY_VERSION)
+ out << [" Cert File", OpenSSL::X509::DEFAULT_CERT_FILE] if defined?(OpenSSL::X509::DEFAULT_CERT_FILE)
+ out << [" Cert Dir", OpenSSL::X509::DEFAULT_CERT_DIR] if defined?(OpenSSL::X509::DEFAULT_CERT_DIR)
+ end
+ out << ["Tools"]
+ out << [" Git", git_version]
+ out << [" RVM", ENV.fetch("rvm_version") { version_of("rvm") }]
+ out << [" rbenv", version_of("rbenv")]
+ out << [" chruby", chruby_version]
+
+ %w[rubygems-bundler open_gem].each do |name|
+ specs = Bundler.rubygems.find_name(name)
+ out << [" #{name}", "(#{specs.map(&:version).join(",")})"] unless specs.empty?
+ end
+ if (exe = caller.last.split(":").first) && exe =~ %r{(exe|bin)/bundler?\z}
+ shebang = File.read(exe).lines.first
+ shebang.sub!(/^#!\s*/, "")
+ unless shebang.start_with?(Gem.ruby, "/usr/bin/env ruby")
+ out << ["Gem.ruby", Gem.ruby]
+ out << ["bundle #!", shebang]
+ end
+ end
+
+ out
+ end
+
+ def self.append_formatted_table(title, pairs, out)
+ return if pairs.empty?
+ out << "\n" unless out.empty?
+ out << "## #{title}\n\n```\n"
+ ljust = pairs.map {|k, _v| k.to_s.length }.max
+ pairs.each do |k, v|
+ out << "#{k.to_s.ljust(ljust)} #{v}\n"
+ end
+ out << "```\n"
+ end
+
+ private_class_method :read_file, :ruby_version, :git_version, :append_formatted_table, :version_of, :chruby_version
+ end
+end
diff --git a/lib/bundler/environment_preserver.rb b/lib/bundler/environment_preserver.rb
new file mode 100644
index 00000000000000..af7c1ef0a49897
--- /dev/null
+++ b/lib/bundler/environment_preserver.rb
@@ -0,0 +1,59 @@
+# frozen_string_literal: true
+
+module Bundler
+ class EnvironmentPreserver
+ INTENTIONALLY_NIL = "BUNDLER_ENVIRONMENT_PRESERVER_INTENTIONALLY_NIL".freeze
+ BUNDLER_KEYS = %w[
+ BUNDLE_BIN_PATH
+ BUNDLE_GEMFILE
+ BUNDLER_ORIG_MANPATH
+ BUNDLER_VERSION
+ GEM_HOME
+ GEM_PATH
+ MANPATH
+ PATH
+ RB_USER_INSTALL
+ RUBYLIB
+ RUBYOPT
+ ].map(&:freeze).freeze
+ BUNDLER_PREFIX = "BUNDLER_ORIG_".freeze
+
+ # @param env [ENV]
+ # @param keys [Array]
+ def initialize(env, keys)
+ @original = env.to_hash
+ @keys = keys
+ @prefix = BUNDLER_PREFIX
+ end
+
+ # @return [Hash]
+ def backup
+ env = @original.clone
+ @keys.each do |key|
+ value = env[key]
+ if !value.nil? && !value.empty?
+ env[@prefix + key] ||= value
+ elsif value.nil?
+ env[@prefix + key] ||= INTENTIONALLY_NIL
+ end
+ end
+ env
+ end
+
+ # @return [Hash]
+ def restore
+ env = @original.clone
+ @keys.each do |key|
+ value_original = env[@prefix + key]
+ next if value_original.nil? || value_original.empty?
+ if value_original == INTENTIONALLY_NIL
+ env.delete(key)
+ else
+ env[key] = value_original
+ end
+ env.delete(@prefix + key)
+ end
+ env
+ end
+ end
+end
diff --git a/lib/bundler/errors.rb b/lib/bundler/errors.rb
new file mode 100644
index 00000000000000..e471bce0b6f78a
--- /dev/null
+++ b/lib/bundler/errors.rb
@@ -0,0 +1,158 @@
+# frozen_string_literal: true
+
+module Bundler
+ class BundlerError < StandardError
+ def self.status_code(code)
+ define_method(:status_code) { code }
+ if match = BundlerError.all_errors.find {|_k, v| v == code }
+ error, _ = match
+ raise ArgumentError,
+ "Trying to register #{self} for status code #{code} but #{error} is already registered"
+ end
+ BundlerError.all_errors[self] = code
+ end
+
+ def self.all_errors
+ @all_errors ||= {}
+ end
+ end
+
+ class GemfileError < BundlerError; status_code(4); end
+ class InstallError < BundlerError; status_code(5); end
+
+ # Internal error, should be rescued
+ class VersionConflict < BundlerError
+ attr_reader :conflicts
+
+ def initialize(conflicts, msg = nil)
+ super(msg)
+ @conflicts = conflicts
+ end
+
+ status_code(6)
+ end
+
+ class GemNotFound < BundlerError; status_code(7); end
+ class InstallHookError < BundlerError; status_code(8); end
+ class GemfileNotFound < BundlerError; status_code(10); end
+ class GitError < BundlerError; status_code(11); end
+ class DeprecatedError < BundlerError; status_code(12); end
+ class PathError < BundlerError; status_code(13); end
+ class GemspecError < BundlerError; status_code(14); end
+ class InvalidOption < BundlerError; status_code(15); end
+ class ProductionError < BundlerError; status_code(16); end
+ class HTTPError < BundlerError
+ status_code(17)
+ def filter_uri(uri)
+ URICredentialsFilter.credential_filtered_uri(uri)
+ end
+ end
+ class RubyVersionMismatch < BundlerError; status_code(18); end
+ class SecurityError < BundlerError; status_code(19); end
+ class LockfileError < BundlerError; status_code(20); end
+ class CyclicDependencyError < BundlerError; status_code(21); end
+ class GemfileLockNotFound < BundlerError; status_code(22); end
+ class PluginError < BundlerError; status_code(29); end
+ class SudoNotPermittedError < BundlerError; status_code(30); end
+ class ThreadCreationError < BundlerError; status_code(33); end
+ class APIResponseMismatchError < BundlerError; status_code(34); end
+ class GemfileEvalError < GemfileError; end
+ class MarshalError < StandardError; end
+
+ class PermissionError < BundlerError
+ def initialize(path, permission_type = :write)
+ @path = path
+ @permission_type = permission_type
+ end
+
+ def action
+ case @permission_type
+ when :read then "read from"
+ when :write then "write to"
+ when :executable, :exec then "execute"
+ else @permission_type.to_s
+ end
+ end
+
+ def message
+ "There was an error while trying to #{action} `#{@path}`. " \
+ "It is likely that you need to grant #{@permission_type} permissions " \
+ "for that path."
+ end
+
+ status_code(23)
+ end
+
+ class GemRequireError < BundlerError
+ attr_reader :orig_exception
+
+ def initialize(orig_exception, msg)
+ full_message = msg + "\nGem Load Error is: #{orig_exception.message}\n"\
+ "Backtrace for gem load error is:\n"\
+ "#{orig_exception.backtrace.join("\n")}\n"\
+ "Bundler Error Backtrace:\n"
+ super(full_message)
+ @orig_exception = orig_exception
+ end
+
+ status_code(24)
+ end
+
+ class YamlSyntaxError < BundlerError
+ attr_reader :orig_exception
+
+ def initialize(orig_exception, msg)
+ super(msg)
+ @orig_exception = orig_exception
+ end
+
+ status_code(25)
+ end
+
+ class TemporaryResourceError < PermissionError
+ def message
+ "There was an error while trying to #{action} `#{@path}`. " \
+ "Some resource was temporarily unavailable. It's suggested that you try" \
+ "the operation again."
+ end
+
+ status_code(26)
+ end
+
+ class VirtualProtocolError < BundlerError
+ def message
+ "There was an error relating to virtualization and file access." \
+ "It is likely that you need to grant access to or mount some file system correctly."
+ end
+
+ status_code(27)
+ end
+
+ class OperationNotSupportedError < PermissionError
+ def message
+ "Attempting to #{action} `#{@path}` is unsupported by your OS."
+ end
+
+ status_code(28)
+ end
+
+ class NoSpaceOnDeviceError < PermissionError
+ def message
+ "There was an error while trying to #{action} `#{@path}`. " \
+ "There was insufficient space remaining on the device."
+ end
+
+ status_code(31)
+ end
+
+ class GenericSystemCallError < BundlerError
+ attr_reader :underlying_error
+
+ def initialize(underlying_error, message)
+ @underlying_error = underlying_error
+ super("#{message}\nThe underlying system error is #{@underlying_error.class}: #{@underlying_error}")
+ end
+
+ status_code(32)
+ end
+end
diff --git a/lib/bundler/feature_flag.rb b/lib/bundler/feature_flag.rb
new file mode 100644
index 00000000000000..e5b4e84063ff5c
--- /dev/null
+++ b/lib/bundler/feature_flag.rb
@@ -0,0 +1,75 @@
+# frozen_string_literal: true
+
+module Bundler
+ class FeatureFlag
+ def self.settings_flag(flag, &default)
+ unless Bundler::Settings::BOOL_KEYS.include?(flag.to_s)
+ raise "Cannot use `#{flag}` as a settings feature flag since it isn't a bool key"
+ end
+
+ settings_method("#{flag}?", flag, &default)
+ end
+ private_class_method :settings_flag
+
+ def self.settings_option(key, &default)
+ settings_method(key, key, &default)
+ end
+ private_class_method :settings_option
+
+ def self.settings_method(name, key, &default)
+ define_method(name) do
+ value = Bundler.settings[key]
+ value = instance_eval(&default) if value.nil? && !default.nil?
+ value
+ end
+ end
+ private_class_method :settings_method
+
+ (1..10).each {|v| define_method("bundler_#{v}_mode?") { major_version >= v } }
+
+ settings_flag(:allow_bundler_dependency_conflicts) { bundler_3_mode? }
+ settings_flag(:allow_offline_install) { bundler_3_mode? }
+ settings_flag(:auto_clean_without_path) { bundler_3_mode? }
+ settings_flag(:auto_config_jobs) { bundler_3_mode? }
+ settings_flag(:cache_all) { bundler_3_mode? }
+ settings_flag(:cache_command_is_package) { bundler_3_mode? }
+ settings_flag(:console_command) { !bundler_3_mode? }
+ settings_flag(:default_install_uses_path) { bundler_3_mode? }
+ settings_flag(:deployment_means_frozen) { bundler_3_mode? }
+ settings_flag(:disable_multisource) { bundler_3_mode? }
+ settings_flag(:error_on_stderr) { bundler_2_mode? }
+ settings_flag(:forget_cli_options) { bundler_3_mode? }
+ settings_flag(:global_path_appends_ruby_scope) { bundler_3_mode? }
+ settings_flag(:global_gem_cache) { bundler_3_mode? }
+ settings_flag(:init_gems_rb) { bundler_3_mode? }
+ settings_flag(:list_command) { bundler_3_mode? }
+ settings_flag(:lockfile_uses_separate_rubygems_sources) { bundler_3_mode? }
+ settings_flag(:lockfile_upgrade_warning) { bundler_3_mode? }
+ settings_flag(:only_update_to_newer_versions) { bundler_3_mode? }
+ settings_flag(:path_relative_to_cwd) { bundler_3_mode? }
+ settings_flag(:plugins) { @bundler_version >= Gem::Version.new("1.14") }
+ settings_flag(:prefer_gems_rb) { bundler_3_mode? }
+ settings_flag(:print_only_version_number) { bundler_3_mode? }
+ settings_flag(:setup_makes_kernel_gem_public) { !bundler_3_mode? }
+ settings_flag(:skip_default_git_sources) { bundler_3_mode? }
+ settings_flag(:specific_platform) { bundler_3_mode? }
+ settings_flag(:suppress_install_using_messages) { bundler_3_mode? }
+ settings_flag(:unlock_source_unlocks_spec) { !bundler_3_mode? }
+ settings_flag(:update_requires_all_flag) { bundler_3_mode? }
+ settings_flag(:use_gem_version_promoter_for_major_updates) { bundler_3_mode? }
+ settings_flag(:viz_command) { !bundler_3_mode? }
+
+ settings_option(:default_cli_command) { bundler_3_mode? ? :cli_help : :install }
+
+ settings_method(:github_https?, "github.https") { bundler_2_mode? }
+
+ def initialize(bundler_version)
+ @bundler_version = Gem::Version.create(bundler_version)
+ end
+
+ def major_version
+ @bundler_version.segments.first
+ end
+ private :major_version
+ end
+end
diff --git a/lib/bundler/fetcher.rb b/lib/bundler/fetcher.rb
new file mode 100644
index 00000000000000..4dd42e42ffa750
--- /dev/null
+++ b/lib/bundler/fetcher.rb
@@ -0,0 +1,312 @@
+# frozen_string_literal: true
+
+require "bundler/vendored_persistent"
+require "cgi"
+require "securerandom"
+require "zlib"
+
+module Bundler
+ # Handles all the fetching with the rubygems server
+ class Fetcher
+ autoload :CompactIndex, "bundler/fetcher/compact_index"
+ autoload :Downloader, "bundler/fetcher/downloader"
+ autoload :Dependency, "bundler/fetcher/dependency"
+ autoload :Index, "bundler/fetcher/index"
+
+ # This error is raised when it looks like the network is down
+ class NetworkDownError < HTTPError; end
+ # This error is raised if the API returns a 413 (only printed in verbose)
+ class FallbackError < HTTPError; end
+ # This is the error raised if OpenSSL fails the cert verification
+ class CertificateFailureError < HTTPError
+ def initialize(remote_uri)
+ remote_uri = filter_uri(remote_uri)
+ super "Could not verify the SSL certificate for #{remote_uri}.\nThere" \
+ " is a chance you are experiencing a man-in-the-middle attack, but" \
+ " most likely your system doesn't have the CA certificates needed" \
+ " for verification. For information about OpenSSL certificates, see" \
+ " http://bit.ly/ruby-ssl. To connect without using SSL, edit your Gemfile" \
+ " sources and change 'https' to 'http'."
+ end
+ end
+ # This is the error raised when a source is HTTPS and OpenSSL didn't load
+ class SSLError < HTTPError
+ def initialize(msg = nil)
+ super msg || "Could not load OpenSSL.\n" \
+ "You must recompile Ruby with OpenSSL support or change the sources in your " \
+ "Gemfile from 'https' to 'http'. Instructions for compiling with OpenSSL " \
+ "using RVM are available at rvm.io/packages/openssl."
+ end
+ end
+ # This error is raised if HTTP authentication is required, but not provided.
+ class AuthenticationRequiredError < HTTPError
+ def initialize(remote_uri)
+ remote_uri = filter_uri(remote_uri)
+ super "Authentication is required for #{remote_uri}.\n" \
+ "Please supply credentials for this source. You can do this by running:\n" \
+ " bundle config #{remote_uri} username:password"
+ end
+ end
+ # This error is raised if HTTP authentication is provided, but incorrect.
+ class BadAuthenticationError < HTTPError
+ def initialize(remote_uri)
+ remote_uri = filter_uri(remote_uri)
+ super "Bad username or password for #{remote_uri}.\n" \
+ "Please double-check your credentials and correct them."
+ end
+ end
+
+ # Exceptions classes that should bypass retry attempts. If your password didn't work the
+ # first time, it's not going to the third time.
+ NET_ERRORS = [:HTTPBadGateway, :HTTPBadRequest, :HTTPFailedDependency,
+ :HTTPForbidden, :HTTPInsufficientStorage, :HTTPMethodNotAllowed,
+ :HTTPMovedPermanently, :HTTPNoContent, :HTTPNotFound,
+ :HTTPNotImplemented, :HTTPPreconditionFailed, :HTTPRequestEntityTooLarge,
+ :HTTPRequestURITooLong, :HTTPUnauthorized, :HTTPUnprocessableEntity,
+ :HTTPUnsupportedMediaType, :HTTPVersionNotSupported].freeze
+ FAIL_ERRORS = begin
+ fail_errors = [AuthenticationRequiredError, BadAuthenticationError, FallbackError]
+ fail_errors << Gem::Requirement::BadRequirementError if defined?(Gem::Requirement::BadRequirementError)
+ fail_errors.concat(NET_ERRORS.map {|e| SharedHelpers.const_get_safely(e, Net) }.compact)
+ end.freeze
+
+ class << self
+ attr_accessor :disable_endpoint, :api_timeout, :redirect_limit, :max_retries
+ end
+
+ self.redirect_limit = Bundler.settings[:redirect] # How many redirects to allow in one request
+ self.api_timeout = Bundler.settings[:timeout] # How long to wait for each API call
+ self.max_retries = Bundler.settings[:retry] # How many retries for the API call
+
+ def initialize(remote)
+ @remote = remote
+
+ Socket.do_not_reverse_lookup = true
+ connection # create persistent connection
+ end
+
+ def uri
+ @remote.anonymized_uri
+ end
+
+ # fetch a gem specification
+ def fetch_spec(spec)
+ spec -= [nil, "ruby", ""]
+ spec_file_name = "#{spec.join "-"}.gemspec"
+
+ uri = URI.parse("#{remote_uri}#{Gem::MARSHAL_SPEC_DIR}#{spec_file_name}.rz")
+ if uri.scheme == "file"
+ Bundler.load_marshal Bundler.rubygems.inflate(Gem.read_binary(uri.path))
+ elsif cached_spec_path = gemspec_cached_path(spec_file_name)
+ Bundler.load_gemspec(cached_spec_path)
+ else
+ Bundler.load_marshal Bundler.rubygems.inflate(downloader.fetch(uri).body)
+ end
+ rescue MarshalError
+ raise HTTPError, "Gemspec #{spec} contained invalid data.\n" \
+ "Your network or your gem server is probably having issues right now."
+ end
+
+ # return the specs in the bundler format as an index with retries
+ def specs_with_retry(gem_names, source)
+ Bundler::Retry.new("fetcher", FAIL_ERRORS).attempts do
+ specs(gem_names, source)
+ end
+ end
+
+ # return the specs in the bundler format as an index
+ def specs(gem_names, source)
+ old = Bundler.rubygems.sources
+ index = Bundler::Index.new
+
+ if Bundler::Fetcher.disable_endpoint
+ @use_api = false
+ specs = fetchers.last.specs(gem_names)
+ else
+ specs = []
+ fetchers.shift until fetchers.first.available? || fetchers.empty?
+ fetchers.dup.each do |f|
+ break unless f.api_fetcher? && !gem_names || !specs = f.specs(gem_names)
+ fetchers.delete(f)
+ end
+ @use_api = false if fetchers.none?(&:api_fetcher?)
+ end
+
+ specs.each do |name, version, platform, dependencies, metadata|
+ next if name == "bundler"
+ spec = if dependencies
+ EndpointSpecification.new(name, version, platform, dependencies, metadata)
+ else
+ RemoteSpecification.new(name, version, platform, self)
+ end
+ spec.source = source
+ spec.remote = @remote
+ index << spec
+ end
+
+ index
+ rescue CertificateFailureError
+ Bundler.ui.info "" if gem_names && use_api # newline after dots
+ raise
+ ensure
+ Bundler.rubygems.sources = old
+ end
+
+ def use_api
+ return @use_api if defined?(@use_api)
+
+ fetchers.shift until fetchers.first.available?
+
+ @use_api = if remote_uri.scheme == "file" || Bundler::Fetcher.disable_endpoint
+ false
+ else
+ fetchers.first.api_fetcher?
+ end
+ end
+
+ def user_agent
+ @user_agent ||= begin
+ ruby = Bundler::RubyVersion.system
+
+ agent = String.new("bundler/#{Bundler::VERSION}")
+ agent << " rubygems/#{Gem::VERSION}"
+ agent << " ruby/#{ruby.versions_string(ruby.versions)}"
+ agent << " (#{ruby.host})"
+ agent << " command/#{ARGV.first}"
+
+ if ruby.engine != "ruby"
+ # engine_version raises on unknown engines
+ engine_version = begin
+ ruby.engine_versions
+ rescue RuntimeError
+ "???"
+ end
+ agent << " #{ruby.engine}/#{ruby.versions_string(engine_version)}"
+ end
+
+ agent << " options/#{Bundler.settings.all.join(",")}"
+
+ agent << " ci/#{cis.join(",")}" if cis.any?
+
+ # add a random ID so we can consolidate runs server-side
+ agent << " " << SecureRandom.hex(8)
+
+ # add any user agent strings set in the config
+ extra_ua = Bundler.settings[:user_agent]
+ agent << " " << extra_ua if extra_ua
+
+ agent
+ end
+ end
+
+ def fetchers
+ @fetchers ||= FETCHERS.map {|f| f.new(downloader, @remote, uri) }
+ end
+
+ def http_proxy
+ return unless uri = connection.proxy_uri
+ uri.to_s
+ end
+
+ def inspect
+ "#<#{self.class}:0x#{object_id} uri=#{uri}>"
+ end
+
+ private
+
+ FETCHERS = [CompactIndex, Dependency, Index].freeze
+
+ def cis
+ env_cis = {
+ "TRAVIS" => "travis",
+ "CIRCLECI" => "circle",
+ "SEMAPHORE" => "semaphore",
+ "JENKINS_URL" => "jenkins",
+ "BUILDBOX" => "buildbox",
+ "GO_SERVER_URL" => "go",
+ "SNAP_CI" => "snap",
+ "CI_NAME" => ENV["CI_NAME"],
+ "CI" => "ci"
+ }
+ env_cis.find_all {|env, _| ENV[env] }.map {|_, ci| ci }
+ end
+
+ def connection
+ @connection ||= begin
+ needs_ssl = remote_uri.scheme == "https" ||
+ Bundler.settings[:ssl_verify_mode] ||
+ Bundler.settings[:ssl_client_cert]
+ raise SSLError if needs_ssl && !defined?(OpenSSL::SSL)
+
+ con = PersistentHTTP.new "bundler", :ENV
+ if gem_proxy = Bundler.rubygems.configuration[:http_proxy]
+ con.proxy = URI.parse(gem_proxy) if gem_proxy != :no_proxy
+ end
+
+ if remote_uri.scheme == "https"
+ con.verify_mode = (Bundler.settings[:ssl_verify_mode] ||
+ OpenSSL::SSL::VERIFY_PEER)
+ con.cert_store = bundler_cert_store
+ end
+
+ ssl_client_cert = Bundler.settings[:ssl_client_cert] ||
+ (Bundler.rubygems.configuration.ssl_client_cert if
+ Bundler.rubygems.configuration.respond_to?(:ssl_client_cert))
+ if ssl_client_cert
+ pem = File.read(ssl_client_cert)
+ con.cert = OpenSSL::X509::Certificate.new(pem)
+ con.key = OpenSSL::PKey::RSA.new(pem)
+ end
+
+ con.read_timeout = Fetcher.api_timeout
+ con.open_timeout = Fetcher.api_timeout
+ con.override_headers["User-Agent"] = user_agent
+ con.override_headers["X-Gemfile-Source"] = @remote.original_uri.to_s if @remote.original_uri
+ con
+ end
+ end
+
+ # cached gem specification path, if one exists
+ def gemspec_cached_path(spec_file_name)
+ paths = Bundler.rubygems.spec_cache_dirs.map {|dir| File.join(dir, spec_file_name) }
+ paths = paths.select {|path| File.file? path }
+ paths.first
+ end
+
+ HTTP_ERRORS = [
+ Timeout::Error, EOFError, SocketError, Errno::ENETDOWN, Errno::ENETUNREACH,
+ Errno::EINVAL, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EAGAIN,
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,
+ PersistentHTTP::Error, Zlib::BufError, Errno::EHOSTUNREACH
+ ].freeze
+
+ def bundler_cert_store
+ store = OpenSSL::X509::Store.new
+ ssl_ca_cert = Bundler.settings[:ssl_ca_cert] ||
+ (Bundler.rubygems.configuration.ssl_ca_cert if
+ Bundler.rubygems.configuration.respond_to?(:ssl_ca_cert))
+ if ssl_ca_cert
+ if File.directory? ssl_ca_cert
+ store.add_path ssl_ca_cert
+ else
+ store.add_file ssl_ca_cert
+ end
+ else
+ store.set_default_paths
+ certs = File.expand_path("../ssl_certs/*/*.pem", __FILE__)
+ Dir.glob(certs).each {|c| store.add_file c }
+ end
+ store
+ end
+
+ private
+
+ def remote_uri
+ @remote.uri
+ end
+
+ def downloader
+ @downloader ||= Downloader.new(connection, self.class.redirect_limit)
+ end
+ end
+end
diff --git a/lib/bundler/fetcher/base.rb b/lib/bundler/fetcher/base.rb
new file mode 100644
index 00000000000000..27987f670ae3ca
--- /dev/null
+++ b/lib/bundler/fetcher/base.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Fetcher
+ class Base
+ attr_reader :downloader
+ attr_reader :display_uri
+ attr_reader :remote
+
+ def initialize(downloader, remote, display_uri)
+ raise "Abstract class" if self.class == Base
+ @downloader = downloader
+ @remote = remote
+ @display_uri = display_uri
+ end
+
+ def remote_uri
+ @remote.uri
+ end
+
+ def fetch_uri
+ @fetch_uri ||= begin
+ if remote_uri.host == "rubygems.org"
+ uri = remote_uri.dup
+ uri.host = "index.rubygems.org"
+ uri
+ else
+ remote_uri
+ end
+ end
+ end
+
+ def available?
+ true
+ end
+
+ def api_fetcher?
+ false
+ end
+
+ private
+
+ def log_specs(debug_msg)
+ if Bundler.ui.debug?
+ Bundler.ui.debug debug_msg
+ else
+ Bundler.ui.info ".", false
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/fetcher/compact_index.rb b/lib/bundler/fetcher/compact_index.rb
new file mode 100644
index 00000000000000..cfc74d642cf040
--- /dev/null
+++ b/lib/bundler/fetcher/compact_index.rb
@@ -0,0 +1,126 @@
+# frozen_string_literal: true
+
+require "bundler/fetcher/base"
+require "bundler/worker"
+
+module Bundler
+ autoload :CompactIndexClient, "bundler/compact_index_client"
+
+ class Fetcher
+ class CompactIndex < Base
+ def self.compact_index_request(method_name)
+ method = instance_method(method_name)
+ undef_method(method_name)
+ define_method(method_name) do |*args, &blk|
+ begin
+ method.bind(self).call(*args, &blk)
+ rescue NetworkDownError, CompactIndexClient::Updater::MisMatchedChecksumError => e
+ raise HTTPError, e.message
+ rescue AuthenticationRequiredError
+ # Fail since we got a 401 from the server.
+ raise
+ rescue HTTPError => e
+ Bundler.ui.trace(e)
+ nil
+ end
+ end
+ end
+
+ def specs(gem_names)
+ specs_for_names(gem_names)
+ end
+ compact_index_request :specs
+
+ def specs_for_names(gem_names)
+ gem_info = []
+ complete_gems = []
+ remaining_gems = gem_names.dup
+
+ until remaining_gems.empty?
+ log_specs "Looking up gems #{remaining_gems.inspect}"
+
+ deps = compact_index_client.dependencies(remaining_gems)
+ next_gems = deps.map {|d| d[3].map(&:first).flatten(1) }.flatten(1).uniq
+ deps.each {|dep| gem_info << dep }
+ complete_gems.concat(deps.map(&:first)).uniq!
+ remaining_gems = next_gems - complete_gems
+ end
+ @bundle_worker.stop if @bundle_worker
+ @bundle_worker = nil # reset it. Not sure if necessary
+
+ gem_info
+ end
+
+ def fetch_spec(spec)
+ spec -= [nil, "ruby", ""]
+ contents = compact_index_client.spec(*spec)
+ return nil if contents.nil?
+ contents.unshift(spec.first)
+ contents[3].map! {|d| Gem::Dependency.new(*d) }
+ EndpointSpecification.new(*contents)
+ end
+ compact_index_request :fetch_spec
+
+ def available?
+ return nil unless SharedHelpers.md5_available?
+ user_home = Bundler.user_home
+ return nil unless user_home.directory? && user_home.writable?
+ # Read info file checksums out of /versions, so we can know if gems are up to date
+ fetch_uri.scheme != "file" && compact_index_client.update_and_parse_checksums!
+ rescue CompactIndexClient::Updater::MisMatchedChecksumError => e
+ Bundler.ui.debug(e.message)
+ nil
+ end
+ compact_index_request :available?
+
+ def api_fetcher?
+ true
+ end
+
+ private
+
+ def compact_index_client
+ @compact_index_client ||= begin
+ SharedHelpers.filesystem_access(cache_path) do
+ CompactIndexClient.new(cache_path, client_fetcher)
+ end.tap do |client|
+ client.in_parallel = lambda do |inputs, &blk|
+ func = lambda {|object, _index| blk.call(object) }
+ worker = bundle_worker(func)
+ inputs.each {|input| worker.enq(input) }
+ inputs.map { worker.deq }
+ end
+ end
+ end
+ end
+
+ def bundle_worker(func = nil)
+ @bundle_worker ||= begin
+ worker_name = "Compact Index (#{display_uri.host})"
+ Bundler::Worker.new(Bundler.current_ruby.rbx? ? 1 : 25, worker_name, func)
+ end
+ @bundle_worker.tap do |worker|
+ worker.instance_variable_set(:@func, func) if func
+ end
+ end
+
+ def cache_path
+ Bundler.user_cache.join("compact_index", remote.cache_slug)
+ end
+
+ def client_fetcher
+ ClientFetcher.new(self, Bundler.ui)
+ end
+
+ ClientFetcher = Struct.new(:fetcher, :ui) do
+ def call(path, headers)
+ fetcher.downloader.fetch(fetcher.fetch_uri + path, headers)
+ rescue NetworkDownError => e
+ raise unless Bundler.feature_flag.allow_offline_install? && headers["If-None-Match"]
+ ui.warn "Using the cached data for the new index because of a network error: #{e}"
+ Net::HTTPNotModified.new(nil, nil, nil)
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/fetcher/dependency.rb b/lib/bundler/fetcher/dependency.rb
new file mode 100644
index 00000000000000..1430d1ebeb9494
--- /dev/null
+++ b/lib/bundler/fetcher/dependency.rb
@@ -0,0 +1,82 @@
+# frozen_string_literal: true
+
+require "bundler/fetcher/base"
+require "cgi"
+
+module Bundler
+ class Fetcher
+ class Dependency < Base
+ def available?
+ @available ||= fetch_uri.scheme != "file" && downloader.fetch(dependency_api_uri)
+ rescue NetworkDownError => e
+ raise HTTPError, e.message
+ rescue AuthenticationRequiredError
+ # Fail since we got a 401 from the server.
+ raise
+ rescue HTTPError
+ false
+ end
+
+ def api_fetcher?
+ true
+ end
+
+ def specs(gem_names, full_dependency_list = [], last_spec_list = [])
+ query_list = gem_names.uniq - full_dependency_list
+
+ log_specs "Query List: #{query_list.inspect}"
+
+ return last_spec_list if query_list.empty?
+
+ spec_list, deps_list = Bundler::Retry.new("dependency api", FAIL_ERRORS).attempts do
+ dependency_specs(query_list)
+ end
+
+ returned_gems = spec_list.map(&:first).uniq
+ specs(deps_list, full_dependency_list + returned_gems, spec_list + last_spec_list)
+ rescue MarshalError
+ Bundler.ui.info "" unless Bundler.ui.debug? # new line now that the dots are over
+ Bundler.ui.debug "could not fetch from the dependency API, trying the full index"
+ nil
+ rescue HTTPError, GemspecError
+ Bundler.ui.info "" unless Bundler.ui.debug? # new line now that the dots are over
+ Bundler.ui.debug "could not fetch from the dependency API\nit's suggested to retry using the full index via `bundle install --full-index`"
+ nil
+ end
+
+ def dependency_specs(gem_names)
+ Bundler.ui.debug "Query Gemcutter Dependency Endpoint API: #{gem_names.join(",")}"
+
+ gem_list = unmarshalled_dep_gems(gem_names)
+ get_formatted_specs_and_deps(gem_list)
+ end
+
+ def unmarshalled_dep_gems(gem_names)
+ gem_list = []
+ gem_names.each_slice(Source::Rubygems::API_REQUEST_SIZE) do |names|
+ marshalled_deps = downloader.fetch(dependency_api_uri(names)).body
+ gem_list.concat(Bundler.load_marshal(marshalled_deps))
+ end
+ gem_list
+ end
+
+ def get_formatted_specs_and_deps(gem_list)
+ deps_list = []
+ spec_list = []
+
+ gem_list.each do |s|
+ deps_list.concat(s[:dependencies].map(&:first))
+ deps = s[:dependencies].map {|n, d| [n, d.split(", ")] }
+ spec_list.push([s[:name], s[:number], s[:platform], deps])
+ end
+ [spec_list, deps_list]
+ end
+
+ def dependency_api_uri(gem_names = [])
+ uri = fetch_uri + "api/v1/dependencies"
+ uri.query = "gems=#{CGI.escape(gem_names.sort.join(","))}" if gem_names.any?
+ uri
+ end
+ end
+ end
+end
diff --git a/lib/bundler/fetcher/downloader.rb b/lib/bundler/fetcher/downloader.rb
new file mode 100644
index 00000000000000..e0e0cbf1c9afb3
--- /dev/null
+++ b/lib/bundler/fetcher/downloader.rb
@@ -0,0 +1,84 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Fetcher
+ class Downloader
+ attr_reader :connection
+ attr_reader :redirect_limit
+
+ def initialize(connection, redirect_limit)
+ @connection = connection
+ @redirect_limit = redirect_limit
+ end
+
+ def fetch(uri, headers = {}, counter = 0)
+ raise HTTPError, "Too many redirects" if counter >= redirect_limit
+
+ response = request(uri, headers)
+ Bundler.ui.debug("HTTP #{response.code} #{response.message} #{uri}")
+
+ case response
+ when Net::HTTPSuccess, Net::HTTPNotModified
+ response
+ when Net::HTTPRedirection
+ new_uri = URI.parse(response["location"])
+ if new_uri.host == uri.host
+ new_uri.user = uri.user
+ new_uri.password = uri.password
+ end
+ fetch(new_uri, headers, counter + 1)
+ when Net::HTTPRequestedRangeNotSatisfiable
+ new_headers = headers.dup
+ new_headers.delete("Range")
+ new_headers["Accept-Encoding"] = "gzip"
+ fetch(uri, new_headers)
+ when Net::HTTPRequestEntityTooLarge
+ raise FallbackError, response.body
+ when Net::HTTPUnauthorized
+ raise AuthenticationRequiredError, uri.host
+ when Net::HTTPNotFound
+ raise FallbackError, "Net::HTTPNotFound"
+ else
+ raise HTTPError, "#{response.class}#{": #{response.body}" unless response.body.empty?}"
+ end
+ end
+
+ def request(uri, headers)
+ validate_uri_scheme!(uri)
+
+ Bundler.ui.debug "HTTP GET #{uri}"
+ req = Net::HTTP::Get.new uri.request_uri, headers
+ if uri.user
+ user = CGI.unescape(uri.user)
+ password = uri.password ? CGI.unescape(uri.password) : nil
+ req.basic_auth(user, password)
+ end
+ connection.request(uri, req)
+ rescue NoMethodError => e
+ raise unless ["undefined method", "use_ssl="].all? {|snippet| e.message.include? snippet }
+ raise LoadError.new("cannot load such file -- openssl")
+ rescue OpenSSL::SSL::SSLError
+ raise CertificateFailureError.new(uri)
+ rescue *HTTP_ERRORS => e
+ Bundler.ui.trace e
+ case e.message
+ when /host down:/, /getaddrinfo: nodename nor servname provided/
+ raise NetworkDownError, "Could not reach host #{uri.host}. Check your network " \
+ "connection and try again."
+ else
+ raise HTTPError, "Network error while fetching #{URICredentialsFilter.credential_filtered_uri(uri)}" \
+ " (#{e})"
+ end
+ end
+
+ private
+
+ def validate_uri_scheme!(uri)
+ return if uri.scheme =~ /\Ahttps?\z/
+ raise InvalidOption,
+ "The request uri `#{uri}` has an invalid scheme (`#{uri.scheme}`). " \
+ "Did you mean `http` or `https`?"
+ end
+ end
+ end
+end
diff --git a/lib/bundler/fetcher/index.rb b/lib/bundler/fetcher/index.rb
new file mode 100644
index 00000000000000..1a8064624d8046
--- /dev/null
+++ b/lib/bundler/fetcher/index.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+require "bundler/fetcher/base"
+require "rubygems/remote_fetcher"
+
+module Bundler
+ class Fetcher
+ class Index < Base
+ def specs(_gem_names)
+ Bundler.rubygems.fetch_all_remote_specs(remote)
+ rescue Gem::RemoteFetcher::FetchError, OpenSSL::SSL::SSLError, Net::HTTPFatalError => e
+ case e.message
+ when /certificate verify failed/
+ raise CertificateFailureError.new(display_uri)
+ when /401/
+ raise AuthenticationRequiredError, remote_uri
+ when /403/
+ raise BadAuthenticationError, remote_uri if remote_uri.userinfo
+ raise AuthenticationRequiredError, remote_uri
+ else
+ Bundler.ui.trace e
+ raise HTTPError, "Could not fetch specs from #{display_uri}"
+ end
+ end
+
+ def fetch_spec(spec)
+ spec -= [nil, "ruby", ""]
+ spec_file_name = "#{spec.join "-"}.gemspec"
+
+ uri = URI.parse("#{remote_uri}#{Gem::MARSHAL_SPEC_DIR}#{spec_file_name}.rz")
+ if uri.scheme == "file"
+ Bundler.load_marshal Bundler.rubygems.inflate(Gem.read_binary(uri.path))
+ elsif cached_spec_path = gemspec_cached_path(spec_file_name)
+ Bundler.load_gemspec(cached_spec_path)
+ else
+ Bundler.load_marshal Bundler.rubygems.inflate(downloader.fetch(uri).body)
+ end
+ rescue MarshalError
+ raise HTTPError, "Gemspec #{spec} contained invalid data.\n" \
+ "Your network or your gem server is probably having issues right now."
+ end
+
+ private
+
+ # cached gem specification path, if one exists
+ def gemspec_cached_path(spec_file_name)
+ paths = Bundler.rubygems.spec_cache_dirs.map {|dir| File.join(dir, spec_file_name) }
+ paths.find {|path| File.file? path }
+ end
+ end
+ end
+end
diff --git a/lib/bundler/friendly_errors.rb b/lib/bundler/friendly_errors.rb
new file mode 100644
index 00000000000000..ae3299a7c8c110
--- /dev/null
+++ b/lib/bundler/friendly_errors.rb
@@ -0,0 +1,131 @@
+# encoding: utf-8
+# frozen_string_literal: true
+
+require "cgi"
+require "bundler/vendored_thor"
+
+module Bundler
+ module FriendlyErrors
+ module_function
+
+ def log_error(error)
+ case error
+ when YamlSyntaxError
+ Bundler.ui.error error.message
+ Bundler.ui.trace error.orig_exception
+ when Dsl::DSLError, GemspecError
+ Bundler.ui.error error.message
+ when GemRequireError
+ Bundler.ui.error error.message
+ Bundler.ui.trace error.orig_exception, nil, true
+ when BundlerError
+ Bundler.ui.error error.message, :wrap => true
+ Bundler.ui.trace error
+ when Thor::Error
+ Bundler.ui.error error.message
+ when LoadError
+ raise error unless error.message =~ /cannot load such file -- openssl|openssl.so|libcrypto.so/
+ Bundler.ui.error "\nCould not load OpenSSL."
+ Bundler.ui.warn <<-WARN, :wrap => true
+ You must recompile Ruby with OpenSSL support or change the sources in your \
+ Gemfile from 'https' to 'http'. Instructions for compiling with OpenSSL \
+ using RVM are available at http://rvm.io/packages/openssl.
+ WARN
+ Bundler.ui.trace error
+ when Interrupt
+ Bundler.ui.error "\nQuitting..."
+ Bundler.ui.trace error
+ when Gem::InvalidSpecificationException
+ Bundler.ui.error error.message, :wrap => true
+ when SystemExit
+ when *[defined?(Java::JavaLang::OutOfMemoryError) && Java::JavaLang::OutOfMemoryError].compact
+ Bundler.ui.error "\nYour JVM has run out of memory, and Bundler cannot continue. " \
+ "You can decrease the amount of memory Bundler needs by removing gems from your Gemfile, " \
+ "especially large gems. (Gems can be as large as hundreds of megabytes, and Bundler has to read those files!). " \
+ "Alternatively, you can increase the amount of memory the JVM is able to use by running Bundler with jruby -J-Xmx1024m -S bundle (JRuby defaults to 500MB)."
+ else request_issue_report_for(error)
+ end
+ rescue
+ raise error
+ end
+
+ def exit_status(error)
+ case error
+ when BundlerError then error.status_code
+ when Thor::Error then 15
+ when SystemExit then error.status
+ else 1
+ end
+ end
+
+ def request_issue_report_for(e)
+ Bundler.ui.info <<-EOS.gsub(/^ {8}/, "")
+ --- ERROR REPORT TEMPLATE -------------------------------------------------------
+ # Error Report
+
+ ## Questions
+
+ Please fill out answers to these questions, it'll help us figure out
+ why things are going wrong.
+
+ - **What did you do?**
+
+ I ran the command `#{$PROGRAM_NAME} #{ARGV.join(" ")}`
+
+ - **What did you expect to happen?**
+
+ I expected Bundler to...
+
+ - **What happened instead?**
+
+ Instead, what happened was...
+
+ - **Have you tried any solutions posted on similar issues in our issue tracker, stack overflow, or google?**
+
+ I tried...
+
+ - **Have you read our issues document, https://github.com/bundler/bundler/blob/master/doc/contributing/ISSUES.md?**
+
+ ...
+
+ ## Backtrace
+
+ ```
+ #{e.class}: #{e.message}
+ #{e.backtrace && e.backtrace.join("\n ").chomp}
+ ```
+
+ #{Bundler::Env.report}
+ --- TEMPLATE END ----------------------------------------------------------------
+
+ EOS
+
+ Bundler.ui.error "Unfortunately, an unexpected error occurred, and Bundler cannot continue."
+
+ Bundler.ui.warn <<-EOS.gsub(/^ {8}/, "")
+
+ First, try this link to see if there are any existing issue reports for this error:
+ #{issues_url(e)}
+
+ If there aren't any reports for this error yet, please create copy and paste the report template above into a new issue. Don't forget to anonymize any private data! The new issue form is located at:
+ https://github.com/bundler/bundler/issues/new
+ EOS
+ end
+
+ def issues_url(exception)
+ message = exception.message.lines.first.tr(":", " ").chomp
+ message = message.split("-").first if exception.is_a?(Errno)
+ "https://github.com/bundler/bundler/search?q=" \
+ "#{CGI.escape(message)}&type=Issues"
+ end
+ end
+
+ def self.with_friendly_errors
+ yield
+ rescue SignalException
+ raise
+ rescue Exception => e
+ FriendlyErrors.log_error(e)
+ exit FriendlyErrors.exit_status(e)
+ end
+end
diff --git a/lib/bundler/gem_helper.rb b/lib/bundler/gem_helper.rb
new file mode 100644
index 00000000000000..e7673cba88347e
--- /dev/null
+++ b/lib/bundler/gem_helper.rb
@@ -0,0 +1,202 @@
+# frozen_string_literal: true
+
+require "bundler/vendored_thor" unless defined?(Thor)
+require "bundler"
+
+module Bundler
+ class GemHelper
+ include Rake::DSL if defined? Rake::DSL
+
+ class << self
+ # set when install'd.
+ attr_accessor :instance
+
+ def install_tasks(opts = {})
+ new(opts[:dir], opts[:name]).install
+ end
+
+ def gemspec(&block)
+ gemspec = instance.gemspec
+ block.call(gemspec) if block
+ gemspec
+ end
+ end
+
+ attr_reader :spec_path, :base, :gemspec
+
+ def initialize(base = nil, name = nil)
+ Bundler.ui = UI::Shell.new
+ @base = (base ||= SharedHelpers.pwd)
+ gemspecs = name ? [File.join(base, "#{name}.gemspec")] : Dir[File.join(base, "{,*}.gemspec")]
+ raise "Unable to determine name from existing gemspec. Use :name => 'gemname' in #install_tasks to manually set it." unless gemspecs.size == 1
+ @spec_path = gemspecs.first
+ @gemspec = Bundler.load_gemspec(@spec_path)
+ end
+
+ def install
+ built_gem_path = nil
+
+ desc "Build #{name}-#{version}.gem into the pkg directory."
+ task "build" do
+ built_gem_path = build_gem
+ end
+
+ desc "Build and install #{name}-#{version}.gem into system gems."
+ task "install" => "build" do
+ install_gem(built_gem_path)
+ end
+
+ desc "Build and install #{name}-#{version}.gem into system gems without network access."
+ task "install:local" => "build" do
+ install_gem(built_gem_path, :local)
+ end
+
+ desc "Create tag #{version_tag} and build and push #{name}-#{version}.gem to #{gem_push_host}\n" \
+ "To prevent publishing in RubyGems use `gem_push=no rake release`"
+ task "release", [:remote] => ["build", "release:guard_clean",
+ "release:source_control_push", "release:rubygem_push"] do
+ end
+
+ task "release:guard_clean" do
+ guard_clean
+ end
+
+ task "release:source_control_push", [:remote] do |_, args|
+ tag_version { git_push(args[:remote]) } unless already_tagged?
+ end
+
+ task "release:rubygem_push" do
+ rubygem_push(built_gem_path) if gem_push?
+ end
+
+ GemHelper.instance = self
+ end
+
+ def build_gem
+ file_name = nil
+ sh("gem build -V '#{spec_path}'") do
+ file_name = File.basename(built_gem_path)
+ SharedHelpers.filesystem_access(File.join(base, "pkg")) {|p| FileUtils.mkdir_p(p) }
+ FileUtils.mv(built_gem_path, "pkg")
+ Bundler.ui.confirm "#{name} #{version} built to pkg/#{file_name}."
+ end
+ File.join(base, "pkg", file_name)
+ end
+
+ def install_gem(built_gem_path = nil, local = false)
+ built_gem_path ||= build_gem
+ out, _ = sh_with_code("gem install '#{built_gem_path}'#{" --local" if local}")
+ raise "Couldn't install gem, run `gem install #{built_gem_path}' for more detailed output" unless out[/Successfully installed/]
+ Bundler.ui.confirm "#{name} (#{version}) installed."
+ end
+
+ protected
+
+ def rubygem_push(path)
+ gem_command = "gem push '#{path}'"
+ gem_command += " --key #{gem_key}" if gem_key
+ gem_command += " --host #{allowed_push_host}" if allowed_push_host
+ unless allowed_push_host || Bundler.user_home.join(".gem/credentials").file?
+ raise "Your rubygems.org credentials aren't set. Run `gem push` to set them."
+ end
+ sh(gem_command)
+ Bundler.ui.confirm "Pushed #{name} #{version} to #{gem_push_host}"
+ end
+
+ def built_gem_path
+ Dir[File.join(base, "#{name}-*.gem")].sort_by {|f| File.mtime(f) }.last
+ end
+
+ def git_push(remote = "")
+ perform_git_push remote
+ perform_git_push "#{remote} --tags"
+ Bundler.ui.confirm "Pushed git commits and tags."
+ end
+
+ def allowed_push_host
+ @gemspec.metadata["allowed_push_host"] if @gemspec.respond_to?(:metadata)
+ end
+
+ def gem_push_host
+ env_rubygems_host = ENV["RUBYGEMS_HOST"]
+ env_rubygems_host = nil if
+ env_rubygems_host && env_rubygems_host.empty?
+
+ allowed_push_host || env_rubygems_host || "rubygems.org"
+ end
+
+ def perform_git_push(options = "")
+ cmd = "git push #{options}"
+ out, code = sh_with_code(cmd)
+ raise "Couldn't git push. `#{cmd}' failed with the following output:\n\n#{out}\n" unless code == 0
+ end
+
+ def already_tagged?
+ return false unless sh("git tag").split(/\n/).include?(version_tag)
+ Bundler.ui.confirm "Tag #{version_tag} has already been created."
+ true
+ end
+
+ def guard_clean
+ clean? && committed? || raise("There are files that need to be committed first.")
+ end
+
+ def clean?
+ sh_with_code("git diff --exit-code")[1] == 0
+ end
+
+ def committed?
+ sh_with_code("git diff-index --quiet --cached HEAD")[1] == 0
+ end
+
+ def tag_version
+ sh "git tag -m \"Version #{version}\" #{version_tag}"
+ Bundler.ui.confirm "Tagged #{version_tag}."
+ yield if block_given?
+ rescue RuntimeError
+ Bundler.ui.error "Untagging #{version_tag} due to error."
+ sh_with_code "git tag -d #{version_tag}"
+ raise
+ end
+
+ def version
+ gemspec.version
+ end
+
+ def version_tag
+ "v#{version}"
+ end
+
+ def name
+ gemspec.name
+ end
+
+ def sh(cmd, &block)
+ out, code = sh_with_code(cmd, &block)
+ unless code.zero?
+ raise(out.empty? ? "Running `#{cmd}` failed. Run this command directly for more detailed output." : out)
+ end
+ out
+ end
+
+ def sh_with_code(cmd, &block)
+ cmd += " 2>&1"
+ outbuf = String.new
+ Bundler.ui.debug(cmd)
+ SharedHelpers.chdir(base) do
+ outbuf = `#{cmd}`
+ status = $?.exitstatus
+ block.call(outbuf) if status.zero? && block
+ [outbuf, status]
+ end
+ end
+
+ def gem_key
+ Bundler.settings["gem.push_key"].to_s.downcase if Bundler.settings["gem.push_key"]
+ end
+
+ def gem_push?
+ !%w[n no nil false off 0].include?(ENV["gem_push"].to_s.downcase)
+ end
+ end
+end
diff --git a/lib/bundler/gem_helpers.rb b/lib/bundler/gem_helpers.rb
new file mode 100644
index 00000000000000..019ae10c66cb67
--- /dev/null
+++ b/lib/bundler/gem_helpers.rb
@@ -0,0 +1,101 @@
+# frozen_string_literal: true
+
+module Bundler
+ module GemHelpers
+ GENERIC_CACHE = {} # rubocop:disable MutableConstant
+ GENERICS = [
+ [Gem::Platform.new("java"), Gem::Platform.new("java")],
+ [Gem::Platform.new("mswin32"), Gem::Platform.new("mswin32")],
+ [Gem::Platform.new("mswin64"), Gem::Platform.new("mswin64")],
+ [Gem::Platform.new("universal-mingw32"), Gem::Platform.new("universal-mingw32")],
+ [Gem::Platform.new("x64-mingw32"), Gem::Platform.new("x64-mingw32")],
+ [Gem::Platform.new("x86_64-mingw32"), Gem::Platform.new("x64-mingw32")],
+ [Gem::Platform.new("mingw32"), Gem::Platform.new("x86-mingw32")]
+ ].freeze
+
+ def generic(p)
+ return p if p == Gem::Platform::RUBY
+
+ GENERIC_CACHE[p] ||= begin
+ _, found = GENERICS.find do |match, _generic|
+ p.os == match.os && (!match.cpu || p.cpu == match.cpu)
+ end
+ found || Gem::Platform::RUBY
+ end
+ end
+ module_function :generic
+
+ def generic_local_platform
+ generic(Bundler.local_platform)
+ end
+ module_function :generic_local_platform
+
+ def platform_specificity_match(spec_platform, user_platform)
+ spec_platform = Gem::Platform.new(spec_platform)
+ return PlatformMatch::EXACT_MATCH if spec_platform == user_platform
+ return PlatformMatch::WORST_MATCH if spec_platform.nil? || spec_platform == Gem::Platform::RUBY || user_platform == Gem::Platform::RUBY
+
+ PlatformMatch.new(
+ PlatformMatch.os_match(spec_platform, user_platform),
+ PlatformMatch.cpu_match(spec_platform, user_platform),
+ PlatformMatch.platform_version_match(spec_platform, user_platform)
+ )
+ end
+ module_function :platform_specificity_match
+
+ def select_best_platform_match(specs, platform)
+ specs.select {|spec| spec.match_platform(platform) }.
+ min_by {|spec| platform_specificity_match(spec.platform, platform) }
+ end
+ module_function :select_best_platform_match
+
+ PlatformMatch = Struct.new(:os_match, :cpu_match, :platform_version_match)
+ class PlatformMatch
+ def <=>(other)
+ return nil unless other.is_a?(PlatformMatch)
+
+ m = os_match <=> other.os_match
+ return m unless m.zero?
+
+ m = cpu_match <=> other.cpu_match
+ return m unless m.zero?
+
+ m = platform_version_match <=> other.platform_version_match
+ m
+ end
+
+ EXACT_MATCH = new(-1, -1, -1).freeze
+ WORST_MATCH = new(1_000_000, 1_000_000, 1_000_000).freeze
+
+ def self.os_match(spec_platform, user_platform)
+ if spec_platform.os == user_platform.os
+ 0
+ else
+ 1
+ end
+ end
+
+ def self.cpu_match(spec_platform, user_platform)
+ if spec_platform.cpu == user_platform.cpu
+ 0
+ elsif spec_platform.cpu == "arm" && user_platform.cpu.to_s.start_with?("arm")
+ 0
+ elsif spec_platform.cpu.nil? || spec_platform.cpu == "universal"
+ 1
+ else
+ 2
+ end
+ end
+
+ def self.platform_version_match(spec_platform, user_platform)
+ if spec_platform.version == user_platform.version
+ 0
+ elsif spec_platform.version.nil?
+ 1
+ else
+ 2
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/gem_remote_fetcher.rb b/lib/bundler/gem_remote_fetcher.rb
new file mode 100644
index 00000000000000..9577535d63c71e
--- /dev/null
+++ b/lib/bundler/gem_remote_fetcher.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+require "rubygems/remote_fetcher"
+
+module Bundler
+ # Adds support for setting custom HTTP headers when fetching gems from the
+ # server.
+ #
+ # TODO: Get rid of this when and if gemstash only supports RubyGems versions
+ # that contain https://github.com/rubygems/rubygems/commit/3db265cc20b2f813.
+ class GemRemoteFetcher < Gem::RemoteFetcher
+ attr_accessor :headers
+
+ # Extracted from RubyGems 2.4.
+ def fetch_http(uri, last_modified = nil, head = false, depth = 0)
+ fetch_type = head ? Net::HTTP::Head : Net::HTTP::Get
+ # beginning of change
+ response = request uri, fetch_type, last_modified do |req|
+ headers.each {|k, v| req.add_field(k, v) } if headers
+ end
+ # end of change
+
+ case response
+ when Net::HTTPOK, Net::HTTPNotModified then
+ response.uri = uri if response.respond_to? :uri
+ head ? response : response.body
+ when Net::HTTPMovedPermanently, Net::HTTPFound, Net::HTTPSeeOther,
+ Net::HTTPTemporaryRedirect then
+ raise FetchError.new("too many redirects", uri) if depth > 10
+
+ location = URI.parse response["Location"]
+
+ if https?(uri) && !https?(location)
+ raise FetchError.new("redirecting to non-https resource: #{location}", uri)
+ end
+
+ fetch_http(location, last_modified, head, depth + 1)
+ else
+ raise FetchError.new("bad response #{response.message} #{response.code}", uri)
+ end
+ end
+ end
+end
diff --git a/lib/bundler/gem_tasks.rb b/lib/bundler/gem_tasks.rb
new file mode 100644
index 00000000000000..f736517bd76a48
--- /dev/null
+++ b/lib/bundler/gem_tasks.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+require "rake/clean"
+CLOBBER.include "pkg"
+
+require "bundler/gem_helper"
+Bundler::GemHelper.install_tasks
diff --git a/lib/bundler/gem_version_promoter.rb b/lib/bundler/gem_version_promoter.rb
new file mode 100644
index 00000000000000..adb951a7a0cc1d
--- /dev/null
+++ b/lib/bundler/gem_version_promoter.rb
@@ -0,0 +1,190 @@
+# frozen_string_literal: true
+
+module Bundler
+ # This class contains all of the logic for determining the next version of a
+ # Gem to update to based on the requested level (patch, minor, major).
+ # Primarily designed to work with Resolver which will provide it the list of
+ # available dependency versions as found in its index, before returning it to
+ # to the resolution engine to select the best version.
+ class GemVersionPromoter
+ DEBUG = ENV["DEBUG_RESOLVER"]
+
+ attr_reader :level, :locked_specs, :unlock_gems
+
+ # By default, strict is false, meaning every available version of a gem
+ # is returned from sort_versions. The order gives preference to the
+ # requested level (:patch, :minor, :major) but in complicated requirement
+ # cases some gems will by necessity by promoted past the requested level,
+ # or even reverted to older versions.
+ #
+ # If strict is set to true, the results from sort_versions will be
+ # truncated, eliminating any version outside the current level scope.
+ # This can lead to unexpected outcomes or even VersionConflict exceptions
+ # that report a version of a gem not existing for versions that indeed do
+ # existing in the referenced source.
+ attr_accessor :strict
+
+ attr_accessor :prerelease_specified
+
+ # Given a list of locked_specs and a list of gems to unlock creates a
+ # GemVersionPromoter instance.
+ #
+ # @param locked_specs [SpecSet] All current locked specs. Unlike Definition
+ # where this list is empty if all gems are being updated, this should
+ # always be populated for all gems so this class can properly function.
+ # @param unlock_gems [String] List of gem names being unlocked. If empty,
+ # all gems will be considered unlocked.
+ # @return [GemVersionPromoter]
+ def initialize(locked_specs = SpecSet.new([]), unlock_gems = [])
+ @level = :major
+ @strict = false
+ @locked_specs = locked_specs
+ @unlock_gems = unlock_gems
+ @sort_versions = {}
+ @prerelease_specified = {}
+ end
+
+ # @param value [Symbol] One of three Symbols: :major, :minor or :patch.
+ def level=(value)
+ v = case value
+ when String, Symbol
+ value.to_sym
+ end
+
+ raise ArgumentError, "Unexpected level #{v}. Must be :major, :minor or :patch" unless [:major, :minor, :patch].include?(v)
+ @level = v
+ end
+
+ # Given a Dependency and an Array of SpecGroups of available versions for a
+ # gem, this method will return the Array of SpecGroups sorted (and possibly
+ # truncated if strict is true) in an order to give preference to the current
+ # level (:major, :minor or :patch) when resolution is deciding what versions
+ # best resolve all dependencies in the bundle.
+ # @param dep [Dependency] The Dependency of the gem.
+ # @param spec_groups [SpecGroup] An array of SpecGroups for the same gem
+ # named in the @dep param.
+ # @return [SpecGroup] A new instance of the SpecGroup Array sorted and
+ # possibly filtered.
+ def sort_versions(dep, spec_groups)
+ before_result = "before sort_versions: #{debug_format_result(dep, spec_groups).inspect}" if DEBUG
+
+ @sort_versions[dep] ||= begin
+ gem_name = dep.name
+
+ # An Array per version returned, different entries for different platforms.
+ # We only need the version here so it's ok to hard code this to the first instance.
+ locked_spec = locked_specs[gem_name].first
+
+ if strict
+ filter_dep_specs(spec_groups, locked_spec)
+ else
+ sort_dep_specs(spec_groups, locked_spec)
+ end.tap do |specs|
+ if DEBUG
+ STDERR.puts before_result
+ STDERR.puts " after sort_versions: #{debug_format_result(dep, specs).inspect}"
+ end
+ end
+ end
+ end
+
+ # @return [bool] Convenience method for testing value of level variable.
+ def major?
+ level == :major
+ end
+
+ # @return [bool] Convenience method for testing value of level variable.
+ def minor?
+ level == :minor
+ end
+
+ private
+
+ def filter_dep_specs(spec_groups, locked_spec)
+ res = spec_groups.select do |spec_group|
+ if locked_spec && !major?
+ gsv = spec_group.version
+ lsv = locked_spec.version
+
+ must_match = minor? ? [0] : [0, 1]
+
+ matches = must_match.map {|idx| gsv.segments[idx] == lsv.segments[idx] }
+ (matches.uniq == [true]) ? (gsv >= lsv) : false
+ else
+ true
+ end
+ end
+
+ sort_dep_specs(res, locked_spec)
+ end
+
+ def sort_dep_specs(spec_groups, locked_spec)
+ return spec_groups unless locked_spec
+ @gem_name = locked_spec.name
+ @locked_version = locked_spec.version
+
+ result = spec_groups.sort do |a, b|
+ @a_ver = a.version
+ @b_ver = b.version
+
+ unless @prerelease_specified[@gem_name]
+ a_pre = @a_ver.prerelease?
+ b_pre = @b_ver.prerelease?
+
+ next -1 if a_pre && !b_pre
+ next 1 if b_pre && !a_pre
+ end
+
+ if major?
+ @a_ver <=> @b_ver
+ elsif either_version_older_than_locked
+ @a_ver <=> @b_ver
+ elsif segments_do_not_match(:major)
+ @b_ver <=> @a_ver
+ elsif !minor? && segments_do_not_match(:minor)
+ @b_ver <=> @a_ver
+ else
+ @a_ver <=> @b_ver
+ end
+ end
+ post_sort(result)
+ end
+
+ def either_version_older_than_locked
+ @a_ver < @locked_version || @b_ver < @locked_version
+ end
+
+ def segments_do_not_match(level)
+ index = [:major, :minor].index(level)
+ @a_ver.segments[index] != @b_ver.segments[index]
+ end
+
+ def unlocking_gem?
+ unlock_gems.empty? || unlock_gems.include?(@gem_name)
+ end
+
+ # Specific version moves can't always reliably be done during sorting
+ # as not all elements are compared against each other.
+ def post_sort(result)
+ # default :major behavior in Bundler does not do this
+ return result if major?
+ if unlocking_gem?
+ result
+ else
+ move_version_to_end(result, @locked_version)
+ end
+ end
+
+ def move_version_to_end(result, version)
+ move, keep = result.partition {|s| s.version.to_s == version.to_s }
+ keep.concat(move)
+ end
+
+ def debug_format_result(dep, spec_groups)
+ a = [dep.to_s,
+ spec_groups.map {|sg| [sg.version, sg.dependencies_for_activated_platforms.map {|dp| [dp.name, dp.requirement.to_s] }] }]
+ last_map = a.last.map {|sg_data| [sg_data.first.version, sg_data.last.map {|aa| aa.join(" ") }] }
+ [a.first, last_map, level, strict ? :strict : :not_strict]
+ end
+ end
+end
diff --git a/lib/bundler/gemdeps.rb b/lib/bundler/gemdeps.rb
new file mode 100644
index 00000000000000..cd4b25d0e698f1
--- /dev/null
+++ b/lib/bundler/gemdeps.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Gemdeps
+ def initialize(runtime)
+ @runtime = runtime
+ end
+
+ def requested_specs
+ @runtime.requested_specs
+ end
+
+ def specs
+ @runtime.specs
+ end
+
+ def dependencies
+ @runtime.dependencies
+ end
+
+ def current_dependencies
+ @runtime.current_dependencies
+ end
+
+ def requires
+ @runtime.requires
+ end
+ end
+end
diff --git a/lib/bundler/graph.rb b/lib/bundler/graph.rb
new file mode 100644
index 00000000000000..de6bba021484ce
--- /dev/null
+++ b/lib/bundler/graph.rb
@@ -0,0 +1,152 @@
+# frozen_string_literal: true
+
+require "set"
+module Bundler
+ class Graph
+ GRAPH_NAME = :Gemfile
+
+ def initialize(env, output_file, show_version = false, show_requirements = false, output_format = "png", without = [])
+ @env = env
+ @output_file = output_file
+ @show_version = show_version
+ @show_requirements = show_requirements
+ @output_format = output_format
+ @without_groups = without.map(&:to_sym)
+
+ @groups = []
+ @relations = Hash.new {|h, k| h[k] = Set.new }
+ @node_options = {}
+ @edge_options = {}
+
+ _populate_relations
+ end
+
+ attr_reader :groups, :relations, :node_options, :edge_options, :output_file, :output_format
+
+ def viz
+ GraphVizClient.new(self).run
+ end
+
+ private
+
+ def _populate_relations
+ parent_dependencies = _groups.values.to_set.flatten
+ loop do
+ break if parent_dependencies.empty?
+
+ tmp = Set.new
+ parent_dependencies.each do |dependency|
+ child_dependencies = spec_for_dependency(dependency).runtime_dependencies.to_set
+ @relations[dependency.name] += child_dependencies.map(&:name).to_set
+ tmp += child_dependencies
+
+ @node_options[dependency.name] = _make_label(dependency, :node)
+ child_dependencies.each do |c_dependency|
+ @edge_options["#{dependency.name}_#{c_dependency.name}"] = _make_label(c_dependency, :edge)
+ end
+ end
+ parent_dependencies = tmp
+ end
+ end
+
+ def _groups
+ relations = Hash.new {|h, k| h[k] = Set.new }
+ @env.current_dependencies.each do |dependency|
+ dependency.groups.each do |group|
+ next if @without_groups.include?(group)
+
+ relations[group.to_s].add(dependency)
+ @relations[group.to_s].add(dependency.name)
+
+ @node_options[group.to_s] ||= _make_label(group, :node)
+ @edge_options["#{group}_#{dependency.name}"] = _make_label(dependency, :edge)
+ end
+ end
+ @groups = relations.keys
+ relations
+ end
+
+ def _make_label(symbol_or_string_or_dependency, element_type)
+ case element_type.to_sym
+ when :node
+ if symbol_or_string_or_dependency.is_a?(Gem::Dependency)
+ label = symbol_or_string_or_dependency.name.dup
+ label << "\n#{spec_for_dependency(symbol_or_string_or_dependency).version}" if @show_version
+ else
+ label = symbol_or_string_or_dependency.to_s
+ end
+ when :edge
+ label = nil
+ if symbol_or_string_or_dependency.respond_to?(:requirements_list) && @show_requirements
+ tmp = symbol_or_string_or_dependency.requirements_list.join(", ")
+ label = tmp if tmp != ">= 0"
+ end
+ else
+ raise ArgumentError, "2nd argument is invalid"
+ end
+ label.nil? ? {} : { :label => label }
+ end
+
+ def spec_for_dependency(dependency)
+ @env.requested_specs.find {|s| s.name == dependency.name }
+ end
+
+ class GraphVizClient
+ def initialize(graph_instance)
+ @graph_name = graph_instance.class::GRAPH_NAME
+ @groups = graph_instance.groups
+ @relations = graph_instance.relations
+ @node_options = graph_instance.node_options
+ @edge_options = graph_instance.edge_options
+ @output_file = graph_instance.output_file
+ @output_format = graph_instance.output_format
+ end
+
+ def g
+ @g ||= ::GraphViz.digraph(@graph_name, :concentrate => true, :normalize => true, :nodesep => 0.55) do |g|
+ g.edge[:weight] = 2
+ g.edge[:fontname] = g.node[:fontname] = "Arial, Helvetica, SansSerif"
+ g.edge[:fontsize] = 12
+ end
+ end
+
+ def run
+ @groups.each do |group|
+ g.add_nodes(
+ group, {
+ :style => "filled",
+ :fillcolor => "#B9B9D5",
+ :shape => "box3d",
+ :fontsize => 16
+ }.merge(@node_options[group])
+ )
+ end
+
+ @relations.each do |parent, children|
+ children.each do |child|
+ if @groups.include?(parent)
+ g.add_nodes(child, { :style => "filled", :fillcolor => "#B9B9D5" }.merge(@node_options[child]))
+ g.add_edges(parent, child, { :constraint => false }.merge(@edge_options["#{parent}_#{child}"]))
+ else
+ g.add_nodes(child, @node_options[child])
+ g.add_edges(parent, child, @edge_options["#{parent}_#{child}"])
+ end
+ end
+ end
+
+ if @output_format.to_s == "debug"
+ $stdout.puts g.output :none => String
+ Bundler.ui.info "debugging bundle viz..."
+ else
+ begin
+ g.output @output_format.to_sym => "#{@output_file}.#{@output_format}"
+ Bundler.ui.info "#{@output_file}.#{@output_format}"
+ rescue ArgumentError => e
+ $stderr.puts "Unsupported output format. See Ruby-Graphviz/lib/graphviz/constants.rb"
+ raise e
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/index.rb b/lib/bundler/index.rb
new file mode 100644
index 00000000000000..9166a927388fa6
--- /dev/null
+++ b/lib/bundler/index.rb
@@ -0,0 +1,213 @@
+# frozen_string_literal: true
+
+require "set"
+
+module Bundler
+ class Index
+ include Enumerable
+
+ def self.build
+ i = new
+ yield i
+ i
+ end
+
+ attr_reader :specs, :all_specs, :sources
+ protected :specs, :all_specs
+
+ RUBY = "ruby".freeze
+ NULL = "\0".freeze
+
+ def initialize
+ @sources = []
+ @cache = {}
+ @specs = Hash.new {|h, k| h[k] = {} }
+ @all_specs = Hash.new {|h, k| h[k] = EMPTY_SEARCH }
+ end
+
+ def initialize_copy(o)
+ @sources = o.sources.dup
+ @cache = {}
+ @specs = Hash.new {|h, k| h[k] = {} }
+ @all_specs = Hash.new {|h, k| h[k] = EMPTY_SEARCH }
+
+ o.specs.each do |name, hash|
+ @specs[name] = hash.dup
+ end
+ o.all_specs.each do |name, array|
+ @all_specs[name] = array.dup
+ end
+ end
+
+ def inspect
+ "#<#{self.class}:0x#{object_id} sources=#{sources.map(&:inspect)} specs.size=#{specs.size}>"
+ end
+
+ def empty?
+ each { return false }
+ true
+ end
+
+ def search_all(name)
+ all_matches = local_search(name) + @all_specs[name]
+ @sources.each do |source|
+ all_matches.concat(source.search_all(name))
+ end
+ all_matches
+ end
+
+ # Search this index's specs, and any source indexes that this index knows
+ # about, returning all of the results.
+ def search(query, base = nil)
+ sort_specs(unsorted_search(query, base))
+ end
+
+ def unsorted_search(query, base)
+ results = local_search(query, base)
+
+ seen = results.map(&:full_name).to_set unless @sources.empty?
+
+ @sources.each do |source|
+ source.unsorted_search(query, base).each do |spec|
+ results << spec if seen.add?(spec.full_name)
+ end
+ end
+
+ results
+ end
+ protected :unsorted_search
+
+ def self.sort_specs(specs)
+ specs.sort_by do |s|
+ platform_string = s.platform.to_s
+ [s.version, platform_string == RUBY ? NULL : platform_string]
+ end
+ end
+
+ def sort_specs(specs)
+ self.class.sort_specs(specs)
+ end
+
+ def local_search(query, base = nil)
+ case query
+ when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification then search_by_spec(query)
+ when String then specs_by_name(query)
+ when Gem::Dependency then search_by_dependency(query, base)
+ when DepProxy then search_by_dependency(query.dep, base)
+ else
+ raise "You can't search for a #{query.inspect}."
+ end
+ end
+
+ alias_method :[], :search
+
+ def <<(spec)
+ @specs[spec.name][spec.full_name] = spec
+ spec
+ end
+
+ def each(&blk)
+ return enum_for(:each) unless blk
+ specs.values.each do |spec_sets|
+ spec_sets.values.each(&blk)
+ end
+ sources.each {|s| s.each(&blk) }
+ self
+ end
+
+ def spec_names
+ names = specs.keys + sources.map(&:spec_names)
+ names.uniq!
+ names
+ end
+
+ # returns a list of the dependencies
+ def unmet_dependency_names
+ dependency_names.select do |name|
+ name != "bundler" && search(name).empty?
+ end
+ end
+
+ def dependency_names
+ names = []
+ each do |spec|
+ spec.dependencies.each do |dep|
+ next if dep.type == :development
+ names << dep.name
+ end
+ end
+ names.uniq
+ end
+
+ def use(other, override_dupes = false)
+ return unless other
+ other.each do |s|
+ if (dupes = search_by_spec(s)) && !dupes.empty?
+ # safe to << since it's a new array when it has contents
+ @all_specs[s.name] = dupes << s
+ next unless override_dupes
+ end
+ self << s
+ end
+ self
+ end
+
+ def size
+ @sources.inject(@specs.size) do |size, source|
+ size += source.size
+ end
+ end
+
+ # Whether all the specs in self are in other
+ # TODO: rename to #include?
+ def ==(other)
+ all? do |spec|
+ other_spec = other[spec].first
+ other_spec && dependencies_eql?(spec, other_spec) && spec.source == other_spec.source
+ end
+ end
+
+ def dependencies_eql?(spec, other_spec)
+ deps = spec.dependencies.select {|d| d.type != :development }
+ other_deps = other_spec.dependencies.select {|d| d.type != :development }
+ Set.new(deps) == Set.new(other_deps)
+ end
+
+ def add_source(index)
+ raise ArgumentError, "Source must be an index, not #{index.class}" unless index.is_a?(Index)
+ @sources << index
+ @sources.uniq! # need to use uniq! here instead of checking for the item before adding
+ end
+
+ private
+
+ def specs_by_name(name)
+ @specs[name].values
+ end
+
+ def search_by_dependency(dependency, base = nil)
+ @cache[base || false] ||= {}
+ @cache[base || false][dependency] ||= begin
+ specs = specs_by_name(dependency.name)
+ specs += base if base
+ found = specs.select do |spec|
+ next true if spec.source.is_a?(Source::Gemspec)
+ if base # allow all platforms when searching from a lockfile
+ dependency.matches_spec?(spec)
+ else
+ dependency.matches_spec?(spec) && Gem::Platform.match(spec.platform)
+ end
+ end
+
+ found
+ end
+ end
+
+ EMPTY_SEARCH = [].freeze
+
+ def search_by_spec(spec)
+ spec = @specs[spec.name][spec.full_name]
+ spec ? [spec] : EMPTY_SEARCH
+ end
+ end
+end
diff --git a/lib/bundler/injector.rb b/lib/bundler/injector.rb
new file mode 100644
index 00000000000000..1bb29f0b3673a2
--- /dev/null
+++ b/lib/bundler/injector.rb
@@ -0,0 +1,253 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Injector
+ INJECTED_GEMS = "injected gems".freeze
+
+ def self.inject(new_deps, options = {})
+ injector = new(new_deps, options)
+ injector.inject(Bundler.default_gemfile, Bundler.default_lockfile)
+ end
+
+ def self.remove(gems, options = {})
+ injector = new(gems, options)
+ injector.remove(Bundler.default_gemfile, Bundler.default_lockfile)
+ end
+
+ def initialize(deps, options = {})
+ @deps = deps
+ @options = options
+ end
+
+ # @param [Pathname] gemfile_path The Gemfile in which to inject the new dependency.
+ # @param [Pathname] lockfile_path The lockfile in which to inject the new dependency.
+ # @return [Array]
+ def inject(gemfile_path, lockfile_path)
+ if Bundler.frozen_bundle?
+ # ensure the lock and Gemfile are synced
+ Bundler.definition.ensure_equivalent_gemfile_and_lockfile(true)
+ end
+
+ # temporarily unfreeze
+ Bundler.settings.temporary(:deployment => false, :frozen => false) do
+ # evaluate the Gemfile we have now
+ builder = Dsl.new
+ builder.eval_gemfile(gemfile_path)
+
+ # don't inject any gems that are already in the Gemfile
+ @deps -= builder.dependencies
+
+ # add new deps to the end of the in-memory Gemfile
+ # Set conservative versioning to false because
+ # we want to let the resolver resolve the version first
+ builder.eval_gemfile(INJECTED_GEMS, build_gem_lines(false)) if @deps.any?
+
+ # resolve to see if the new deps broke anything
+ @definition = builder.to_definition(lockfile_path, {})
+ @definition.resolve_remotely!
+
+ # since nothing broke, we can add those gems to the gemfile
+ append_to(gemfile_path, build_gem_lines(@options[:conservative_versioning])) if @deps.any?
+
+ # since we resolved successfully, write out the lockfile
+ @definition.lock(Bundler.default_lockfile)
+
+ # invalidate the cached Bundler.definition
+ Bundler.reset_paths!
+
+ # return an array of the deps that we added
+ @deps
+ end
+ end
+
+ # @param [Pathname] gemfile_path The Gemfile from which to remove dependencies.
+ # @param [Pathname] lockfile_path The lockfile from which to remove dependencies.
+ # @return [Array]
+ def remove(gemfile_path, lockfile_path)
+ # remove gems from each gemfiles we have
+ Bundler.definition.gemfiles.each do |path|
+ deps = remove_deps(path)
+
+ show_warning("No gems were removed from the gemfile.") if deps.empty?
+
+ deps.each {|dep| Bundler.ui.confirm "#{SharedHelpers.pretty_dependency(dep, false)} was removed." }
+ end
+ end
+
+ private
+
+ def conservative_version(spec)
+ version = spec.version
+ return ">= 0" if version.nil?
+ segments = version.segments
+ seg_end_index = version >= Gem::Version.new("1.0") ? 1 : 2
+
+ prerelease_suffix = version.to_s.gsub(version.release.to_s, "") if version.prerelease?
+ "#{version_prefix}#{segments[0..seg_end_index].join(".")}#{prerelease_suffix}"
+ end
+
+ def version_prefix
+ if @options[:strict]
+ "= "
+ elsif @options[:optimistic]
+ ">= "
+ else
+ "~> "
+ end
+ end
+
+ def build_gem_lines(conservative_versioning)
+ @deps.map do |d|
+ name = d.name.dump
+
+ requirement = if conservative_versioning
+ ", \"#{conservative_version(@definition.specs[d.name][0])}\""
+ else
+ ", #{d.requirement.as_list.map(&:dump).join(", ")}"
+ end
+
+ if d.groups != Array(:default)
+ group = d.groups.size == 1 ? ", :group => #{d.groups.first.inspect}" : ", :groups => #{d.groups.inspect}"
+ end
+
+ source = ", :source => \"#{d.source}\"" unless d.source.nil?
+
+ %(gem #{name}#{requirement}#{group}#{source})
+ end.join("\n")
+ end
+
+ def append_to(gemfile_path, new_gem_lines)
+ gemfile_path.open("a") do |f|
+ f.puts
+ f.puts new_gem_lines
+ end
+ end
+
+ # evalutes a gemfile to remove the specified gem
+ # from it.
+ def remove_deps(gemfile_path)
+ initial_gemfile = IO.readlines(gemfile_path)
+
+ Bundler.ui.info "Removing gems from #{gemfile_path}"
+
+ # evaluate the Gemfile we have
+ builder = Dsl.new
+ builder.eval_gemfile(gemfile_path)
+
+ removed_deps = remove_gems_from_dependencies(builder, @deps, gemfile_path)
+
+ # abort the opertion if no gems were removed
+ # no need to operate on gemfile furthur
+ return [] if removed_deps.empty?
+
+ cleaned_gemfile = remove_gems_from_gemfile(@deps, gemfile_path)
+
+ SharedHelpers.write_to_gemfile(gemfile_path, cleaned_gemfile)
+
+ # check for errors
+ # including extra gems being removed
+ # or some gems not being removed
+ # and return the actual removed deps
+ cross_check_for_errors(gemfile_path, builder.dependencies, removed_deps, initial_gemfile)
+ end
+
+ # @param [Dsl] builder Dsl object of current Gemfile.
+ # @param [Array] gems Array of names of gems to be removed.
+ # @param [Pathname] path of the Gemfile
+ # @return [Array] removed_deps Array of removed dependencies.
+ def remove_gems_from_dependencies(builder, gems, gemfile_path)
+ removed_deps = []
+
+ gems.each do |gem_name|
+ deleted_dep = builder.dependencies.find {|d| d.name == gem_name }
+
+ if deleted_dep.nil?
+ raise GemfileError, "`#{gem_name}` is not specified in #{gemfile_path} so it could not be removed."
+ end
+
+ builder.dependencies.delete(deleted_dep)
+
+ removed_deps << deleted_dep
+ end
+
+ removed_deps
+ end
+
+ # @param [Array] gems Array of names of gems to be removed.
+ # @param [Pathname] gemfile_path The Gemfile from which to remove dependencies.
+ def remove_gems_from_gemfile(gems, gemfile_path)
+ patterns = /gem\s+(['"])#{Regexp.union(gems)}\1|gem\s*\((['"])#{Regexp.union(gems)}\2\)/
+
+ # remove lines which match the regex
+ new_gemfile = IO.readlines(gemfile_path).reject {|line| line.match(patterns) }
+
+ # remove lone \n and append them with other strings
+ new_gemfile.each_with_index do |_line, index|
+ if new_gemfile[index + 1] == "\n"
+ new_gemfile[index] += new_gemfile[index + 1]
+ new_gemfile.delete_at(index + 1)
+ end
+ end
+
+ %w[group source env install_if].each {|block| remove_nested_blocks(new_gemfile, block) }
+
+ new_gemfile.join.chomp
+ end
+
+ # @param [Array] gemfile Array of gemfile contents.
+ # @param [String] block_name Name of block name to look for.
+ def remove_nested_blocks(gemfile, block_name)
+ nested_blocks = 0
+
+ # count number of nested blocks
+ gemfile.each_with_index {|line, index| nested_blocks += 1 if !gemfile[index + 1].nil? && gemfile[index + 1].include?(block_name) && line.include?(block_name) }
+
+ while nested_blocks >= 0
+ nested_blocks -= 1
+
+ gemfile.each_with_index do |line, index|
+ next unless !line.nil? && line.include?(block_name)
+ if gemfile[index + 1] =~ /^\s*end\s*$/
+ gemfile[index] = nil
+ gemfile[index + 1] = nil
+ end
+ end
+
+ gemfile.compact!
+ end
+ end
+
+ # @param [Pathname] gemfile_path The Gemfile from which to remove dependencies.
+ # @param [Array] original_deps Array of original dependencies.
+ # @param [Array] removed_deps Array of removed dependencies.
+ # @param [Array] initial_gemfile Contents of original Gemfile before any operation.
+ def cross_check_for_errors(gemfile_path, original_deps, removed_deps, initial_gemfile)
+ # evalute the new gemfile to look for any failure cases
+ builder = Dsl.new
+ builder.eval_gemfile(gemfile_path)
+
+ # record gems which were removed but not requested
+ extra_removed_gems = original_deps - builder.dependencies
+
+ # if some extra gems were removed then raise error
+ # and revert Gemfile to original
+ unless extra_removed_gems.empty?
+ SharedHelpers.write_to_gemfile(gemfile_path, initial_gemfile.join)
+
+ raise InvalidOption, "Gems could not be removed. #{extra_removed_gems.join(", ")} would also have been removed. Bundler cannot continue."
+ end
+
+ # record gems which could not be removed due to some reasons
+ errored_deps = builder.dependencies.select {|d| d.gemfile == gemfile_path } & removed_deps.select {|d| d.gemfile == gemfile_path }
+
+ show_warning "#{errored_deps.map(&:name).join(", ")} could not be removed." unless errored_deps.empty?
+
+ # return actual removed dependencies
+ removed_deps - errored_deps
+ end
+
+ def show_warning(message)
+ Bundler.ui.info Bundler.ui.add_color(message, :yellow)
+ end
+ end
+end
diff --git a/lib/bundler/inline.rb b/lib/bundler/inline.rb
new file mode 100644
index 00000000000000..9d25f3261a19a0
--- /dev/null
+++ b/lib/bundler/inline.rb
@@ -0,0 +1,74 @@
+# frozen_string_literal: true
+
+require "bundler/compatibility_guard"
+
+# Allows for declaring a Gemfile inline in a ruby script, optionally installing
+# any gems that aren't already installed on the user's system.
+#
+# @note Every gem that is specified in this 'Gemfile' will be `require`d, as if
+# the user had manually called `Bundler.require`. To avoid a requested gem
+# being automatically required, add the `:require => false` option to the
+# `gem` dependency declaration.
+#
+# @param install [Boolean] whether gems that aren't already installed on the
+# user's system should be installed.
+# Defaults to `false`.
+#
+# @param gemfile [Proc] a block that is evaluated as a `Gemfile`.
+#
+# @example Using an inline Gemfile
+#
+# #!/usr/bin/env ruby
+#
+# require 'bundler/inline'
+#
+# gemfile do
+# source 'https://rubygems.org'
+# gem 'json', require: false
+# gem 'nap', require: 'rest'
+# gem 'cocoapods', '~> 0.34.1'
+# end
+#
+# puts Pod::VERSION # => "0.34.4"
+#
+def gemfile(install = false, options = {}, &gemfile)
+ require "bundler"
+
+ opts = options.dup
+ ui = opts.delete(:ui) { Bundler::UI::Shell.new }
+ raise ArgumentError, "Unknown options: #{opts.keys.join(", ")}" unless opts.empty?
+
+ old_root = Bundler.method(:root)
+ def Bundler.root
+ Bundler::SharedHelpers.pwd.expand_path
+ end
+ Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", "Gemfile"
+
+ Bundler::Plugin.gemfile_install(&gemfile) if Bundler.feature_flag.plugins?
+ builder = Bundler::Dsl.new
+ builder.instance_eval(&gemfile)
+
+ definition = builder.to_definition(nil, true)
+ def definition.lock(*); end
+ definition.validate_runtime!
+
+ missing_specs = proc do
+ definition.missing_specs?
+ end
+
+ Bundler.ui = ui if install
+ if install || missing_specs.call
+ Bundler.settings.temporary(:inline => true) do
+ installer = Bundler::Installer.install(Bundler.root, definition, :system => true)
+ installer.post_install_messages.each do |name, message|
+ Bundler.ui.info "Post-install message from #{name}:\n#{message}"
+ end
+ end
+ end
+
+ runtime = Bundler::Runtime.new(nil, definition)
+ runtime.setup.require
+ensure
+ bundler_module = class << Bundler; self; end
+ bundler_module.send(:define_method, :root, old_root) if old_root
+end
diff --git a/lib/bundler/installer.rb b/lib/bundler/installer.rb
new file mode 100644
index 00000000000000..b49cfb67031b5d
--- /dev/null
+++ b/lib/bundler/installer.rb
@@ -0,0 +1,318 @@
+# frozen_string_literal: true
+
+require "erb"
+require "rubygems/dependency_installer"
+require "bundler/worker"
+require "bundler/installer/parallel_installer"
+require "bundler/installer/standalone"
+require "bundler/installer/gem_installer"
+
+module Bundler
+ class Installer
+ class << self
+ attr_accessor :ambiguous_gems
+
+ Installer.ambiguous_gems = []
+ end
+
+ attr_reader :post_install_messages
+
+ # Begins the installation process for Bundler.
+ # For more information see the #run method on this class.
+ def self.install(root, definition, options = {})
+ installer = new(root, definition)
+ Plugin.hook(Plugin::Events::GEM_BEFORE_INSTALL_ALL, definition.dependencies)
+ installer.run(options)
+ Plugin.hook(Plugin::Events::GEM_AFTER_INSTALL_ALL, definition.dependencies)
+ installer
+ end
+
+ def initialize(root, definition)
+ @root = root
+ @definition = definition
+ @post_install_messages = {}
+ end
+
+ # Runs the install procedures for a specific Gemfile.
+ #
+ # Firstly, this method will check to see if `Bundler.bundle_path` exists
+ # and if not then Bundler will create the directory. This is usually the same
+ # location as RubyGems which typically is the `~/.gem` directory
+ # unless other specified.
+ #
+ # Secondly, it checks if Bundler has been configured to be "frozen".
+ # Frozen ensures that the Gemfile and the Gemfile.lock file are matching.
+ # This stops a situation where a developer may update the Gemfile but may not run
+ # `bundle install`, which leads to the Gemfile.lock file not being correctly updated.
+ # If this file is not correctly updated then any other developer running
+ # `bundle install` will potentially not install the correct gems.
+ #
+ # Thirdly, Bundler checks if there are any dependencies specified in the Gemfile.
+ # If there are no dependencies specified then Bundler returns a warning message stating
+ # so and this method returns.
+ #
+ # Fourthly, Bundler checks if the Gemfile.lock exists, and if so
+ # then proceeds to set up a definition based on the Gemfile and the Gemfile.lock.
+ # During this step Bundler will also download information about any new gems
+ # that are not in the Gemfile.lock and resolve any dependencies if needed.
+ #
+ # Fifthly, Bundler resolves the dependencies either through a cache of gems or by remote.
+ # This then leads into the gems being installed, along with stubs for their executables,
+ # but only if the --binstubs option has been passed or Bundler.options[:bin] has been set
+ # earlier.
+ #
+ # Sixthly, a new Gemfile.lock is created from the installed gems to ensure that the next time
+ # that a user runs `bundle install` they will receive any updates from this process.
+ #
+ # Finally, if the user has specified the standalone flag, Bundler will generate the needed
+ # require paths and save them in a `setup.rb` file. See `bundle standalone --help` for more
+ # information.
+ def run(options)
+ create_bundle_path
+
+ ProcessLock.lock do
+ if Bundler.frozen_bundle?
+ @definition.ensure_equivalent_gemfile_and_lockfile(options[:deployment])
+ end
+
+ if @definition.dependencies.empty?
+ Bundler.ui.warn "The Gemfile specifies no dependencies"
+ lock
+ return
+ end
+
+ if resolve_if_needed(options)
+ ensure_specs_are_compatible!
+ warn_on_incompatible_bundler_deps
+ load_plugins
+ options.delete(:jobs)
+ else
+ options[:jobs] = 1 # to avoid the overhead of Bundler::Worker
+ end
+ install(options)
+
+ lock unless Bundler.frozen_bundle?
+ Standalone.new(options[:standalone], @definition).generate if options[:standalone]
+ end
+ end
+
+ def generate_bundler_executable_stubs(spec, options = {})
+ if options[:binstubs_cmd] && spec.executables.empty?
+ options = {}
+ spec.runtime_dependencies.each do |dep|
+ bins = @definition.specs[dep].first.executables
+ options[dep.name] = bins unless bins.empty?
+ end
+ if options.any?
+ Bundler.ui.warn "#{spec.name} has no executables, but you may want " \
+ "one from a gem it depends on."
+ options.each {|name, bins| Bundler.ui.warn " #{name} has: #{bins.join(", ")}" }
+ else
+ Bundler.ui.warn "There are no executables for the gem #{spec.name}."
+ end
+ return
+ end
+
+ # double-assignment to avoid warnings about variables that will be used by ERB
+ bin_path = Bundler.bin_path
+ bin_path = bin_path
+ relative_gemfile_path = Bundler.default_gemfile.relative_path_from(bin_path)
+ relative_gemfile_path = relative_gemfile_path
+ ruby_command = Thor::Util.ruby_command
+ ruby_command = ruby_command
+ template_path = File.expand_path("../templates/Executable", __FILE__)
+ if spec.name == "bundler"
+ template_path += ".bundler"
+ spec.executables = %(bundle)
+ end
+ template = File.read(template_path)
+
+ exists = []
+ spec.executables.each do |executable|
+ binstub_path = "#{bin_path}/#{executable}"
+ if File.exist?(binstub_path) && !options[:force]
+ exists << executable
+ next
+ end
+
+ File.open(binstub_path, "w", 0o777 & ~File.umask) do |f|
+ if RUBY_VERSION >= "2.6"
+ f.puts ERB.new(template, :trim_mode => "-").result(binding)
+ else
+ f.puts ERB.new(template, nil, "-").result(binding)
+ end
+ end
+ end
+
+ if options[:binstubs_cmd] && exists.any?
+ case exists.size
+ when 1
+ Bundler.ui.warn "Skipped #{exists[0]} since it already exists."
+ when 2
+ Bundler.ui.warn "Skipped #{exists.join(" and ")} since they already exist."
+ else
+ items = exists[0...-1].empty? ? nil : exists[0...-1].join(", ")
+ skipped = [items, exists[-1]].compact.join(" and ")
+ Bundler.ui.warn "Skipped #{skipped} since they already exist."
+ end
+ Bundler.ui.warn "If you want to overwrite skipped stubs, use --force."
+ end
+ end
+
+ def generate_standalone_bundler_executable_stubs(spec)
+ # double-assignment to avoid warnings about variables that will be used by ERB
+ bin_path = Bundler.bin_path
+ unless path = Bundler.settings[:path]
+ raise "Can't standalone without an explicit path set"
+ end
+ standalone_path = Bundler.root.join(path).relative_path_from(bin_path)
+ standalone_path = standalone_path
+ template = File.read(File.expand_path("../templates/Executable.standalone", __FILE__))
+ ruby_command = Thor::Util.ruby_command
+ ruby_command = ruby_command
+
+ spec.executables.each do |executable|
+ next if executable == "bundle"
+ executable_path = Pathname(spec.full_gem_path).join(spec.bindir, executable).relative_path_from(bin_path)
+ executable_path = executable_path
+ File.open "#{bin_path}/#{executable}", "w", 0o755 do |f|
+ if RUBY_VERSION >= "2.6"
+ f.puts ERB.new(template, :trim_mode => "-").result(binding)
+ else
+ f.puts ERB.new(template, nil, "-").result(binding)
+ end
+ end
+ end
+ end
+
+ private
+
+ # the order that the resolver provides is significant, since
+ # dependencies might affect the installation of a gem.
+ # that said, it's a rare situation (other than rake), and parallel
+ # installation is SO MUCH FASTER. so we let people opt in.
+ def install(options)
+ force = options["force"]
+ jobs = installation_parallelization(options)
+ install_in_parallel jobs, options[:standalone], force
+ end
+
+ def installation_parallelization(options)
+ if jobs = options.delete(:jobs)
+ return jobs
+ end
+
+ return 1 unless can_install_in_parallel?
+
+ auto_config_jobs = Bundler.feature_flag.auto_config_jobs?
+ if jobs = Bundler.settings[:jobs]
+ if auto_config_jobs
+ jobs
+ else
+ [jobs.pred, 1].max
+ end
+ elsif auto_config_jobs
+ processor_count
+ else
+ 1
+ end
+ end
+
+ def processor_count
+ require "etc"
+ Etc.nprocessors
+ rescue
+ 1
+ end
+
+ def load_plugins
+ Bundler.rubygems.load_plugins
+
+ requested_path_gems = @definition.requested_specs.select {|s| s.source.is_a?(Source::Path) }
+ path_plugin_files = requested_path_gems.map do |spec|
+ begin
+ Bundler.rubygems.spec_matches_for_glob(spec, "rubygems_plugin#{Bundler.rubygems.suffix_pattern}")
+ rescue TypeError
+ error_message = "#{spec.name} #{spec.version} has an invalid gemspec"
+ raise Gem::InvalidSpecificationException, error_message
+ end
+ end.flatten
+ Bundler.rubygems.load_plugin_files(path_plugin_files)
+ end
+
+ def ensure_specs_are_compatible!
+ system_ruby = Bundler::RubyVersion.system
+ rubygems_version = Gem::Version.create(Gem::VERSION)
+ @definition.specs.each do |spec|
+ if required_ruby_version = spec.required_ruby_version
+ unless required_ruby_version.satisfied_by?(system_ruby.gem_version)
+ raise InstallError, "#{spec.full_name} requires ruby version #{required_ruby_version}, " \
+ "which is incompatible with the current version, #{system_ruby}"
+ end
+ end
+ next unless required_rubygems_version = spec.required_rubygems_version
+ unless required_rubygems_version.satisfied_by?(rubygems_version)
+ raise InstallError, "#{spec.full_name} requires rubygems version #{required_rubygems_version}, " \
+ "which is incompatible with the current version, #{rubygems_version}"
+ end
+ end
+ end
+
+ def warn_on_incompatible_bundler_deps
+ bundler_version = Gem::Version.create(Bundler::VERSION)
+ @definition.specs.each do |spec|
+ spec.dependencies.each do |dep|
+ next if dep.type == :development
+ next unless dep.name == "bundler".freeze
+ next if dep.requirement.satisfied_by?(bundler_version)
+
+ Bundler.ui.warn "#{spec.name} (#{spec.version}) has dependency" \
+ " #{SharedHelpers.pretty_dependency(dep)}" \
+ ", which is unsatisfied by the current bundler version #{VERSION}" \
+ ", so the dependency is being ignored"
+ end
+ end
+ end
+
+ def can_install_in_parallel?
+ if Bundler.rubygems.provides?(">= 2.1.0")
+ true
+ else
+ Bundler.ui.warn "RubyGems #{Gem::VERSION} is not threadsafe, so your "\
+ "gems will be installed one at a time. Upgrade to RubyGems 2.1.0 " \
+ "or higher to enable parallel gem installation."
+ false
+ end
+ end
+
+ def install_in_parallel(size, standalone, force = false)
+ spec_installations = ParallelInstaller.call(self, @definition.specs, size, standalone, force)
+ spec_installations.each do |installation|
+ post_install_messages[installation.name] = installation.post_install_message if installation.has_post_install_message?
+ end
+ end
+
+ def create_bundle_path
+ SharedHelpers.filesystem_access(Bundler.bundle_path.to_s) do |p|
+ Bundler.mkdir_p(p)
+ end unless Bundler.bundle_path.exist?
+ rescue Errno::EEXIST
+ raise PathError, "Could not install to path `#{Bundler.bundle_path}` " \
+ "because a file already exists at that path. Either remove or rename the file so the directory can be created."
+ end
+
+ # returns whether or not a re-resolve was needed
+ def resolve_if_needed(options)
+ if !@definition.unlocking? && !options["force"] && !Bundler.settings[:inline] && Bundler.default_lockfile.file?
+ return false if @definition.nothing_changed? && !@definition.missing_specs?
+ end
+
+ options["local"] ? @definition.resolve_with_cache! : @definition.resolve_remotely!
+ true
+ end
+
+ def lock(opts = {})
+ @definition.lock(Bundler.default_lockfile, opts[:preserve_unknown_sections])
+ end
+ end
+end
diff --git a/lib/bundler/installer/gem_installer.rb b/lib/bundler/installer/gem_installer.rb
new file mode 100644
index 00000000000000..e5e245f9706b6f
--- /dev/null
+++ b/lib/bundler/installer/gem_installer.rb
@@ -0,0 +1,85 @@
+# frozen_string_literal: true
+
+module Bundler
+ class GemInstaller
+ attr_reader :spec, :standalone, :worker, :force, :installer
+
+ def initialize(spec, installer, standalone = false, worker = 0, force = false)
+ @spec = spec
+ @installer = installer
+ @standalone = standalone
+ @worker = worker
+ @force = force
+ end
+
+ def install_from_spec
+ post_install_message = spec_settings ? install_with_settings : install
+ Bundler.ui.debug "#{worker}: #{spec.name} (#{spec.version}) from #{spec.loaded_from}"
+ generate_executable_stubs
+ return true, post_install_message
+ rescue Bundler::InstallHookError, Bundler::SecurityError, APIResponseMismatchError
+ raise
+ rescue Errno::ENOSPC
+ return false, out_of_space_message
+ rescue StandardError => e
+ return false, specific_failure_message(e)
+ end
+
+ private
+
+ def specific_failure_message(e)
+ message = "#{e.class}: #{e.message}\n"
+ message += " " + e.backtrace.join("\n ") + "\n\n" if Bundler.ui.debug?
+ message = message.lines.first + Bundler.ui.add_color(message.lines.drop(1).join, :clear)
+ message + Bundler.ui.add_color(failure_message, :red)
+ end
+
+ def failure_message
+ return install_error_message if spec.source.options["git"]
+ "#{install_error_message}\n#{gem_install_message}"
+ end
+
+ def install_error_message
+ "An error occurred while installing #{spec.name} (#{spec.version}), and Bundler cannot continue."
+ end
+
+ def gem_install_message
+ source = spec.source
+ return unless source.respond_to?(:remotes)
+
+ if source.remotes.size == 1
+ "Make sure that `gem install #{spec.name} -v '#{spec.version}' --source '#{source.remotes.first}'` succeeds before bundling."
+ else
+ "Make sure that `gem install #{spec.name} -v '#{spec.version}'` succeeds before bundling."
+ end
+ end
+
+ def spec_settings
+ # Fetch the build settings, if there are any
+ Bundler.settings["build.#{spec.name}"]
+ end
+
+ def install
+ spec.source.install(spec, :force => force, :ensure_builtin_gems_cached => standalone, :build_args => Array(spec_settings))
+ end
+
+ def install_with_settings
+ # Build arguments are global, so this is mutexed
+ Bundler.rubygems.install_with_build_args([spec_settings]) { install }
+ end
+
+ def out_of_space_message
+ "#{install_error_message}\nYour disk is out of space. Free some space to be able to install your bundle."
+ end
+
+ def generate_executable_stubs
+ return if Bundler.feature_flag.forget_cli_options?
+ return if Bundler.settings[:inline]
+ if Bundler.settings[:bin] && standalone
+ installer.generate_standalone_bundler_executable_stubs(spec)
+ elsif Bundler.settings[:bin]
+ installer.generate_bundler_executable_stubs(spec, :force => true)
+ end
+ end
+ end
+end
diff --git a/lib/bundler/installer/parallel_installer.rb b/lib/bundler/installer/parallel_installer.rb
new file mode 100644
index 00000000000000..f8a849ccfcaaf8
--- /dev/null
+++ b/lib/bundler/installer/parallel_installer.rb
@@ -0,0 +1,233 @@
+# frozen_string_literal: true
+
+require "bundler/worker"
+require "bundler/installer/gem_installer"
+
+module Bundler
+ class ParallelInstaller
+ class SpecInstallation
+ attr_accessor :spec, :name, :post_install_message, :state, :error
+ def initialize(spec)
+ @spec = spec
+ @name = spec.name
+ @state = :none
+ @post_install_message = ""
+ @error = nil
+ end
+
+ def installed?
+ state == :installed
+ end
+
+ def enqueued?
+ state == :enqueued
+ end
+
+ def failed?
+ state == :failed
+ end
+
+ def installation_attempted?
+ installed? || failed?
+ end
+
+ # Only true when spec in neither installed nor already enqueued
+ def ready_to_enqueue?
+ !enqueued? && !installation_attempted?
+ end
+
+ def has_post_install_message?
+ !post_install_message.empty?
+ end
+
+ def ignorable_dependency?(dep)
+ dep.type == :development || dep.name == @name
+ end
+
+ # Checks installed dependencies against spec's dependencies to make
+ # sure needed dependencies have been installed.
+ def dependencies_installed?(all_specs)
+ installed_specs = all_specs.select(&:installed?).map(&:name)
+ dependencies.all? {|d| installed_specs.include? d.name }
+ end
+
+ # Represents only the non-development dependencies, the ones that are
+ # itself and are in the total list.
+ def dependencies
+ @dependencies ||= begin
+ all_dependencies.reject {|dep| ignorable_dependency? dep }
+ end
+ end
+
+ def missing_lockfile_dependencies(all_spec_names)
+ deps = all_dependencies.reject {|dep| ignorable_dependency? dep }
+ deps.reject {|dep| all_spec_names.include? dep.name }
+ end
+
+ # Represents all dependencies
+ def all_dependencies
+ @spec.dependencies
+ end
+
+ def to_s
+ "#<#{self.class} #{@spec.full_name} (#{state})>"
+ end
+ end
+
+ def self.call(*args)
+ new(*args).call
+ end
+
+ attr_reader :size
+
+ def initialize(installer, all_specs, size, standalone, force)
+ @installer = installer
+ @size = size
+ @standalone = standalone
+ @force = force
+ @specs = all_specs.map {|s| SpecInstallation.new(s) }
+ @spec_set = all_specs
+ @rake = @specs.find {|s| s.name == "rake" }
+ end
+
+ def call
+ # Since `autoload` has the potential for threading issues on 1.8.7
+ # TODO: remove in bundler 2.0
+ require "bundler/gem_remote_fetcher" if RUBY_VERSION < "1.9"
+
+ check_for_corrupt_lockfile
+
+ if @size > 1
+ install_with_worker
+ else
+ install_serially
+ end
+
+ handle_error if @specs.any?(&:failed?)
+ @specs
+ ensure
+ worker_pool && worker_pool.stop
+ end
+
+ def check_for_corrupt_lockfile
+ missing_dependencies = @specs.map do |s|
+ [
+ s,
+ s.missing_lockfile_dependencies(@specs.map(&:name)),
+ ]
+ end.reject { |a| a.last.empty? }
+ return if missing_dependencies.empty?
+
+ warning = []
+ warning << "Your lockfile was created by an old Bundler that left some things out."
+ if @size != 1
+ warning << "Because of the missing DEPENDENCIES, we can only install gems one at a time, instead of installing #{@size} at a time."
+ @size = 1
+ end
+ warning << "You can fix this by adding the missing gems to your Gemfile, running bundle install, and then removing the gems from your Gemfile."
+ warning << "The missing gems are:"
+
+ missing_dependencies.each do |spec, missing|
+ warning << "* #{missing.map(&:name).join(", ")} depended upon by #{spec.name}"
+ end
+
+ Bundler.ui.warn(warning.join("\n"))
+ end
+
+ private
+
+ def install_with_worker
+ enqueue_specs
+ process_specs until finished_installing?
+ end
+
+ def install_serially
+ until finished_installing?
+ raise "failed to find a spec to enqueue while installing serially" unless spec_install = @specs.find(&:ready_to_enqueue?)
+ spec_install.state = :enqueued
+ do_install(spec_install, 0)
+ end
+ end
+
+ def worker_pool
+ @worker_pool ||= Bundler::Worker.new @size, "Parallel Installer", lambda { |spec_install, worker_num|
+ do_install(spec_install, worker_num)
+ }
+ end
+
+ def do_install(spec_install, worker_num)
+ Plugin.hook(Plugin::Events::GEM_BEFORE_INSTALL, spec_install)
+ gem_installer = Bundler::GemInstaller.new(
+ spec_install.spec, @installer, @standalone, worker_num, @force
+ )
+ success, message = begin
+ gem_installer.install_from_spec
+ rescue RuntimeError => e
+ raise e, "#{e}\n\n#{require_tree_for_spec(spec_install.spec)}"
+ end
+ if success
+ spec_install.state = :installed
+ spec_install.post_install_message = message unless message.nil?
+ else
+ spec_install.state = :failed
+ spec_install.error = "#{message}\n\n#{require_tree_for_spec(spec_install.spec)}"
+ end
+ Plugin.hook(Plugin::Events::GEM_AFTER_INSTALL, spec_install)
+ spec_install
+ end
+
+ # Dequeue a spec and save its post-install message and then enqueue the
+ # remaining specs.
+ # Some specs might've had to wait til this spec was installed to be
+ # processed so the call to `enqueue_specs` is important after every
+ # dequeue.
+ def process_specs
+ worker_pool.deq
+ enqueue_specs
+ end
+
+ def finished_installing?
+ @specs.all? do |spec|
+ return true if spec.failed?
+ spec.installed?
+ end
+ end
+
+ def handle_error
+ errors = @specs.select(&:failed?).map(&:error)
+ if exception = errors.find {|e| e.is_a?(Bundler::BundlerError) }
+ raise exception
+ end
+ raise Bundler::InstallError, errors.map(&:to_s).join("\n\n")
+ end
+
+ def require_tree_for_spec(spec)
+ tree = @spec_set.what_required(spec)
+ t = String.new("In #{File.basename(SharedHelpers.default_gemfile)}:\n")
+ tree.each_with_index do |s, depth|
+ t << " " * depth.succ << s.name
+ unless tree.last == s
+ t << %( was resolved to #{s.version}, which depends on)
+ end
+ t << %(\n)
+ end
+ t
+ end
+
+ # Keys in the remains hash represent uninstalled gems specs.
+ # We enqueue all gem specs that do not have any dependencies.
+ # Later we call this lambda again to install specs that depended on
+ # previously installed specifications. We continue until all specs
+ # are installed.
+ def enqueue_specs
+ @specs.select(&:ready_to_enqueue?).each do |spec|
+ next if @rake && !@rake.installed? && spec.name != @rake.name
+
+ if spec.dependencies_installed? @specs
+ spec.state = :enqueued
+ worker_pool.enq spec
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/installer/standalone.rb b/lib/bundler/installer/standalone.rb
new file mode 100644
index 00000000000000..ce0c9df1ebcb13
--- /dev/null
+++ b/lib/bundler/installer/standalone.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Standalone
+ def initialize(groups, definition)
+ @specs = groups.empty? ? definition.requested_specs : definition.specs_for(groups.map(&:to_sym))
+ end
+
+ def generate
+ SharedHelpers.filesystem_access(bundler_path) do |p|
+ FileUtils.mkdir_p(p)
+ end
+ File.open File.join(bundler_path, "setup.rb"), "w" do |file|
+ file.puts "require 'rbconfig'"
+ file.puts "# ruby 1.8.7 doesn't define RUBY_ENGINE"
+ file.puts "ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'"
+ file.puts "ruby_version = RbConfig::CONFIG[\"ruby_version\"]"
+ file.puts "path = File.expand_path('..', __FILE__)"
+ paths.each do |path|
+ file.puts %($:.unshift "\#{path}/#{path}")
+ end
+ end
+ end
+
+ private
+
+ def paths
+ @specs.map do |spec|
+ next if spec.name == "bundler"
+ Array(spec.require_paths).map do |path|
+ gem_path(path, spec).sub(version_dir, '#{ruby_engine}/#{ruby_version}')
+ # This is a static string intentionally. It's interpolated at a later time.
+ end
+ end.flatten
+ end
+
+ def version_dir
+ "#{Bundler::RubyVersion.system.engine}/#{RbConfig::CONFIG["ruby_version"]}"
+ end
+
+ def bundler_path
+ Bundler.root.join(Bundler.settings[:path], "bundler")
+ end
+
+ def gem_path(path, spec)
+ full_path = Pathname.new(path).absolute? ? path : File.join(spec.full_gem_path, path)
+ Pathname.new(full_path).relative_path_from(Bundler.root.join(bundler_path)).to_s
+ rescue TypeError
+ error_message = "#{spec.name} #{spec.version} has an invalid gemspec"
+ raise Gem::InvalidSpecificationException.new(error_message)
+ end
+ end
+end
diff --git a/lib/bundler/lazy_specification.rb b/lib/bundler/lazy_specification.rb
new file mode 100644
index 00000000000000..d9cb01f810e8db
--- /dev/null
+++ b/lib/bundler/lazy_specification.rb
@@ -0,0 +1,123 @@
+# frozen_string_literal: true
+
+require "uri"
+require "bundler/match_platform"
+
+module Bundler
+ class LazySpecification
+ Identifier = Struct.new(:name, :version, :source, :platform, :dependencies)
+ class Identifier
+ include Comparable
+ def <=>(other)
+ return unless other.is_a?(Identifier)
+ [name, version, platform_string] <=> [other.name, other.version, other.platform_string]
+ end
+
+ protected
+
+ def platform_string
+ platform_string = platform.to_s
+ platform_string == Index::RUBY ? Index::NULL : platform_string
+ end
+ end
+
+ include MatchPlatform
+
+ attr_reader :name, :version, :dependencies, :platform
+ attr_accessor :source, :remote
+
+ def initialize(name, version, platform, source = nil)
+ @name = name
+ @version = version
+ @dependencies = []
+ @platform = platform || Gem::Platform::RUBY
+ @source = source
+ @specification = nil
+ end
+
+ def full_name
+ if platform == Gem::Platform::RUBY || platform.nil?
+ "#{@name}-#{@version}"
+ else
+ "#{@name}-#{@version}-#{platform}"
+ end
+ end
+
+ def ==(other)
+ identifier == other.identifier
+ end
+
+ def satisfies?(dependency)
+ @name == dependency.name && dependency.requirement.satisfied_by?(Gem::Version.new(@version))
+ end
+
+ def to_lock
+ out = String.new
+
+ if platform == Gem::Platform::RUBY || platform.nil?
+ out << " #{name} (#{version})\n"
+ else
+ out << " #{name} (#{version}-#{platform})\n"
+ end
+
+ dependencies.sort_by(&:to_s).uniq.each do |dep|
+ next if dep.type == :development
+ out << " #{dep.to_lock}\n"
+ end
+
+ out
+ end
+
+ def __materialize__
+ search_object = Bundler.feature_flag.specific_platform? || Bundler.settings[:force_ruby_platform] ? self : Dependency.new(name, version)
+ @specification = if source.is_a?(Source::Gemspec) && source.gemspec.name == name
+ source.gemspec.tap {|s| s.source = source }
+ else
+ search = source.specs.search(search_object).last
+ if search && Gem::Platform.new(search.platform) != Gem::Platform.new(platform) && !search.runtime_dependencies.-(dependencies.reject {|d| d.type == :development }).empty?
+ Bundler.ui.warn "Unable to use the platform-specific (#{search.platform}) version of #{name} (#{version}) " \
+ "because it has different dependencies from the #{platform} version. " \
+ "To use the platform-specific version of the gem, run `bundle config specific_platform true` and install again."
+ search = source.specs.search(self).last
+ end
+ search.dependencies = dependencies if search && (search.is_a?(RemoteSpecification) || search.is_a?(EndpointSpecification))
+ search
+ end
+ end
+
+ def respond_to?(*args)
+ super || @specification ? @specification.respond_to?(*args) : nil
+ end
+
+ def to_s
+ @__to_s ||= if platform == Gem::Platform::RUBY || platform.nil?
+ "#{name} (#{version})"
+ else
+ "#{name} (#{version}-#{platform})"
+ end
+ end
+
+ def identifier
+ @__identifier ||= Identifier.new(name, version, source, platform, dependencies)
+ end
+
+ def git_version
+ return unless source.is_a?(Bundler::Source::Git)
+ " #{source.revision[0..6]}"
+ end
+
+ private
+
+ def to_ary
+ nil
+ end
+
+ def method_missing(method, *args, &blk)
+ raise "LazySpecification has not been materialized yet (calling :#{method} #{args.inspect})" unless @specification
+
+ return super unless respond_to?(method)
+
+ @specification.send(method, *args, &blk)
+ end
+ end
+end
diff --git a/lib/bundler/lockfile_generator.rb b/lib/bundler/lockfile_generator.rb
new file mode 100644
index 00000000000000..585077d18dc4cb
--- /dev/null
+++ b/lib/bundler/lockfile_generator.rb
@@ -0,0 +1,95 @@
+# frozen_string_literal: true
+
+module Bundler
+ class LockfileGenerator
+ attr_reader :definition
+ attr_reader :out
+
+ # @private
+ def initialize(definition)
+ @definition = definition
+ @out = String.new
+ end
+
+ def self.generate(definition)
+ new(definition).generate!
+ end
+
+ def generate!
+ add_sources
+ add_platforms
+ add_dependencies
+ add_locked_ruby_version
+ add_bundled_with
+
+ out
+ end
+
+ private
+
+ def add_sources
+ definition.send(:sources).lock_sources.each_with_index do |source, idx|
+ out << "\n" unless idx.zero?
+
+ # Add the source header
+ out << source.to_lock
+
+ # Find all specs for this source
+ specs = definition.resolve.select {|s| source.can_lock?(s) }
+ add_specs(specs)
+ end
+ end
+
+ def add_specs(specs)
+ # This needs to be sorted by full name so that
+ # gems with the same name, but different platform
+ # are ordered consistently
+ specs.sort_by(&:full_name).each do |spec|
+ next if spec.name == "bundler".freeze
+ out << spec.to_lock
+ end
+ end
+
+ def add_platforms
+ add_section("PLATFORMS", definition.platforms)
+ end
+
+ def add_dependencies
+ out << "\nDEPENDENCIES\n"
+
+ handled = []
+ definition.dependencies.sort_by(&:to_s).each do |dep|
+ next if handled.include?(dep.name)
+ out << dep.to_lock
+ handled << dep.name
+ end
+ end
+
+ def add_locked_ruby_version
+ return unless locked_ruby_version = definition.locked_ruby_version
+ add_section("RUBY VERSION", locked_ruby_version.to_s)
+ end
+
+ def add_bundled_with
+ add_section("BUNDLED WITH", definition.locked_bundler_version.to_s)
+ end
+
+ def add_section(name, value)
+ out << "\n#{name}\n"
+ case value
+ when Array
+ value.map(&:to_s).sort.each do |val|
+ out << " #{val}\n"
+ end
+ when Hash
+ value.to_a.sort_by {|k, _| k.to_s }.each do |key, val|
+ out << " #{key}: #{val}\n"
+ end
+ when String
+ out << " #{value}\n"
+ else
+ raise ArgumentError, "#{value.inspect} can't be serialized in a lockfile"
+ end
+ end
+ end
+end
diff --git a/lib/bundler/lockfile_parser.rb b/lib/bundler/lockfile_parser.rb
new file mode 100644
index 00000000000000..ff706fca1d5194
--- /dev/null
+++ b/lib/bundler/lockfile_parser.rb
@@ -0,0 +1,256 @@
+# frozen_string_literal: true
+
+# Some versions of the Bundler 1.1 RC series introduced corrupted
+# lockfiles. There were two major problems:
+#
+# * multiple copies of the same GIT section appeared in the lockfile
+# * when this happened, those sections got multiple copies of gems
+# in those sections.
+#
+# As a result, Bundler 1.1 contains code that fixes the earlier
+# corruption. We will remove this fix-up code in Bundler 1.2.
+
+module Bundler
+ class LockfileParser
+ attr_reader :sources, :dependencies, :specs, :platforms, :bundler_version, :ruby_version
+
+ BUNDLED = "BUNDLED WITH".freeze
+ DEPENDENCIES = "DEPENDENCIES".freeze
+ PLATFORMS = "PLATFORMS".freeze
+ RUBY = "RUBY VERSION".freeze
+ GIT = "GIT".freeze
+ GEM = "GEM".freeze
+ PATH = "PATH".freeze
+ PLUGIN = "PLUGIN SOURCE".freeze
+ SPECS = " specs:".freeze
+ OPTIONS = /^ ([a-z]+): (.*)$/i
+ SOURCE = [GIT, GEM, PATH, PLUGIN].freeze
+
+ SECTIONS_BY_VERSION_INTRODUCED = {
+ # The strings have to be dup'ed for old RG on Ruby 2.3+
+ # TODO: remove dup in Bundler 2.0
+ Gem::Version.create("1.0".dup) => [DEPENDENCIES, PLATFORMS, GIT, GEM, PATH].freeze,
+ Gem::Version.create("1.10".dup) => [BUNDLED].freeze,
+ Gem::Version.create("1.12".dup) => [RUBY].freeze,
+ Gem::Version.create("1.13".dup) => [PLUGIN].freeze,
+ }.freeze
+
+ KNOWN_SECTIONS = SECTIONS_BY_VERSION_INTRODUCED.values.flatten.freeze
+
+ ENVIRONMENT_VERSION_SECTIONS = [BUNDLED, RUBY].freeze
+
+ def self.sections_in_lockfile(lockfile_contents)
+ lockfile_contents.scan(/^\w[\w ]*$/).uniq
+ end
+
+ def self.unknown_sections_in_lockfile(lockfile_contents)
+ sections_in_lockfile(lockfile_contents) - KNOWN_SECTIONS
+ end
+
+ def self.sections_to_ignore(base_version = nil)
+ base_version &&= base_version.release
+ base_version ||= Gem::Version.create("1.0".dup)
+ attributes = []
+ SECTIONS_BY_VERSION_INTRODUCED.each do |version, introduced|
+ next if version <= base_version
+ attributes += introduced
+ end
+ attributes
+ end
+
+ def initialize(lockfile)
+ @platforms = []
+ @sources = []
+ @dependencies = {}
+ @state = nil
+ @specs = {}
+
+ @rubygems_aggregate = Source::Rubygems.new
+
+ if lockfile.match(/<<<<<<<|=======|>>>>>>>|\|\|\|\|\|\|\|/)
+ raise LockfileError, "Your #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)} contains merge conflicts.\n" \
+ "Run `git checkout HEAD -- #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)}` first to get a clean lock."
+ end
+
+ lockfile.split(/(?:\r?\n)+/).each do |line|
+ if SOURCE.include?(line)
+ @state = :source
+ parse_source(line)
+ elsif line == DEPENDENCIES
+ @state = :dependency
+ elsif line == PLATFORMS
+ @state = :platform
+ elsif line == RUBY
+ @state = :ruby
+ elsif line == BUNDLED
+ @state = :bundled_with
+ elsif line =~ /^[^\s]/
+ @state = nil
+ elsif @state
+ send("parse_#{@state}", line)
+ end
+ end
+ @sources << @rubygems_aggregate unless Bundler.feature_flag.lockfile_uses_separate_rubygems_sources?
+ @specs = @specs.values.sort_by(&:identifier)
+ warn_for_outdated_bundler_version
+ rescue ArgumentError => e
+ Bundler.ui.debug(e)
+ raise LockfileError, "Your lockfile is unreadable. Run `rm #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)}` " \
+ "and then `bundle install` to generate a new lockfile."
+ end
+
+ def warn_for_outdated_bundler_version
+ return unless bundler_version
+ prerelease_text = bundler_version.prerelease? ? " --pre" : ""
+ current_version = Gem::Version.create(Bundler::VERSION)
+ case current_version.segments.first <=> bundler_version.segments.first
+ when -1
+ raise LockfileError, "You must use Bundler #{bundler_version.segments.first} or greater with this lockfile."
+ when 0
+ if current_version < bundler_version
+ Bundler.ui.warn "Warning: the running version of Bundler (#{current_version}) is older " \
+ "than the version that created the lockfile (#{bundler_version}). We suggest you " \
+ "upgrade to the latest version of Bundler by running `gem " \
+ "install bundler#{prerelease_text}`.\n"
+ end
+ end
+ end
+
+ private
+
+ TYPES = {
+ GIT => Bundler::Source::Git,
+ GEM => Bundler::Source::Rubygems,
+ PATH => Bundler::Source::Path,
+ PLUGIN => Bundler::Plugin,
+ }.freeze
+
+ def parse_source(line)
+ case line
+ when SPECS
+ case @type
+ when PATH
+ @current_source = TYPES[@type].from_lock(@opts)
+ @sources << @current_source
+ when GIT
+ @current_source = TYPES[@type].from_lock(@opts)
+ # Strip out duplicate GIT sections
+ if @sources.include?(@current_source)
+ @current_source = @sources.find {|s| s == @current_source }
+ else
+ @sources << @current_source
+ end
+ when GEM
+ if Bundler.feature_flag.lockfile_uses_separate_rubygems_sources?
+ @opts["remotes"] = @opts.delete("remote")
+ @current_source = TYPES[@type].from_lock(@opts)
+ @sources << @current_source
+ else
+ Array(@opts["remote"]).each do |url|
+ @rubygems_aggregate.add_remote(url)
+ end
+ @current_source = @rubygems_aggregate
+ end
+ when PLUGIN
+ @current_source = Plugin.source_from_lock(@opts)
+ @sources << @current_source
+ end
+ when OPTIONS
+ value = $2
+ value = true if value == "true"
+ value = false if value == "false"
+
+ key = $1
+
+ if @opts[key]
+ @opts[key] = Array(@opts[key])
+ @opts[key] << value
+ else
+ @opts[key] = value
+ end
+ when *SOURCE
+ @current_source = nil
+ @opts = {}
+ @type = line
+ else
+ parse_spec(line)
+ end
+ end
+
+ space = / /
+ NAME_VERSION = /
+ ^(#{space}{2}|#{space}{4}|#{space}{6})(?!#{space}) # Exactly 2, 4, or 6 spaces at the start of the line
+ (.*?) # Name
+ (?:#{space}\(([^-]*) # Space, followed by version
+ (?:-(.*))?\))? # Optional platform
+ (!)? # Optional pinned marker
+ $ # Line end
+ /xo
+
+ def parse_dependency(line)
+ return unless line =~ NAME_VERSION
+ spaces = $1
+ return unless spaces.size == 2
+ name = $2
+ version = $3
+ pinned = $5
+
+ version = version.split(",").map(&:strip) if version
+
+ dep = Bundler::Dependency.new(name, version)
+
+ if pinned && dep.name != "bundler"
+ spec = @specs.find {|_, v| v.name == dep.name }
+ dep.source = spec.last.source if spec
+
+ # Path sources need to know what the default name / version
+ # to use in the case that there are no gemspecs present. A fake
+ # gemspec is created based on the version set on the dependency
+ # TODO: Use the version from the spec instead of from the dependency
+ if version && version.size == 1 && version.first =~ /^\s*= (.+)\s*$/ && dep.source.is_a?(Bundler::Source::Path)
+ dep.source.name = name
+ dep.source.version = $1
+ end
+ end
+
+ @dependencies[dep.name] = dep
+ end
+
+ def parse_spec(line)
+ return unless line =~ NAME_VERSION
+ spaces = $1
+ name = $2
+ version = $3
+ platform = $4
+
+ if spaces.size == 4
+ version = Gem::Version.new(version)
+ platform = platform ? Gem::Platform.new(platform) : Gem::Platform::RUBY
+ @current_spec = LazySpecification.new(name, version, platform)
+ @current_spec.source = @current_source
+
+ # Avoid introducing multiple copies of the same spec (caused by
+ # duplicate GIT sections)
+ @specs[@current_spec.identifier] ||= @current_spec
+ elsif spaces.size == 6
+ version = version.split(",").map(&:strip) if version
+ dep = Gem::Dependency.new(name, version)
+ @current_spec.dependencies << dep
+ end
+ end
+
+ def parse_platform(line)
+ @platforms << Gem::Platform.new($1) if line =~ /^ (.*)$/
+ end
+
+ def parse_bundled_with(line)
+ line = line.strip
+ return unless Gem::Version.correct?(line)
+ @bundler_version = Gem::Version.create(line)
+ end
+
+ def parse_ruby(line)
+ @ruby_version = line.strip
+ end
+ end
+end
diff --git a/lib/bundler/match_platform.rb b/lib/bundler/match_platform.rb
new file mode 100644
index 00000000000000..56cbbfb95dcc6c
--- /dev/null
+++ b/lib/bundler/match_platform.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+require "bundler/gem_helpers"
+
+module Bundler
+ module MatchPlatform
+ include GemHelpers
+
+ def match_platform(p)
+ MatchPlatform.platforms_match?(platform, p)
+ end
+
+ def self.platforms_match?(gemspec_platform, local_platform)
+ return true if gemspec_platform.nil?
+ return true if Gem::Platform::RUBY == gemspec_platform
+ return true if local_platform == gemspec_platform
+ gemspec_platform = Gem::Platform.new(gemspec_platform)
+ return true if GemHelpers.generic(gemspec_platform) === local_platform
+ return true if gemspec_platform === local_platform
+
+ false
+ end
+ end
+end
diff --git a/lib/bundler/mirror.rb b/lib/bundler/mirror.rb
new file mode 100644
index 00000000000000..b15190e7e579e2
--- /dev/null
+++ b/lib/bundler/mirror.rb
@@ -0,0 +1,223 @@
+# frozen_string_literal: true
+
+require "socket"
+
+module Bundler
+ class Settings
+ # Class used to build the mirror set and then find a mirror for a given URI
+ #
+ # @param prober [Prober object, nil] by default a TCPSocketProbe, this object
+ # will be used to probe the mirror address to validate that the mirror replies.
+ class Mirrors
+ def initialize(prober = nil)
+ @all = Mirror.new
+ @prober = prober || TCPSocketProbe.new
+ @mirrors = {}
+ end
+
+ # Returns a mirror for the given uri.
+ #
+ # Depending on the uri having a valid mirror or not, it may be a
+ # mirror that points to the provided uri
+ def for(uri)
+ if @all.validate!(@prober).valid?
+ @all
+ else
+ fetch_valid_mirror_for(Settings.normalize_uri(uri))
+ end
+ end
+
+ def each
+ @mirrors.each do |k, v|
+ yield k, v.uri.to_s
+ end
+ end
+
+ def parse(key, value)
+ config = MirrorConfig.new(key, value)
+ mirror = if config.all?
+ @all
+ else
+ @mirrors[config.uri] ||= Mirror.new
+ end
+ config.update_mirror(mirror)
+ end
+
+ private
+
+ def fetch_valid_mirror_for(uri)
+ downcased = uri.to_s.downcase
+ mirror = @mirrors[downcased] || @mirrors[URI(downcased).host] || Mirror.new(uri)
+ mirror.validate!(@prober)
+ mirror = Mirror.new(uri) unless mirror.valid?
+ mirror
+ end
+ end
+
+ # A mirror
+ #
+ # Contains both the uri that should be used as a mirror and the
+ # fallback timeout which will be used for probing if the mirror
+ # replies on time or not.
+ class Mirror
+ DEFAULT_FALLBACK_TIMEOUT = 0.1
+
+ attr_reader :uri, :fallback_timeout
+
+ def initialize(uri = nil, fallback_timeout = 0)
+ self.uri = uri
+ self.fallback_timeout = fallback_timeout
+ @valid = nil
+ end
+
+ def uri=(uri)
+ @uri = if uri.nil?
+ nil
+ else
+ URI(uri.to_s)
+ end
+ @valid = nil
+ end
+
+ def fallback_timeout=(timeout)
+ case timeout
+ when true, "true"
+ @fallback_timeout = DEFAULT_FALLBACK_TIMEOUT
+ when false, "false"
+ @fallback_timeout = 0
+ else
+ @fallback_timeout = timeout.to_i
+ end
+ @valid = nil
+ end
+
+ def ==(other)
+ !other.nil? && uri == other.uri && fallback_timeout == other.fallback_timeout
+ end
+
+ def valid?
+ return false if @uri.nil?
+ return @valid unless @valid.nil?
+ false
+ end
+
+ def validate!(probe = nil)
+ @valid = false if uri.nil?
+ if @valid.nil?
+ @valid = fallback_timeout == 0 || (probe || TCPSocketProbe.new).replies?(self)
+ end
+ self
+ end
+ end
+
+ # Class used to parse one configuration line
+ #
+ # Gets the configuration line and the value.
+ # This object provides a `update_mirror` method
+ # used to setup the given mirror value.
+ class MirrorConfig
+ attr_accessor :uri, :value
+
+ def initialize(config_line, value)
+ uri, fallback =
+ config_line.match(%r{\Amirror\.(all|.+?)(\.fallback_timeout)?\/?\z}).captures
+ @fallback = !fallback.nil?
+ @all = false
+ if uri == "all"
+ @all = true
+ else
+ @uri = URI(uri).absolute? ? Settings.normalize_uri(uri) : uri
+ end
+ @value = value
+ end
+
+ def all?
+ @all
+ end
+
+ def update_mirror(mirror)
+ if @fallback
+ mirror.fallback_timeout = @value
+ else
+ mirror.uri = Settings.normalize_uri(@value)
+ end
+ end
+ end
+
+ # Class used for probing TCP availability for a given mirror.
+ class TCPSocketProbe
+ def replies?(mirror)
+ MirrorSockets.new(mirror).any? do |socket, address, timeout|
+ begin
+ socket.connect_nonblock(address)
+ rescue Errno::EINPROGRESS
+ wait_for_writtable_socket(socket, address, timeout)
+ rescue RuntimeError # Connection failed somehow, again
+ false
+ end
+ end
+ end
+
+ private
+
+ def wait_for_writtable_socket(socket, address, timeout)
+ if IO.select(nil, [socket], nil, timeout)
+ probe_writtable_socket(socket, address)
+ else # TCP Handshake timed out, or there is something dropping packets
+ false
+ end
+ end
+
+ def probe_writtable_socket(socket, address)
+ socket.connect_nonblock(address)
+ rescue Errno::EISCONN
+ true
+ rescue StandardError # Connection failed
+ false
+ end
+ end
+ end
+
+ # Class used to build the list of sockets that correspond to
+ # a given mirror.
+ #
+ # One mirror may correspond to many different addresses, both
+ # because of it having many dns entries or because
+ # the network interface is both ipv4 and ipv5
+ class MirrorSockets
+ def initialize(mirror)
+ @timeout = mirror.fallback_timeout
+ @addresses = Socket.getaddrinfo(mirror.uri.host, mirror.uri.port).map do |address|
+ SocketAddress.new(address[0], address[3], address[1])
+ end
+ end
+
+ def any?
+ @addresses.any? do |address|
+ socket = Socket.new(Socket.const_get(address.type), Socket::SOCK_STREAM, 0)
+ socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
+ value = yield socket, address.to_socket_address, @timeout
+ socket.close unless socket.closed?
+ value
+ end
+ end
+ end
+
+ # Socket address builder.
+ #
+ # Given a socket type, a host and a port,
+ # provides a method to build sockaddr string
+ class SocketAddress
+ attr_reader :type, :host, :port
+
+ def initialize(type, host, port)
+ @type = type
+ @host = host
+ @port = port
+ end
+
+ def to_socket_address
+ Socket.pack_sockaddr_in(@port, @host)
+ end
+ end
+end
diff --git a/lib/bundler/plugin.rb b/lib/bundler/plugin.rb
new file mode 100644
index 00000000000000..53f9806b73d403
--- /dev/null
+++ b/lib/bundler/plugin.rb
@@ -0,0 +1,292 @@
+# frozen_string_literal: true
+
+require "bundler/plugin/api"
+
+module Bundler
+ module Plugin
+ autoload :DSL, "bundler/plugin/dsl"
+ autoload :Events, "bundler/plugin/events"
+ autoload :Index, "bundler/plugin/index"
+ autoload :Installer, "bundler/plugin/installer"
+ autoload :SourceList, "bundler/plugin/source_list"
+
+ class MalformattedPlugin < PluginError; end
+ class UndefinedCommandError < PluginError; end
+ class UnknownSourceError < PluginError; end
+
+ PLUGIN_FILE_NAME = "plugins.rb".freeze
+
+ module_function
+
+ def reset!
+ instance_variables.each {|i| remove_instance_variable(i) }
+
+ @sources = {}
+ @commands = {}
+ @hooks_by_event = Hash.new {|h, k| h[k] = [] }
+ @loaded_plugin_names = []
+ end
+
+ reset!
+
+ # Installs a new plugin by the given name
+ #
+ # @param [Array] names the name of plugin to be installed
+ # @param [Hash] options various parameters as described in description.
+ # Refer to cli/plugin for available options
+ def install(names, options)
+ specs = Installer.new.install(names, options)
+
+ save_plugins names, specs
+ rescue PluginError => e
+ if specs
+ specs_to_delete = Hash[specs.select {|k, _v| names.include?(k) && !index.commands.values.include?(k) }]
+ specs_to_delete.values.each {|spec| Bundler.rm_rf(spec.full_gem_path) }
+ end
+
+ Bundler.ui.error "Failed to install plugin #{name}: #{e.message}\n #{e.backtrace.join("\n ")}"
+ end
+
+ # Evaluates the Gemfile with a limited DSL and installs the plugins
+ # specified by plugin method
+ #
+ # @param [Pathname] gemfile path
+ # @param [Proc] block that can be evaluated for (inline) Gemfile
+ def gemfile_install(gemfile = nil, &inline)
+ builder = DSL.new
+ if block_given?
+ builder.instance_eval(&inline)
+ else
+ builder.eval_gemfile(gemfile)
+ end
+ definition = builder.to_definition(nil, true)
+
+ return if definition.dependencies.empty?
+
+ plugins = definition.dependencies.map(&:name).reject {|p| index.installed? p }
+ installed_specs = Installer.new.install_definition(definition)
+
+ save_plugins plugins, installed_specs, builder.inferred_plugins
+ rescue RuntimeError => e
+ unless e.is_a?(GemfileError)
+ Bundler.ui.error "Failed to install plugin: #{e.message}\n #{e.backtrace[0]}"
+ end
+ raise
+ end
+
+ # The index object used to store the details about the plugin
+ def index
+ @index ||= Index.new
+ end
+
+ # The directory root for all plugin related data
+ #
+ # If run in an app, points to local root, in app_config_path
+ # Otherwise, points to global root, in Bundler.user_bundle_path("plugin")
+ def root
+ @root ||= if SharedHelpers.in_bundle?
+ local_root
+ else
+ global_root
+ end
+ end
+
+ def local_root
+ Bundler.app_config_path.join("plugin")
+ end
+
+ # The global directory root for all plugin related data
+ def global_root
+ Bundler.user_bundle_path("plugin")
+ end
+
+ # The cache directory for plugin stuffs
+ def cache
+ @cache ||= root.join("cache")
+ end
+
+ # To be called via the API to register to handle a command
+ def add_command(command, cls)
+ @commands[command] = cls
+ end
+
+ # Checks if any plugin handles the command
+ def command?(command)
+ !index.command_plugin(command).nil?
+ end
+
+ # To be called from Cli class to pass the command and argument to
+ # approriate plugin class
+ def exec_command(command, args)
+ raise UndefinedCommandError, "Command `#{command}` not found" unless command? command
+
+ load_plugin index.command_plugin(command) unless @commands.key? command
+
+ @commands[command].new.exec(command, args)
+ end
+
+ # To be called via the API to register to handle a source plugin
+ def add_source(source, cls)
+ @sources[source] = cls
+ end
+
+ # Checks if any plugin declares the source
+ def source?(name)
+ !index.source_plugin(name.to_s).nil?
+ end
+
+ # @return [Class] that handles the source. The calss includes API::Source
+ def source(name)
+ raise UnknownSourceError, "Source #{name} not found" unless source? name
+
+ load_plugin(index.source_plugin(name)) unless @sources.key? name
+
+ @sources[name]
+ end
+
+ # @param [Hash] The options that are present in the lock file
+ # @return [API::Source] the instance of the class that handles the source
+ # type passed in locked_opts
+ def source_from_lock(locked_opts)
+ src = source(locked_opts["type"])
+
+ src.new(locked_opts.merge("uri" => locked_opts["remote"]))
+ end
+
+ # To be called via the API to register a hooks and corresponding block that
+ # will be called to handle the hook
+ def add_hook(event, &block)
+ unless Events.defined_event?(event)
+ raise ArgumentError, "Event '#{event}' not defined in Bundler::Plugin::Events"
+ end
+ @hooks_by_event[event.to_s] << block
+ end
+
+ # Runs all the hooks that are registered for the passed event
+ #
+ # It passes the passed arguments and block to the block registered with
+ # the api.
+ #
+ # @param [String] event
+ def hook(event, *args, &arg_blk)
+ return unless Bundler.feature_flag.plugins?
+ unless Events.defined_event?(event)
+ raise ArgumentError, "Event '#{event}' not defined in Bundler::Plugin::Events"
+ end
+
+ plugins = index.hook_plugins(event)
+ return unless plugins.any?
+
+ (plugins - @loaded_plugin_names).each {|name| load_plugin(name) }
+
+ @hooks_by_event[event].each {|blk| blk.call(*args, &arg_blk) }
+ end
+
+ # currently only intended for specs
+ #
+ # @return [String, nil] installed path
+ def installed?(plugin)
+ Index.new.installed?(plugin)
+ end
+
+ # Post installation processing and registering with index
+ #
+ # @param [Array] plugins list to be installed
+ # @param [Hash] specs of plugins mapped to installation path (currently they
+ # contain all the installed specs, including plugins)
+ # @param [Array] names of inferred source plugins that can be ignored
+ def save_plugins(plugins, specs, optional_plugins = [])
+ plugins.each do |name|
+ spec = specs[name]
+ validate_plugin! Pathname.new(spec.full_gem_path)
+ installed = register_plugin(name, spec, optional_plugins.include?(name))
+ Bundler.ui.info "Installed plugin #{name}" if installed
+ end
+ end
+
+ # Checks if the gem is good to be a plugin
+ #
+ # At present it only checks whether it contains plugins.rb file
+ #
+ # @param [Pathname] plugin_path the path plugin is installed at
+ # @raise [MalformattedPlugin] if plugins.rb file is not found
+ def validate_plugin!(plugin_path)
+ plugin_file = plugin_path.join(PLUGIN_FILE_NAME)
+ raise MalformattedPlugin, "#{PLUGIN_FILE_NAME} was not found in the plugin." unless plugin_file.file?
+ end
+
+ # Runs the plugins.rb file in an isolated namespace, records the plugin
+ # actions it registers for and then passes the data to index to be stored.
+ #
+ # @param [String] name the name of the plugin
+ # @param [Specification] spec of installed plugin
+ # @param [Boolean] optional_plugin, removed if there is conflict with any
+ # other plugin (used for default source plugins)
+ #
+ # @raise [MalformattedPlugin] if plugins.rb raises any error
+ def register_plugin(name, spec, optional_plugin = false)
+ commands = @commands
+ sources = @sources
+ hooks = @hooks_by_event
+
+ @commands = {}
+ @sources = {}
+ @hooks_by_event = Hash.new {|h, k| h[k] = [] }
+
+ load_paths = spec.load_paths
+ add_to_load_path(load_paths)
+ path = Pathname.new spec.full_gem_path
+
+ begin
+ load path.join(PLUGIN_FILE_NAME), true
+ rescue StandardError => e
+ raise MalformattedPlugin, "#{e.class}: #{e.message}"
+ end
+
+ if optional_plugin && @sources.keys.any? {|s| source? s }
+ Bundler.rm_rf(path)
+ false
+ else
+ index.register_plugin(name, path.to_s, load_paths, @commands.keys,
+ @sources.keys, @hooks_by_event.keys)
+ true
+ end
+ ensure
+ @commands = commands
+ @sources = sources
+ @hooks_by_event = hooks
+ end
+
+ # Executes the plugins.rb file
+ #
+ # @param [String] name of the plugin
+ def load_plugin(name)
+ # Need to ensure before this that plugin root where the rest of gems
+ # are installed to be on load path to support plugin deps. Currently not
+ # done to avoid conflicts
+ path = index.plugin_path(name)
+
+ add_to_load_path(index.load_paths(name))
+
+ load path.join(PLUGIN_FILE_NAME)
+
+ @loaded_plugin_names << name
+ rescue RuntimeError => e
+ Bundler.ui.error "Failed loading plugin #{name}: #{e.message}"
+ raise
+ end
+
+ def add_to_load_path(load_paths)
+ if insert_index = Bundler.rubygems.load_path_insert_index
+ $LOAD_PATH.insert(insert_index, *load_paths)
+ else
+ $LOAD_PATH.unshift(*load_paths)
+ end
+ end
+
+ class << self
+ private :load_plugin, :register_plugin, :save_plugins, :validate_plugin!,
+ :add_to_load_path
+ end
+ end
+end
diff --git a/lib/bundler/plugin/api.rb b/lib/bundler/plugin/api.rb
new file mode 100644
index 00000000000000..a2d5cbb4ac5af1
--- /dev/null
+++ b/lib/bundler/plugin/api.rb
@@ -0,0 +1,81 @@
+# frozen_string_literal: true
+
+module Bundler
+ # This is the interfacing class represents the API that we intend to provide
+ # the plugins to use.
+ #
+ # For plugins to be independent of the Bundler internals they shall limit their
+ # interactions to methods of this class only. This will save them from breaking
+ # when some internal change.
+ #
+ # Currently we are delegating the methods defined in Bundler class to
+ # itself. So, this class acts as a buffer.
+ #
+ # If there is some change in the Bundler class that is incompatible to its
+ # previous behavior or if otherwise desired, we can reimplement(or implement)
+ # the method to preserve compatibility.
+ #
+ # To use this, either the class can inherit this class or use it directly.
+ # For example of both types of use, refer the file `spec/plugins/command.rb`
+ #
+ # To use it without inheriting, you will have to create an object of this
+ # to use the functions (except for declaration functions like command, source,
+ # and hooks).
+ module Plugin
+ class API
+ autoload :Source, "bundler/plugin/api/source"
+
+ # The plugins should declare that they handle a command through this helper.
+ #
+ # @param [String] command being handled by them
+ # @param [Class] (optional) class that handles the command. If not
+ # provided, the `self` class will be used.
+ def self.command(command, cls = self)
+ Plugin.add_command command, cls
+ end
+
+ # The plugins should declare that they provide a installation source
+ # through this helper.
+ #
+ # @param [String] the source type they provide
+ # @param [Class] (optional) class that handles the source. If not
+ # provided, the `self` class will be used.
+ def self.source(source, cls = self)
+ cls.send :include, Bundler::Plugin::API::Source
+ Plugin.add_source source, cls
+ end
+
+ def self.hook(event, &block)
+ Plugin.add_hook(event, &block)
+ end
+
+ # The cache dir to be used by the plugins for storage
+ #
+ # @return [Pathname] path of the cache dir
+ def cache_dir
+ Plugin.cache.join("plugins")
+ end
+
+ # A tmp dir to be used by plugins
+ # Accepts names that get concatenated as suffix
+ #
+ # @return [Pathname] object for the new directory created
+ def tmp(*names)
+ Bundler.tmp(["plugin", *names].join("-"))
+ end
+
+ def method_missing(name, *args, &blk)
+ return Bundler.send(name, *args, &blk) if Bundler.respond_to?(name)
+
+ return SharedHelpers.send(name, *args, &blk) if SharedHelpers.respond_to?(name)
+
+ super
+ end
+
+ def respond_to_missing?(name, include_private = false)
+ SharedHelpers.respond_to?(name, include_private) ||
+ Bundler.respond_to?(name, include_private) || super
+ end
+ end
+ end
+end
diff --git a/lib/bundler/plugin/api/source.rb b/lib/bundler/plugin/api/source.rb
new file mode 100644
index 00000000000000..586477efb55b74
--- /dev/null
+++ b/lib/bundler/plugin/api/source.rb
@@ -0,0 +1,306 @@
+# frozen_string_literal: true
+
+require "uri"
+
+module Bundler
+ module Plugin
+ class API
+ # This class provides the base to build source plugins
+ # All the method here are required to build a source plugin (except
+ # `uri_hash`, `gem_install_dir`; they are helpers).
+ #
+ # Defaults for methods, where ever possible are provided which is
+ # expected to work. But, all source plugins have to override
+ # `fetch_gemspec_files` and `install`. Defaults are also not provided for
+ # `remote!`, `cache!` and `unlock!`.
+ #
+ # The defaults shall work for most situations but nevertheless they can
+ # be (preferably should be) overridden as per the plugins' needs safely
+ # (as long as they behave as expected).
+ # On overriding `initialize` you should call super first.
+ #
+ # If required plugin should override `hash`, `==` and `eql?` methods to be
+ # able to match objects representing same sources, but may be created in
+ # different situation (like form gemfile and lockfile). The default ones
+ # checks only for class and uri, but elaborate source plugins may need
+ # more comparisons (e.g. git checking on branch or tag).
+ #
+ # @!attribute [r] uri
+ # @return [String] the remote specified with `source` block in Gemfile
+ #
+ # @!attribute [r] options
+ # @return [String] options passed during initialization (either from
+ # lockfile or Gemfile)
+ #
+ # @!attribute [r] name
+ # @return [String] name that can be used to uniquely identify a source
+ #
+ # @!attribute [rw] dependency_names
+ # @return [Array] Names of dependencies that the source should
+ # try to resolve. It is not necessary to use this list intenally. This
+ # is present to be compatible with `Definition` and is used by
+ # rubygems source.
+ module Source
+ attr_reader :uri, :options, :name
+ attr_accessor :dependency_names
+
+ def initialize(opts)
+ @options = opts
+ @dependency_names = []
+ @uri = opts["uri"]
+ @type = opts["type"]
+ @name = opts["name"] || "#{@type} at #{@uri}"
+ end
+
+ # This is used by the default `spec` method to constructs the
+ # Specification objects for the gems and versions that can be installed
+ # by this source plugin.
+ #
+ # Note: If the spec method is overridden, this function is not necessary
+ #
+ # @return [Array] paths of the gemspec files for gems that can
+ # be installed
+ def fetch_gemspec_files
+ []
+ end
+
+ # Options to be saved in the lockfile so that the source plugin is able
+ # to check out same version of gem later.
+ #
+ # There options are passed when the source plugin is created from the
+ # lock file.
+ #
+ # @return [Hash]
+ def options_to_lock
+ {}
+ end
+
+ # Install the gem specified by the spec at appropriate path.
+ # `install_path` provides a sufficient default, if the source can only
+ # satisfy one gem, but is not binding.
+ #
+ # @return [String] post installation message (if any)
+ def install(spec, opts)
+ raise MalformattedPlugin, "Source plugins need to override the install method."
+ end
+
+ # It builds extensions, generates bins and installs them for the spec
+ # provided.
+ #
+ # It depends on `spec.loaded_from` to get full_gem_path. The source
+ # plugins should set that.
+ #
+ # It should be called in `install` after the plugin is done placing the
+ # gem at correct install location.
+ #
+ # It also runs Gem hooks `pre_install`, `post_build` and `post_install`
+ #
+ # Note: Do not override if you don't know what you are doing.
+ def post_install(spec, disable_exts = false)
+ opts = { :env_shebang => false, :disable_extensions => disable_exts }
+ installer = Bundler::Source::Path::Installer.new(spec, opts)
+ installer.post_install
+ end
+
+ # A default installation path to install a single gem. If the source
+ # servers multiple gems, it's not of much use and the source should one
+ # of its own.
+ def install_path
+ @install_path ||=
+ begin
+ base_name = File.basename(URI.parse(uri).normalize.path)
+
+ gem_install_dir.join("#{base_name}-#{uri_hash[0..11]}")
+ end
+ end
+
+ # Parses the gemspec files to find the specs for the gems that can be
+ # satisfied by the source.
+ #
+ # Few important points to keep in mind:
+ # - If the gems are not installed then it shall return specs for all
+ # the gems it can satisfy
+ # - If gem is installed (that is to be detected by the plugin itself)
+ # then it shall return at least the specs that are installed.
+ # - The `loaded_from` for each of the specs shall be correct (it is
+ # used to find the load path)
+ #
+ # @return [Bundler::Index] index containing the specs
+ def specs
+ files = fetch_gemspec_files
+
+ Bundler::Index.build do |index|
+ files.each do |file|
+ next unless spec = Bundler.load_gemspec(file)
+ Bundler.rubygems.set_installed_by_version(spec)
+
+ spec.source = self
+ Bundler.rubygems.validate(spec)
+
+ index << spec
+ end
+ end
+ end
+
+ # Set internal representation to fetch the gems/specs from remote.
+ #
+ # When this is called, the source should try to fetch the specs and
+ # install from remote path.
+ def remote!
+ end
+
+ # Set internal representation to fetch the gems/specs from app cache.
+ #
+ # When this is called, the source should try to fetch the specs and
+ # install from the path provided by `app_cache_path`.
+ def cached!
+ end
+
+ # This is called to update the spec and installation.
+ #
+ # If the source plugin is loaded from lockfile or otherwise, it shall
+ # refresh the cache/specs (e.g. git sources can make a fresh clone).
+ def unlock!
+ end
+
+ # Name of directory where plugin the is expected to cache the gems when
+ # #cache is called.
+ #
+ # Also this name is matched against the directories in cache for pruning
+ #
+ # This is used by `app_cache_path`
+ def app_cache_dirname
+ base_name = File.basename(URI.parse(uri).normalize.path)
+ "#{base_name}-#{uri_hash}"
+ end
+
+ # This method is called while caching to save copy of the gems that the
+ # source can resolve to path provided by `app_cache_app`so that they can
+ # be reinstalled from the cache without querying the remote (i.e. an
+ # alternative to remote)
+ #
+ # This is stored with the app and source plugins should try to provide
+ # specs and install only from this cache when `cached!` is called.
+ #
+ # This cache is different from the internal caching that can be done
+ # at sub paths of `cache_path` (from API). This can be though as caching
+ # by bundler.
+ def cache(spec, custom_path = nil)
+ new_cache_path = app_cache_path(custom_path)
+
+ FileUtils.rm_rf(new_cache_path)
+ FileUtils.cp_r(install_path, new_cache_path)
+ FileUtils.touch(app_cache_path.join(".bundlecache"))
+ end
+
+ # This shall check if two source object represent the same source.
+ #
+ # The comparison shall take place only on the attribute that can be
+ # inferred from the options passed from Gemfile and not on attibutes
+ # that are used to pin down the gem to specific version (e.g. Git
+ # sources should compare on branch and tag but not on commit hash)
+ #
+ # The sources objects are constructed from Gemfile as well as from
+ # lockfile. To converge the sources, it is necessary that they match.
+ #
+ # The same applies for `eql?` and `hash`
+ def ==(other)
+ other.is_a?(self.class) && uri == other.uri
+ end
+
+ # When overriding `eql?` please preserve the behaviour as mentioned in
+ # docstring for `==` method.
+ alias_method :eql?, :==
+
+ # When overriding `hash` please preserve the behaviour as mentioned in
+ # docstring for `==` method, i.e. two methods equal by above comparison
+ # should have same hash.
+ def hash
+ [self.class, uri].hash
+ end
+
+ # A helper method, not necessary if not used internally.
+ def installed?
+ File.directory?(install_path)
+ end
+
+ # The full path where the plugin should cache the gem so that it can be
+ # installed latter.
+ #
+ # Note: Do not override if you don't know what you are doing.
+ def app_cache_path(custom_path = nil)
+ @app_cache_path ||= Bundler.app_cache(custom_path).join(app_cache_dirname)
+ end
+
+ # Used by definition.
+ #
+ # Note: Do not override if you don't know what you are doing.
+ def unmet_deps
+ specs.unmet_dependency_names
+ end
+
+ # Note: Do not override if you don't know what you are doing.
+ def can_lock?(spec)
+ spec.source == self
+ end
+
+ # Generates the content to be entered into the lockfile.
+ # Saves type and remote and also calls to `options_to_lock`.
+ #
+ # Plugin should use `options_to_lock` to save information in lockfile
+ # and not override this.
+ #
+ # Note: Do not override if you don't know what you are doing.
+ def to_lock
+ out = String.new("#{LockfileParser::PLUGIN}\n")
+ out << " remote: #{@uri}\n"
+ out << " type: #{@type}\n"
+ options_to_lock.each do |opt, value|
+ out << " #{opt}: #{value}\n"
+ end
+ out << " specs:\n"
+ end
+
+ def to_s
+ "plugin source for #{options[:type]} with uri #{uri}"
+ end
+
+ # Note: Do not override if you don't know what you are doing.
+ def include?(other)
+ other == self
+ end
+
+ def uri_hash
+ SharedHelpers.digest(:SHA1).hexdigest(uri)
+ end
+
+ # Note: Do not override if you don't know what you are doing.
+ def gem_install_dir
+ Bundler.install_path
+ end
+
+ # It is used to obtain the full_gem_path.
+ #
+ # spec's loaded_from path is expanded against this to get full_gem_path
+ #
+ # Note: Do not override if you don't know what you are doing.
+ def root
+ Bundler.root
+ end
+
+ # @private
+ # Returns true
+ def bundler_plugin_api_source?
+ true
+ end
+
+ # @private
+ # This API on source might not be stable, and for now we expect plugins
+ # to download all specs in `#specs`, so we implement the method for
+ # compatibility purposes and leave it undocumented (and don't support)
+ # overriding it)
+ def double_check_for(*); end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/plugin/dsl.rb b/lib/bundler/plugin/dsl.rb
new file mode 100644
index 00000000000000..4bfc8437e0e656
--- /dev/null
+++ b/lib/bundler/plugin/dsl.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+module Bundler
+ module Plugin
+ # Dsl to parse the Gemfile looking for plugins to install
+ class DSL < Bundler::Dsl
+ class PluginGemfileError < PluginError; end
+ alias_method :_gem, :gem # To use for plugin installation as gem
+
+ # So that we don't have to override all there methods to dummy ones
+ # explicitly.
+ # They will be handled by method_missing
+ [:gemspec, :gem, :path, :install_if, :platforms, :env].each {|m| undef_method m }
+
+ # This lists the plugins that was added automatically and not specified by
+ # the user.
+ #
+ # When we encounter :type attribute with a source block, we add a plugin
+ # by name bundler-source- to list of plugins to be installed.
+ #
+ # These plugins are optional and are not installed when there is conflict
+ # with any other plugin.
+ attr_reader :inferred_plugins
+
+ def initialize
+ super
+ @sources = Plugin::SourceList.new
+ @inferred_plugins = [] # The source plugins inferred from :type
+ end
+
+ def plugin(name, *args)
+ _gem(name, *args)
+ end
+
+ def method_missing(name, *args)
+ raise PluginGemfileError, "Undefined local variable or method `#{name}' for Gemfile" unless Bundler::Dsl.method_defined? name
+ end
+
+ def source(source, *args, &blk)
+ options = args.last.is_a?(Hash) ? args.pop.dup : {}
+ options = normalize_hash(options)
+ return super unless options.key?("type")
+
+ plugin_name = "bundler-source-#{options["type"]}"
+
+ return if @dependencies.any? {|d| d.name == plugin_name }
+
+ plugin(plugin_name)
+ @inferred_plugins << plugin_name
+ end
+ end
+ end
+end
diff --git a/lib/bundler/plugin/events.rb b/lib/bundler/plugin/events.rb
new file mode 100644
index 00000000000000..bc037d1af507b7
--- /dev/null
+++ b/lib/bundler/plugin/events.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+module Bundler
+ module Plugin
+ module Events
+ def self.define(const, event)
+ const = const.to_sym.freeze
+ if const_defined?(const) && const_get(const) != event
+ raise ArgumentError, "Attempting to reassign #{const} to a different value"
+ end
+ const_set(const, event) unless const_defined?(const)
+ @events ||= {}
+ @events[event] = const
+ end
+ private_class_method :define
+
+ def self.reset
+ @events.each_value do |const|
+ remove_const(const)
+ end
+ @events = nil
+ end
+ private_class_method :reset
+
+ # Check if an event has been defined
+ # @param event [String] An event to check
+ # @return [Boolean] A boolean indicating if the event has been defined
+ def self.defined_event?(event)
+ @events ||= {}
+ @events.key?(event)
+ end
+
+ # @!parse
+ # A hook called before each individual gem is installed
+ # Includes a Bundler::ParallelInstaller::SpecInstallation.
+ # No state, error, post_install_message will be present as nothing has installed yet
+ # GEM_BEFORE_INSTALL = "before-install"
+ define :GEM_BEFORE_INSTALL, "before-install"
+
+ # @!parse
+ # A hook called after each individual gem is installed
+ # Includes a Bundler::ParallelInstaller::SpecInstallation.
+ # - If state is failed, an error will be present.
+ # - If state is success, a post_install_message may be present.
+ # GEM_AFTER_INSTALL = "after-install"
+ define :GEM_AFTER_INSTALL, "after-install"
+
+ # @!parse
+ # A hook called before any gems install
+ # Includes an Array of Bundler::Dependency objects
+ # GEM_BEFORE_INSTALL_ALL = "before-install-all"
+ define :GEM_BEFORE_INSTALL_ALL, "before-install-all"
+
+ # @!parse
+ # A hook called after any gems install
+ # Includes an Array of Bundler::Dependency objects
+ # GEM_AFTER_INSTALL_ALL = "after-install-all"
+ define :GEM_AFTER_INSTALL_ALL, "after-install-all"
+ end
+ end
+end
diff --git a/lib/bundler/plugin/index.rb b/lib/bundler/plugin/index.rb
new file mode 100644
index 00000000000000..f09587dfda7bb5
--- /dev/null
+++ b/lib/bundler/plugin/index.rb
@@ -0,0 +1,162 @@
+# frozen_string_literal: true
+
+module Bundler
+ # Manages which plugins are installed and their sources. This also is supposed to map
+ # which plugin does what (currently the features are not implemented so this class is
+ # now a stub class).
+ module Plugin
+ class Index
+ class CommandConflict < PluginError
+ def initialize(plugin, commands)
+ msg = "Command(s) `#{commands.join("`, `")}` declared by #{plugin} are already registered."
+ super msg
+ end
+ end
+
+ class SourceConflict < PluginError
+ def initialize(plugin, sources)
+ msg = "Source(s) `#{sources.join("`, `")}` declared by #{plugin} are already registered."
+ super msg
+ end
+ end
+
+ attr_reader :commands
+
+ def initialize
+ @plugin_paths = {}
+ @commands = {}
+ @sources = {}
+ @hooks = {}
+ @load_paths = {}
+
+ begin
+ load_index(global_index_file, true)
+ rescue GenericSystemCallError
+ # no need to fail when on a read-only FS, for example
+ nil
+ end
+ load_index(local_index_file) if SharedHelpers.in_bundle?
+ end
+
+ # This function is to be called when a new plugin is installed. This
+ # function shall add the functions of the plugin to existing maps and also
+ # the name to source location.
+ #
+ # @param [String] name of the plugin to be registered
+ # @param [String] path where the plugin is installed
+ # @param [Array] load_paths for the plugin
+ # @param [Array] commands that are handled by the plugin
+ # @param [Array] sources that are handled by the plugin
+ def register_plugin(name, path, load_paths, commands, sources, hooks)
+ old_commands = @commands.dup
+
+ common = commands & @commands.keys
+ raise CommandConflict.new(name, common) unless common.empty?
+ commands.each {|c| @commands[c] = name }
+
+ common = sources & @sources.keys
+ raise SourceConflict.new(name, common) unless common.empty?
+ sources.each {|k| @sources[k] = name }
+
+ hooks.each {|e| (@hooks[e] ||= []) << name }
+
+ @plugin_paths[name] = path
+ @load_paths[name] = load_paths
+ save_index
+ rescue StandardError
+ @commands = old_commands
+ raise
+ end
+
+ # Path of default index file
+ def index_file
+ Plugin.root.join("index")
+ end
+
+ # Path where the global index file is stored
+ def global_index_file
+ Plugin.global_root.join("index")
+ end
+
+ # Path where the local index file is stored
+ def local_index_file
+ Plugin.local_root.join("index")
+ end
+
+ def plugin_path(name)
+ Pathname.new @plugin_paths[name]
+ end
+
+ def load_paths(name)
+ @load_paths[name]
+ end
+
+ # Fetch the name of plugin handling the command
+ def command_plugin(command)
+ @commands[command]
+ end
+
+ def installed?(name)
+ @plugin_paths[name]
+ end
+
+ def source?(source)
+ @sources.key? source
+ end
+
+ def source_plugin(name)
+ @sources[name]
+ end
+
+ # Returns the list of plugin names handling the passed event
+ def hook_plugins(event)
+ @hooks[event] || []
+ end
+
+ private
+
+ # Reads the index file from the directory and initializes the instance
+ # variables.
+ #
+ # It skips the sources if the second param is true
+ # @param [Pathname] index file path
+ # @param [Boolean] is the index file global index
+ def load_index(index_file, global = false)
+ SharedHelpers.filesystem_access(index_file, :read) do |index_f|
+ valid_file = index_f && index_f.exist? && !index_f.size.zero?
+ break unless valid_file
+
+ data = index_f.read
+
+ require "bundler/yaml_serializer"
+ index = YAMLSerializer.load(data)
+
+ @commands.merge!(index["commands"])
+ @hooks.merge!(index["hooks"])
+ @load_paths.merge!(index["load_paths"])
+ @plugin_paths.merge!(index["plugin_paths"])
+ @sources.merge!(index["sources"]) unless global
+ end
+ end
+
+ # Should be called when any of the instance variables change. Stores the
+ # instance variables in YAML format. (The instance variables are supposed
+ # to be only String key value pairs)
+ def save_index
+ index = {
+ "commands" => @commands,
+ "hooks" => @hooks,
+ "load_paths" => @load_paths,
+ "plugin_paths" => @plugin_paths,
+ "sources" => @sources,
+ }
+
+ require "bundler/yaml_serializer"
+ SharedHelpers.filesystem_access(index_file) do |index_f|
+ FileUtils.mkdir_p(index_f.dirname)
+ File.open(index_f, "w") {|f| f.puts YAMLSerializer.dump(index) }
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/plugin/installer.rb b/lib/bundler/plugin/installer.rb
new file mode 100644
index 00000000000000..5379c38979c97d
--- /dev/null
+++ b/lib/bundler/plugin/installer.rb
@@ -0,0 +1,96 @@
+# frozen_string_literal: true
+
+module Bundler
+ # Handles the installation of plugin in appropriate directories.
+ #
+ # This class is supposed to be wrapper over the existing gem installation infra
+ # but currently it itself handles everything as the Source's subclasses (e.g. Source::RubyGems)
+ # are heavily dependent on the Gemfile.
+ module Plugin
+ class Installer
+ autoload :Rubygems, "bundler/plugin/installer/rubygems"
+ autoload :Git, "bundler/plugin/installer/git"
+
+ def install(names, options)
+ version = options[:version] || [">= 0"]
+ Bundler.settings.temporary(:lockfile_uses_separate_rubygems_sources => false, :disable_multisource => false) do
+ if options[:git]
+ install_git(names, version, options)
+ else
+ sources = options[:source] || Bundler.rubygems.sources
+ install_rubygems(names, version, sources)
+ end
+ end
+ end
+
+ # Installs the plugin from Definition object created by limited parsing of
+ # Gemfile searching for plugins to be installed
+ #
+ # @param [Definition] definition object
+ # @return [Hash] map of names to their specs they are installed with
+ def install_definition(definition)
+ def definition.lock(*); end
+ definition.resolve_remotely!
+ specs = definition.specs
+
+ install_from_specs specs
+ end
+
+ private
+
+ def install_git(names, version, options)
+ uri = options.delete(:git)
+ options["uri"] = uri
+
+ source_list = SourceList.new
+ source_list.add_git_source(options)
+
+ # To support both sources
+ if options[:source]
+ source_list.add_rubygems_source("remotes" => options[:source])
+ end
+
+ deps = names.map {|name| Dependency.new name, version }
+
+ definition = Definition.new(nil, deps, source_list, true)
+ install_definition(definition)
+ end
+
+ # Installs the plugin from rubygems source and returns the path where the
+ # plugin was installed
+ #
+ # @param [String] name of the plugin gem to search in the source
+ # @param [Array] version of the gem to install
+ # @param [String, Array] source(s) to resolve the gem
+ #
+ # @return [Hash] map of names to the specs of plugins installed
+ def install_rubygems(names, version, sources)
+ deps = names.map {|name| Dependency.new name, version }
+
+ source_list = SourceList.new
+ source_list.add_rubygems_source("remotes" => sources)
+
+ definition = Definition.new(nil, deps, source_list, true)
+ install_definition(definition)
+ end
+
+ # Installs the plugins and deps from the provided specs and returns map of
+ # gems to their paths
+ #
+ # @param specs to install
+ #
+ # @return [Hash] map of names to the specs
+ def install_from_specs(specs)
+ paths = {}
+
+ specs.each do |spec|
+ spec.source.install spec
+
+ paths[spec.name] = spec
+ end
+
+ paths
+ end
+ end
+ end
+end
diff --git a/lib/bundler/plugin/installer/git.rb b/lib/bundler/plugin/installer/git.rb
new file mode 100644
index 00000000000000..fbb6c5e40e6f3e
--- /dev/null
+++ b/lib/bundler/plugin/installer/git.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+module Bundler
+ module Plugin
+ class Installer
+ class Git < Bundler::Source::Git
+ def cache_path
+ @cache_path ||= begin
+ git_scope = "#{base_name}-#{uri_hash}"
+
+ Plugin.cache.join("bundler", "git", git_scope)
+ end
+ end
+
+ def install_path
+ @install_path ||= begin
+ git_scope = "#{base_name}-#{shortref_for_path(revision)}"
+
+ Plugin.root.join("bundler", "gems", git_scope)
+ end
+ end
+
+ def version_message(spec)
+ "#{spec.name} #{spec.version}"
+ end
+
+ def root
+ Plugin.root
+ end
+
+ def generate_bin(spec, disable_extensions = false)
+ # Need to find a way without code duplication
+ # For now, we can ignore this
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/plugin/installer/rubygems.rb b/lib/bundler/plugin/installer/rubygems.rb
new file mode 100644
index 00000000000000..7ae74fa93b3bf5
--- /dev/null
+++ b/lib/bundler/plugin/installer/rubygems.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+module Bundler
+ module Plugin
+ class Installer
+ class Rubygems < Bundler::Source::Rubygems
+ def version_message(spec)
+ "#{spec.name} #{spec.version}"
+ end
+
+ private
+
+ def requires_sudo?
+ false # Will change on implementation of project level plugins
+ end
+
+ def rubygems_dir
+ Plugin.root
+ end
+
+ def cache_path
+ Plugin.cache
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/plugin/source_list.rb b/lib/bundler/plugin/source_list.rb
new file mode 100644
index 00000000000000..f0e212205ff7a8
--- /dev/null
+++ b/lib/bundler/plugin/source_list.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+module Bundler
+ # SourceList object to be used while parsing the Gemfile, setting the
+ # approptiate options to be used with Source classes for plugin installation
+ module Plugin
+ class SourceList < Bundler::SourceList
+ def add_git_source(options = {})
+ add_source_to_list Plugin::Installer::Git.new(options), git_sources
+ end
+
+ def add_rubygems_source(options = {})
+ add_source_to_list Plugin::Installer::Rubygems.new(options), @rubygems_sources
+ end
+
+ def all_sources
+ path_sources + git_sources + rubygems_sources + [metadata_source]
+ end
+
+ private
+
+ def rubygems_aggregate_class
+ Plugin::Installer::Rubygems
+ end
+ end
+ end
+end
diff --git a/lib/bundler/process_lock.rb b/lib/bundler/process_lock.rb
new file mode 100644
index 00000000000000..cba4fcdba5f66f
--- /dev/null
+++ b/lib/bundler/process_lock.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+module Bundler
+ class ProcessLock
+ def self.lock(bundle_path = Bundler.bundle_path)
+ lock_file_path = File.join(bundle_path, "bundler.lock")
+ has_lock = false
+
+ File.open(lock_file_path, "w") do |f|
+ f.flock(File::LOCK_EX)
+ has_lock = true
+ yield
+ f.flock(File::LOCK_UN)
+ end
+ rescue Errno::EACCES, Errno::ENOLCK, *[SharedHelpers.const_get_safely(:ENOTSUP, Errno)].compact
+ # In the case the user does not have access to
+ # create the lock file or is using NFS where
+ # locks are not available we skip locking.
+ yield
+ ensure
+ FileUtils.rm_f(lock_file_path) if has_lock
+ end
+ end
+end
diff --git a/lib/bundler/psyched_yaml.rb b/lib/bundler/psyched_yaml.rb
new file mode 100644
index 00000000000000..e654416a5a296f
--- /dev/null
+++ b/lib/bundler/psyched_yaml.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+# Psych could be a gem, so try to ask for it
+begin
+ gem "psych"
+rescue LoadError
+end if defined?(gem)
+
+# Psych could be in the stdlib
+# but it's too late if Syck is already loaded
+begin
+ require "psych" unless defined?(Syck)
+rescue LoadError
+ # Apparently Psych wasn't available. Oh well.
+end
+
+# At least load the YAML stdlib, whatever that may be
+require "yaml" unless defined?(YAML.dump)
+
+module Bundler
+ # On encountering invalid YAML,
+ # Psych raises Psych::SyntaxError
+ if defined?(::Psych::SyntaxError)
+ YamlLibrarySyntaxError = ::Psych::SyntaxError
+ else # Syck raises ArgumentError
+ YamlLibrarySyntaxError = ::ArgumentError
+ end
+end
+
+require "bundler/deprecate"
+begin
+ Bundler::Deprecate.skip_during do
+ require "rubygems/safe_yaml"
+ end
+rescue LoadError
+ # it's OK if the file isn't there
+end
diff --git a/lib/bundler/remote_specification.rb b/lib/bundler/remote_specification.rb
new file mode 100644
index 00000000000000..23e1234330773f
--- /dev/null
+++ b/lib/bundler/remote_specification.rb
@@ -0,0 +1,114 @@
+# frozen_string_literal: true
+
+require "uri"
+
+module Bundler
+ # Represents a lazily loaded gem specification, where the full specification
+ # is on the source server in rubygems' "quick" index. The proxy object is to
+ # be seeded with what we're given from the source's abbreviated index - the
+ # full specification will only be fetched when necessary.
+ class RemoteSpecification
+ include MatchPlatform
+ include Comparable
+
+ attr_reader :name, :version, :platform
+ attr_writer :dependencies
+ attr_accessor :source, :remote
+
+ def initialize(name, version, platform, spec_fetcher)
+ @name = name
+ @version = Gem::Version.create version
+ @platform = platform
+ @spec_fetcher = spec_fetcher
+ @dependencies = nil
+ end
+
+ # Needed before installs, since the arch matters then and quick
+ # specs don't bother to include the arch in the platform string
+ def fetch_platform
+ @platform = _remote_specification.platform
+ end
+
+ def full_name
+ if platform == Gem::Platform::RUBY || platform.nil?
+ "#{@name}-#{@version}"
+ else
+ "#{@name}-#{@version}-#{platform}"
+ end
+ end
+
+ # Compare this specification against another object. Using sort_obj
+ # is compatible with Gem::Specification and other Bundler or RubyGems
+ # objects. Otherwise, use the default Object comparison.
+ def <=>(other)
+ if other.respond_to?(:sort_obj)
+ sort_obj <=> other.sort_obj
+ else
+ super
+ end
+ end
+
+ # Because Rubyforge cannot be trusted to provide valid specifications
+ # once the remote gem is downloaded, the backend specification will
+ # be swapped out.
+ def __swap__(spec)
+ SharedHelpers.ensure_same_dependencies(self, dependencies, spec.dependencies)
+ @_remote_specification = spec
+ end
+
+ # Create a delegate used for sorting. This strategy is copied from
+ # RubyGems 2.23 and ensures that Bundler's specifications can be
+ # compared and sorted with RubyGems' own specifications.
+ #
+ # @see #<=>
+ # @see Gem::Specification#sort_obj
+ #
+ # @return [Array] an object you can use to compare and sort this
+ # specification against other specifications
+ def sort_obj
+ [@name, @version, @platform == Gem::Platform::RUBY ? -1 : 1]
+ end
+
+ def to_s
+ "#<#{self.class} name=#{name} version=#{version} platform=#{platform}>"
+ end
+
+ def dependencies
+ @dependencies ||= begin
+ deps = method_missing(:dependencies)
+
+ # allow us to handle when the specs dependencies are an array of array of string
+ # see https://github.com/bundler/bundler/issues/5797
+ deps = deps.map {|d| d.is_a?(Gem::Dependency) ? d : Gem::Dependency.new(*d) }
+
+ deps
+ end
+ end
+
+ def git_version
+ return unless loaded_from && source.is_a?(Bundler::Source::Git)
+ " #{source.revision[0..6]}"
+ end
+
+ private
+
+ def to_ary
+ nil
+ end
+
+ def _remote_specification
+ @_remote_specification ||= @spec_fetcher.fetch_spec([@name, @version, @platform])
+ @_remote_specification || raise(GemspecError, "Gemspec data for #{full_name} was" \
+ " missing from the server! Try installing with `--full-index` as a workaround.")
+ end
+
+ def method_missing(method, *args, &blk)
+ _remote_specification.send(method, *args, &blk)
+ end
+
+ def respond_to?(method, include_all = false)
+ super || _remote_specification.respond_to?(method, include_all)
+ end
+ public :respond_to?
+ end
+end
diff --git a/lib/bundler/resolver.rb b/lib/bundler/resolver.rb
new file mode 100644
index 00000000000000..545b4cc88ace86
--- /dev/null
+++ b/lib/bundler/resolver.rb
@@ -0,0 +1,373 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Resolver
+ require "bundler/vendored_molinillo"
+ require "bundler/resolver/spec_group"
+
+ # Figures out the best possible configuration of gems that satisfies
+ # the list of passed dependencies and any child dependencies without
+ # causing any gem activation errors.
+ #
+ # ==== Parameters
+ # *dependencies:: The list of dependencies to resolve
+ #
+ # ==== Returns
+ # ,nil:: If the list of dependencies can be resolved, a
+ # collection of gemspecs is returned. Otherwise, nil is returned.
+ def self.resolve(requirements, index, source_requirements = {}, base = [], gem_version_promoter = GemVersionPromoter.new, additional_base_requirements = [], platforms = nil)
+ platforms = Set.new(platforms) if platforms
+ base = SpecSet.new(base) unless base.is_a?(SpecSet)
+ resolver = new(index, source_requirements, base, gem_version_promoter, additional_base_requirements, platforms)
+ result = resolver.start(requirements)
+ SpecSet.new(result)
+ end
+
+ def initialize(index, source_requirements, base, gem_version_promoter, additional_base_requirements, platforms)
+ @index = index
+ @source_requirements = source_requirements
+ @base = base
+ @resolver = Molinillo::Resolver.new(self, self)
+ @search_for = {}
+ @base_dg = Molinillo::DependencyGraph.new
+ @base.each do |ls|
+ dep = Dependency.new(ls.name, ls.version)
+ @base_dg.add_vertex(ls.name, DepProxy.new(dep, ls.platform), true)
+ end
+ additional_base_requirements.each {|d| @base_dg.add_vertex(d.name, d) }
+ @platforms = platforms
+ @gem_version_promoter = gem_version_promoter
+ @allow_bundler_dependency_conflicts = Bundler.feature_flag.allow_bundler_dependency_conflicts?
+ @lockfile_uses_separate_rubygems_sources = Bundler.feature_flag.lockfile_uses_separate_rubygems_sources?
+ @use_gvp = Bundler.feature_flag.use_gem_version_promoter_for_major_updates? || !@gem_version_promoter.major?
+ end
+
+ def start(requirements)
+ @gem_version_promoter.prerelease_specified = @prerelease_specified = {}
+ requirements.each {|dep| @prerelease_specified[dep.name] ||= dep.prerelease? }
+
+ verify_gemfile_dependencies_are_found!(requirements)
+ dg = @resolver.resolve(requirements, @base_dg)
+ dg.map(&:payload).
+ reject {|sg| sg.name.end_with?("\0") }.
+ map(&:to_specs).flatten
+ rescue Molinillo::VersionConflict => e
+ message = version_conflict_message(e)
+ raise VersionConflict.new(e.conflicts.keys.uniq, message)
+ rescue Molinillo::CircularDependencyError => e
+ names = e.dependencies.sort_by(&:name).map {|d| "gem '#{d.name}'" }
+ raise CyclicDependencyError, "Your bundle requires gems that depend" \
+ " on each other, creating an infinite loop. Please remove" \
+ " #{names.count > 1 ? "either " : ""}#{names.join(" or ")}" \
+ " and try again."
+ end
+
+ include Molinillo::UI
+
+ # Conveys debug information to the user.
+ #
+ # @param [Integer] depth the current depth of the resolution process.
+ # @return [void]
+ def debug(depth = 0)
+ return unless debug?
+ debug_info = yield
+ debug_info = debug_info.inspect unless debug_info.is_a?(String)
+ STDERR.puts debug_info.split("\n").map {|s| " " * depth + s }
+ end
+
+ def debug?
+ return @debug_mode if defined?(@debug_mode)
+ @debug_mode = ENV["DEBUG_RESOLVER"] || ENV["DEBUG_RESOLVER_TREE"] || false
+ end
+
+ def before_resolution
+ Bundler.ui.info "Resolving dependencies...", debug?
+ end
+
+ def after_resolution
+ Bundler.ui.info ""
+ end
+
+ def indicate_progress
+ Bundler.ui.info ".", false unless debug?
+ end
+
+ include Molinillo::SpecificationProvider
+
+ def dependencies_for(specification)
+ specification.dependencies_for_activated_platforms
+ end
+
+ def search_for(dependency)
+ platform = dependency.__platform
+ dependency = dependency.dep unless dependency.is_a? Gem::Dependency
+ search = @search_for[dependency] ||= begin
+ index = index_for(dependency)
+ results = index.search(dependency, @base[dependency.name])
+
+ if vertex = @base_dg.vertex_named(dependency.name)
+ locked_requirement = vertex.payload.requirement
+ end
+
+ if !@prerelease_specified[dependency.name] && (!@use_gvp || locked_requirement.nil?)
+ # Move prereleases to the beginning of the list, so they're considered
+ # last during resolution.
+ pre, results = results.partition {|spec| spec.version.prerelease? }
+ results = pre + results
+ end
+
+ spec_groups = if results.any?
+ nested = []
+ results.each do |spec|
+ version, specs = nested.last
+ if version == spec.version
+ specs << spec
+ else
+ nested << [spec.version, [spec]]
+ end
+ end
+ nested.reduce([]) do |groups, (version, specs)|
+ next groups if locked_requirement && !locked_requirement.satisfied_by?(version)
+ spec_group = SpecGroup.new(specs)
+ spec_group.ignores_bundler_dependencies = @allow_bundler_dependency_conflicts
+ groups << spec_group
+ end
+ else
+ []
+ end
+ # GVP handles major itself, but it's still a bit risky to trust it with it
+ # until we get it settled with new behavior. For 2.x it can take over all cases.
+ if !@use_gvp
+ spec_groups
+ else
+ @gem_version_promoter.sort_versions(dependency, spec_groups)
+ end
+ end
+ search.select {|sg| sg.for?(platform) }.each {|sg| sg.activate_platform!(platform) }
+ end
+
+ def index_for(dependency)
+ source = @source_requirements[dependency.name]
+ if source
+ source.specs
+ elsif @lockfile_uses_separate_rubygems_sources
+ Index.build do |idx|
+ if dependency.all_sources
+ dependency.all_sources.each {|s| idx.add_source(s.specs) if s }
+ else
+ idx.add_source @source_requirements[:default].specs
+ end
+ end
+ else
+ @index
+ end
+ end
+
+ def name_for(dependency)
+ dependency.name
+ end
+
+ def name_for_explicit_dependency_source
+ Bundler.default_gemfile.basename.to_s
+ rescue
+ "Gemfile"
+ end
+
+ def name_for_locking_dependency_source
+ Bundler.default_lockfile.basename.to_s
+ rescue
+ "Gemfile.lock"
+ end
+
+ def requirement_satisfied_by?(requirement, activated, spec)
+ return false unless requirement.matches_spec?(spec) || spec.source.is_a?(Source::Gemspec)
+ spec.activate_platform!(requirement.__platform) if !@platforms || @platforms.include?(requirement.__platform)
+ true
+ end
+
+ def relevant_sources_for_vertex(vertex)
+ if vertex.root?
+ [@source_requirements[vertex.name]]
+ elsif @lockfile_uses_separate_rubygems_sources
+ vertex.recursive_predecessors.map do |v|
+ @source_requirements[v.name]
+ end << @source_requirements[:default]
+ end
+ end
+
+ def sort_dependencies(dependencies, activated, conflicts)
+ dependencies.sort_by do |dependency|
+ dependency.all_sources = relevant_sources_for_vertex(activated.vertex_named(dependency.name))
+ name = name_for(dependency)
+ vertex = activated.vertex_named(name)
+ [
+ @base_dg.vertex_named(name) ? 0 : 1,
+ vertex.payload ? 0 : 1,
+ vertex.root? ? 0 : 1,
+ amount_constrained(dependency),
+ conflicts[name] ? 0 : 1,
+ vertex.payload ? 0 : search_for(dependency).count,
+ self.class.platform_sort_key(dependency.__platform),
+ ]
+ end
+ end
+
+ # Sort platforms from most general to most specific
+ def self.sort_platforms(platforms)
+ platforms.sort_by do |platform|
+ platform_sort_key(platform)
+ end
+ end
+
+ def self.platform_sort_key(platform)
+ return ["", "", ""] if Gem::Platform::RUBY == platform
+ platform.to_a.map {|part| part || "" }
+ end
+
+ private
+
+ # returns an integer \in (-\infty, 0]
+ # a number closer to 0 means the dependency is less constraining
+ #
+ # dependencies w/ 0 or 1 possibilities (ignoring version requirements)
+ # are given very negative values, so they _always_ sort first,
+ # before dependencies that are unconstrained
+ def amount_constrained(dependency)
+ @amount_constrained ||= {}
+ @amount_constrained[dependency.name] ||= begin
+ if (base = @base[dependency.name]) && !base.empty?
+ dependency.requirement.satisfied_by?(base.first.version) ? 0 : 1
+ else
+ all = index_for(dependency).search(dependency.name).size
+
+ if all <= 1
+ all - 1_000_000
+ else
+ search = search_for(dependency)
+ search = @prerelease_specified[dependency.name] ? search.count : search.count {|s| !s.version.prerelease? }
+ search - all
+ end
+ end
+ end
+ end
+
+ def verify_gemfile_dependencies_are_found!(requirements)
+ requirements.each do |requirement|
+ name = requirement.name
+ next if name == "bundler"
+ next unless search_for(requirement).empty?
+
+ cache_message = begin
+ " or in gems cached in #{Bundler.settings.app_cache_path}" if Bundler.app_cache.exist?
+ rescue GemfileNotFound
+ nil
+ end
+
+ if (base = @base[name]) && !base.empty?
+ version = base.first.version
+ message = "You have requested:\n" \
+ " #{name} #{requirement.requirement}\n\n" \
+ "The bundle currently has #{name} locked at #{version}.\n" \
+ "Try running `bundle update #{name}`\n\n" \
+ "If you are updating multiple gems in your Gemfile at once,\n" \
+ "try passing them all to `bundle update`"
+ elsif source = @source_requirements[name]
+ specs = source.specs[name]
+ versions_with_platforms = specs.map {|s| [s.version, s.platform] }
+ message = String.new("Could not find gem '#{SharedHelpers.pretty_dependency(requirement)}' in #{source}#{cache_message}.\n")
+ message << if versions_with_platforms.any?
+ "The source contains '#{name}' at: #{formatted_versions_with_platforms(versions_with_platforms)}"
+ else
+ "The source does not contain any versions of '#{name}'"
+ end
+ else
+ message = "Could not find gem '#{requirement}' in any of the gem sources " \
+ "listed in your Gemfile#{cache_message}."
+ end
+ raise GemNotFound, message
+ end
+ end
+
+ def formatted_versions_with_platforms(versions_with_platforms)
+ version_platform_strs = versions_with_platforms.map do |vwp|
+ version = vwp.first
+ platform = vwp.last
+ version_platform_str = String.new(version.to_s)
+ version_platform_str << " #{platform}" unless platform.nil? || platform == Gem::Platform::RUBY
+ version_platform_str
+ end
+ version_platform_strs.join(", ")
+ end
+
+ def version_conflict_message(e)
+ e.message_with_trees(
+ :solver_name => "Bundler",
+ :possibility_type => "gem",
+ :reduce_trees => lambda do |trees|
+ # called first, because we want to reduce the amount of work required to find maximal empty sets
+ trees = trees.uniq {|t| t.flatten.map {|dep| [dep.name, dep.requirement] } }
+
+ # bail out if tree size is too big for Array#combination to make any sense
+ return trees if trees.size > 15
+ maximal = 1.upto(trees.size).map do |size|
+ trees.map(&:last).flatten(1).combination(size).to_a
+ end.flatten(1).select do |deps|
+ Bundler::VersionRanges.empty?(*Bundler::VersionRanges.for_many(deps.map(&:requirement)))
+ end.min_by(&:size)
+ trees.reject! {|t| !maximal.include?(t.last) } if maximal
+
+ trees = trees.sort_by {|t| t.flatten.map(&:to_s) }
+ trees.uniq! {|t| t.flatten.map {|dep| [dep.name, dep.requirement] } }
+
+ trees.sort_by {|t| t.reverse.map(&:name) }
+ end,
+ :printable_requirement => lambda {|req| SharedHelpers.pretty_dependency(req) },
+ :additional_message_for_conflict => lambda do |o, name, conflict|
+ if name == "bundler"
+ o << %(\n Current Bundler version:\n bundler (#{Bundler::VERSION}))
+ other_bundler_required = !conflict.requirement.requirement.satisfied_by?(Gem::Version.new Bundler::VERSION)
+ end
+
+ if name == "bundler" && other_bundler_required
+ o << "\n"
+ o << "This Gemfile requires a different version of Bundler.\n"
+ o << "Perhaps you need to update Bundler by running `gem install bundler`?\n"
+ end
+ if conflict.locked_requirement
+ o << "\n"
+ o << %(Running `bundle update` will rebuild your snapshot from scratch, using only\n)
+ o << %(the gems in your Gemfile, which may resolve the conflict.\n)
+ elsif !conflict.existing
+ o << "\n"
+
+ relevant_sources = if conflict.requirement.source
+ [conflict.requirement.source]
+ elsif conflict.requirement.all_sources
+ conflict.requirement.all_sources
+ elsif @lockfile_uses_separate_rubygems_sources
+ # every conflict should have an explicit group of sources when we
+ # enforce strict pinning
+ raise "no source set for #{conflict}"
+ else
+ []
+ end.compact.map(&:to_s).uniq.sort
+
+ o << "Could not find gem '#{SharedHelpers.pretty_dependency(conflict.requirement)}'"
+ if conflict.requirement_trees.first.size > 1
+ o << ", which is required by "
+ o << "gem '#{SharedHelpers.pretty_dependency(conflict.requirement_trees.first[-2])}',"
+ end
+ o << " "
+
+ o << if relevant_sources.empty?
+ "in any of the sources.\n"
+ else
+ "in any of the relevant sources:\n #{relevant_sources * "\n "}\n"
+ end
+ end
+ end,
+ :version_for_spec => lambda {|spec| spec.version }
+ )
+ end
+ end
+end
diff --git a/lib/bundler/resolver/spec_group.rb b/lib/bundler/resolver/spec_group.rb
new file mode 100644
index 00000000000000..34d043aed7fcac
--- /dev/null
+++ b/lib/bundler/resolver/spec_group.rb
@@ -0,0 +1,106 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Resolver
+ class SpecGroup
+ include GemHelpers
+
+ attr_accessor :name, :version, :source
+ attr_accessor :ignores_bundler_dependencies
+
+ def initialize(all_specs)
+ raise ArgumentError, "cannot initialize with an empty value" unless exemplary_spec = all_specs.first
+ @name = exemplary_spec.name
+ @version = exemplary_spec.version
+ @source = exemplary_spec.source
+
+ @activated_platforms = []
+ @dependencies = nil
+ @specs = Hash.new do |specs, platform|
+ specs[platform] = select_best_platform_match(all_specs, platform)
+ end
+ @ignores_bundler_dependencies = true
+ end
+
+ def to_specs
+ @activated_platforms.map do |p|
+ next unless s = @specs[p]
+ lazy_spec = LazySpecification.new(name, version, s.platform, source)
+ lazy_spec.dependencies.replace s.dependencies
+ lazy_spec
+ end.compact
+ end
+
+ def activate_platform!(platform)
+ return unless for?(platform)
+ return if @activated_platforms.include?(platform)
+ @activated_platforms << platform
+ end
+
+ def for?(platform)
+ spec = @specs[platform]
+ !spec.nil?
+ end
+
+ def to_s
+ @to_s ||= "#{name} (#{version})"
+ end
+
+ def dependencies_for_activated_platforms
+ dependencies = @activated_platforms.map {|p| __dependencies[p] }
+ metadata_dependencies = @activated_platforms.map do |platform|
+ metadata_dependencies(@specs[platform], platform)
+ end
+ dependencies.concat(metadata_dependencies).flatten
+ end
+
+ def ==(other)
+ return unless other.is_a?(SpecGroup)
+ name == other.name &&
+ version == other.version &&
+ source == other.source
+ end
+
+ def eql?(other)
+ name.eql?(other.name) &&
+ version.eql?(other.version) &&
+ source.eql?(other.source)
+ end
+
+ def hash
+ to_s.hash ^ source.hash
+ end
+
+ private
+
+ def __dependencies
+ @dependencies = Hash.new do |dependencies, platform|
+ dependencies[platform] = []
+ if spec = @specs[platform]
+ spec.dependencies.each do |dep|
+ next if dep.type == :development
+ next if @ignores_bundler_dependencies && dep.name == "bundler".freeze
+ dependencies[platform] << DepProxy.new(dep, platform)
+ end
+ end
+ dependencies[platform]
+ end
+ end
+
+ def metadata_dependencies(spec, platform)
+ return [] unless spec
+ # Only allow endpoint specifications since they won't hit the network to
+ # fetch the full gemspec when calling required_ruby_version
+ return [] if !spec.is_a?(EndpointSpecification) && !spec.is_a?(Gem::Specification)
+ dependencies = []
+ if !spec.required_ruby_version.nil? && !spec.required_ruby_version.none?
+ dependencies << DepProxy.new(Gem::Dependency.new("ruby\0", spec.required_ruby_version), platform)
+ end
+ if !spec.required_rubygems_version.nil? && !spec.required_rubygems_version.none?
+ dependencies << DepProxy.new(Gem::Dependency.new("rubygems\0", spec.required_rubygems_version), platform)
+ end
+ dependencies
+ end
+ end
+ end
+end
diff --git a/lib/bundler/retry.rb b/lib/bundler/retry.rb
new file mode 100644
index 00000000000000..244606dcc93d37
--- /dev/null
+++ b/lib/bundler/retry.rb
@@ -0,0 +1,66 @@
+# frozen_string_literal: true
+
+module Bundler
+ # General purpose class for retrying code that may fail
+ class Retry
+ attr_accessor :name, :total_runs, :current_run
+
+ class << self
+ def default_attempts
+ default_retries + 1
+ end
+ alias_method :attempts, :default_attempts
+
+ def default_retries
+ Bundler.settings[:retry]
+ end
+ end
+
+ def initialize(name, exceptions = nil, retries = self.class.default_retries)
+ @name = name
+ @retries = retries
+ @exceptions = Array(exceptions) || []
+ @total_runs = @retries + 1 # will run once, then upto attempts.times
+ end
+
+ def attempt(&block)
+ @current_run = 0
+ @failed = false
+ @error = nil
+ run(&block) while keep_trying?
+ @result
+ end
+ alias_method :attempts, :attempt
+
+ private
+
+ def run(&block)
+ @failed = false
+ @current_run += 1
+ @result = block.call
+ rescue => e
+ fail_attempt(e)
+ end
+
+ def fail_attempt(e)
+ @failed = true
+ if last_attempt? || @exceptions.any? {|k| e.is_a?(k) }
+ Bundler.ui.info "" unless Bundler.ui.debug?
+ raise e
+ end
+ return true unless name
+ Bundler.ui.info "" unless Bundler.ui.debug? # Add new line incase dots preceded this
+ Bundler.ui.warn "Retrying #{name} due to error (#{current_run.next}/#{total_runs}): #{e.class} #{e.message}", Bundler.ui.debug?
+ end
+
+ def keep_trying?
+ return true if current_run.zero?
+ return false if last_attempt?
+ return true if @failed
+ end
+
+ def last_attempt?
+ current_run >= total_runs
+ end
+ end
+end
diff --git a/lib/bundler/ruby_dsl.rb b/lib/bundler/ruby_dsl.rb
new file mode 100644
index 00000000000000..f6ba220cd55904
--- /dev/null
+++ b/lib/bundler/ruby_dsl.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+module Bundler
+ module RubyDsl
+ def ruby(*ruby_version)
+ options = ruby_version.last.is_a?(Hash) ? ruby_version.pop : {}
+ ruby_version.flatten!
+ raise GemfileError, "Please define :engine_version" if options[:engine] && options[:engine_version].nil?
+ raise GemfileError, "Please define :engine" if options[:engine_version] && options[:engine].nil?
+
+ if options[:engine] == "ruby" && options[:engine_version] &&
+ ruby_version != Array(options[:engine_version])
+ raise GemfileEvalError, "ruby_version must match the :engine_version for MRI"
+ end
+ @ruby_version = RubyVersion.new(ruby_version, options[:patchlevel], options[:engine], options[:engine_version])
+ end
+ end
+end
diff --git a/lib/bundler/ruby_version.rb b/lib/bundler/ruby_version.rb
new file mode 100644
index 00000000000000..e6c31a94c917cb
--- /dev/null
+++ b/lib/bundler/ruby_version.rb
@@ -0,0 +1,152 @@
+# frozen_string_literal: true
+
+module Bundler
+ class RubyVersion
+ attr_reader :versions,
+ :patchlevel,
+ :engine,
+ :engine_versions,
+ :gem_version,
+ :engine_gem_version
+
+ def initialize(versions, patchlevel, engine, engine_version)
+ # The parameters to this method must satisfy the
+ # following constraints, which are verified in
+ # the DSL:
+ #
+ # * If an engine is specified, an engine version
+ # must also be specified
+ # * If an engine version is specified, an engine
+ # must also be specified
+ # * If the engine is "ruby", the engine version
+ # must not be specified, or the engine version
+ # specified must match the version.
+
+ @versions = Array(versions).map do |v|
+ op, v = Gem::Requirement.parse(v)
+ op == "=" ? v.to_s : "#{op} #{v}"
+ end
+
+ @gem_version = Gem::Requirement.create(@versions.first).requirements.first.last
+ @input_engine = engine && engine.to_s
+ @engine = engine && engine.to_s || "ruby"
+ @engine_versions = (engine_version && Array(engine_version)) || @versions
+ @engine_gem_version = Gem::Requirement.create(@engine_versions.first).requirements.first.last
+ @patchlevel = patchlevel
+ end
+
+ def to_s(versions = self.versions)
+ output = String.new("ruby #{versions_string(versions)}")
+ output << "p#{patchlevel}" if patchlevel
+ output << " (#{engine} #{versions_string(engine_versions)})" unless engine == "ruby"
+
+ output
+ end
+
+ # @private
+ PATTERN = /
+ ruby\s
+ ([\d.]+) # ruby version
+ (?:p(-?\d+))? # optional patchlevel
+ (?:\s\((\S+)\s(.+)\))? # optional engine info
+ /xo
+
+ # Returns a RubyVersion from the given string.
+ # @param [String] the version string to match.
+ # @return [RubyVersion,Nil] The version if the string is a valid RubyVersion
+ # description, and nil otherwise.
+ def self.from_string(string)
+ new($1, $2, $3, $4) if string =~ PATTERN
+ end
+
+ def single_version_string
+ to_s(gem_version)
+ end
+
+ def ==(other)
+ versions == other.versions &&
+ engine == other.engine &&
+ engine_versions == other.engine_versions &&
+ patchlevel == other.patchlevel
+ end
+
+ def host
+ @host ||= [
+ RbConfig::CONFIG["host_cpu"],
+ RbConfig::CONFIG["host_vendor"],
+ RbConfig::CONFIG["host_os"]
+ ].join("-")
+ end
+
+ # Returns a tuple of these things:
+ # [diff, this, other]
+ # The priority of attributes are
+ # 1. engine
+ # 2. ruby_version
+ # 3. engine_version
+ def diff(other)
+ raise ArgumentError, "Can only diff with a RubyVersion, not a #{other.class}" unless other.is_a?(RubyVersion)
+ if engine != other.engine && @input_engine
+ [:engine, engine, other.engine]
+ elsif versions.empty? || !matches?(versions, other.gem_version)
+ [:version, versions_string(versions), versions_string(other.versions)]
+ elsif @input_engine && !matches?(engine_versions, other.engine_gem_version)
+ [:engine_version, versions_string(engine_versions), versions_string(other.engine_versions)]
+ elsif patchlevel && (!patchlevel.is_a?(String) || !other.patchlevel.is_a?(String) || !matches?(patchlevel, other.patchlevel))
+ [:patchlevel, patchlevel, other.patchlevel]
+ end
+ end
+
+ def versions_string(versions)
+ Array(versions).join(", ")
+ end
+
+ def self.system
+ ruby_engine = if defined?(RUBY_ENGINE) && !RUBY_ENGINE.nil?
+ RUBY_ENGINE.dup
+ else
+ # not defined in ruby 1.8.7
+ "ruby"
+ end
+ # :sob: mocking RUBY_VERSION breaks stuff on 1.8.7
+ ruby_version = ENV.fetch("BUNDLER_SPEC_RUBY_VERSION") { RUBY_VERSION }.dup
+ ruby_engine_version = case ruby_engine
+ when "ruby"
+ ruby_version
+ when "rbx"
+ Rubinius::VERSION.dup
+ when "jruby"
+ JRUBY_VERSION.dup
+ else
+ RUBY_ENGINE_VERSION.dup
+ end
+ patchlevel = RUBY_PATCHLEVEL.to_s
+
+ @ruby_version ||= RubyVersion.new(ruby_version, patchlevel, ruby_engine, ruby_engine_version)
+ end
+
+ def to_gem_version_with_patchlevel
+ @gem_version_with_patch ||= begin
+ Gem::Version.create("#{@gem_version}.#{@patchlevel}")
+ rescue ArgumentError
+ @gem_version
+ end
+ end
+
+ def exact?
+ return @exact if defined?(@exact)
+ @exact = versions.all? {|v| Gem::Requirement.create(v).exact? }
+ end
+
+ private
+
+ def matches?(requirements, version)
+ # Handles RUBY_PATCHLEVEL of -1 for instances like ruby-head
+ return requirements == version if requirements.to_s == "-1" || version.to_s == "-1"
+
+ Array(requirements).all? do |requirement|
+ Gem::Requirement.create(requirement).satisfied_by?(Gem::Version.create(version))
+ end
+ end
+ end
+end
diff --git a/lib/bundler/rubygems_ext.rb b/lib/bundler/rubygems_ext.rb
new file mode 100644
index 00000000000000..e9f0eac3558513
--- /dev/null
+++ b/lib/bundler/rubygems_ext.rb
@@ -0,0 +1,210 @@
+# frozen_string_literal: true
+
+require "pathname"
+
+if defined?(Gem::QuickLoader)
+ # Gem Prelude makes me a sad panda :'(
+ Gem::QuickLoader.load_full_rubygems_library
+end
+
+require "rubygems"
+require "rubygems/specification"
+
+begin
+ # Possible use in Gem::Specification#source below and require
+ # shouldn't be deferred.
+ require "rubygems/source"
+rescue LoadError
+ # Not available before RubyGems 2.0.0, ignore
+ nil
+end
+
+require "bundler/match_platform"
+
+module Gem
+ @loaded_stacks = Hash.new {|h, k| h[k] = [] }
+
+ class Specification
+ attr_accessor :remote, :location, :relative_loaded_from
+
+ if instance_methods(false).map(&:to_sym).include?(:source)
+ remove_method :source
+ attr_writer :source
+ def source
+ (defined?(@source) && @source) || Gem::Source::Installed.new
+ end
+ else
+ attr_accessor :source
+ end
+
+ alias_method :rg_full_gem_path, :full_gem_path
+ alias_method :rg_loaded_from, :loaded_from
+
+ attr_writer :full_gem_path unless instance_methods.include?(:full_gem_path=)
+
+ def full_gem_path
+ # this cannot check source.is_a?(Bundler::Plugin::API::Source)
+ # because that _could_ trip the autoload, and if there are unresolved
+ # gems at that time, this method could be called inside another require,
+ # thus raising with that constant being undefined. Better to check a method
+ if source.respond_to?(:path) || (source.respond_to?(:bundler_plugin_api_source?) && source.bundler_plugin_api_source?)
+ Pathname.new(loaded_from).dirname.expand_path(source.root).to_s.untaint
+ else
+ rg_full_gem_path
+ end
+ end
+
+ def loaded_from
+ if relative_loaded_from
+ source.path.join(relative_loaded_from).to_s
+ else
+ rg_loaded_from
+ end
+ end
+
+ def load_paths
+ return full_require_paths if respond_to?(:full_require_paths)
+
+ require_paths.map do |require_path|
+ if require_path.include?(full_gem_path)
+ require_path
+ else
+ File.join(full_gem_path, require_path)
+ end
+ end
+ end
+
+ if method_defined?(:extension_dir)
+ alias_method :rg_extension_dir, :extension_dir
+ def extension_dir
+ @bundler_extension_dir ||= if source.respond_to?(:extension_dir_name)
+ File.expand_path(File.join(extensions_dir, source.extension_dir_name))
+ else
+ rg_extension_dir
+ end
+ end
+ end
+
+ # RubyGems 1.8+ used only.
+ methods = instance_methods(false)
+ gem_dir = methods.first.is_a?(String) ? "gem_dir" : :gem_dir
+ remove_method :gem_dir if methods.include?(gem_dir)
+ def gem_dir
+ full_gem_path
+ end
+
+ def groups
+ @groups ||= []
+ end
+
+ def git_version
+ return unless loaded_from && source.is_a?(Bundler::Source::Git)
+ " #{source.revision[0..6]}"
+ end
+
+ def to_gemfile(path = nil)
+ gemfile = String.new("source 'https://rubygems.org'\n")
+ gemfile << dependencies_to_gemfile(nondevelopment_dependencies)
+ unless development_dependencies.empty?
+ gemfile << "\n"
+ gemfile << dependencies_to_gemfile(development_dependencies, :development)
+ end
+ gemfile
+ end
+
+ def nondevelopment_dependencies
+ dependencies - development_dependencies
+ end
+
+ private
+
+ def dependencies_to_gemfile(dependencies, group = nil)
+ gemfile = String.new
+ if dependencies.any?
+ gemfile << "group :#{group} do\n" if group
+ dependencies.each do |dependency|
+ gemfile << " " if group
+ gemfile << %(gem "#{dependency.name}")
+ req = dependency.requirements_list.first
+ gemfile << %(, "#{req}") if req
+ gemfile << "\n"
+ end
+ gemfile << "end\n" if group
+ end
+ gemfile
+ end
+ end
+
+ class Dependency
+ attr_accessor :source, :groups, :all_sources
+
+ alias_method :eql?, :==
+
+ def encode_with(coder)
+ to_yaml_properties.each do |ivar|
+ coder[ivar.to_s.sub(/^@/, "")] = instance_variable_get(ivar)
+ end
+ end
+
+ def to_yaml_properties
+ instance_variables.reject {|p| ["@source", "@groups", "@all_sources"].include?(p.to_s) }
+ end
+
+ def to_lock
+ out = String.new(" #{name}")
+ unless requirement.none?
+ reqs = requirement.requirements.map {|o, v| "#{o} #{v}" }.sort.reverse
+ out << " (#{reqs.join(", ")})"
+ end
+ out
+ end
+
+ # Backport of performance enhancement added to RubyGems 1.4
+ def matches_spec?(spec)
+ # name can be a Regexp, so use ===
+ return false unless name === spec.name
+ return true if requirement.none?
+
+ requirement.satisfied_by?(spec.version)
+ end unless allocate.respond_to?(:matches_spec?)
+ end
+
+ class Requirement
+ # Backport of performance enhancement added to RubyGems 1.4
+ def none?
+ # note that it might be tempting to replace with with RubyGems 2.0's
+ # improved implementation. Don't. It requires `DefaultRequirement` to be
+ # defined, and more importantantly, these overrides are not used when the
+ # running RubyGems defines these methods
+ to_s == ">= 0"
+ end unless allocate.respond_to?(:none?)
+
+ # Backport of performance enhancement added to RubyGems 2.2
+ def exact?
+ return false unless @requirements.size == 1
+ @requirements[0][0] == "="
+ end unless allocate.respond_to?(:exact?)
+ end
+
+ class Platform
+ JAVA = Gem::Platform.new("java") unless defined?(JAVA)
+ MSWIN = Gem::Platform.new("mswin32") unless defined?(MSWIN)
+ MSWIN64 = Gem::Platform.new("mswin64") unless defined?(MSWIN64)
+ MINGW = Gem::Platform.new("x86-mingw32") unless defined?(MINGW)
+ X64_MINGW = Gem::Platform.new("x64-mingw32") unless defined?(X64_MINGW)
+
+ undef_method :hash if method_defined? :hash
+ def hash
+ @cpu.hash ^ @os.hash ^ @version.hash
+ end
+
+ undef_method :eql? if method_defined? :eql?
+ alias_method :eql?, :==
+ end
+end
+
+module Gem
+ class Specification
+ include ::Bundler::MatchPlatform
+ end
+end
diff --git a/lib/bundler/rubygems_gem_installer.rb b/lib/bundler/rubygems_gem_installer.rb
new file mode 100644
index 00000000000000..2b7fa8e0f6f250
--- /dev/null
+++ b/lib/bundler/rubygems_gem_installer.rb
@@ -0,0 +1,99 @@
+# frozen_string_literal: true
+
+require "rubygems/installer"
+
+module Bundler
+ class RubyGemsGemInstaller < Gem::Installer
+ unless respond_to?(:at)
+ def self.at(*args)
+ new(*args)
+ end
+ end
+
+ def check_executable_overwrite(filename)
+ # Bundler needs to install gems regardless of binstub overwriting
+ end
+
+ def pre_install_checks
+ super && validate_bundler_checksum(options[:bundler_expected_checksum])
+ end
+
+ def build_extensions
+ extension_cache_path = options[:bundler_extension_cache_path]
+ return super unless extension_cache_path && extension_dir = Bundler.rubygems.spec_extension_dir(spec)
+
+ extension_dir = Pathname.new(extension_dir)
+ build_complete = SharedHelpers.filesystem_access(extension_cache_path.join("gem.build_complete"), :read, &:file?)
+ if build_complete && !options[:force]
+ SharedHelpers.filesystem_access(extension_dir.parent, &:mkpath)
+ SharedHelpers.filesystem_access(extension_cache_path) do
+ FileUtils.cp_r extension_cache_path, spec.extension_dir
+ end
+ else
+ super
+ if extension_dir.directory? # not made for gems without extensions
+ SharedHelpers.filesystem_access(extension_cache_path.parent, &:mkpath)
+ SharedHelpers.filesystem_access(extension_cache_path) do
+ FileUtils.cp_r extension_dir, extension_cache_path
+ end
+ end
+ end
+ end
+
+ private
+
+ def validate_bundler_checksum(checksum)
+ return true if Bundler.settings[:disable_checksum_validation]
+ return true unless checksum
+ return true unless source = @package.instance_variable_get(:@gem)
+ return true unless source.respond_to?(:with_read_io)
+ digest = source.with_read_io do |io|
+ digest = SharedHelpers.digest(:SHA256).new
+ digest << io.read(16_384) until io.eof?
+ io.rewind
+ send(checksum_type(checksum), digest)
+ end
+ unless digest == checksum
+ raise SecurityError, <<-MESSAGE
+ Bundler cannot continue installing #{spec.name} (#{spec.version}).
+ The checksum for the downloaded `#{spec.full_name}.gem` does not match \
+ the checksum given by the server. This means the contents of the downloaded \
+ gem is different from what was uploaded to the server, and could be a potential security issue.
+
+ To resolve this issue:
+ 1. delete the downloaded gem located at: `#{spec.gem_dir}/#{spec.full_name}.gem`
+ 2. run `bundle install`
+
+ If you wish to continue installing the downloaded gem, and are certain it does not pose a \
+ security issue despite the mismatching checksum, do the following:
+ 1. run `bundle config disable_checksum_validation true` to turn off checksum verification
+ 2. run `bundle install`
+
+ (More info: The expected SHA256 checksum was #{checksum.inspect}, but the \
+ checksum for the downloaded gem was #{digest.inspect}.)
+ MESSAGE
+ end
+ true
+ end
+
+ def checksum_type(checksum)
+ case checksum.length
+ when 64 then :hexdigest!
+ when 44 then :base64digest!
+ else raise InstallError, "The given checksum for #{spec.full_name} (#{checksum.inspect}) is not a valid SHA256 hexdigest nor base64digest"
+ end
+ end
+
+ def hexdigest!(digest)
+ digest.hexdigest!
+ end
+
+ def base64digest!(digest)
+ if digest.respond_to?(:base64digest!)
+ digest.base64digest!
+ else
+ [digest.digest!].pack("m0")
+ end
+ end
+ end
+end
diff --git a/lib/bundler/rubygems_integration.rb b/lib/bundler/rubygems_integration.rb
new file mode 100644
index 00000000000000..f088c2fdfba3c5
--- /dev/null
+++ b/lib/bundler/rubygems_integration.rb
@@ -0,0 +1,898 @@
+# frozen_string_literal: true
+
+require "monitor"
+require "rubygems"
+require "rubygems/config_file"
+
+module Bundler
+ class RubygemsIntegration
+ if defined?(Gem::Ext::Builder::CHDIR_MONITOR)
+ EXT_LOCK = Gem::Ext::Builder::CHDIR_MONITOR
+ else
+ EXT_LOCK = Monitor.new
+ end
+
+ def self.version
+ @version ||= Gem::Version.new(Gem::VERSION)
+ end
+
+ def self.provides?(req_str)
+ Gem::Requirement.new(req_str).satisfied_by?(version)
+ end
+
+ def initialize
+ @replaced_methods = {}
+ end
+
+ def version
+ self.class.version
+ end
+
+ def provides?(req_str)
+ self.class.provides?(req_str)
+ end
+
+ def build_args
+ Gem::Command.build_args
+ end
+
+ def build_args=(args)
+ Gem::Command.build_args = args
+ end
+
+ def load_path_insert_index
+ Gem.load_path_insert_index
+ end
+
+ def loaded_specs(name)
+ Gem.loaded_specs[name]
+ end
+
+ def mark_loaded(spec)
+ if spec.respond_to?(:activated=)
+ current = Gem.loaded_specs[spec.name]
+ current.activated = false if current
+ spec.activated = true
+ end
+ Gem.loaded_specs[spec.name] = spec
+ end
+
+ def validate(spec)
+ Bundler.ui.silence { spec.validate(false) }
+ rescue Gem::InvalidSpecificationException => e
+ error_message = "The gemspec at #{spec.loaded_from} is not valid. Please fix this gemspec.\n" \
+ "The validation error was '#{e.message}'\n"
+ raise Gem::InvalidSpecificationException.new(error_message)
+ rescue Errno::ENOENT
+ nil
+ end
+
+ def set_installed_by_version(spec, installed_by_version = Gem::VERSION)
+ return unless spec.respond_to?(:installed_by_version=)
+ spec.installed_by_version = Gem::Version.create(installed_by_version)
+ end
+
+ def spec_missing_extensions?(spec, default = true)
+ return spec.missing_extensions? if spec.respond_to?(:missing_extensions?)
+
+ return false if spec_default_gem?(spec)
+ return false if spec.extensions.empty?
+
+ default
+ end
+
+ def spec_default_gem?(spec)
+ spec.respond_to?(:default_gem?) && spec.default_gem?
+ end
+
+ def spec_matches_for_glob(spec, glob)
+ return spec.matches_for_glob(glob) if spec.respond_to?(:matches_for_glob)
+
+ spec.load_paths.map do |lp|
+ Dir["#{lp}/#{glob}#{suffix_pattern}"]
+ end.flatten(1)
+ end
+
+ def spec_extension_dir(spec)
+ return unless spec.respond_to?(:extension_dir)
+ spec.extension_dir
+ end
+
+ def stub_set_spec(stub, spec)
+ stub.instance_variable_set(:@spec, spec)
+ end
+
+ def path(obj)
+ obj.to_s
+ end
+
+ def platforms
+ return [Gem::Platform::RUBY] if Bundler.settings[:force_ruby_platform]
+ Gem.platforms
+ end
+
+ def configuration
+ require "bundler/psyched_yaml"
+ Gem.configuration
+ rescue Gem::SystemExitException, LoadError => e
+ Bundler.ui.error "#{e.class}: #{e.message}"
+ Bundler.ui.trace e
+ raise
+ rescue YamlLibrarySyntaxError => e
+ raise YamlSyntaxError.new(e, "Your RubyGems configuration, which is " \
+ "usually located in ~/.gemrc, contains invalid YAML syntax.")
+ end
+
+ def ruby_engine
+ Gem.ruby_engine
+ end
+
+ def read_binary(path)
+ Gem.read_binary(path)
+ end
+
+ def inflate(obj)
+ if defined?(Gem::Util)
+ Gem::Util.inflate(obj)
+ else
+ Gem.inflate(obj)
+ end
+ end
+
+ def sources=(val)
+ # Gem.configuration creates a new Gem::ConfigFile, which by default will read ~/.gemrc
+ # If that file exists, its settings (including sources) will overwrite the values we
+ # are about to set here. In order to avoid that, we force memoizing the config file now.
+ configuration
+
+ Gem.sources = val
+ end
+
+ def sources
+ Gem.sources
+ end
+
+ def gem_dir
+ Gem.dir
+ end
+
+ def gem_bindir
+ Gem.bindir
+ end
+
+ def user_home
+ Gem.user_home
+ end
+
+ def gem_path
+ Gem.path
+ end
+
+ def reset
+ Gem::Specification.reset
+ end
+
+ def post_reset_hooks
+ Gem.post_reset_hooks
+ end
+
+ def suffix_pattern
+ Gem.suffix_pattern
+ end
+
+ def gem_cache
+ gem_path.map {|p| File.expand_path("cache", p) }
+ end
+
+ def spec_cache_dirs
+ @spec_cache_dirs ||= begin
+ dirs = gem_path.map {|dir| File.join(dir, "specifications") }
+ dirs << Gem.spec_cache_dir if Gem.respond_to?(:spec_cache_dir) # Not in RubyGems 2.0.3 or earlier
+ dirs.uniq.select {|dir| File.directory? dir }
+ end
+ end
+
+ def marshal_spec_dir
+ Gem::MARSHAL_SPEC_DIR
+ end
+
+ def config_map
+ Gem::ConfigMap
+ end
+
+ def repository_subdirectories
+ %w[cache doc gems specifications]
+ end
+
+ def clear_paths
+ Gem.clear_paths
+ end
+
+ def bin_path(gem, bin, ver)
+ Gem.bin_path(gem, bin, ver)
+ end
+
+ def path_separator
+ File::PATH_SEPARATOR
+ end
+
+ def preserve_paths
+ # this is a no-op outside of RubyGems 1.8
+ yield
+ end
+
+ def loaded_gem_paths
+ # RubyGems 2.2+ can put binary extension into dedicated folders,
+ # therefore use RubyGems facilities to obtain their load paths.
+ if Gem::Specification.method_defined? :full_require_paths
+ loaded_gem_paths = Gem.loaded_specs.map {|_, s| s.full_require_paths }
+ loaded_gem_paths.flatten
+ else
+ $LOAD_PATH.select do |p|
+ Bundler.rubygems.gem_path.any? {|gp| p =~ /^#{Regexp.escape(gp)}/ }
+ end
+ end
+ end
+
+ def load_plugins
+ Gem.load_plugins if Gem.respond_to?(:load_plugins)
+ end
+
+ def load_plugin_files(files)
+ Gem.load_plugin_files(files) if Gem.respond_to?(:load_plugin_files)
+ end
+
+ def ui=(obj)
+ Gem::DefaultUserInteraction.ui = obj
+ end
+
+ def ext_lock
+ EXT_LOCK
+ end
+
+ def fetch_specs(all, pre, &blk)
+ require "rubygems/spec_fetcher"
+ specs = Gem::SpecFetcher.new.list(all, pre)
+ specs.each { yield } if block_given?
+ specs
+ end
+
+ def fetch_prerelease_specs
+ fetch_specs(false, true)
+ rescue Gem::RemoteFetcher::FetchError
+ {} # if we can't download them, there aren't any
+ end
+
+ # TODO: This is for older versions of RubyGems... should we support the
+ # X-Gemfile-Source header on these old versions?
+ # Maybe the newer implementation will work on older RubyGems?
+ # It seems difficult to keep this implementation and still send the header.
+ def fetch_all_remote_specs(remote)
+ old_sources = Bundler.rubygems.sources
+ Bundler.rubygems.sources = [remote.uri.to_s]
+ # Fetch all specs, minus prerelease specs
+ spec_list = fetch_specs(true, false)
+ # Then fetch the prerelease specs
+ fetch_prerelease_specs.each {|k, v| spec_list[k].concat(v) }
+
+ spec_list.values.first
+ ensure
+ Bundler.rubygems.sources = old_sources
+ end
+
+ def with_build_args(args)
+ ext_lock.synchronize do
+ old_args = build_args
+ begin
+ self.build_args = args
+ yield
+ ensure
+ self.build_args = old_args
+ end
+ end
+ end
+
+ def install_with_build_args(args)
+ with_build_args(args) { yield }
+ end
+
+ def gem_from_path(path, policy = nil)
+ require "rubygems/format"
+ Gem::Format.from_file_by_path(path, policy)
+ end
+
+ def spec_from_gem(path, policy = nil)
+ require "rubygems/security"
+ require "bundler/psyched_yaml"
+ gem_from_path(path, security_policies[policy]).spec
+ rescue Gem::Package::FormatError
+ raise GemspecError, "Could not read gem at #{path}. It may be corrupted."
+ rescue Exception, Gem::Exception, Gem::Security::Exception => e
+ if e.is_a?(Gem::Security::Exception) ||
+ e.message =~ /unknown trust policy|unsigned gem/i ||
+ e.message =~ /couldn't verify (meta)?data signature/i
+ raise SecurityError,
+ "The gem #{File.basename(path, ".gem")} can't be installed because " \
+ "the security policy didn't allow it, with the message: #{e.message}"
+ else
+ raise e
+ end
+ end
+
+ def build(spec, skip_validation = false)
+ require "rubygems/builder"
+ Gem::Builder.new(spec).build
+ end
+
+ def build_gem(gem_dir, spec)
+ build(spec)
+ end
+
+ def download_gem(spec, uri, path)
+ uri = Bundler.settings.mirror_for(uri)
+ fetcher = Gem::RemoteFetcher.new(configuration[:http_proxy])
+ Bundler::Retry.new("download gem from #{uri}").attempts do
+ fetcher.download(spec, uri, path)
+ end
+ end
+
+ def security_policy_keys
+ %w[High Medium Low AlmostNo No].map {|level| "#{level}Security" }
+ end
+
+ def security_policies
+ @security_policies ||= begin
+ require "rubygems/security"
+ Gem::Security::Policies
+ rescue LoadError, NameError
+ {}
+ end
+ end
+
+ def reverse_rubygems_kernel_mixin
+ # Disable rubygems' gem activation system
+ kernel = (class << ::Kernel; self; end)
+ [kernel, ::Kernel].each do |k|
+ if k.private_method_defined?(:gem_original_require)
+ redefine_method(k, :require, k.instance_method(:gem_original_require))
+ end
+ end
+ end
+
+ def binstubs_call_gem?
+ true
+ end
+
+ def stubs_provide_full_functionality?
+ false
+ end
+
+ def replace_gem(specs, specs_by_name)
+ reverse_rubygems_kernel_mixin
+
+ executables = nil
+
+ kernel = (class << ::Kernel; self; end)
+ [kernel, ::Kernel].each do |kernel_class|
+ redefine_method(kernel_class, :gem) do |dep, *reqs|
+ executables ||= specs.map(&:executables).flatten if ::Bundler.rubygems.binstubs_call_gem?
+ if executables && executables.include?(File.basename(caller.first.split(":").first))
+ break
+ end
+
+ reqs.pop if reqs.last.is_a?(Hash)
+
+ unless dep.respond_to?(:name) && dep.respond_to?(:requirement)
+ dep = Gem::Dependency.new(dep, reqs)
+ end
+
+ if spec = specs_by_name[dep.name]
+ return true if dep.matches_spec?(spec)
+ end
+
+ message = if spec.nil?
+ "#{dep.name} is not part of the bundle." \
+ " Add it to your #{Bundler.default_gemfile.basename}."
+ else
+ "can't activate #{dep}, already activated #{spec.full_name}. " \
+ "Make sure all dependencies are added to Gemfile."
+ end
+
+ e = Gem::LoadError.new(message)
+ e.name = dep.name
+ if e.respond_to?(:requirement=)
+ e.requirement = dep.requirement
+ elsif e.respond_to?(:version_requirement=)
+ e.version_requirement = dep.requirement
+ end
+ raise e
+ end
+
+ # backwards compatibility shim, see https://github.com/bundler/bundler/issues/5102
+ kernel_class.send(:public, :gem) if Bundler.feature_flag.setup_makes_kernel_gem_public?
+ end
+ end
+
+ def stub_source_index(specs)
+ Gem::SourceIndex.send(:alias_method, :old_initialize, :initialize)
+ redefine_method(Gem::SourceIndex, :initialize) do |*args|
+ @gems = {}
+ # You're looking at this thinking: Oh! This is how I make those
+ # rubygems deprecations go away!
+ #
+ # You'd be correct BUT using of this method in production code
+ # must be approved by the rubygems team itself!
+ #
+ # This is your warning. If you use this and don't have approval
+ # we can't protect you.
+ #
+ Deprecate.skip_during do
+ self.spec_dirs = *args
+ add_specs(*specs)
+ end
+ end
+ end
+
+ # Used to make bin stubs that are not created by bundler work
+ # under bundler. The new Gem.bin_path only considers gems in
+ # +specs+
+ def replace_bin_path(specs, specs_by_name)
+ gem_class = (class << Gem; self; end)
+
+ redefine_method(gem_class, :find_spec_for_exe) do |gem_name, *args|
+ exec_name = args.first
+
+ spec_with_name = specs_by_name[gem_name]
+ spec = if exec_name
+ if spec_with_name && spec_with_name.executables.include?(exec_name)
+ spec_with_name
+ else
+ specs.find {|s| s.executables.include?(exec_name) }
+ end
+ else
+ spec_with_name
+ end
+
+ unless spec
+ message = "can't find executable #{exec_name} for gem #{gem_name}"
+ if !exec_name || spec_with_name.nil?
+ message += ". #{gem_name} is not currently included in the bundle, " \
+ "perhaps you meant to add it to your #{Bundler.default_gemfile.basename}?"
+ end
+ raise Gem::Exception, message
+ end
+
+ raise Gem::Exception, "no default executable for #{spec.full_name}" unless exec_name ||= spec.default_executable
+
+ unless spec.name == gem_name
+ Bundler::SharedHelpers.major_deprecation 3,
+ "Bundler is using a binstub that was created for a different gem (#{spec.name}).\n" \
+ "You should run `bundle binstub #{gem_name}` " \
+ "to work around a system/bundle conflict."
+ end
+ spec
+ end
+
+ redefine_method(gem_class, :activate_bin_path) do |name, *args|
+ exec_name = args.first
+ return ENV["BUNDLE_BIN_PATH"] if exec_name == "bundle"
+
+ # Copy of Rubygems activate_bin_path impl
+ requirement = args.last
+ spec = find_spec_for_exe name, exec_name, [requirement]
+
+ gem_bin = File.join(spec.full_gem_path, spec.bindir, exec_name)
+ gem_from_path_bin = File.join(File.dirname(spec.loaded_from), spec.bindir, exec_name)
+ File.exist?(gem_bin) ? gem_bin : gem_from_path_bin
+ end
+
+ redefine_method(gem_class, :bin_path) do |name, *args|
+ exec_name = args.first
+ return ENV["BUNDLE_BIN_PATH"] if exec_name == "bundle"
+
+ spec = find_spec_for_exe(name, *args)
+ exec_name ||= spec.default_executable
+
+ gem_bin = File.join(spec.full_gem_path, spec.bindir, exec_name)
+ gem_from_path_bin = File.join(File.dirname(spec.loaded_from), spec.bindir, exec_name)
+ File.exist?(gem_bin) ? gem_bin : gem_from_path_bin
+ end
+ end
+
+ # Because Bundler has a static view of what specs are available,
+ # we don't #refresh, so stub it out.
+ def replace_refresh
+ gem_class = (class << Gem; self; end)
+ redefine_method(gem_class, :refresh) {}
+ end
+
+ # Replace or hook into RubyGems to provide a bundlerized view
+ # of the world.
+ def replace_entrypoints(specs)
+ specs_by_name = specs.reduce({}) do |h, s|
+ h[s.name] = s
+ h
+ end
+
+ replace_gem(specs, specs_by_name)
+ stub_rubygems(specs)
+ replace_bin_path(specs, specs_by_name)
+ replace_refresh
+
+ Gem.clear_paths
+ end
+
+ # This backports the correct segment generation code from RubyGems 1.4+
+ # by monkeypatching it into the method in RubyGems 1.3.6 and 1.3.7.
+ def backport_segment_generation
+ redefine_method(Gem::Version, :segments) do
+ @segments ||= @version.scan(/[0-9]+|[a-z]+/i).map do |s|
+ /^\d+$/ =~ s ? s.to_i : s
+ end
+ end
+ end
+
+ # This backport fixes the marshaling of @segments.
+ def backport_yaml_initialize
+ redefine_method(Gem::Version, :yaml_initialize) do |_, map|
+ @version = map["version"]
+ @segments = nil
+ @hash = nil
+ end
+ end
+
+ # This backports base_dir which replaces installation path
+ # RubyGems 1.8+
+ def backport_base_dir
+ redefine_method(Gem::Specification, :base_dir) do
+ return Gem.dir unless loaded_from
+ File.dirname File.dirname loaded_from
+ end
+ end
+
+ def backport_cache_file
+ redefine_method(Gem::Specification, :cache_dir) do
+ @cache_dir ||= File.join base_dir, "cache"
+ end
+
+ redefine_method(Gem::Specification, :cache_file) do
+ @cache_file ||= File.join cache_dir, "#{full_name}.gem"
+ end
+ end
+
+ def backport_spec_file
+ redefine_method(Gem::Specification, :spec_dir) do
+ @spec_dir ||= File.join base_dir, "specifications"
+ end
+
+ redefine_method(Gem::Specification, :spec_file) do
+ @spec_file ||= File.join spec_dir, "#{full_name}.gemspec"
+ end
+ end
+
+ def undo_replacements
+ @replaced_methods.each do |(sym, klass), method|
+ redefine_method(klass, sym, method)
+ end
+ if Binding.public_method_defined?(:source_location)
+ post_reset_hooks.reject! {|proc| proc.binding.source_location[0] == __FILE__ }
+ else
+ post_reset_hooks.reject! {|proc| proc.binding.eval("__FILE__") == __FILE__ }
+ end
+ @replaced_methods.clear
+ end
+
+ def redefine_method(klass, method, unbound_method = nil, &block)
+ visibility = method_visibility(klass, method)
+ begin
+ if (instance_method = klass.instance_method(method)) && method != :initialize
+ # doing this to ensure we also get private methods
+ klass.send(:remove_method, method)
+ end
+ rescue NameError
+ # method isn't defined
+ nil
+ end
+ @replaced_methods[[method, klass]] = instance_method
+ if unbound_method
+ klass.send(:define_method, method, unbound_method)
+ klass.send(visibility, method)
+ elsif block
+ klass.send(:define_method, method, &block)
+ klass.send(visibility, method)
+ end
+ end
+
+ def method_visibility(klass, method)
+ if klass.private_method_defined?(method)
+ :private
+ elsif klass.protected_method_defined?(method)
+ :protected
+ else
+ :public
+ end
+ end
+
+ # RubyGems 1.4 through 1.6
+ class Legacy < RubygemsIntegration
+ def initialize
+ super
+ backport_base_dir
+ backport_cache_file
+ backport_spec_file
+ backport_yaml_initialize
+ end
+
+ def stub_rubygems(specs)
+ # RubyGems versions lower than 1.7 use SourceIndex#from_gems_in
+ source_index_class = (class << Gem::SourceIndex; self; end)
+ redefine_method(source_index_class, :from_gems_in) do |*args|
+ Gem::SourceIndex.new.tap do |source_index|
+ source_index.spec_dirs = *args
+ source_index.add_specs(*specs)
+ end
+ end
+ end
+
+ def all_specs
+ Gem.source_index.gems.values
+ end
+
+ def find_name(name)
+ Gem.source_index.find_name(name)
+ end
+
+ def validate(spec)
+ # These versions of RubyGems always validate in "packaging" mode,
+ # which is too strict for the kinds of checks we care about. As a
+ # result, validation is disabled on versions of RubyGems below 1.7.
+ end
+
+ def post_reset_hooks
+ []
+ end
+
+ def reset
+ end
+ end
+
+ # RubyGems versions 1.3.6 and 1.3.7
+ class Ancient < Legacy
+ def initialize
+ super
+ backport_segment_generation
+ end
+ end
+
+ # RubyGems 1.7
+ class Transitional < Legacy
+ def stub_rubygems(specs)
+ stub_source_index(specs)
+ end
+
+ def validate(spec)
+ # Missing summary is downgraded to a warning in later versions,
+ # so we set it to an empty string to prevent an exception here.
+ spec.summary ||= ""
+ RubygemsIntegration.instance_method(:validate).bind(self).call(spec)
+ end
+ end
+
+ # RubyGems 1.8.5-1.8.19
+ class Modern < RubygemsIntegration
+ def stub_rubygems(specs)
+ Gem::Specification.all = specs
+
+ Gem.post_reset do
+ Gem::Specification.all = specs
+ end
+
+ stub_source_index(specs)
+ end
+
+ def all_specs
+ Gem::Specification.to_a
+ end
+
+ def find_name(name)
+ Gem::Specification.find_all_by_name name
+ end
+ end
+
+ # RubyGems 1.8.0 to 1.8.4
+ class AlmostModern < Modern
+ # RubyGems [>= 1.8.0, < 1.8.5] has a bug that changes Gem.dir whenever
+ # you call Gem::Installer#install with an :install_dir set. We have to
+ # change it back for our sudo mode to work.
+ def preserve_paths
+ old_dir = gem_dir
+ old_path = gem_path
+ yield
+ Gem.use_paths(old_dir, old_path)
+ end
+ end
+
+ # RubyGems 1.8.20+
+ class MoreModern < Modern
+ # RubyGems 1.8.20 and adds the skip_validation parameter, so that's
+ # when we start passing it through.
+ def build(spec, skip_validation = false)
+ require "rubygems/builder"
+ Gem::Builder.new(spec).build(skip_validation)
+ end
+ end
+
+ # RubyGems 2.0
+ class Future < RubygemsIntegration
+ def stub_rubygems(specs)
+ Gem::Specification.all = specs
+
+ Gem.post_reset do
+ Gem::Specification.all = specs
+ end
+
+ redefine_method((class << Gem; self; end), :finish_resolve) do |*|
+ []
+ end
+ end
+
+ def all_specs
+ Gem::Specification.to_a
+ end
+
+ def find_name(name)
+ Gem::Specification.find_all_by_name name
+ end
+
+ def fetch_specs(source, remote, name)
+ path = source + "#{name}.#{Gem.marshal_version}.gz"
+ fetcher = gem_remote_fetcher
+ fetcher.headers = { "X-Gemfile-Source" => remote.original_uri.to_s } if remote.original_uri
+ string = fetcher.fetch_path(path)
+ Bundler.load_marshal(string)
+ rescue Gem::RemoteFetcher::FetchError => e
+ # it's okay for prerelease to fail
+ raise e unless name == "prerelease_specs"
+ end
+
+ def fetch_all_remote_specs(remote)
+ source = remote.uri.is_a?(URI) ? remote.uri : URI.parse(source.to_s)
+
+ specs = fetch_specs(source, remote, "specs")
+ pres = fetch_specs(source, remote, "prerelease_specs") || []
+
+ specs.concat(pres)
+ end
+
+ def download_gem(spec, uri, path)
+ uri = Bundler.settings.mirror_for(uri)
+ fetcher = gem_remote_fetcher
+ fetcher.headers = { "X-Gemfile-Source" => spec.remote.original_uri.to_s } if spec.remote.original_uri
+ Bundler::Retry.new("download gem from #{uri}").attempts do
+ fetcher.download(spec, uri, path)
+ end
+ end
+
+ def gem_remote_fetcher
+ require "resolv"
+ proxy = configuration[:http_proxy]
+ dns = Resolv::DNS.new
+ Bundler::GemRemoteFetcher.new(proxy, dns)
+ end
+
+ def gem_from_path(path, policy = nil)
+ require "rubygems/package"
+ p = Gem::Package.new(path)
+ p.security_policy = policy if policy
+ p
+ end
+
+ def build(spec, skip_validation = false)
+ require "rubygems/package"
+ Gem::Package.build(spec, skip_validation)
+ end
+
+ def repository_subdirectories
+ Gem::REPOSITORY_SUBDIRECTORIES
+ end
+
+ def install_with_build_args(args)
+ yield
+ end
+
+ def path_separator
+ Gem.path_separator
+ end
+ end
+
+ # RubyGems 2.1.0
+ class MoreFuture < Future
+ def initialize
+ super
+ backport_ext_builder_monitor
+ end
+
+ def all_specs
+ require "bundler/remote_specification"
+ Gem::Specification.stubs.map do |stub|
+ StubSpecification.from_stub(stub)
+ end
+ end
+
+ def backport_ext_builder_monitor
+ # So we can avoid requiring "rubygems/ext" in its entirety
+ Gem.module_eval <<-RB, __FILE__, __LINE__ + 1
+ module Ext
+ end
+ RB
+
+ require "rubygems/ext/builder"
+
+ Gem::Ext::Builder.class_eval do
+ unless const_defined?(:CHDIR_MONITOR)
+ const_set(:CHDIR_MONITOR, EXT_LOCK)
+ end
+
+ remove_const(:CHDIR_MUTEX) if const_defined?(:CHDIR_MUTEX)
+ const_set(:CHDIR_MUTEX, const_get(:CHDIR_MONITOR))
+ end
+ end
+
+ if Gem::Specification.respond_to?(:stubs_for)
+ def find_name(name)
+ Gem::Specification.stubs_for(name).map(&:to_spec)
+ end
+ else
+ def find_name(name)
+ Gem::Specification.stubs.find_all do |spec|
+ spec.name == name
+ end.map(&:to_spec)
+ end
+ end
+
+ def use_gemdeps(gemfile)
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path(gemfile)
+ require "bundler/gemdeps"
+ runtime = Bundler.setup
+ Bundler.ui = nil
+ activated_spec_names = runtime.requested_specs.map(&:to_spec).sort_by(&:name)
+ [Gemdeps.new(runtime), activated_spec_names]
+ end
+
+ if provides?(">= 2.5.2")
+ # RubyGems-generated binstubs call Kernel#gem
+ def binstubs_call_gem?
+ false
+ end
+
+ # only 2.5.2+ has all of the stub methods we want to use, and since this
+ # is a performance optimization _only_,
+ # we'll restrict ourselves to the most
+ # recent RG versions instead of all versions that have stubs
+ def stubs_provide_full_functionality?
+ true
+ end
+ end
+ end
+ end
+
+ def self.rubygems
+ @rubygems ||= if RubygemsIntegration.provides?(">= 2.1.0")
+ RubygemsIntegration::MoreFuture.new
+ elsif RubygemsIntegration.provides?(">= 1.99.99")
+ RubygemsIntegration::Future.new
+ elsif RubygemsIntegration.provides?(">= 1.8.20")
+ RubygemsIntegration::MoreModern.new
+ elsif RubygemsIntegration.provides?(">= 1.8.5")
+ RubygemsIntegration::Modern.new
+ elsif RubygemsIntegration.provides?(">= 1.8.0")
+ RubygemsIntegration::AlmostModern.new
+ elsif RubygemsIntegration.provides?(">= 1.7.0")
+ RubygemsIntegration::Transitional.new
+ elsif RubygemsIntegration.provides?(">= 1.4.0")
+ RubygemsIntegration::Legacy.new
+ else # RubyGems 1.3.6 and 1.3.7
+ RubygemsIntegration::Ancient.new
+ end
+ end
+end
diff --git a/lib/bundler/runtime.rb b/lib/bundler/runtime.rb
new file mode 100644
index 00000000000000..762e7b3ec66ea5
--- /dev/null
+++ b/lib/bundler/runtime.rb
@@ -0,0 +1,322 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Runtime
+ include SharedHelpers
+
+ def initialize(root, definition)
+ @root = root
+ @definition = definition
+ end
+
+ def setup(*groups)
+ @definition.ensure_equivalent_gemfile_and_lockfile if Bundler.frozen_bundle?
+
+ groups.map!(&:to_sym)
+
+ # Has to happen first
+ clean_load_path
+
+ specs = groups.any? ? @definition.specs_for(groups) : requested_specs
+
+ SharedHelpers.set_bundle_environment
+ Bundler.rubygems.replace_entrypoints(specs)
+
+ # Activate the specs
+ load_paths = specs.map do |spec|
+ unless spec.loaded_from
+ raise GemNotFound, "#{spec.full_name} is missing. Run `bundle install` to get it."
+ end
+
+ check_for_activated_spec!(spec)
+
+ Bundler.rubygems.mark_loaded(spec)
+ spec.load_paths.reject {|path| $LOAD_PATH.include?(path) }
+ end.reverse.flatten
+
+ # See Gem::Specification#add_self_to_load_path (since RubyGems 1.8)
+ if insert_index = Bundler.rubygems.load_path_insert_index
+ # Gem directories must come after -I and ENV['RUBYLIB']
+ $LOAD_PATH.insert(insert_index, *load_paths)
+ else
+ # We are probably testing in core, -I and RUBYLIB don't apply
+ $LOAD_PATH.unshift(*load_paths)
+ end
+
+ setup_manpath
+
+ lock(:preserve_unknown_sections => true)
+
+ self
+ end
+
+ REQUIRE_ERRORS = [
+ /^no such file to load -- (.+)$/i,
+ /^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
+ /^Missing API definition file in (.+)$/i,
+ /^cannot load such file -- (.+)$/i,
+ /^dlopen\([^)]*\): Library not loaded: (.+)$/i,
+ ].freeze
+
+ def require(*groups)
+ groups.map!(&:to_sym)
+ groups = [:default] if groups.empty?
+
+ @definition.dependencies.each do |dep|
+ # Skip the dependency if it is not in any of the requested groups, or
+ # not for the current platform, or doesn't match the gem constraints.
+ next unless (dep.groups & groups).any? && dep.should_include?
+
+ required_file = nil
+
+ begin
+ # Loop through all the specified autorequires for the
+ # dependency. If there are none, use the dependency's name
+ # as the autorequire.
+ Array(dep.autorequire || dep.name).each do |file|
+ # Allow `require: true` as an alias for `require: `
+ file = dep.name if file == true
+ required_file = file
+ begin
+ Kernel.require file
+ rescue RuntimeError => e
+ raise e if e.is_a?(LoadError) # we handle this a little later
+ raise Bundler::GemRequireError.new e,
+ "There was an error while trying to load the gem '#{file}'."
+ end
+ end
+ rescue LoadError => e
+ REQUIRE_ERRORS.find {|r| r =~ e.message }
+ raise if dep.autorequire || $1 != required_file
+
+ if dep.autorequire.nil? && dep.name.include?("-")
+ begin
+ namespaced_file = dep.name.tr("-", "/")
+ Kernel.require namespaced_file
+ rescue LoadError => e
+ REQUIRE_ERRORS.find {|r| r =~ e.message }
+ raise if $1 != namespaced_file
+ end
+ end
+ end
+ end
+ end
+
+ def self.definition_method(meth)
+ define_method(meth) do
+ raise ArgumentError, "no definition when calling Runtime##{meth}" unless @definition
+ @definition.send(meth)
+ end
+ end
+ private_class_method :definition_method
+
+ definition_method :requested_specs
+ definition_method :specs
+ definition_method :dependencies
+ definition_method :current_dependencies
+ definition_method :requires
+
+ def lock(opts = {})
+ return if @definition.nothing_changed? && !@definition.unlocking?
+ @definition.lock(Bundler.default_lockfile, opts[:preserve_unknown_sections])
+ end
+
+ alias_method :gems, :specs
+
+ def cache(custom_path = nil)
+ cache_path = Bundler.app_cache(custom_path)
+ SharedHelpers.filesystem_access(cache_path) do |p|
+ FileUtils.mkdir_p(p)
+ end unless File.exist?(cache_path)
+
+ Bundler.ui.info "Updating files in #{Bundler.settings.app_cache_path}"
+
+ specs_to_cache = Bundler.settings[:cache_all_platforms] ? @definition.resolve.materialized_for_all_platforms : specs
+ specs_to_cache.each do |spec|
+ next if spec.name == "bundler"
+ next if spec.source.is_a?(Source::Gemspec)
+ spec.source.send(:fetch_gem, spec) if Bundler.settings[:cache_all_platforms] && spec.source.respond_to?(:fetch_gem, true)
+ spec.source.cache(spec, custom_path) if spec.source.respond_to?(:cache)
+ end
+
+ Dir[cache_path.join("*/.git")].each do |git_dir|
+ FileUtils.rm_rf(git_dir)
+ FileUtils.touch(File.expand_path("../.bundlecache", git_dir))
+ end
+
+ prune_cache(cache_path) unless Bundler.settings[:no_prune]
+ end
+
+ def prune_cache(cache_path)
+ SharedHelpers.filesystem_access(cache_path) do |p|
+ FileUtils.mkdir_p(p)
+ end unless File.exist?(cache_path)
+ resolve = @definition.resolve
+ prune_gem_cache(resolve, cache_path)
+ prune_git_and_path_cache(resolve, cache_path)
+ end
+
+ def clean(dry_run = false)
+ gem_bins = Dir["#{Gem.dir}/bin/*"]
+ git_dirs = Dir["#{Gem.dir}/bundler/gems/*"]
+ git_cache_dirs = Dir["#{Gem.dir}/cache/bundler/git/*"]
+ gem_dirs = Dir["#{Gem.dir}/gems/*"]
+ gem_files = Dir["#{Gem.dir}/cache/*.gem"]
+ gemspec_files = Dir["#{Gem.dir}/specifications/*.gemspec"]
+ extension_dirs = Dir["#{Gem.dir}/extensions/*/*/*"]
+ spec_gem_paths = []
+ # need to keep git sources around
+ spec_git_paths = @definition.spec_git_paths
+ spec_git_cache_dirs = []
+ spec_gem_executables = []
+ spec_cache_paths = []
+ spec_gemspec_paths = []
+ spec_extension_paths = []
+ specs.each do |spec|
+ spec_gem_paths << spec.full_gem_path
+ # need to check here in case gems are nested like for the rails git repo
+ md = %r{(.+bundler/gems/.+-[a-f0-9]{7,12})}.match(spec.full_gem_path)
+ spec_git_paths << md[1] if md
+ spec_gem_executables << spec.executables.collect do |executable|
+ e = "#{Bundler.rubygems.gem_bindir}/#{executable}"
+ [e, "#{e}.bat"]
+ end
+ spec_cache_paths << spec.cache_file
+ spec_gemspec_paths << spec.spec_file
+ spec_extension_paths << spec.extension_dir if spec.respond_to?(:extension_dir)
+ spec_git_cache_dirs << spec.source.cache_path.to_s if spec.source.is_a?(Bundler::Source::Git)
+ end
+ spec_gem_paths.uniq!
+ spec_gem_executables.flatten!
+
+ stale_gem_bins = gem_bins - spec_gem_executables
+ stale_git_dirs = git_dirs - spec_git_paths - ["#{Gem.dir}/bundler/gems/extensions"]
+ stale_git_cache_dirs = git_cache_dirs - spec_git_cache_dirs
+ stale_gem_dirs = gem_dirs - spec_gem_paths
+ stale_gem_files = gem_files - spec_cache_paths
+ stale_gemspec_files = gemspec_files - spec_gemspec_paths
+ stale_extension_dirs = extension_dirs - spec_extension_paths
+
+ removed_stale_gem_dirs = stale_gem_dirs.collect {|dir| remove_dir(dir, dry_run) }
+ removed_stale_git_dirs = stale_git_dirs.collect {|dir| remove_dir(dir, dry_run) }
+ output = removed_stale_gem_dirs + removed_stale_git_dirs
+
+ unless dry_run
+ stale_files = stale_gem_bins + stale_gem_files + stale_gemspec_files
+ stale_files.each do |file|
+ SharedHelpers.filesystem_access(File.dirname(file)) do |_p|
+ FileUtils.rm(file) if File.exist?(file)
+ end
+ end
+
+ stale_dirs = stale_git_cache_dirs + stale_extension_dirs
+ stale_dirs.each do |stale_dir|
+ SharedHelpers.filesystem_access(stale_dir) do |dir|
+ FileUtils.rm_rf(dir) if File.exist?(dir)
+ end
+ end
+ end
+
+ output
+ end
+
+ private
+
+ def prune_gem_cache(resolve, cache_path)
+ cached = Dir["#{cache_path}/*.gem"]
+
+ cached = cached.delete_if do |path|
+ spec = Bundler.rubygems.spec_from_gem path
+
+ resolve.any? do |s|
+ s.name == spec.name && s.version == spec.version && !s.source.is_a?(Bundler::Source::Git)
+ end
+ end
+
+ if cached.any?
+ Bundler.ui.info "Removing outdated .gem files from #{Bundler.settings.app_cache_path}"
+
+ cached.each do |path|
+ Bundler.ui.info " * #{File.basename(path)}"
+ File.delete(path)
+ end
+ end
+ end
+
+ def prune_git_and_path_cache(resolve, cache_path)
+ cached = Dir["#{cache_path}/*/.bundlecache"]
+
+ cached = cached.delete_if do |path|
+ name = File.basename(File.dirname(path))
+
+ resolve.any? do |s|
+ source = s.source
+ source.respond_to?(:app_cache_dirname) && source.app_cache_dirname == name
+ end
+ end
+
+ if cached.any?
+ Bundler.ui.info "Removing outdated git and path gems from #{Bundler.settings.app_cache_path}"
+
+ cached.each do |path|
+ path = File.dirname(path)
+ Bundler.ui.info " * #{File.basename(path)}"
+ FileUtils.rm_rf(path)
+ end
+ end
+ end
+
+ def setup_manpath
+ # Add man/ subdirectories from activated bundles to MANPATH for man(1)
+ manuals = $LOAD_PATH.map do |path|
+ man_subdir = path.sub(/lib$/, "man")
+ man_subdir unless Dir[man_subdir + "/man?/"].empty?
+ end.compact
+
+ return if manuals.empty?
+ Bundler::SharedHelpers.set_env "MANPATH", manuals.concat(
+ ENV["MANPATH"].to_s.split(File::PATH_SEPARATOR)
+ ).uniq.join(File::PATH_SEPARATOR)
+ end
+
+ def remove_dir(dir, dry_run)
+ full_name = Pathname.new(dir).basename.to_s
+
+ parts = full_name.split("-")
+ name = parts[0..-2].join("-")
+ version = parts.last
+ output = "#{name} (#{version})"
+
+ if dry_run
+ Bundler.ui.info "Would have removed #{output}"
+ else
+ Bundler.ui.info "Removing #{output}"
+ FileUtils.rm_rf(dir)
+ end
+
+ output
+ end
+
+ def check_for_activated_spec!(spec)
+ return unless activated_spec = Bundler.rubygems.loaded_specs(spec.name)
+ return if activated_spec.version == spec.version
+
+ suggestion = if Bundler.rubygems.spec_default_gem?(activated_spec)
+ "Since #{spec.name} is a default gem, you can either remove your dependency on it" \
+ " or try updating to a newer version of bundler that supports #{spec.name} as a default gem."
+ else
+ "Prepending `bundle exec` to your command may solve this."
+ end
+
+ e = Gem::LoadError.new "You have already activated #{activated_spec.name} #{activated_spec.version}, " \
+ "but your Gemfile requires #{spec.name} #{spec.version}. #{suggestion}"
+ e.name = spec.name
+ if e.respond_to?(:requirement=)
+ e.requirement = Gem::Requirement.new(spec.version.to_s)
+ else
+ e.version_requirement = Gem::Requirement.new(spec.version.to_s)
+ end
+ raise e
+ end
+ end
+end
diff --git a/lib/bundler/settings.rb b/lib/bundler/settings.rb
new file mode 100644
index 00000000000000..66af31dab25787
--- /dev/null
+++ b/lib/bundler/settings.rb
@@ -0,0 +1,464 @@
+# frozen_string_literal: true
+
+require "uri"
+
+module Bundler
+ class Settings
+ autoload :Mirror, "bundler/mirror"
+ autoload :Mirrors, "bundler/mirror"
+ autoload :Validator, "bundler/settings/validator"
+
+ BOOL_KEYS = %w[
+ allow_bundler_dependency_conflicts
+ allow_deployment_source_credential_changes
+ allow_offline_install
+ auto_clean_without_path
+ auto_install
+ auto_config_jobs
+ cache_all
+ cache_all_platforms
+ cache_command_is_package
+ console_command
+ default_install_uses_path
+ deployment
+ deployment_means_frozen
+ disable_checksum_validation
+ disable_exec_load
+ disable_local_branch_check
+ disable_multisource
+ disable_platform_warnings
+ disable_shared_gems
+ disable_version_check
+ error_on_stderr
+ force_ruby_platform
+ forget_cli_options
+ frozen
+ gem.coc
+ gem.mit
+ global_path_appends_ruby_scope
+ global_gem_cache
+ ignore_messages
+ init_gems_rb
+ list_command
+ lockfile_upgrade_warning
+ lockfile_uses_separate_rubygems_sources
+ major_deprecations
+ no_install
+ no_prune
+ only_update_to_newer_versions
+ path_relative_to_cwd
+ path.system
+ plugins
+ prefer_gems_rb
+ print_only_version_number
+ setup_makes_kernel_gem_public
+ silence_root_warning
+ skip_default_git_sources
+ specific_platform
+ suppress_install_using_messages
+ unlock_source_unlocks_spec
+ update_requires_all_flag
+ use_gem_version_promoter_for_major_updates
+ viz_command
+ ].freeze
+
+ NUMBER_KEYS = %w[
+ jobs
+ redirect
+ retry
+ ssl_verify_mode
+ timeout
+ ].freeze
+
+ ARRAY_KEYS = %w[
+ with
+ without
+ ].freeze
+
+ DEFAULT_CONFIG = {
+ :disable_version_check => true,
+ :redirect => 5,
+ :retry => 3,
+ :timeout => 10,
+ }.freeze
+
+ def initialize(root = nil)
+ @root = root
+ @local_config = load_config(local_config_file)
+ @global_config = load_config(global_config_file)
+ @temporary = {}
+ end
+
+ def [](name)
+ key = key_for(name)
+ value = @temporary.fetch(key) do
+ @local_config.fetch(key) do
+ ENV.fetch(key) do
+ @global_config.fetch(key) do
+ DEFAULT_CONFIG.fetch(name) do
+ nil
+ end end end end end
+
+ converted_value(value, name)
+ end
+
+ def set_command_option(key, value)
+ if Bundler.feature_flag.forget_cli_options?
+ temporary(key => value)
+ value
+ else
+ command = if value.nil?
+ "bundle config --delete #{key}"
+ else
+ "bundle config #{key} #{Array(value).join(":")}"
+ end
+
+ Bundler::SharedHelpers.major_deprecation 3,\
+ "flags passed to commands " \
+ "will no longer be automatically remembered. Instead please set flags " \
+ "you want remembered between commands using `bundle config " \
+ " `, i.e. `#{command}`"
+
+ set_local(key, value)
+ end
+ end
+
+ def set_command_option_if_given(key, value)
+ return if value.nil?
+ set_command_option(key, value)
+ end
+
+ def set_local(key, value)
+ local_config_file || raise(GemfileNotFound, "Could not locate Gemfile")
+
+ set_key(key, value, @local_config, local_config_file)
+ end
+
+ def temporary(update)
+ existing = Hash[update.map {|k, _| [k, @temporary[key_for(k)]] }]
+ update.each do |k, v|
+ set_key(k, v, @temporary, nil)
+ end
+ return unless block_given?
+ begin
+ yield
+ ensure
+ existing.each {|k, v| set_key(k, v, @temporary, nil) }
+ end
+ end
+
+ def set_global(key, value)
+ set_key(key, value, @global_config, global_config_file)
+ end
+
+ def all
+ env_keys = ENV.keys.grep(/\ABUNDLE_.+/)
+
+ keys = @temporary.keys | @global_config.keys | @local_config.keys | env_keys
+
+ keys.map do |key|
+ key.sub(/^BUNDLE_/, "").gsub(/__/, ".").downcase
+ end
+ end
+
+ def local_overrides
+ repos = {}
+ all.each do |k|
+ repos[$'] = self[k] if k =~ /^local\./
+ end
+ repos
+ end
+
+ def mirror_for(uri)
+ uri = URI(uri.to_s) unless uri.is_a?(URI)
+ gem_mirrors.for(uri.to_s).uri
+ end
+
+ def credentials_for(uri)
+ self[uri.to_s] || self[uri.host]
+ end
+
+ def gem_mirrors
+ all.inject(Mirrors.new) do |mirrors, k|
+ mirrors.parse(k, self[k]) if k.start_with?("mirror.")
+ mirrors
+ end
+ end
+
+ def locations(key)
+ key = key_for(key)
+ locations = {}
+ locations[:temporary] = @temporary[key] if @temporary.key?(key)
+ locations[:local] = @local_config[key] if @local_config.key?(key)
+ locations[:env] = ENV[key] if ENV[key]
+ locations[:global] = @global_config[key] if @global_config.key?(key)
+ locations[:default] = DEFAULT_CONFIG[key] if DEFAULT_CONFIG.key?(key)
+ locations
+ end
+
+ def pretty_values_for(exposed_key)
+ key = key_for(exposed_key)
+
+ locations = []
+
+ if @temporary.key?(key)
+ locations << "Set for the current command: #{converted_value(@temporary[key], exposed_key).inspect}"
+ end
+
+ if @local_config.key?(key)
+ locations << "Set for your local app (#{local_config_file}): #{converted_value(@local_config[key], exposed_key).inspect}"
+ end
+
+ if value = ENV[key]
+ locations << "Set via #{key}: #{converted_value(value, exposed_key).inspect}"
+ end
+
+ if @global_config.key?(key)
+ locations << "Set for the current user (#{global_config_file}): #{converted_value(@global_config[key], exposed_key).inspect}"
+ end
+
+ return ["You have not configured a value for `#{exposed_key}`"] if locations.empty?
+ locations
+ end
+
+ # for legacy reasons, in Bundler 1, the ruby scope isnt appended when the setting comes from ENV or the global config,
+ # nor do we respect :disable_shared_gems
+ def path
+ key = key_for(:path)
+ path = ENV[key] || @global_config[key]
+ if path && !@temporary.key?(key) && !@local_config.key?(key)
+ return Path.new(path, Bundler.feature_flag.global_path_appends_ruby_scope?, false, false)
+ end
+
+ system_path = self["path.system"] || (self[:disable_shared_gems] == false)
+ Path.new(self[:path], true, system_path, Bundler.feature_flag.default_install_uses_path?)
+ end
+
+ Path = Struct.new(:explicit_path, :append_ruby_scope, :system_path, :default_install_uses_path) do
+ def path
+ path = base_path
+ path = File.join(path, Bundler.ruby_scope) if append_ruby_scope && !use_system_gems?
+ path
+ end
+
+ def use_system_gems?
+ return true if system_path
+ return false if explicit_path
+ !default_install_uses_path
+ end
+
+ def base_path
+ path = explicit_path
+ path ||= ".bundle" unless use_system_gems?
+ path ||= Bundler.rubygems.gem_dir
+ path
+ end
+
+ def base_path_relative_to_pwd
+ base_path = Pathname.new(self.base_path)
+ expanded_base_path = base_path.expand_path(Bundler.root)
+ relative_path = expanded_base_path.relative_path_from(Pathname.pwd)
+ if relative_path.to_s.start_with?("..")
+ relative_path = base_path if base_path.absolute?
+ else
+ relative_path = Pathname.new(File.join(".", relative_path))
+ end
+ relative_path
+ rescue ArgumentError
+ expanded_base_path
+ end
+
+ def validate!
+ return unless explicit_path && system_path
+ path = Bundler.settings.pretty_values_for(:path)
+ path.unshift(nil, "path:") unless path.empty?
+ system_path = Bundler.settings.pretty_values_for("path.system")
+ system_path.unshift(nil, "path.system:") unless system_path.empty?
+ disable_shared_gems = Bundler.settings.pretty_values_for(:disable_shared_gems)
+ disable_shared_gems.unshift(nil, "disable_shared_gems:") unless disable_shared_gems.empty?
+ raise InvalidOption,
+ "Using a custom path while using system gems is unsupported.\n#{path.join("\n")}\n#{system_path.join("\n")}\n#{disable_shared_gems.join("\n")}"
+ end
+ end
+
+ def allow_sudo?
+ key = key_for(:path)
+ path_configured = @temporary.key?(key) || @local_config.key?(key)
+ !path_configured
+ end
+
+ def ignore_config?
+ ENV["BUNDLE_IGNORE_CONFIG"]
+ end
+
+ def app_cache_path
+ @app_cache_path ||= self[:cache_path] || "vendor/cache"
+ end
+
+ def validate!
+ all.each do |raw_key|
+ [@local_config, ENV, @global_config].each do |settings|
+ value = converted_value(settings[key_for(raw_key)], raw_key)
+ Validator.validate!(raw_key, value, settings.to_hash.dup)
+ end
+ end
+ end
+
+ def key_for(key)
+ key = Settings.normalize_uri(key).to_s if key.is_a?(String) && /https?:/ =~ key
+ key = key.to_s.gsub(".", "__").upcase
+ "BUNDLE_#{key}"
+ end
+
+ private
+
+ def parent_setting_for(name)
+ split_specific_setting_for(name)[0]
+ end
+
+ def specific_gem_for(name)
+ split_specific_setting_for(name)[1]
+ end
+
+ def split_specific_setting_for(name)
+ name.split(".")
+ end
+
+ def is_bool(name)
+ BOOL_KEYS.include?(name.to_s) || BOOL_KEYS.include?(parent_setting_for(name.to_s))
+ end
+
+ def to_bool(value)
+ case value
+ when nil, /\A(false|f|no|n|0|)\z/i, false
+ false
+ else
+ true
+ end
+ end
+
+ def is_num(key)
+ NUMBER_KEYS.include?(key.to_s)
+ end
+
+ def is_array(key)
+ ARRAY_KEYS.include?(key.to_s)
+ end
+
+ def to_array(value)
+ return [] unless value
+ value.split(":").map(&:to_sym)
+ end
+
+ def array_to_s(array)
+ array = Array(array)
+ return nil if array.empty?
+ array.join(":").tr(" ", ":")
+ end
+
+ def set_key(raw_key, value, hash, file)
+ raw_key = raw_key.to_s
+ value = array_to_s(value) if is_array(raw_key)
+
+ key = key_for(raw_key)
+
+ return if hash[key] == value
+
+ hash[key] = value
+ hash.delete(key) if value.nil?
+
+ Validator.validate!(raw_key, converted_value(value, raw_key), hash)
+
+ return unless file
+ SharedHelpers.filesystem_access(file) do |p|
+ FileUtils.mkdir_p(p.dirname)
+ require "bundler/yaml_serializer"
+ p.open("w") {|f| f.write(YAMLSerializer.dump(hash)) }
+ end
+ end
+
+ def converted_value(value, key)
+ if is_array(key)
+ to_array(value)
+ elsif value.nil?
+ nil
+ elsif is_bool(key) || value == "false"
+ to_bool(value)
+ elsif is_num(key)
+ value.to_i
+ else
+ value.to_s
+ end
+ end
+
+ def global_config_file
+ if ENV["BUNDLE_CONFIG"] && !ENV["BUNDLE_CONFIG"].empty?
+ Pathname.new(ENV["BUNDLE_CONFIG"])
+ else
+ begin
+ Bundler.user_bundle_path("config")
+ rescue PermissionError, GenericSystemCallError
+ nil
+ end
+ end
+ end
+
+ def local_config_file
+ Pathname.new(@root).join("config") if @root
+ end
+
+ CONFIG_REGEX = %r{ # rubocop:disable Style/RegexpLiteral
+ ^
+ (BUNDLE_.+):\s # the key
+ (?: !\s)? # optional exclamation mark found with ruby 1.9.3
+ (['"]?) # optional opening quote
+ (.* # contents of the value
+ (?: # optionally, up until the next key
+ (\n(?!BUNDLE).+)*
+ )
+ )
+ \2 # matching closing quote
+ $
+ }xo
+
+ def load_config(config_file)
+ return {} if !config_file || ignore_config?
+ SharedHelpers.filesystem_access(config_file, :read) do |file|
+ valid_file = file.exist? && !file.size.zero?
+ return {} unless valid_file
+ require "bundler/yaml_serializer"
+ YAMLSerializer.load file.read
+ end
+ end
+
+ PER_URI_OPTIONS = %w[
+ fallback_timeout
+ ].freeze
+
+ NORMALIZE_URI_OPTIONS_PATTERN =
+ /
+ \A
+ (\w+\.)? # optional prefix key
+ (https?.*?) # URI
+ (\.#{Regexp.union(PER_URI_OPTIONS)})? # optional suffix key
+ \z
+ /ix
+
+ # TODO: duplicates Rubygems#normalize_uri
+ # TODO: is this the correct place to validate mirror URIs?
+ def self.normalize_uri(uri)
+ uri = uri.to_s
+ if uri =~ NORMALIZE_URI_OPTIONS_PATTERN
+ prefix = $1
+ uri = $2
+ suffix = $3
+ end
+ uri = "#{uri}/" unless uri.end_with?("/")
+ uri = URI(uri)
+ unless uri.absolute?
+ raise ArgumentError, format("Gem sources must be absolute. You provided '%s'.", uri)
+ end
+ "#{prefix}#{uri}#{suffix}"
+ end
+ end
+end
diff --git a/lib/bundler/settings/validator.rb b/lib/bundler/settings/validator.rb
new file mode 100644
index 00000000000000..0a57ea7f03894f
--- /dev/null
+++ b/lib/bundler/settings/validator.rb
@@ -0,0 +1,102 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Settings
+ class Validator
+ class Rule
+ attr_reader :description
+
+ def initialize(keys, description, &validate)
+ @keys = keys
+ @description = description
+ @validate = validate
+ end
+
+ def validate!(key, value, settings)
+ instance_exec(key, value, settings, &@validate)
+ end
+
+ def fail!(key, value, *reasons)
+ reasons.unshift @description
+ raise InvalidOption, "Setting `#{key}` to #{value.inspect} failed:\n#{reasons.map {|r| " - #{r}" }.join("\n")}"
+ end
+
+ def set(settings, key, value, *reasons)
+ hash_key = k(key)
+ return if settings[hash_key] == value
+ reasons.unshift @description
+ Bundler.ui.info "Setting `#{key}` to #{value.inspect}, since #{reasons.join(", ")}"
+ if value.nil?
+ settings.delete(hash_key)
+ else
+ settings[hash_key] = value
+ end
+ end
+
+ def k(key)
+ Bundler.settings.key_for(key)
+ end
+ end
+
+ def self.rules
+ @rules ||= Hash.new {|h, k| h[k] = [] }
+ end
+ private_class_method :rules
+
+ def self.rule(keys, description, &blk)
+ rule = Rule.new(keys, description, &blk)
+ keys.each {|k| rules[k] << rule }
+ end
+ private_class_method :rule
+
+ def self.validate!(key, value, settings)
+ rules_to_validate = rules[key]
+ rules_to_validate.each {|rule| rule.validate!(key, value, settings) }
+ end
+
+ rule %w[path path.system], "path and path.system are mutually exclusive" do |key, value, settings|
+ if key == "path" && value
+ set(settings, "path.system", nil)
+ elsif key == "path.system" && value
+ set(settings, :path, nil)
+ end
+ end
+
+ rule %w[with without], "a group cannot be in both `with` & `without` simultaneously" do |key, value, settings|
+ with = settings.fetch(k(:with), "").split(":").map(&:to_sym)
+ without = settings.fetch(k(:without), "").split(":").map(&:to_sym)
+
+ other_key = key == "with" ? :without : :with
+ other_setting = key == "with" ? without : with
+
+ conflicting = with & without
+ if conflicting.any?
+ fail!(key, value, "`#{other_key}` is current set to #{other_setting.inspect}", "the `#{conflicting.join("`, `")}` groups conflict")
+ end
+ end
+
+ rule %w[path], "relative paths are expanded relative to the current working directory" do |key, value, settings|
+ next if value.nil?
+
+ path = Pathname.new(value)
+ next if !path.relative? || !Bundler.feature_flag.path_relative_to_cwd?
+
+ path = path.expand_path
+
+ root = begin
+ Bundler.root
+ rescue GemfileNotFound
+ Pathname.pwd.expand_path
+ end
+
+ path = begin
+ path.relative_path_from(root)
+ rescue ArgumentError
+ path
+ end
+
+ set(settings, key, path.to_s)
+ end
+ end
+ end
+end
diff --git a/lib/bundler/setup.rb b/lib/bundler/setup.rb
new file mode 100644
index 00000000000000..0ab582c2a2398e
--- /dev/null
+++ b/lib/bundler/setup.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+return unless defined?(Gem)
+
+require "bundler/shared_helpers"
+
+if Bundler::SharedHelpers.in_bundle?
+ require "bundler"
+
+ if STDOUT.tty? || ENV["BUNDLER_FORCE_TTY"]
+ begin
+ Bundler.setup
+ rescue Bundler::BundlerError => e
+ puts "\e[31m#{e.message}\e[0m"
+ puts e.backtrace.join("\n") if ENV["DEBUG"]
+ if e.is_a?(Bundler::GemNotFound)
+ puts "\e[33mRun `bundle install` to install missing gems.\e[0m"
+ end
+ exit e.status_code
+ end
+ else
+ Bundler.setup
+ end
+
+ # Add bundler to the load path after disabling system gems
+ bundler_lib = File.expand_path("../..", __FILE__)
+ $LOAD_PATH.unshift(bundler_lib) unless $LOAD_PATH.include?(bundler_lib)
+
+ Bundler.ui = nil
+end
diff --git a/lib/bundler/shared_helpers.rb b/lib/bundler/shared_helpers.rb
new file mode 100644
index 00000000000000..50214901c5c323
--- /dev/null
+++ b/lib/bundler/shared_helpers.rb
@@ -0,0 +1,384 @@
+# frozen_string_literal: true
+
+require "bundler/compatibility_guard"
+
+require "pathname"
+require "rubygems"
+
+require "bundler/version"
+require "bundler/constants"
+require "bundler/rubygems_integration"
+require "bundler/current_ruby"
+
+module Gem
+ class Dependency
+ # This is only needed for RubyGems < 1.4
+ unless method_defined? :requirement
+ def requirement
+ version_requirements
+ end
+ end
+ end
+end
+
+module Bundler
+ module SharedHelpers
+ def root
+ gemfile = find_gemfile
+ raise GemfileNotFound, "Could not locate Gemfile" unless gemfile
+ Pathname.new(gemfile).untaint.expand_path.parent
+ end
+
+ def default_gemfile
+ gemfile = find_gemfile(:order_matters)
+ raise GemfileNotFound, "Could not locate Gemfile" unless gemfile
+ Pathname.new(gemfile).untaint.expand_path
+ end
+
+ def default_lockfile
+ gemfile = default_gemfile
+
+ case gemfile.basename.to_s
+ when "gems.rb" then Pathname.new(gemfile.sub(/.rb$/, ".locked"))
+ else Pathname.new("#{gemfile}.lock")
+ end.untaint
+ end
+
+ def default_bundle_dir
+ bundle_dir = find_directory(".bundle")
+ return nil unless bundle_dir
+
+ bundle_dir = Pathname.new(bundle_dir)
+
+ global_bundle_dir = Bundler.user_home.join(".bundle")
+ return nil if bundle_dir == global_bundle_dir
+
+ bundle_dir
+ end
+
+ def in_bundle?
+ find_gemfile
+ end
+
+ def chdir(dir, &blk)
+ Bundler.rubygems.ext_lock.synchronize do
+ Dir.chdir dir, &blk
+ end
+ end
+
+ def pwd
+ Bundler.rubygems.ext_lock.synchronize do
+ Pathname.pwd
+ end
+ end
+
+ def with_clean_git_env(&block)
+ keys = %w[GIT_DIR GIT_WORK_TREE]
+ old_env = keys.inject({}) do |h, k|
+ h.update(k => ENV[k])
+ end
+
+ keys.each {|key| ENV.delete(key) }
+
+ block.call
+ ensure
+ keys.each {|key| ENV[key] = old_env[key] }
+ end
+
+ def set_bundle_environment
+ set_bundle_variables
+ set_path
+ set_rubyopt
+ set_rubylib
+ end
+
+ # Rescues permissions errors raised by file system operations
+ # (ie. Errno:EACCESS, Errno::EAGAIN) and raises more friendly errors instead.
+ #
+ # @param path [String] the path that the action will be attempted to
+ # @param action [Symbol, #to_s] the type of operation that will be
+ # performed. For example: :write, :read, :exec
+ #
+ # @yield path
+ #
+ # @raise [Bundler::PermissionError] if Errno:EACCES is raised in the
+ # given block
+ # @raise [Bundler::TemporaryResourceError] if Errno:EAGAIN is raised in the
+ # given block
+ #
+ # @example
+ # filesystem_access("vendor/cache", :write) do
+ # FileUtils.mkdir_p("vendor/cache")
+ # end
+ #
+ # @see {Bundler::PermissionError}
+ def filesystem_access(path, action = :write, &block)
+ # Use block.call instead of yield because of a bug in Ruby 2.2.2
+ # See https://github.com/bundler/bundler/issues/5341 for details
+ block.call(path.dup.untaint)
+ rescue Errno::EACCES
+ raise PermissionError.new(path, action)
+ rescue Errno::EAGAIN
+ raise TemporaryResourceError.new(path, action)
+ rescue Errno::EPROTO
+ raise VirtualProtocolError.new
+ rescue Errno::ENOSPC
+ raise NoSpaceOnDeviceError.new(path, action)
+ rescue *[const_get_safely(:ENOTSUP, Errno)].compact
+ raise OperationNotSupportedError.new(path, action)
+ rescue Errno::EEXIST, Errno::ENOENT
+ raise
+ rescue SystemCallError => e
+ raise GenericSystemCallError.new(e, "There was an error accessing `#{path}`.")
+ end
+
+ def const_get_safely(constant_name, namespace)
+ const_in_namespace = namespace.constants.include?(constant_name.to_s) ||
+ namespace.constants.include?(constant_name.to_sym)
+ return nil unless const_in_namespace
+ namespace.const_get(constant_name)
+ end
+
+ def major_deprecation(major_version, message)
+ if Bundler.bundler_major_version >= major_version
+ require "bundler/errors"
+ raise DeprecatedError, "[REMOVED FROM #{major_version}.0] #{message}"
+ end
+
+ return unless prints_major_deprecations?
+ @major_deprecation_ui ||= Bundler::UI::Shell.new("no-color" => true)
+ ui = Bundler.ui.is_a?(@major_deprecation_ui.class) ? Bundler.ui : @major_deprecation_ui
+ ui.warn("[DEPRECATED FOR #{major_version}.0] #{message}")
+ end
+
+ def print_major_deprecations!
+ multiple_gemfiles = search_up(".") do |dir|
+ gemfiles = gemfile_names.select {|gf| File.file? File.expand_path(gf, dir) }
+ next if gemfiles.empty?
+ break false if gemfiles.size == 1
+ end
+ if multiple_gemfiles && Bundler.bundler_major_version == 2
+ Bundler::SharedHelpers.major_deprecation 3, \
+ "gems.rb and gems.locked will be preferred to Gemfile and Gemfile.lock."
+ end
+
+ if RUBY_VERSION < "2"
+ major_deprecation(2, "Bundler will only support ruby >= 2.0, you are running #{RUBY_VERSION}")
+ end
+ return if Bundler.rubygems.provides?(">= 2")
+ major_deprecation(2, "Bundler will only support rubygems >= 2.0, you are running #{Bundler.rubygems.version}")
+ end
+
+ def trap(signal, override = false, &block)
+ prior = Signal.trap(signal) do
+ block.call
+ prior.call unless override
+ end
+ end
+
+ def ensure_same_dependencies(spec, old_deps, new_deps)
+ new_deps = new_deps.reject {|d| d.type == :development }
+ old_deps = old_deps.reject {|d| d.type == :development }
+
+ without_type = proc {|d| Gem::Dependency.new(d.name, d.requirements_list.sort) }
+ new_deps.map!(&without_type)
+ old_deps.map!(&without_type)
+
+ extra_deps = new_deps - old_deps
+ return if extra_deps.empty?
+
+ Bundler.ui.debug "#{spec.full_name} from #{spec.remote} has either corrupted API or lockfile dependencies" \
+ " (was expecting #{old_deps.map(&:to_s)}, but the real spec has #{new_deps.map(&:to_s)})"
+ raise APIResponseMismatchError,
+ "Downloading #{spec.full_name} revealed dependencies not in the API or the lockfile (#{extra_deps.join(", ")})." \
+ "\nEither installing with `--full-index` or running `bundle update #{spec.name}` should fix the problem."
+ end
+
+ def pretty_dependency(dep, print_source = false)
+ msg = String.new(dep.name)
+ msg << " (#{dep.requirement})" unless dep.requirement == Gem::Requirement.default
+
+ if dep.is_a?(Bundler::Dependency)
+ platform_string = dep.platforms.join(", ")
+ msg << " " << platform_string if !platform_string.empty? && platform_string != Gem::Platform::RUBY
+ end
+
+ msg << " from the `#{dep.source}` source" if print_source && dep.source
+ msg
+ end
+
+ def md5_available?
+ return @md5_available if defined?(@md5_available)
+ @md5_available = begin
+ require "openssl"
+ OpenSSL::Digest::MD5.digest("")
+ true
+ rescue LoadError
+ true
+ rescue OpenSSL::Digest::DigestError
+ false
+ end
+ end
+
+ def digest(name)
+ require "digest"
+ Digest(name)
+ end
+
+ def write_to_gemfile(gemfile_path, contents)
+ filesystem_access(gemfile_path) {|g| File.open(g, "w") {|file| file.puts contents } }
+ end
+
+ private
+
+ def validate_bundle_path
+ path_separator = Bundler.rubygems.path_separator
+ return unless Bundler.bundle_path.to_s.split(path_separator).size > 1
+ message = "Your bundle path contains text matching #{path_separator.inspect}, " \
+ "which is the path separator for your system. Bundler cannot " \
+ "function correctly when the Bundle path contains the " \
+ "system's PATH separator. Please change your " \
+ "bundle path to not match #{path_separator.inspect}." \
+ "\nYour current bundle path is '#{Bundler.bundle_path}'."
+ raise Bundler::PathError, message
+ end
+
+ def find_gemfile(order_matters = false)
+ given = ENV["BUNDLE_GEMFILE"]
+ return given if given && !given.empty?
+ names = gemfile_names
+ names.reverse! if order_matters && Bundler.feature_flag.prefer_gems_rb?
+ find_file(*names)
+ end
+
+ def gemfile_names
+ ["Gemfile", "gems.rb"]
+ end
+
+ def find_file(*names)
+ search_up(*names) do |filename|
+ return filename if File.file?(filename)
+ end
+ end
+
+ def find_directory(*names)
+ search_up(*names) do |dirname|
+ return dirname if File.directory?(dirname)
+ end
+ end
+
+ def search_up(*names)
+ previous = nil
+ current = File.expand_path(SharedHelpers.pwd).untaint
+
+ until !File.directory?(current) || current == previous
+ if ENV["BUNDLE_SPEC_RUN"]
+ # avoid stepping above the tmp directory when testing
+ gemspec = if ENV["BUNDLE_RUBY"] && ENV["BUNDLE_GEM"]
+ # for Ruby Core
+ "lib/bundler.gemspec"
+ else
+ "bundler.gemspec"
+ end
+
+ # avoid stepping above the tmp directory when testing
+ return nil if File.file?(File.join(current, gemspec))
+ end
+
+ names.each do |name|
+ filename = File.join(current, name)
+ yield filename
+ end
+ previous = current
+ current = File.expand_path("..", current)
+ end
+ end
+
+ def set_env(key, value)
+ raise ArgumentError, "new key #{key}" unless EnvironmentPreserver::BUNDLER_KEYS.include?(key)
+ orig_key = "#{EnvironmentPreserver::BUNDLER_PREFIX}#{key}"
+ orig = ENV[key]
+ orig ||= EnvironmentPreserver::INTENTIONALLY_NIL
+ ENV[orig_key] ||= orig
+
+ ENV[key] = value
+ end
+ public :set_env
+
+ def set_bundle_variables
+ begin
+ exe_file = Bundler.rubygems.bin_path("bundler", "bundle", VERSION)
+ unless File.exist?(exe_file)
+ exe_file = File.expand_path("../../../exe/bundle", __FILE__)
+ end
+ Bundler::SharedHelpers.set_env "BUNDLE_BIN_PATH", exe_file
+ rescue Gem::GemNotFoundException
+ exe_file = File.expand_path("../../../exe/bundle", __FILE__)
+ # for Ruby core repository
+ exe_file = File.expand_path("../../../../bin/bundle", __FILE__) unless File.exist?(exe_file)
+ Bundler::SharedHelpers.set_env "BUNDLE_BIN_PATH", exe_file
+ end
+
+ # Set BUNDLE_GEMFILE
+ Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", find_gemfile(:order_matters).to_s
+ Bundler::SharedHelpers.set_env "BUNDLER_VERSION", Bundler::VERSION
+ end
+
+ def set_path
+ validate_bundle_path
+ paths = (ENV["PATH"] || "").split(File::PATH_SEPARATOR)
+ paths.unshift "#{Bundler.bundle_path}/bin"
+ Bundler::SharedHelpers.set_env "PATH", paths.uniq.join(File::PATH_SEPARATOR)
+ end
+
+ def set_rubyopt
+ rubyopt = [ENV["RUBYOPT"]].compact
+ return if !rubyopt.empty? && rubyopt.first =~ %r{-rbundler/setup}
+ rubyopt.unshift %(-rbundler/setup)
+ Bundler::SharedHelpers.set_env "RUBYOPT", rubyopt.join(" ")
+ end
+
+ def set_rubylib
+ rubylib = (ENV["RUBYLIB"] || "").split(File::PATH_SEPARATOR)
+ rubylib.unshift bundler_ruby_lib
+ Bundler::SharedHelpers.set_env "RUBYLIB", rubylib.uniq.join(File::PATH_SEPARATOR)
+ end
+
+ def bundler_ruby_lib
+ resolve_path File.expand_path("../..", __FILE__)
+ end
+
+ def clean_load_path
+ # handle 1.9 where system gems are always on the load path
+ return unless defined?(::Gem)
+
+ bundler_lib = bundler_ruby_lib
+
+ loaded_gem_paths = Bundler.rubygems.loaded_gem_paths
+
+ $LOAD_PATH.reject! do |p|
+ next if resolve_path(p).start_with?(bundler_lib)
+ loaded_gem_paths.delete(p)
+ end
+ $LOAD_PATH.uniq!
+ end
+
+ def resolve_path(path)
+ expanded = File.expand_path(path)
+ return expanded unless File.respond_to?(:realpath) && File.exist?(expanded)
+
+ File.realpath(expanded)
+ end
+
+ def prints_major_deprecations?
+ require "bundler"
+ deprecation_release = Bundler::VERSION.split(".").drop(1).include?("99")
+ return false if !deprecation_release && !Bundler.settings[:major_deprecations]
+ require "bundler/deprecate"
+ return false if Bundler::Deprecate.skip
+ true
+ end
+
+ extend self
+ end
+end
diff --git a/lib/bundler/similarity_detector.rb b/lib/bundler/similarity_detector.rb
new file mode 100644
index 00000000000000..b7f3ee7afaddf6
--- /dev/null
+++ b/lib/bundler/similarity_detector.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+module Bundler
+ class SimilarityDetector
+ SimilarityScore = Struct.new(:string, :distance)
+
+ # initialize with an array of words to be matched against
+ def initialize(corpus)
+ @corpus = corpus
+ end
+
+ # return an array of words similar to 'word' from the corpus
+ def similar_words(word, limit = 3)
+ words_by_similarity = @corpus.map {|w| SimilarityScore.new(w, levenshtein_distance(word, w)) }
+ words_by_similarity.select {|s| s.distance <= limit }.sort_by(&:distance).map(&:string)
+ end
+
+ # return the result of 'similar_words', concatenated into a list
+ # (eg "a, b, or c")
+ def similar_word_list(word, limit = 3)
+ words = similar_words(word, limit)
+ if words.length == 1
+ words[0]
+ elsif words.length > 1
+ [words[0..-2].join(", "), words[-1]].join(" or ")
+ end
+ end
+
+ protected
+
+ # http://www.informit.com/articles/article.aspx?p=683059&seqNum=36
+ def levenshtein_distance(this, that, ins = 2, del = 2, sub = 1)
+ # ins, del, sub are weighted costs
+ return nil if this.nil?
+ return nil if that.nil?
+ dm = [] # distance matrix
+
+ # Initialize first row values
+ dm[0] = (0..this.length).collect {|i| i * ins }
+ fill = [0] * (this.length - 1)
+
+ # Initialize first column values
+ (1..that.length).each do |i|
+ dm[i] = [i * del, fill.flatten]
+ end
+
+ # populate matrix
+ (1..that.length).each do |i|
+ (1..this.length).each do |j|
+ # critical comparison
+ dm[i][j] = [
+ dm[i - 1][j - 1] + (this[j - 1] == that[i - 1] ? 0 : sub),
+ dm[i][j - 1] + ins,
+ dm[i - 1][j] + del
+ ].min
+ end
+ end
+
+ # The last value in matrix is the Levenshtein distance between the strings
+ dm[that.length][this.length]
+ end
+ end
+end
diff --git a/lib/bundler/source.rb b/lib/bundler/source.rb
new file mode 100644
index 00000000000000..26a3625bb17578
--- /dev/null
+++ b/lib/bundler/source.rb
@@ -0,0 +1,94 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Source
+ autoload :Gemspec, "bundler/source/gemspec"
+ autoload :Git, "bundler/source/git"
+ autoload :Metadata, "bundler/source/metadata"
+ autoload :Path, "bundler/source/path"
+ autoload :Rubygems, "bundler/source/rubygems"
+
+ attr_accessor :dependency_names
+
+ def unmet_deps
+ specs.unmet_dependency_names
+ end
+
+ def version_message(spec)
+ message = "#{spec.name} #{spec.version}"
+ message += " (#{spec.platform})" if spec.platform != Gem::Platform::RUBY && !spec.platform.nil?
+
+ if Bundler.locked_gems
+ locked_spec = Bundler.locked_gems.specs.find {|s| s.name == spec.name }
+ locked_spec_version = locked_spec.version if locked_spec
+ if locked_spec_version && spec.version != locked_spec_version
+ message += Bundler.ui.add_color(" (was #{locked_spec_version})", version_color(spec.version, locked_spec_version))
+ end
+ end
+
+ message
+ end
+
+ def can_lock?(spec)
+ spec.source == self
+ end
+
+ # it's possible that gems from one source depend on gems from some
+ # other source, so now we download gemspecs and iterate over those
+ # dependencies, looking for gems we don't have info on yet.
+ def double_check_for(*); end
+
+ def dependency_names_to_double_check
+ specs.dependency_names
+ end
+
+ def include?(other)
+ other == self
+ end
+
+ def inspect
+ "#<#{self.class}:0x#{object_id} #{self}>"
+ end
+
+ def path?
+ instance_of?(Bundler::Source::Path)
+ end
+
+ def extension_cache_path(spec)
+ return unless Bundler.feature_flag.global_gem_cache?
+ return unless source_slug = extension_cache_slug(spec)
+ Bundler.user_cache.join(
+ "extensions", Gem::Platform.local.to_s, Bundler.ruby_scope,
+ source_slug, spec.full_name
+ )
+ end
+
+ private
+
+ def version_color(spec_version, locked_spec_version)
+ if Gem::Version.correct?(spec_version) && Gem::Version.correct?(locked_spec_version)
+ # display yellow if there appears to be a regression
+ earlier_version?(spec_version, locked_spec_version) ? :yellow : :green
+ else
+ # default to green if the versions cannot be directly compared
+ :green
+ end
+ end
+
+ def earlier_version?(spec_version, locked_spec_version)
+ Gem::Version.new(spec_version) < Gem::Version.new(locked_spec_version)
+ end
+
+ def print_using_message(message)
+ if !message.include?("(was ") && Bundler.feature_flag.suppress_install_using_messages?
+ Bundler.ui.debug message
+ else
+ Bundler.ui.info message
+ end
+ end
+
+ def extension_cache_slug(_)
+ nil
+ end
+ end
+end
diff --git a/lib/bundler/source/gemspec.rb b/lib/bundler/source/gemspec.rb
new file mode 100644
index 00000000000000..7e3447e7765402
--- /dev/null
+++ b/lib/bundler/source/gemspec.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Source
+ class Gemspec < Path
+ attr_reader :gemspec
+
+ def initialize(options)
+ super
+ @gemspec = options["gemspec"]
+ end
+
+ def as_path_source
+ Path.new(options)
+ end
+ end
+ end
+end
diff --git a/lib/bundler/source/git.rb b/lib/bundler/source/git.rb
new file mode 100644
index 00000000000000..0b00608bdd6177
--- /dev/null
+++ b/lib/bundler/source/git.rb
@@ -0,0 +1,329 @@
+# frozen_string_literal: true
+
+require "bundler/vendored_fileutils"
+require "uri"
+
+module Bundler
+ class Source
+ class Git < Path
+ autoload :GitProxy, "bundler/source/git/git_proxy"
+
+ attr_reader :uri, :ref, :branch, :options, :submodules
+
+ def initialize(options)
+ @options = options
+ @glob = options["glob"] || DEFAULT_GLOB
+
+ @allow_cached = false
+ @allow_remote = false
+
+ # Stringify options that could be set as symbols
+ %w[ref branch tag revision].each {|k| options[k] = options[k].to_s if options[k] }
+
+ @uri = options["uri"] || ""
+ @safe_uri = URICredentialsFilter.credential_filtered_uri(@uri)
+ @branch = options["branch"]
+ @ref = options["ref"] || options["branch"] || options["tag"] || "master"
+ @submodules = options["submodules"]
+ @name = options["name"]
+ @version = options["version"].to_s.strip.gsub("-", ".pre.")
+
+ @copied = false
+ @local = false
+ end
+
+ def self.from_lock(options)
+ new(options.merge("uri" => options.delete("remote")))
+ end
+
+ def to_lock
+ out = String.new("GIT\n")
+ out << " remote: #{@uri}\n"
+ out << " revision: #{revision}\n"
+ %w[ref branch tag submodules].each do |opt|
+ out << " #{opt}: #{options[opt]}\n" if options[opt]
+ end
+ out << " glob: #{@glob}\n" unless @glob == DEFAULT_GLOB
+ out << " specs:\n"
+ end
+
+ def hash
+ [self.class, uri, ref, branch, name, version, submodules].hash
+ end
+
+ def eql?(other)
+ other.is_a?(Git) && uri == other.uri && ref == other.ref &&
+ branch == other.branch && name == other.name &&
+ version == other.version && submodules == other.submodules
+ end
+
+ alias_method :==, :eql?
+
+ def to_s
+ at = if local?
+ path
+ elsif user_ref = options["ref"]
+ if ref =~ /\A[a-z0-9]{4,}\z/i
+ shortref_for_display(user_ref)
+ else
+ user_ref
+ end
+ else
+ ref
+ end
+
+ rev = begin
+ "@#{shortref_for_display(revision)}"
+ rescue GitError
+ nil
+ end
+
+ "#{@safe_uri} (at #{at}#{rev})"
+ end
+
+ def name
+ File.basename(@uri, ".git")
+ end
+
+ # This is the path which is going to contain a specific
+ # checkout of the git repository. When using local git
+ # repos, this is set to the local repo.
+ def install_path
+ @install_path ||= begin
+ git_scope = "#{base_name}-#{shortref_for_path(revision)}"
+
+ path = Bundler.install_path.join(git_scope)
+
+ if !path.exist? && Bundler.requires_sudo?
+ Bundler.user_bundle_path.join(Bundler.ruby_scope).join(git_scope)
+ else
+ path
+ end
+ end
+ end
+
+ alias_method :path, :install_path
+
+ def extension_dir_name
+ "#{base_name}-#{shortref_for_path(revision)}"
+ end
+
+ def unlock!
+ git_proxy.revision = nil
+ options["revision"] = nil
+
+ @unlocked = true
+ end
+
+ def local_override!(path)
+ return false if local?
+
+ path = Pathname.new(path)
+ path = path.expand_path(Bundler.root) unless path.relative?
+
+ unless options["branch"] || Bundler.settings[:disable_local_branch_check]
+ raise GitError, "Cannot use local override for #{name} at #{path} because " \
+ ":branch is not specified in Gemfile. Specify a branch or use " \
+ "`bundle config --delete` to remove the local override"
+ end
+
+ unless path.exist?
+ raise GitError, "Cannot use local override for #{name} because #{path} " \
+ "does not exist. Check `bundle config --delete` to remove the local override"
+ end
+
+ set_local!(path)
+
+ # Create a new git proxy without the cached revision
+ # so the Gemfile.lock always picks up the new revision.
+ @git_proxy = GitProxy.new(path, uri, ref)
+
+ if git_proxy.branch != options["branch"] && !Bundler.settings[:disable_local_branch_check]
+ raise GitError, "Local override for #{name} at #{path} is using branch " \
+ "#{git_proxy.branch} but Gemfile specifies #{options["branch"]}"
+ end
+
+ changed = cached_revision && cached_revision != git_proxy.revision
+
+ if changed && !@unlocked && !git_proxy.contains?(cached_revision)
+ raise GitError, "The Gemfile lock is pointing to revision #{shortref_for_display(cached_revision)} " \
+ "but the current branch in your local override for #{name} does not contain such commit. " \
+ "Please make sure your branch is up to date."
+ end
+
+ changed
+ end
+
+ def specs(*)
+ set_local!(app_cache_path) if has_app_cache? && !local?
+
+ if requires_checkout? && !@copied
+ fetch
+ git_proxy.copy_to(install_path, submodules)
+ serialize_gemspecs_in(install_path)
+ @copied = true
+ end
+
+ local_specs
+ end
+
+ def install(spec, options = {})
+ force = options[:force]
+
+ print_using_message "Using #{version_message(spec)} from #{self}"
+
+ if (requires_checkout? && !@copied) || force
+ Bundler.ui.debug " * Checking out revision: #{ref}"
+ git_proxy.copy_to(install_path, submodules)
+ serialize_gemspecs_in(install_path)
+ @copied = true
+ end
+
+ generate_bin_options = { :disable_extensions => !Bundler.rubygems.spec_missing_extensions?(spec), :build_args => options[:build_args] }
+ generate_bin(spec, generate_bin_options)
+
+ requires_checkout? ? spec.post_install_message : nil
+ end
+
+ def cache(spec, custom_path = nil)
+ app_cache_path = app_cache_path(custom_path)
+ return unless Bundler.feature_flag.cache_all?
+ return if path == app_cache_path
+ cached!
+ FileUtils.rm_rf(app_cache_path)
+ git_proxy.checkout if requires_checkout?
+ git_proxy.copy_to(app_cache_path, @submodules)
+ serialize_gemspecs_in(app_cache_path)
+ end
+
+ def load_spec_files
+ super
+ rescue PathError => e
+ Bundler.ui.trace e
+ raise GitError, "#{self} is not yet checked out. Run `bundle install` first."
+ end
+
+ # This is the path which is going to contain a cache
+ # of the git repository. When using the same git repository
+ # across different projects, this cache will be shared.
+ # When using local git repos, this is set to the local repo.
+ def cache_path
+ @cache_path ||= begin
+ if Bundler.requires_sudo? || Bundler.feature_flag.global_gem_cache?
+ Bundler.user_cache
+ else
+ Bundler.bundle_path.join("cache", "bundler")
+ end.join("git", git_scope)
+ end
+ end
+
+ def app_cache_dirname
+ "#{base_name}-#{shortref_for_path(cached_revision || revision)}"
+ end
+
+ def revision
+ git_proxy.revision
+ end
+
+ def allow_git_ops?
+ @allow_remote || @allow_cached
+ end
+
+ private
+
+ def serialize_gemspecs_in(destination)
+ destination = destination.expand_path(Bundler.root) if destination.relative?
+ Dir["#{destination}/#{@glob}"].each do |spec_path|
+ # Evaluate gemspecs and cache the result. Gemspecs
+ # in git might require git or other dependencies.
+ # The gemspecs we cache should already be evaluated.
+ spec = Bundler.load_gemspec(spec_path)
+ next unless spec
+ Bundler.rubygems.set_installed_by_version(spec)
+ Bundler.rubygems.validate(spec)
+ File.open(spec_path, "wb") {|file| file.write(spec.to_ruby) }
+ end
+ end
+
+ def set_local!(path)
+ @local = true
+ @local_specs = @git_proxy = nil
+ @cache_path = @install_path = path
+ end
+
+ def has_app_cache?
+ cached_revision && super
+ end
+
+ def local?
+ @local
+ end
+
+ def requires_checkout?
+ allow_git_ops? && !local?
+ end
+
+ def base_name
+ File.basename(uri.sub(%r{^(\w+://)?([^/:]+:)?(//\w*/)?(\w*/)*}, ""), ".git")
+ end
+
+ def shortref_for_display(ref)
+ ref[0..6]
+ end
+
+ def shortref_for_path(ref)
+ ref[0..11]
+ end
+
+ def uri_hash
+ if uri =~ %r{^\w+://(\w+@)?}
+ # Downcase the domain component of the URI
+ # and strip off a trailing slash, if one is present
+ input = URI.parse(uri).normalize.to_s.sub(%r{/$}, "")
+ else
+ # If there is no URI scheme, assume it is an ssh/git URI
+ input = uri
+ end
+ SharedHelpers.digest(:SHA1).hexdigest(input)
+ end
+
+ def cached_revision
+ options["revision"]
+ end
+
+ def cached?
+ cache_path.exist?
+ end
+
+ def git_proxy
+ @git_proxy ||= GitProxy.new(cache_path, uri, ref, cached_revision, self)
+ end
+
+ def fetch
+ git_proxy.checkout
+ rescue GitError => e
+ raise unless Bundler.feature_flag.allow_offline_install?
+ Bundler.ui.warn "Using cached git data because of network errors:\n#{e}"
+ end
+
+ # no-op, since we validate when re-serializing the gemspec
+ def validate_spec(_spec); end
+
+ if Bundler.rubygems.stubs_provide_full_functionality?
+ def load_gemspec(file)
+ stub = Gem::StubSpecification.gemspec_stub(file, install_path.parent, install_path.parent)
+ stub.full_gem_path = Pathname.new(file).dirname.expand_path(root).to_s.untaint
+ StubSpecification.from_stub(stub)
+ end
+ end
+
+ def git_scope
+ "#{base_name}-#{uri_hash}"
+ end
+
+ def extension_cache_slug(_)
+ extension_dir_name
+ end
+ end
+ end
+end
diff --git a/lib/bundler/source/git/git_proxy.rb b/lib/bundler/source/git/git_proxy.rb
new file mode 100644
index 00000000000000..cd964f7e56491a
--- /dev/null
+++ b/lib/bundler/source/git/git_proxy.rb
@@ -0,0 +1,262 @@
+# frozen_string_literal: true
+
+require "shellwords"
+require "tempfile"
+module Bundler
+ class Source
+ class Git
+ class GitNotInstalledError < GitError
+ def initialize
+ msg = String.new
+ msg << "You need to install git to be able to use gems from git repositories. "
+ msg << "For help installing git, please refer to GitHub's tutorial at https://help.github.com/articles/set-up-git"
+ super msg
+ end
+ end
+
+ class GitNotAllowedError < GitError
+ def initialize(command)
+ msg = String.new
+ msg << "Bundler is trying to run a `git #{command}` at runtime. You probably need to run `bundle install`. However, "
+ msg << "this error message could probably be more useful. Please submit a ticket at http://github.com/bundler/bundler/issues "
+ msg << "with steps to reproduce as well as the following\n\nCALLER: #{caller.join("\n")}"
+ super msg
+ end
+ end
+
+ class GitCommandError < GitError
+ def initialize(command, path = nil, extra_info = nil)
+ msg = String.new
+ msg << "Git error: command `git #{command}` in directory #{SharedHelpers.pwd} has failed."
+ msg << "\n#{extra_info}" if extra_info
+ msg << "\nIf this error persists you could try removing the cache directory '#{path}'" if path && path.exist?
+ super msg
+ end
+ end
+
+ class MissingGitRevisionError < GitError
+ def initialize(ref, repo)
+ msg = "Revision #{ref} does not exist in the repository #{repo}. Maybe you misspelled it?"
+ super msg
+ end
+ end
+
+ # The GitProxy is responsible to interact with git repositories.
+ # All actions required by the Git source is encapsulated in this
+ # object.
+ class GitProxy
+ attr_accessor :path, :uri, :ref
+ attr_writer :revision
+
+ def initialize(path, uri, ref, revision = nil, git = nil)
+ @path = path
+ @uri = uri
+ @ref = ref
+ @revision = revision
+ @git = git
+ raise GitNotInstalledError.new if allow? && !Bundler.git_present?
+ end
+
+ def revision
+ return @revision if @revision
+
+ begin
+ @revision ||= find_local_revision
+ rescue GitCommandError
+ raise MissingGitRevisionError.new(ref, URICredentialsFilter.credential_filtered_uri(uri))
+ end
+
+ @revision
+ end
+
+ def branch
+ @branch ||= allowed_in_path do
+ git("rev-parse --abbrev-ref HEAD").strip
+ end
+ end
+
+ def contains?(commit)
+ allowed_in_path do
+ result = git_null("branch --contains #{commit}")
+ $? == 0 && result =~ /^\* (.*)$/
+ end
+ end
+
+ def version
+ git("--version").match(/(git version\s*)?((\.?\d+)+).*/)[2]
+ end
+
+ def full_version
+ git("--version").sub("git version", "").strip
+ end
+
+ def checkout
+ return if path.exist? && has_revision_cached?
+ extra_ref = "#{Shellwords.shellescape(ref)}:#{Shellwords.shellescape(ref)}" if ref && ref.start_with?("refs/")
+
+ Bundler.ui.info "Fetching #{URICredentialsFilter.credential_filtered_uri(uri)}"
+
+ unless path.exist?
+ SharedHelpers.filesystem_access(path.dirname) do |p|
+ FileUtils.mkdir_p(p)
+ end
+ git_retry %(clone #{uri_escaped_with_configured_credentials} "#{path}" --bare --no-hardlinks --quiet)
+ return unless extra_ref
+ end
+
+ in_path do
+ git_retry %(fetch --force --quiet --tags #{uri_escaped_with_configured_credentials} "refs/heads/*:refs/heads/*" #{extra_ref})
+ end
+ end
+
+ def copy_to(destination, submodules = false)
+ # method 1
+ unless File.exist?(destination.join(".git"))
+ begin
+ SharedHelpers.filesystem_access(destination.dirname) do |p|
+ FileUtils.mkdir_p(p)
+ end
+ SharedHelpers.filesystem_access(destination) do |p|
+ FileUtils.rm_rf(p)
+ end
+ git_retry %(clone --no-checkout --quiet "#{path}" "#{destination}")
+ File.chmod(((File.stat(destination).mode | 0o777) & ~File.umask), destination)
+ rescue Errno::EEXIST => e
+ file_path = e.message[%r{.*?(/.*)}, 1]
+ raise GitError, "Bundler could not install a gem because it needs to " \
+ "create a directory, but a file exists - #{file_path}. Please delete " \
+ "this file and try again."
+ end
+ end
+ # method 2
+ SharedHelpers.chdir(destination) do
+ git_retry %(fetch --force --quiet --tags "#{path}")
+
+ begin
+ git "reset --hard #{@revision}"
+ rescue GitCommandError
+ raise MissingGitRevisionError.new(@revision, URICredentialsFilter.credential_filtered_uri(uri))
+ end
+
+ if submodules
+ git_retry "submodule update --init --recursive"
+ elsif Gem::Version.create(version) >= Gem::Version.create("2.9.0")
+ git_retry "submodule deinit --all --force"
+ end
+ end
+ end
+
+ private
+
+ # TODO: Do not rely on /dev/null.
+ # Given that open3 is not cross platform until Ruby 1.9.3,
+ # the best solution is to pipe to /dev/null if it exists.
+ # If it doesn't, everything will work fine, but the user
+ # will get the $stderr messages as well.
+ def git_null(command)
+ git("#{command} 2>#{Bundler::NULL}", false)
+ end
+
+ def git_retry(command)
+ Bundler::Retry.new("`git #{URICredentialsFilter.credential_filtered_string(command, uri)}`", GitNotAllowedError).attempts do
+ git(command)
+ end
+ end
+
+ def git(command, check_errors = true, error_msg = nil)
+ command_with_no_credentials = URICredentialsFilter.credential_filtered_string(command, uri)
+ raise GitNotAllowedError.new(command_with_no_credentials) unless allow?
+
+ out = SharedHelpers.with_clean_git_env do
+ capture_and_filter_stderr(uri) { `git #{command}` }
+ end
+
+ stdout_with_no_credentials = URICredentialsFilter.credential_filtered_string(out, uri)
+ raise GitCommandError.new(command_with_no_credentials, path, error_msg) if check_errors && !$?.success?
+ stdout_with_no_credentials
+ end
+
+ def has_revision_cached?
+ return unless @revision
+ in_path { git("cat-file -e #{@revision}") }
+ true
+ rescue GitError
+ false
+ end
+
+ def remove_cache
+ FileUtils.rm_rf(path)
+ end
+
+ def find_local_revision
+ allowed_in_path do
+ git("rev-parse --verify #{Shellwords.shellescape(ref)}", true).strip
+ end
+ end
+
+ # Escape the URI for git commands
+ def uri_escaped_with_configured_credentials
+ remote = configured_uri_for(uri)
+ if Bundler::WINDOWS
+ # Windows quoting requires double quotes only, with double quotes
+ # inside the string escaped by being doubled.
+ '"' + remote.gsub('"') { '""' } + '"'
+ else
+ # Bash requires single quoted strings, with the single quotes escaped
+ # by ending the string, escaping the quote, and restarting the string.
+ "'" + remote.gsub("'") { "'\\''" } + "'"
+ end
+ end
+
+ # Adds credentials to the URI as Fetcher#configured_uri_for does
+ def configured_uri_for(uri)
+ if /https?:/ =~ uri
+ remote = URI(uri)
+ config_auth = Bundler.settings[remote.to_s] || Bundler.settings[remote.host]
+ remote.userinfo ||= config_auth
+ remote.to_s
+ else
+ uri
+ end
+ end
+
+ def allow?
+ @git ? @git.allow_git_ops? : true
+ end
+
+ def in_path(&blk)
+ checkout unless path.exist?
+ _ = URICredentialsFilter # load it before we chdir
+ SharedHelpers.chdir(path, &blk)
+ end
+
+ def allowed_in_path
+ return in_path { yield } if allow?
+ raise GitError, "The git source #{uri} is not yet checked out. Please run `bundle install` before trying to start your application"
+ end
+
+ # TODO: Replace this with Open3 when upgrading to bundler 2
+ # Similar to #git_null, as Open3 is not cross-platform,
+ # a temporary way is to use Tempfile to capture the stderr.
+ # When replacing this using Open3, make sure git_null is
+ # also replaced by Open3, so stdout and stderr all got handled properly.
+ def capture_and_filter_stderr(uri)
+ return_value, captured_err = ""
+ backup_stderr = STDERR.dup
+ begin
+ Tempfile.open("captured_stderr") do |f|
+ STDERR.reopen(f)
+ return_value = yield
+ f.rewind
+ captured_err = f.read
+ end
+ ensure
+ STDERR.reopen backup_stderr
+ end
+ $stderr.puts URICredentialsFilter.credential_filtered_string(captured_err, uri) if uri && !captured_err.empty?
+ return_value
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/source/metadata.rb b/lib/bundler/source/metadata.rb
new file mode 100644
index 00000000000000..9c5657eef61e73
--- /dev/null
+++ b/lib/bundler/source/metadata.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Source
+ class Metadata < Source
+ def specs
+ @specs ||= Index.build do |idx|
+ idx << Gem::Specification.new("ruby\0", RubyVersion.system.to_gem_version_with_patchlevel)
+ idx << Gem::Specification.new("rubygems\0", Gem::VERSION)
+
+ idx << Gem::Specification.new do |s|
+ s.name = "bundler"
+ s.version = VERSION
+ s.platform = Gem::Platform::RUBY
+ s.source = self
+ s.authors = ["bundler team"]
+ s.bindir = "exe"
+ s.executables = %w[bundle]
+ # can't point to the actual gemspec or else the require paths will be wrong
+ s.loaded_from = File.expand_path("..", __FILE__)
+ end
+ if loaded_spec = Bundler.rubygems.loaded_specs("bundler")
+ idx << loaded_spec # this has to come after the fake gemspec, to override it
+ elsif local_spec = Bundler.rubygems.find_name("bundler").find {|s| s.version.to_s == VERSION }
+ idx << local_spec
+ end
+
+ idx.each {|s| s.source = self }
+ end
+ end
+
+ def cached!; end
+
+ def remote!; end
+
+ def options
+ {}
+ end
+
+ def install(spec, _opts = {})
+ print_using_message "Using #{version_message(spec)}"
+ nil
+ end
+
+ def to_s
+ "the local ruby installation"
+ end
+
+ def ==(other)
+ self.class == other.class
+ end
+ alias_method :eql?, :==
+
+ def hash
+ self.class.hash
+ end
+
+ def version_message(spec)
+ "#{spec.name} #{spec.version}"
+ end
+ end
+ end
+end
diff --git a/lib/bundler/source/path.rb b/lib/bundler/source/path.rb
new file mode 100644
index 00000000000000..ed734bf549aae6
--- /dev/null
+++ b/lib/bundler/source/path.rb
@@ -0,0 +1,249 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Source
+ class Path < Source
+ autoload :Installer, "bundler/source/path/installer"
+
+ attr_reader :path, :options, :root_path, :original_path
+ attr_writer :name
+ attr_accessor :version
+
+ protected :original_path
+
+ DEFAULT_GLOB = "{,*,*/*}.gemspec".freeze
+
+ def initialize(options)
+ @options = options.dup
+ @glob = options["glob"] || DEFAULT_GLOB
+
+ @allow_cached = false
+ @allow_remote = false
+
+ @root_path = options["root_path"] || Bundler.root
+
+ if options["path"]
+ @path = Pathname.new(options["path"])
+ @path = expand(@path) unless @path.relative?
+ end
+
+ @name = options["name"]
+ @version = options["version"]
+
+ # Stores the original path. If at any point we move to the
+ # cached directory, we still have the original path to copy from.
+ @original_path = @path
+ end
+
+ def remote!
+ @local_specs = nil
+ @allow_remote = true
+ end
+
+ def cached!
+ @local_specs = nil
+ @allow_cached = true
+ end
+
+ def self.from_lock(options)
+ new(options.merge("path" => options.delete("remote")))
+ end
+
+ def to_lock
+ out = String.new("PATH\n")
+ out << " remote: #{lockfile_path}\n"
+ out << " glob: #{@glob}\n" unless @glob == DEFAULT_GLOB
+ out << " specs:\n"
+ end
+
+ def to_s
+ "source at `#{@path}`"
+ end
+
+ def hash
+ [self.class, expanded_path, version].hash
+ end
+
+ def eql?(other)
+ return unless other.class == self.class
+ expanded_original_path == other.expanded_original_path &&
+ version == other.version
+ end
+
+ alias_method :==, :eql?
+
+ def name
+ File.basename(expanded_path.to_s)
+ end
+
+ def install(spec, options = {})
+ print_using_message "Using #{version_message(spec)} from #{self}"
+ generate_bin(spec, :disable_extensions => true)
+ nil # no post-install message
+ end
+
+ def cache(spec, custom_path = nil)
+ app_cache_path = app_cache_path(custom_path)
+ return unless Bundler.feature_flag.cache_all?
+ return if expand(@original_path).to_s.index(root_path.to_s + "/") == 0
+
+ unless @original_path.exist?
+ raise GemNotFound, "Can't cache gem #{version_message(spec)} because #{self} is missing!"
+ end
+
+ FileUtils.rm_rf(app_cache_path)
+ FileUtils.cp_r("#{@original_path}/.", app_cache_path)
+ FileUtils.touch(app_cache_path.join(".bundlecache"))
+ end
+
+ def local_specs(*)
+ @local_specs ||= load_spec_files
+ end
+
+ def specs
+ if has_app_cache?
+ @path = app_cache_path
+ @expanded_path = nil # Invalidate
+ end
+ local_specs
+ end
+
+ def app_cache_dirname
+ name
+ end
+
+ def root
+ Bundler.root
+ end
+
+ def expanded_original_path
+ @expanded_original_path ||= expand(original_path)
+ end
+
+ private
+
+ def expanded_path
+ @expanded_path ||= expand(path)
+ end
+
+ def expand(somepath)
+ somepath.expand_path(root_path)
+ rescue ArgumentError => e
+ Bundler.ui.debug(e)
+ raise PathError, "There was an error while trying to use the path " \
+ "`#{somepath}`.\nThe error message was: #{e.message}."
+ end
+
+ def lockfile_path
+ return relative_path(original_path) if original_path.absolute?
+ expand(original_path).relative_path_from(Bundler.root)
+ end
+
+ def app_cache_path(custom_path = nil)
+ @app_cache_path ||= Bundler.app_cache(custom_path).join(app_cache_dirname)
+ end
+
+ def has_app_cache?
+ SharedHelpers.in_bundle? && app_cache_path.exist?
+ end
+
+ def load_gemspec(file)
+ return unless spec = Bundler.load_gemspec(file)
+ Bundler.rubygems.set_installed_by_version(spec)
+ spec
+ end
+
+ def validate_spec(spec)
+ Bundler.rubygems.validate(spec)
+ end
+
+ def load_spec_files
+ index = Index.new
+
+ if File.directory?(expanded_path)
+ # We sort depth-first since `<<` will override the earlier-found specs
+ Dir["#{expanded_path}/#{@glob}"].sort_by {|p| -p.split(File::SEPARATOR).size }.each do |file|
+ next unless spec = load_gemspec(file)
+ spec.source = self
+
+ # Validation causes extension_dir to be calculated, which depends
+ # on #source, so we validate here instead of load_gemspec
+ validate_spec(spec)
+ index << spec
+ end
+
+ if index.empty? && @name && @version
+ index << Gem::Specification.new do |s|
+ s.name = @name
+ s.source = self
+ s.version = Gem::Version.new(@version)
+ s.platform = Gem::Platform::RUBY
+ s.summary = "Fake gemspec for #{@name}"
+ s.relative_loaded_from = "#{@name}.gemspec"
+ s.authors = ["no one"]
+ if expanded_path.join("bin").exist?
+ executables = expanded_path.join("bin").children
+ executables.reject! {|p| File.directory?(p) }
+ s.executables = executables.map {|c| c.basename.to_s }
+ end
+ end
+ end
+ else
+ message = String.new("The path `#{expanded_path}` ")
+ message << if File.exist?(expanded_path)
+ "is not a directory."
+ else
+ "does not exist."
+ end
+ raise PathError, message
+ end
+
+ index
+ end
+
+ def relative_path(path = self.path)
+ if path.to_s.start_with?(root_path.to_s)
+ return path.relative_path_from(root_path)
+ end
+ path
+ end
+
+ def generate_bin(spec, options = {})
+ gem_dir = Pathname.new(spec.full_gem_path)
+
+ # Some gem authors put absolute paths in their gemspec
+ # and we have to save them from themselves
+ spec.files = spec.files.map do |p|
+ next p unless p =~ /\A#{Pathname::SEPARATOR_PAT}/
+ next if File.directory?(p)
+ begin
+ Pathname.new(p).relative_path_from(gem_dir).to_s
+ rescue ArgumentError
+ p
+ end
+ end.compact
+
+ installer = Path::Installer.new(
+ spec,
+ :env_shebang => false,
+ :disable_extensions => options[:disable_extensions],
+ :build_args => options[:build_args],
+ :bundler_extension_cache_path => extension_cache_path(spec)
+ )
+ installer.post_install
+ rescue Gem::InvalidSpecificationException => e
+ Bundler.ui.warn "\n#{spec.name} at #{spec.full_gem_path} did not have a valid gemspec.\n" \
+ "This prevents bundler from installing bins or native extensions, but " \
+ "that may not affect its functionality."
+
+ if !spec.extensions.empty? && !spec.email.empty?
+ Bundler.ui.warn "If you need to use this package without installing it from a gem " \
+ "repository, please contact #{spec.email} and ask them " \
+ "to modify their .gemspec so it can work with `gem build`."
+ end
+
+ Bundler.ui.warn "The validation message from RubyGems was:\n #{e.message}"
+ end
+ end
+ end
+end
diff --git a/lib/bundler/source/path/installer.rb b/lib/bundler/source/path/installer.rb
new file mode 100644
index 00000000000000..a0357ffa391a3b
--- /dev/null
+++ b/lib/bundler/source/path/installer.rb
@@ -0,0 +1,74 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Source
+ class Path
+ class Installer < Bundler::RubyGemsGemInstaller
+ attr_reader :spec
+
+ def initialize(spec, options = {})
+ @options = options
+ @spec = spec
+ @gem_dir = Bundler.rubygems.path(spec.full_gem_path)
+ @wrappers = true
+ @env_shebang = true
+ @format_executable = options[:format_executable] || false
+ @build_args = options[:build_args] || Bundler.rubygems.build_args
+ @gem_bin_dir = "#{Bundler.rubygems.gem_dir}/bin"
+ @disable_extensions = options[:disable_extensions]
+
+ if Bundler.requires_sudo?
+ @tmp_dir = Bundler.tmp(spec.full_name).to_s
+ @bin_dir = "#{@tmp_dir}/bin"
+ else
+ @bin_dir = @gem_bin_dir
+ end
+ end
+
+ def post_install
+ SharedHelpers.chdir(@gem_dir) do
+ run_hooks(:pre_install)
+
+ unless @disable_extensions
+ build_extensions
+ run_hooks(:post_build)
+ end
+
+ generate_bin unless spec.executables.nil? || spec.executables.empty?
+
+ run_hooks(:post_install)
+ end
+ ensure
+ Bundler.rm_rf(@tmp_dir) if Bundler.requires_sudo?
+ end
+
+ private
+
+ def generate_bin
+ super
+
+ if Bundler.requires_sudo?
+ SharedHelpers.filesystem_access(@gem_bin_dir) do |p|
+ Bundler.mkdir_p(p)
+ end
+ spec.executables.each do |exe|
+ Bundler.sudo "cp -R #{@bin_dir}/#{exe} #{@gem_bin_dir}"
+ end
+ end
+ end
+
+ def run_hooks(type)
+ hooks_meth = "#{type}_hooks"
+ return unless Gem.respond_to?(hooks_meth)
+ Gem.send(hooks_meth).each do |hook|
+ result = hook.call(self)
+ next unless result == false
+ location = " at #{$1}" if hook.inspect =~ /@(.*:\d+)/
+ message = "#{type} hook#{location} failed for #{spec.full_name}"
+ raise InstallHookError, message
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/source/rubygems.rb b/lib/bundler/source/rubygems.rb
new file mode 100644
index 00000000000000..485b388a3260e7
--- /dev/null
+++ b/lib/bundler/source/rubygems.rb
@@ -0,0 +1,539 @@
+# frozen_string_literal: true
+
+require "uri"
+require "rubygems/user_interaction"
+
+module Bundler
+ class Source
+ class Rubygems < Source
+ autoload :Remote, "bundler/source/rubygems/remote"
+
+ # Use the API when installing less than X gems
+ API_REQUEST_LIMIT = 500
+ # Ask for X gems per API request
+ API_REQUEST_SIZE = 50
+
+ attr_reader :remotes, :caches
+
+ def initialize(options = {})
+ @options = options
+ @remotes = []
+ @dependency_names = []
+ @allow_remote = false
+ @allow_cached = false
+ @caches = [cache_path, *Bundler.rubygems.gem_cache]
+
+ Array(options["remotes"] || []).reverse_each {|r| add_remote(r) }
+ end
+
+ def remote!
+ @specs = nil
+ @allow_remote = true
+ end
+
+ def cached!
+ @specs = nil
+ @allow_cached = true
+ end
+
+ def hash
+ @remotes.hash
+ end
+
+ def eql?(other)
+ other.is_a?(Rubygems) && other.credless_remotes == credless_remotes
+ end
+
+ alias_method :==, :eql?
+
+ def include?(o)
+ o.is_a?(Rubygems) && (o.credless_remotes - credless_remotes).empty?
+ end
+
+ def can_lock?(spec)
+ return super if Bundler.feature_flag.lockfile_uses_separate_rubygems_sources?
+ spec.source.is_a?(Rubygems)
+ end
+
+ def options
+ { "remotes" => @remotes.map(&:to_s) }
+ end
+
+ def self.from_lock(options)
+ new(options)
+ end
+
+ def to_lock
+ out = String.new("GEM\n")
+ remotes.reverse_each do |remote|
+ out << " remote: #{suppress_configured_credentials remote}\n"
+ end
+ out << " specs:\n"
+ end
+
+ def to_s
+ if remotes.empty?
+ "locally installed gems"
+ else
+ remote_names = remotes.map(&:to_s).join(", ")
+ "rubygems repository #{remote_names} or installed locally"
+ end
+ end
+ alias_method :name, :to_s
+
+ def specs
+ @specs ||= begin
+ # remote_specs usually generates a way larger Index than the other
+ # sources, and large_idx.use small_idx is way faster than
+ # small_idx.use large_idx.
+ idx = @allow_remote ? remote_specs.dup : Index.new
+ idx.use(cached_specs, :override_dupes) if @allow_cached || @allow_remote
+ idx.use(installed_specs, :override_dupes)
+ idx
+ end
+ end
+
+ def install(spec, opts = {})
+ force = opts[:force]
+ ensure_builtin_gems_cached = opts[:ensure_builtin_gems_cached]
+
+ if ensure_builtin_gems_cached && builtin_gem?(spec)
+ if !cached_path(spec)
+ cached_built_in_gem(spec) unless spec.remote
+ force = true
+ else
+ spec.loaded_from = loaded_from(spec)
+ end
+ end
+
+ if installed?(spec) && !force
+ print_using_message "Using #{version_message(spec)}"
+ return nil # no post-install message
+ end
+
+ # Download the gem to get the spec, because some specs that are returned
+ # by rubygems.org are broken and wrong.
+ if spec.remote
+ # Check for this spec from other sources
+ uris = [spec.remote.anonymized_uri]
+ uris += remotes_for_spec(spec).map(&:anonymized_uri)
+ uris.uniq!
+ Installer.ambiguous_gems << [spec.name, *uris] if uris.length > 1
+
+ s = Bundler.rubygems.spec_from_gem(fetch_gem(spec), Bundler.settings["trust-policy"])
+ spec.__swap__(s)
+ end
+
+ unless Bundler.settings[:no_install]
+ message = "Installing #{version_message(spec)}"
+ message += " with native extensions" if spec.extensions.any?
+ Bundler.ui.confirm message
+
+ path = cached_gem(spec)
+ if requires_sudo?
+ install_path = Bundler.tmp(spec.full_name)
+ bin_path = install_path.join("bin")
+ else
+ install_path = rubygems_dir
+ bin_path = Bundler.system_bindir
+ end
+
+ Bundler.mkdir_p bin_path, :no_sudo => true unless spec.executables.empty? || Bundler.rubygems.provides?(">= 2.7.5")
+
+ installed_spec = nil
+ Bundler.rubygems.preserve_paths do
+ installed_spec = Bundler::RubyGemsGemInstaller.at(
+ path,
+ :install_dir => install_path.to_s,
+ :bin_dir => bin_path.to_s,
+ :ignore_dependencies => true,
+ :wrappers => true,
+ :env_shebang => true,
+ :build_args => opts[:build_args],
+ :bundler_expected_checksum => spec.respond_to?(:checksum) && spec.checksum,
+ :bundler_extension_cache_path => extension_cache_path(spec)
+ ).install
+ end
+ spec.full_gem_path = installed_spec.full_gem_path
+
+ # SUDO HAX
+ if requires_sudo?
+ Bundler.rubygems.repository_subdirectories.each do |name|
+ src = File.join(install_path, name, "*")
+ dst = File.join(rubygems_dir, name)
+ if name == "extensions" && Dir.glob(src).any?
+ src = File.join(src, "*/*")
+ ext_src = Dir.glob(src).first
+ ext_src.gsub!(src[0..-6], "")
+ dst = File.dirname(File.join(dst, ext_src))
+ end
+ SharedHelpers.filesystem_access(dst) do |p|
+ Bundler.mkdir_p(p)
+ end
+ Bundler.sudo "cp -R #{src} #{dst}" if Dir[src].any?
+ end
+
+ spec.executables.each do |exe|
+ SharedHelpers.filesystem_access(Bundler.system_bindir) do |p|
+ Bundler.mkdir_p(p)
+ end
+ Bundler.sudo "cp -R #{install_path}/bin/#{exe} #{Bundler.system_bindir}/"
+ end
+ end
+ installed_spec.loaded_from = loaded_from(spec)
+ end
+ spec.loaded_from = loaded_from(spec)
+
+ spec.post_install_message
+ ensure
+ Bundler.rm_rf(install_path) if requires_sudo?
+ end
+
+ def cache(spec, custom_path = nil)
+ if builtin_gem?(spec)
+ cached_path = cached_built_in_gem(spec)
+ else
+ cached_path = cached_gem(spec)
+ end
+ raise GemNotFound, "Missing gem file '#{spec.full_name}.gem'." unless cached_path
+ return if File.dirname(cached_path) == Bundler.app_cache.to_s
+ Bundler.ui.info " * #{File.basename(cached_path)}"
+ FileUtils.cp(cached_path, Bundler.app_cache(custom_path))
+ rescue Errno::EACCES => e
+ Bundler.ui.debug(e)
+ raise InstallError, e.message
+ end
+
+ def cached_built_in_gem(spec)
+ cached_path = cached_path(spec)
+ if cached_path.nil?
+ remote_spec = remote_specs.search(spec).first
+ if remote_spec
+ cached_path = fetch_gem(remote_spec)
+ else
+ Bundler.ui.warn "#{spec.full_name} is built in to Ruby, and can't be cached because your Gemfile doesn't have any sources that contain it."
+ end
+ end
+ cached_path
+ end
+
+ def add_remote(source)
+ uri = normalize_uri(source)
+ @remotes.unshift(uri) unless @remotes.include?(uri)
+ end
+
+ def equivalent_remotes?(other_remotes)
+ other_remotes.map(&method(:remove_auth)) == @remotes.map(&method(:remove_auth))
+ end
+
+ def replace_remotes(other_remotes, allow_equivalent = false)
+ return false if other_remotes == @remotes
+
+ equivalent = allow_equivalent && equivalent_remotes?(other_remotes)
+
+ @remotes = []
+ other_remotes.reverse_each do |r|
+ add_remote r.to_s
+ end
+
+ !equivalent
+ end
+
+ def unmet_deps
+ if @allow_remote && api_fetchers.any?
+ remote_specs.unmet_dependency_names
+ else
+ []
+ end
+ end
+
+ def fetchers
+ @fetchers ||= remotes.map do |uri|
+ remote = Source::Rubygems::Remote.new(uri)
+ Bundler::Fetcher.new(remote)
+ end
+ end
+
+ def double_check_for(unmet_dependency_names)
+ return unless @allow_remote
+ return unless api_fetchers.any?
+
+ unmet_dependency_names = unmet_dependency_names.call
+ unless unmet_dependency_names.nil?
+ if api_fetchers.size <= 1
+ # can't do this when there are multiple fetchers because then we might not fetch from _all_
+ # of them
+ unmet_dependency_names -= remote_specs.spec_names # avoid re-fetching things we've already gotten
+ end
+ return if unmet_dependency_names.empty?
+ end
+
+ Bundler.ui.debug "Double checking for #{unmet_dependency_names || "all specs (due to the size of the request)"} in #{self}"
+
+ fetch_names(api_fetchers, unmet_dependency_names, specs, false)
+ end
+
+ def dependency_names_to_double_check
+ names = []
+ remote_specs.each do |spec|
+ case spec
+ when EndpointSpecification, Gem::Specification, StubSpecification, LazySpecification
+ names.concat(spec.runtime_dependencies)
+ when RemoteSpecification # from the full index
+ return nil
+ else
+ raise "unhandled spec type (#{spec.inspect})"
+ end
+ end
+ names.map!(&:name) if names
+ names
+ end
+
+ protected
+
+ def credless_remotes
+ remotes.map(&method(:suppress_configured_credentials))
+ end
+
+ def remotes_for_spec(spec)
+ specs.search_all(spec.name).inject([]) do |uris, s|
+ uris << s.remote if s.remote
+ uris
+ end
+ end
+
+ def loaded_from(spec)
+ "#{rubygems_dir}/specifications/#{spec.full_name}.gemspec"
+ end
+
+ def cached_gem(spec)
+ cached_gem = cached_path(spec)
+ unless cached_gem
+ raise Bundler::GemNotFound, "Could not find #{spec.file_name} for installation"
+ end
+ cached_gem
+ end
+
+ def cached_path(spec)
+ possibilities = @caches.map {|p| "#{p}/#{spec.file_name}" }
+ possibilities.find {|p| File.exist?(p) }
+ end
+
+ def normalize_uri(uri)
+ uri = uri.to_s
+ uri = "#{uri}/" unless uri =~ %r{/$}
+ uri = URI(uri)
+ raise ArgumentError, "The source must be an absolute URI. For example:\n" \
+ "source 'https://rubygems.org'" if !uri.absolute? || (uri.is_a?(URI::HTTP) && uri.host.nil?)
+ uri
+ end
+
+ def suppress_configured_credentials(remote)
+ remote_nouser = remove_auth(remote)
+ if remote.userinfo && remote.userinfo == Bundler.settings[remote_nouser]
+ remote_nouser
+ else
+ remote
+ end
+ end
+
+ def remove_auth(remote)
+ if remote.user || remote.password
+ remote.dup.tap {|uri| uri.user = uri.password = nil }.to_s
+ else
+ remote.to_s
+ end
+ end
+
+ def installed_specs
+ @installed_specs ||= Index.build do |idx|
+ Bundler.rubygems.all_specs.reverse_each do |spec|
+ next if spec.name == "bundler"
+ spec.source = self
+ if Bundler.rubygems.spec_missing_extensions?(spec, false)
+ Bundler.ui.debug "Source #{self} is ignoring #{spec} because it is missing extensions"
+ next
+ end
+ idx << spec
+ end
+ end
+ end
+
+ def cached_specs
+ @cached_specs ||= begin
+ idx = installed_specs.dup
+
+ Dir["#{cache_path}/*.gem"].each do |gemfile|
+ next if gemfile =~ /^bundler\-[\d\.]+?\.gem/
+ s ||= Bundler.rubygems.spec_from_gem(gemfile)
+ s.source = self
+ if Bundler.rubygems.spec_missing_extensions?(s, false)
+ Bundler.ui.debug "Source #{self} is ignoring #{s} because it is missing extensions"
+ next
+ end
+ idx << s
+ end
+
+ idx
+ end
+ end
+
+ def api_fetchers
+ fetchers.select {|f| f.use_api && f.fetchers.first.api_fetcher? }
+ end
+
+ def remote_specs
+ @remote_specs ||= Index.build do |idx|
+ index_fetchers = fetchers - api_fetchers
+
+ # gather lists from non-api sites
+ fetch_names(index_fetchers, nil, idx, false)
+
+ # because ensuring we have all the gems we need involves downloading
+ # the gemspecs of those gems, if the non-api sites contain more than
+ # about 500 gems, we treat all sites as non-api for speed.
+ allow_api = idx.size < API_REQUEST_LIMIT && dependency_names.size < API_REQUEST_LIMIT
+ Bundler.ui.debug "Need to query more than #{API_REQUEST_LIMIT} gems." \
+ " Downloading full index instead..." unless allow_api
+
+ fetch_names(api_fetchers, allow_api && dependency_names, idx, false)
+ end
+ end
+
+ def fetch_names(fetchers, dependency_names, index, override_dupes)
+ fetchers.each do |f|
+ if dependency_names
+ Bundler.ui.info "Fetching gem metadata from #{f.uri}", Bundler.ui.debug?
+ index.use f.specs_with_retry(dependency_names, self), override_dupes
+ Bundler.ui.info "" unless Bundler.ui.debug? # new line now that the dots are over
+ else
+ Bundler.ui.info "Fetching source index from #{f.uri}"
+ index.use f.specs_with_retry(nil, self), override_dupes
+ end
+ end
+ end
+
+ def fetch_gem(spec)
+ return false unless spec.remote
+
+ spec.fetch_platform
+
+ download_path = requires_sudo? ? Bundler.tmp(spec.full_name) : rubygems_dir
+ gem_path = "#{rubygems_dir}/cache/#{spec.full_name}.gem"
+
+ SharedHelpers.filesystem_access("#{download_path}/cache") do |p|
+ FileUtils.mkdir_p(p)
+ end
+ download_gem(spec, download_path)
+
+ if requires_sudo?
+ SharedHelpers.filesystem_access("#{rubygems_dir}/cache") do |p|
+ Bundler.mkdir_p(p)
+ end
+ Bundler.sudo "mv #{download_path}/cache/#{spec.full_name}.gem #{gem_path}"
+ end
+
+ gem_path
+ ensure
+ Bundler.rm_rf(download_path) if requires_sudo?
+ end
+
+ def builtin_gem?(spec)
+ # Ruby 2.1, where all included gems have this summary
+ return true if spec.summary =~ /is bundled with Ruby/
+
+ # Ruby 2.0, where gemspecs are stored in specifications/default/
+ spec.loaded_from && spec.loaded_from.include?("specifications/default/")
+ end
+
+ def installed?(spec)
+ installed_specs[spec].any?
+ end
+
+ def requires_sudo?
+ Bundler.requires_sudo?
+ end
+
+ def rubygems_dir
+ Bundler.rubygems.gem_dir
+ end
+
+ def cache_path
+ Bundler.app_cache
+ end
+
+ private
+
+ # Checks if the requested spec exists in the global cache. If it does,
+ # we copy it to the download path, and if it does not, we download it.
+ #
+ # @param [Specification] spec
+ # the spec we want to download or retrieve from the cache.
+ #
+ # @param [String] download_path
+ # the local directory the .gem will end up in.
+ #
+ def download_gem(spec, download_path)
+ local_path = File.join(download_path, "cache/#{spec.full_name}.gem")
+
+ if (cache_path = download_cache_path(spec)) && cache_path.file?
+ SharedHelpers.filesystem_access(local_path) do
+ FileUtils.cp(cache_path, local_path)
+ end
+ else
+ uri = spec.remote.uri
+ Bundler.ui.confirm("Fetching #{version_message(spec)}")
+ rubygems_local_path = Bundler.rubygems.download_gem(spec, uri, download_path)
+ if rubygems_local_path != local_path
+ FileUtils.mv(rubygems_local_path, local_path)
+ end
+ cache_globally(spec, local_path)
+ end
+ end
+
+ # Checks if the requested spec exists in the global cache. If it does
+ # not, we create the relevant global cache subdirectory if it does not
+ # exist and copy the spec from the local cache to the global cache.
+ #
+ # @param [Specification] spec
+ # the spec we want to copy to the global cache.
+ #
+ # @param [String] local_cache_path
+ # the local directory from which we want to copy the .gem.
+ #
+ def cache_globally(spec, local_cache_path)
+ return unless cache_path = download_cache_path(spec)
+ return if cache_path.exist?
+
+ SharedHelpers.filesystem_access(cache_path.dirname, &:mkpath)
+ SharedHelpers.filesystem_access(cache_path) do
+ FileUtils.cp(local_cache_path, cache_path)
+ end
+ end
+
+ # Returns the global cache path of the calling Rubygems::Source object.
+ #
+ # Note that the Source determines the path's subdirectory. We use this
+ # subdirectory in the global cache path so that gems with the same name
+ # -- and possibly different versions -- from different sources are saved
+ # to their respective subdirectories and do not override one another.
+ #
+ # @param [Gem::Specification] specification
+ #
+ # @return [Pathname] The global cache path.
+ #
+ def download_cache_path(spec)
+ return unless Bundler.feature_flag.global_gem_cache?
+ return unless remote = spec.remote
+ return unless cache_slug = remote.cache_slug
+
+ Bundler.user_cache.join("gems", cache_slug, spec.file_name)
+ end
+
+ def extension_cache_slug(spec)
+ return unless remote = spec.remote
+ remote.cache_slug
+ end
+ end
+ end
+end
diff --git a/lib/bundler/source/rubygems/remote.rb b/lib/bundler/source/rubygems/remote.rb
new file mode 100644
index 00000000000000..b45f33770aea89
--- /dev/null
+++ b/lib/bundler/source/rubygems/remote.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+module Bundler
+ class Source
+ class Rubygems
+ class Remote
+ attr_reader :uri, :anonymized_uri, :original_uri
+
+ def initialize(uri)
+ orig_uri = uri
+ uri = Bundler.settings.mirror_for(uri)
+ @original_uri = orig_uri if orig_uri != uri
+ fallback_auth = Bundler.settings.credentials_for(uri)
+
+ @uri = apply_auth(uri, fallback_auth).freeze
+ @anonymized_uri = remove_auth(@uri).freeze
+ end
+
+ # @return [String] A slug suitable for use as a cache key for this
+ # remote.
+ #
+ def cache_slug
+ @cache_slug ||= begin
+ return nil unless SharedHelpers.md5_available?
+
+ cache_uri = original_uri || uri
+
+ # URI::File of Ruby 2.6 returns empty string when given "file://".
+ host = defined?(URI::File) && cache_uri.is_a?(URI::File) ? nil : cache_uri.host
+
+ uri_parts = [host, cache_uri.user, cache_uri.port, cache_uri.path]
+ uri_digest = SharedHelpers.digest(:MD5).hexdigest(uri_parts.compact.join("."))
+
+ uri_parts[-1] = uri_digest
+ uri_parts.compact.join(".")
+ end
+ end
+
+ def to_s
+ "rubygems remote at #{anonymized_uri}"
+ end
+
+ private
+
+ def apply_auth(uri, auth)
+ if auth && uri.userinfo.nil?
+ uri = uri.dup
+ uri.userinfo = auth
+ end
+
+ uri
+ rescue URI::InvalidComponentError
+ error_message = "Please CGI escape your usernames and passwords before " \
+ "setting them for authentication."
+ raise HTTPError.new(error_message)
+ end
+
+ def remove_auth(uri)
+ if uri.userinfo
+ uri = uri.dup
+ uri.user = uri.password = nil
+ end
+
+ uri
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/source_list.rb b/lib/bundler/source_list.rb
new file mode 100644
index 00000000000000..ac2adacb3d45e9
--- /dev/null
+++ b/lib/bundler/source_list.rb
@@ -0,0 +1,186 @@
+# frozen_string_literal: true
+
+module Bundler
+ class SourceList
+ attr_reader :path_sources,
+ :git_sources,
+ :plugin_sources,
+ :global_rubygems_source,
+ :metadata_source
+
+ def initialize
+ @path_sources = []
+ @git_sources = []
+ @plugin_sources = []
+ @global_rubygems_source = nil
+ @rubygems_aggregate = rubygems_aggregate_class.new
+ @rubygems_sources = []
+ @metadata_source = Source::Metadata.new
+ end
+
+ def add_path_source(options = {})
+ if options["gemspec"]
+ add_source_to_list Source::Gemspec.new(options), path_sources
+ else
+ add_source_to_list Source::Path.new(options), path_sources
+ end
+ end
+
+ def add_git_source(options = {})
+ add_source_to_list(Source::Git.new(options), git_sources).tap do |source|
+ warn_on_git_protocol(source)
+ end
+ end
+
+ def add_rubygems_source(options = {})
+ add_source_to_list Source::Rubygems.new(options), @rubygems_sources
+ end
+
+ def add_plugin_source(source, options = {})
+ add_source_to_list Plugin.source(source).new(options), @plugin_sources
+ end
+
+ def global_rubygems_source=(uri)
+ if Bundler.feature_flag.lockfile_uses_separate_rubygems_sources?
+ @global_rubygems_source ||= rubygems_aggregate_class.new("remotes" => uri)
+ end
+ add_rubygems_remote(uri)
+ end
+
+ def add_rubygems_remote(uri)
+ if Bundler.feature_flag.lockfile_uses_separate_rubygems_sources?
+ return if Bundler.feature_flag.disable_multisource?
+ raise InvalidOption, "`lockfile_uses_separate_rubygems_sources` cannot be set without `disable_multisource` being set"
+ end
+ @rubygems_aggregate.add_remote(uri)
+ @rubygems_aggregate
+ end
+
+ def default_source
+ global_rubygems_source || @rubygems_aggregate
+ end
+
+ def rubygems_sources
+ @rubygems_sources + [default_source]
+ end
+
+ def rubygems_remotes
+ rubygems_sources.map(&:remotes).flatten.uniq
+ end
+
+ def all_sources
+ path_sources + git_sources + plugin_sources + rubygems_sources + [metadata_source]
+ end
+
+ def get(source)
+ source_list_for(source).find {|s| equal_source?(source, s) || equivalent_source?(source, s) }
+ end
+
+ def lock_sources
+ if Bundler.feature_flag.lockfile_uses_separate_rubygems_sources?
+ [[default_source], @rubygems_sources, git_sources, path_sources, plugin_sources].map do |sources|
+ sources.sort_by(&:to_s)
+ end.flatten(1)
+ else
+ lock_sources = (path_sources + git_sources + plugin_sources).sort_by(&:to_s)
+ lock_sources << combine_rubygems_sources
+ end
+ end
+
+ # Returns true if there are changes
+ def replace_sources!(replacement_sources)
+ return true if replacement_sources.empty?
+
+ [path_sources, git_sources, plugin_sources].each do |source_list|
+ source_list.map! do |source|
+ replacement_sources.find {|s| s == source } || source
+ end
+ end
+
+ replacement_rubygems = !Bundler.feature_flag.lockfile_uses_separate_rubygems_sources? &&
+ replacement_sources.detect {|s| s.is_a?(Source::Rubygems) }
+ @rubygems_aggregate = replacement_rubygems if replacement_rubygems
+
+ return true if !equal_sources?(lock_sources, replacement_sources) && !equivalent_sources?(lock_sources, replacement_sources)
+ return true if replacement_rubygems && rubygems_remotes.to_set != replacement_rubygems.remotes.to_set
+
+ false
+ end
+
+ def cached!
+ all_sources.each(&:cached!)
+ end
+
+ def remote!
+ all_sources.each(&:remote!)
+ end
+
+ def rubygems_primary_remotes
+ @rubygems_aggregate.remotes
+ end
+
+ private
+
+ def rubygems_aggregate_class
+ Source::Rubygems
+ end
+
+ def add_source_to_list(source, list)
+ list.unshift(source).uniq!
+ source
+ end
+
+ def source_list_for(source)
+ case source
+ when Source::Git then git_sources
+ when Source::Path then path_sources
+ when Source::Rubygems then rubygems_sources
+ when Plugin::API::Source then plugin_sources
+ else raise ArgumentError, "Invalid source: #{source.inspect}"
+ end
+ end
+
+ def combine_rubygems_sources
+ Source::Rubygems.new("remotes" => rubygems_remotes)
+ end
+
+ def warn_on_git_protocol(source)
+ return if Bundler.settings["git.allow_insecure"]
+
+ if source.uri =~ /^git\:/
+ Bundler.ui.warn "The git source `#{source.uri}` uses the `git` protocol, " \
+ "which transmits data without encryption. Disable this warning with " \
+ "`bundle config git.allow_insecure true`, or switch to the `https` " \
+ "protocol to keep your data secure."
+ end
+ end
+
+ def equal_sources?(lock_sources, replacement_sources)
+ lock_sources.to_set == replacement_sources.to_set
+ end
+
+ def equal_source?(source, other_source)
+ source == other_source
+ end
+
+ def equivalent_source?(source, other_source)
+ return false unless Bundler.settings[:allow_deployment_source_credential_changes] && source.is_a?(Source::Rubygems)
+
+ equivalent_rubygems_sources?([source], [other_source])
+ end
+
+ def equivalent_sources?(lock_sources, replacement_sources)
+ return false unless Bundler.settings[:allow_deployment_source_credential_changes]
+
+ lock_rubygems_sources, lock_other_sources = lock_sources.partition {|s| s.is_a?(Source::Rubygems) }
+ replacement_rubygems_sources, replacement_other_sources = replacement_sources.partition {|s| s.is_a?(Source::Rubygems) }
+
+ equivalent_rubygems_sources?(lock_rubygems_sources, replacement_rubygems_sources) && equal_sources?(lock_other_sources, replacement_other_sources)
+ end
+
+ def equivalent_rubygems_sources?(lock_sources, replacement_sources)
+ actual_remotes = replacement_sources.map(&:remotes).flatten.uniq
+ lock_sources.all? {|s| s.equivalent_remotes?(actual_remotes) }
+ end
+ end
+end
diff --git a/lib/bundler/spec_set.rb b/lib/bundler/spec_set.rb
new file mode 100644
index 00000000000000..5003b2cbecaf6e
--- /dev/null
+++ b/lib/bundler/spec_set.rb
@@ -0,0 +1,192 @@
+# frozen_string_literal: true
+
+require "tsort"
+require "forwardable"
+require "set"
+
+module Bundler
+ class SpecSet
+ extend Forwardable
+ include TSort, Enumerable
+
+ def_delegators :@specs, :<<, :length, :add, :remove, :size, :empty?
+ def_delegators :sorted, :each
+
+ def initialize(specs)
+ @specs = specs
+ end
+
+ def for(dependencies, skip = [], check = false, match_current_platform = false, raise_on_missing = true)
+ handled = Set.new
+ deps = dependencies.dup
+ specs = []
+ skip += ["bundler"]
+
+ loop do
+ break unless dep = deps.shift
+ next if !handled.add?(dep) || skip.include?(dep.name)
+
+ if spec = spec_for_dependency(dep, match_current_platform)
+ specs << spec
+
+ spec.dependencies.each do |d|
+ next if d.type == :development
+ d = DepProxy.new(d, dep.__platform) unless match_current_platform
+ deps << d
+ end
+ elsif check
+ return false
+ elsif raise_on_missing
+ others = lookup[dep.name] if match_current_platform
+ message = "Unable to find a spec satisfying #{dep} in the set. Perhaps the lockfile is corrupted?"
+ message += " Found #{others.join(", ")} that did not match the current platform." if others && !others.empty?
+ raise GemNotFound, message
+ end
+ end
+
+ if spec = lookup["bundler"].first
+ specs << spec
+ end
+
+ check ? true : SpecSet.new(specs)
+ end
+
+ def valid_for?(deps)
+ self.for(deps, [], true)
+ end
+
+ def [](key)
+ key = key.name if key.respond_to?(:name)
+ lookup[key].reverse
+ end
+
+ def []=(key, value)
+ @specs << value
+ @lookup = nil
+ @sorted = nil
+ value
+ end
+
+ def sort!
+ self
+ end
+
+ def to_a
+ sorted.dup
+ end
+
+ def to_hash
+ lookup.dup
+ end
+
+ def materialize(deps, missing_specs = nil)
+ materialized = self.for(deps, [], false, true, !missing_specs).to_a
+ deps = materialized.map(&:name).uniq
+ materialized.map! do |s|
+ next s unless s.is_a?(LazySpecification)
+ s.source.dependency_names = deps if s.source.respond_to?(:dependency_names=)
+ spec = s.__materialize__
+ unless spec
+ unless missing_specs
+ raise GemNotFound, "Could not find #{s.full_name} in any of the sources"
+ end
+ missing_specs << s
+ end
+ spec
+ end
+ SpecSet.new(missing_specs ? materialized.compact : materialized)
+ end
+
+ # Materialize for all the specs in the spec set, regardless of what platform they're for
+ # This is in contrast to how for does platform filtering (and specifically different from how `materialize` calls `for` only for the current platform)
+ # @return [Array]
+ def materialized_for_all_platforms
+ names = @specs.map(&:name).uniq
+ @specs.map do |s|
+ next s unless s.is_a?(LazySpecification)
+ s.source.dependency_names = names if s.source.respond_to?(:dependency_names=)
+ spec = s.__materialize__
+ raise GemNotFound, "Could not find #{s.full_name} in any of the sources" unless spec
+ spec
+ end
+ end
+
+ def merge(set)
+ arr = sorted.dup
+ set.each do |set_spec|
+ full_name = set_spec.full_name
+ next if arr.any? {|spec| spec.full_name == full_name }
+ arr << set_spec
+ end
+ SpecSet.new(arr)
+ end
+
+ def find_by_name_and_platform(name, platform)
+ @specs.detect {|spec| spec.name == name && spec.match_platform(platform) }
+ end
+
+ def what_required(spec)
+ unless req = find {|s| s.dependencies.any? {|d| d.type == :runtime && d.name == spec.name } }
+ return [spec]
+ end
+ what_required(req) << spec
+ end
+
+ private
+
+ def sorted
+ rake = @specs.find {|s| s.name == "rake" }
+ begin
+ @sorted ||= ([rake] + tsort).compact.uniq
+ rescue TSort::Cyclic => error
+ cgems = extract_circular_gems(error)
+ raise CyclicDependencyError, "Your bundle requires gems that depend" \
+ " on each other, creating an infinite loop. Please remove either" \
+ " gem '#{cgems[1]}' or gem '#{cgems[0]}' and try again."
+ end
+ end
+
+ def extract_circular_gems(error)
+ if Bundler.current_ruby.mri? && Bundler.current_ruby.on_19?
+ error.message.scan(/(\w+) \([^)]/).flatten
+ else
+ error.message.scan(/@name="(.*?)"/).flatten
+ end
+ end
+
+ def lookup
+ @lookup ||= begin
+ lookup = Hash.new {|h, k| h[k] = [] }
+ Index.sort_specs(@specs).reverse_each do |s|
+ lookup[s.name] << s
+ end
+ lookup
+ end
+ end
+
+ def tsort_each_node
+ # MUST sort by name for backwards compatibility
+ @specs.sort_by(&:name).each {|s| yield s }
+ end
+
+ def spec_for_dependency(dep, match_current_platform)
+ specs_for_platforms = lookup[dep.name]
+ if match_current_platform
+ Bundler.rubygems.platforms.reverse_each do |pl|
+ match = GemHelpers.select_best_platform_match(specs_for_platforms, pl)
+ return match if match
+ end
+ nil
+ else
+ GemHelpers.select_best_platform_match(specs_for_platforms, dep.__platform)
+ end
+ end
+
+ def tsort_each_child(s)
+ s.dependencies.sort_by(&:name).each do |d|
+ next if d.type == :development
+ lookup[d.name].each {|s2| yield s2 }
+ end
+ end
+ end
+end
diff --git a/lib/bundler/ssl_certs/.document b/lib/bundler/ssl_certs/.document
new file mode 100644
index 00000000000000..fb66f13c330f04
--- /dev/null
+++ b/lib/bundler/ssl_certs/.document
@@ -0,0 +1 @@
+# Ignore all files in this directory
diff --git a/lib/bundler/ssl_certs/certificate_manager.rb b/lib/bundler/ssl_certs/certificate_manager.rb
new file mode 100644
index 00000000000000..26fc38ec185bde
--- /dev/null
+++ b/lib/bundler/ssl_certs/certificate_manager.rb
@@ -0,0 +1,66 @@
+# frozen_string_literal: true
+
+require "bundler/vendored_fileutils"
+require "net/https"
+require "openssl"
+
+module Bundler
+ module SSLCerts
+ class CertificateManager
+ attr_reader :bundler_cert_path, :bundler_certs, :rubygems_certs
+
+ def self.update_from!(rubygems_path)
+ new(rubygems_path).update!
+ end
+
+ def initialize(rubygems_path = nil)
+ if rubygems_path
+ rubygems_cert_path = File.join(rubygems_path, "lib/rubygems/ssl_certs")
+ @rubygems_certs = certificates_in(rubygems_cert_path)
+ end
+
+ @bundler_cert_path = File.expand_path("..", __FILE__)
+ @bundler_certs = certificates_in(bundler_cert_path)
+ end
+
+ def up_to_date?
+ rubygems_certs.all? do |rc|
+ bundler_certs.find do |bc|
+ File.basename(bc) == File.basename(rc) && FileUtils.compare_file(bc, rc)
+ end
+ end
+ end
+
+ def update!
+ return if up_to_date?
+
+ FileUtils.rm bundler_certs
+ FileUtils.cp rubygems_certs, bundler_cert_path
+ end
+
+ def connect_to(host)
+ http = Net::HTTP.new(host, 443)
+ http.use_ssl = true
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
+ http.cert_store = store
+ http.head("/")
+ end
+
+ private
+
+ def certificates_in(path)
+ Dir[File.join(path, "**/*.pem")].sort
+ end
+
+ def store
+ @store ||= begin
+ store = OpenSSL::X509::Store.new
+ bundler_certs.each do |cert|
+ store.add_file cert
+ end
+ store
+ end
+ end
+ end
+ end
+end
diff --git a/lib/bundler/ssl_certs/index.rubygems.org/GlobalSignRootCA.pem b/lib/bundler/ssl_certs/index.rubygems.org/GlobalSignRootCA.pem
new file mode 100644
index 00000000000000..f4ce4ca43dc0a0
--- /dev/null
+++ b/lib/bundler/ssl_certs/index.rubygems.org/GlobalSignRootCA.pem
@@ -0,0 +1,21 @@
+-----BEGIN CERTIFICATE-----
+MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG
+A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv
+b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw
+MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i
+YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT
+aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ
+jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp
+xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp
+1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG
+snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ
+U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8
+9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E
+BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B
+AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz
+yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE
+38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP
+AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad
+DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME
+HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==
+-----END CERTIFICATE-----
diff --git a/lib/bundler/ssl_certs/rubygems.global.ssl.fastly.net/DigiCertHighAssuranceEVRootCA.pem b/lib/bundler/ssl_certs/rubygems.global.ssl.fastly.net/DigiCertHighAssuranceEVRootCA.pem
new file mode 100644
index 00000000000000..9e6810ab70cfa1
--- /dev/null
+++ b/lib/bundler/ssl_certs/rubygems.global.ssl.fastly.net/DigiCertHighAssuranceEVRootCA.pem
@@ -0,0 +1,23 @@
+-----BEGIN CERTIFICATE-----
+MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs
+MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
+d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j
+ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFoXDTMxMTExMDAwMDAwMFowbDEL
+MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3
+LmRpZ2ljZXJ0LmNvbTErMCkGA1UEAxMiRGlnaUNlcnQgSGlnaCBBc3N1cmFuY2Ug
+RVYgUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMbM5XPm
++9S75S0tMqbf5YE/yc0lSbZxKsPVlDRnogocsF9ppkCxxLeyj9CYpKlBWTrT3JTW
+PNt0OKRKzE0lgvdKpVMSOO7zSW1xkX5jtqumX8OkhPhPYlG++MXs2ziS4wblCJEM
+xChBVfvLWokVfnHoNb9Ncgk9vjo4UFt3MRuNs8ckRZqnrG0AFFoEt7oT61EKmEFB
+Ik5lYYeBQVCmeVyJ3hlKV9Uu5l0cUyx+mM0aBhakaHPQNAQTXKFx01p8VdteZOE3
+hzBWBOURtCmAEvF5OYiiAhF8J2a3iLd48soKqDirCmTCv2ZdlYTBoSUeh10aUAsg
+EsxBu24LUTi4S8sCAwEAAaNjMGEwDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQF
+MAMBAf8wHQYDVR0OBBYEFLE+w2kD+L9HAdSYJhoIAu9jZCvDMB8GA1UdIwQYMBaA
+FLE+w2kD+L9HAdSYJhoIAu9jZCvDMA0GCSqGSIb3DQEBBQUAA4IBAQAcGgaX3Nec
+nzyIZgYIVyHbIUf4KmeqvxgydkAQV8GK83rZEWWONfqe/EW1ntlMMUu4kehDLI6z
+eM7b41N5cdblIZQB2lWHmiRk9opmzN6cN82oNLFpmyPInngiK3BD41VHMWEZ71jF
+hS9OMPagMRYjyOfiZRYzy78aG6A9+MpeizGLYAiJLQwGXFK3xPkKmNEVX58Svnw2
+Yzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe
+vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep
++OkuE6N36B9K
+-----END CERTIFICATE-----
diff --git a/lib/bundler/ssl_certs/rubygems.org/AddTrustExternalCARoot.pem b/lib/bundler/ssl_certs/rubygems.org/AddTrustExternalCARoot.pem
new file mode 100644
index 00000000000000..20585f1c01e187
--- /dev/null
+++ b/lib/bundler/ssl_certs/rubygems.org/AddTrustExternalCARoot.pem
@@ -0,0 +1,25 @@
+-----BEGIN CERTIFICATE-----
+MIIENjCCAx6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBvMQswCQYDVQQGEwJTRTEU
+MBIGA1UEChMLQWRkVHJ1c3QgQUIxJjAkBgNVBAsTHUFkZFRydXN0IEV4dGVybmFs
+IFRUUCBOZXR3b3JrMSIwIAYDVQQDExlBZGRUcnVzdCBFeHRlcm5hbCBDQSBSb290
+MB4XDTAwMDUzMDEwNDgzOFoXDTIwMDUzMDEwNDgzOFowbzELMAkGA1UEBhMCU0Ux
+FDASBgNVBAoTC0FkZFRydXN0IEFCMSYwJAYDVQQLEx1BZGRUcnVzdCBFeHRlcm5h
+bCBUVFAgTmV0d29yazEiMCAGA1UEAxMZQWRkVHJ1c3QgRXh0ZXJuYWwgQ0EgUm9v
+dDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALf3GjPm8gAELTngTlvt
+H7xsD821+iO2zt6bETOXpClMfZOfvUq8k+0DGuOPz+VtUFrWlymUWoCwSXrbLpX9
+uMq/NzgtHj6RQa1wVsfwTz/oMp50ysiQVOnGXw94nZpAPA6sYapeFI+eh6FqUNzX
+mk6vBbOmcZSccbNQYArHE504B4YCqOmoaSYYkKtMsE8jqzpPhNjfzp/haW+710LX
+a0Tkx63ubUFfclpxCDezeWWkWaCUN/cALw3CknLa0Dhy2xSoRcRdKn23tNbE7qzN
+E0S3ySvdQwAl+mG5aWpYIxG3pzOPVnVZ9c0p10a3CitlttNCbxWyuHv77+ldU9U0
+WicCAwEAAaOB3DCB2TAdBgNVHQ4EFgQUrb2YejS0Jvf6xCZU7wO94CTLVBowCwYD
+VR0PBAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wgZkGA1UdIwSBkTCBjoAUrb2YejS0
+Jvf6xCZU7wO94CTLVBqhc6RxMG8xCzAJBgNVBAYTAlNFMRQwEgYDVQQKEwtBZGRU
+cnVzdCBBQjEmMCQGA1UECxMdQWRkVHJ1c3QgRXh0ZXJuYWwgVFRQIE5ldHdvcmsx
+IjAgBgNVBAMTGUFkZFRydXN0IEV4dGVybmFsIENBIFJvb3SCAQEwDQYJKoZIhvcN
+AQEFBQADggEBALCb4IUlwtYj4g+WBpKdQZic2YR5gdkeWxQHIzZlj7DYd7usQWxH
+YINRsPkyPef89iYTx4AWpb9a/IfPeHmJIZriTAcKhjW88t5RxNKWt9x+Tu5w/Rw5
+6wwCURQtjr0W4MHfRnXnJK3s9EK0hZNwEGe6nQY1ShjTK3rMUUKhemPR5ruhxSvC
+Nr4TDea9Y355e6cJDUCrat2PisP29owaQgVR1EX1n6diIWgVIEM8med8vSTYqZEX
+c4g/VhsxOBi0cQ+azcgOno4uG+GMmIPLHzHxREzGBHNJdmAPx/i9F4BrLunMTA5a
+mnkPIAou1Z5jJh5VkpTYghdae9C8x49OhgQ=
+-----END CERTIFICATE-----
diff --git a/lib/bundler/stub_specification.rb b/lib/bundler/stub_specification.rb
new file mode 100644
index 00000000000000..0dd024024a01ac
--- /dev/null
+++ b/lib/bundler/stub_specification.rb
@@ -0,0 +1,108 @@
+# frozen_string_literal: true
+
+require "bundler/remote_specification"
+
+module Bundler
+ class StubSpecification < RemoteSpecification
+ def self.from_stub(stub)
+ return stub if stub.is_a?(Bundler::StubSpecification)
+ spec = new(stub.name, stub.version, stub.platform, nil)
+ spec.stub = stub
+ spec
+ end
+
+ attr_accessor :stub, :ignored
+
+ # Pre 2.2.0 did not include extension_dir
+ # https://github.com/rubygems/rubygems/commit/9485ca2d101b82a946d6f327f4bdcdea6d4946ea
+ if Bundler.rubygems.provides?(">= 2.2.0")
+ def source=(source)
+ super
+ # Stub has no concept of source, which means that extension_dir may be wrong
+ # This is the case for git-based gems. So, instead manually assign the extension dir
+ return unless source.respond_to?(:extension_dir_name)
+ path = File.join(stub.extensions_dir, source.extension_dir_name)
+ stub.extension_dir = File.expand_path(path)
+ end
+ end
+
+ def to_yaml
+ _remote_specification.to_yaml
+ end
+
+ # @!group Stub Delegates
+
+ if Bundler.rubygems.provides?(">= 2.3")
+ # This is defined directly to avoid having to load every installed spec
+ def missing_extensions?
+ stub.missing_extensions?
+ end
+ end
+
+ def activated
+ stub.activated
+ end
+
+ def activated=(activated)
+ stub.instance_variable_set(:@activated, activated)
+ end
+
+ def default_gem
+ stub.default_gem
+ end
+
+ def full_gem_path
+ # deleted gems can have their stubs return nil, so in that case grab the
+ # expired path from the full spec
+ stub.full_gem_path || method_missing(:full_gem_path)
+ end
+
+ if Bundler.rubygems.provides?(">= 2.2.0")
+ def full_require_paths
+ stub.full_require_paths
+ end
+
+ # This is what we do in bundler/rubygems_ext
+ # full_require_paths is always implemented in >= 2.2.0
+ def load_paths
+ full_require_paths
+ end
+ end
+
+ def loaded_from
+ stub.loaded_from
+ end
+
+ if Bundler.rubygems.stubs_provide_full_functionality?
+ def matches_for_glob(glob)
+ stub.matches_for_glob(glob)
+ end
+ end
+
+ def raw_require_paths
+ stub.raw_require_paths
+ end
+
+ private
+
+ def _remote_specification
+ @_remote_specification ||= begin
+ rs = stub.to_spec
+ if rs.equal?(self) # happens when to_spec gets the spec from Gem.loaded_specs
+ rs = Gem::Specification.load(loaded_from)
+ Bundler.rubygems.stub_set_spec(stub, rs)
+ end
+
+ unless rs
+ raise GemspecError, "The gemspec for #{full_name} at #{loaded_from}" \
+ " was missing or broken. Try running `gem pristine #{name} -v #{version}`" \
+ " to fix the cached spec."
+ end
+
+ rs.source = source
+
+ rs
+ end
+ end
+ end
+end
diff --git a/lib/bundler/templates/.document b/lib/bundler/templates/.document
new file mode 100644
index 00000000000000..fb66f13c330f04
--- /dev/null
+++ b/lib/bundler/templates/.document
@@ -0,0 +1 @@
+# Ignore all files in this directory
diff --git a/lib/bundler/templates/Executable b/lib/bundler/templates/Executable
new file mode 100644
index 00000000000000..3e8d5b317ad8b1
--- /dev/null
+++ b/lib/bundler/templates/Executable
@@ -0,0 +1,29 @@
+#!/usr/bin/env <%= Bundler.settings[:shebang] || RbConfig::CONFIG["ruby_install_name"] %>
+# frozen_string_literal: true
+
+#
+# This file was generated by Bundler.
+#
+# The application '<%= executable %>' is installed as part of a gem, and
+# this file is here to facilitate running it.
+#
+
+require "pathname"
+ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../<%= relative_gemfile_path %>",
+ Pathname.new(__FILE__).realpath)
+
+bundle_binstub = File.expand_path("../bundle", __FILE__)
+
+if File.file?(bundle_binstub)
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
+ load(bundle_binstub)
+ else
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
+Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
+ end
+end
+
+require "rubygems"
+require "bundler/setup"
+
+load Gem.bin_path("<%= spec.name %>", "<%= executable %>")
diff --git a/lib/bundler/templates/Executable.bundler b/lib/bundler/templates/Executable.bundler
new file mode 100644
index 00000000000000..eeda90b584b647
--- /dev/null
+++ b/lib/bundler/templates/Executable.bundler
@@ -0,0 +1,105 @@
+#!/usr/bin/env <%= Bundler.settings[:shebang] || RbConfig::CONFIG["ruby_install_name"] %>
+# frozen_string_literal: true
+
+#
+# This file was generated by Bundler.
+#
+# The application '<%= executable %>' is installed as part of a gem, and
+# this file is here to facilitate running it.
+#
+
+require "rubygems"
+
+m = Module.new do
+ module_function
+
+ def invoked_as_script?
+ File.expand_path($0) == File.expand_path(__FILE__)
+ end
+
+ def env_var_version
+ ENV["BUNDLER_VERSION"]
+ end
+
+ def cli_arg_version
+ return unless invoked_as_script? # don't want to hijack other binstubs
+ return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
+ bundler_version = nil
+ update_index = nil
+ ARGV.each_with_index do |a, i|
+ if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
+ bundler_version = a
+ end
+ next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
+ bundler_version = $1 || ">= 0.a"
+ update_index = i
+ end
+ bundler_version
+ end
+
+ def gemfile
+ gemfile = ENV["BUNDLE_GEMFILE"]
+ return gemfile if gemfile && !gemfile.empty?
+
+ File.expand_path("../<%= relative_gemfile_path %>", __FILE__)
+ end
+
+ def lockfile
+ lockfile =
+ case File.basename(gemfile)
+ when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
+ else "#{gemfile}.lock"
+ end
+ File.expand_path(lockfile)
+ end
+
+ def lockfile_version
+ return unless File.file?(lockfile)
+ lockfile_contents = File.read(lockfile)
+ return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
+ Regexp.last_match(1)
+ end
+
+ def bundler_version
+ @bundler_version ||= begin
+ env_var_version || cli_arg_version ||
+ lockfile_version || "#{Gem::Requirement.default}.a"
+ end
+ end
+
+ def load_bundler!
+ ENV["BUNDLE_GEMFILE"] ||= gemfile
+
+ # must dup string for RG < 1.8 compatibility
+ activate_bundler(bundler_version.dup)
+ end
+
+ def activate_bundler(bundler_version)
+ if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0")
+ bundler_version = "< 2"
+ end
+ gem_error = activation_error_handling do
+ gem "bundler", bundler_version
+ end
+ return if gem_error.nil?
+ require_error = activation_error_handling do
+ require "bundler/version"
+ end
+ return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION))
+ warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`"
+ exit 42
+ end
+
+ def activation_error_handling
+ yield
+ nil
+ rescue StandardError, LoadError => e
+ e
+ end
+end
+
+m.load_bundler!
+
+if m.invoked_as_script?
+ load Gem.bin_path("<%= spec.name %>", "<%= executable %>")
+end
diff --git a/lib/bundler/templates/Executable.standalone b/lib/bundler/templates/Executable.standalone
new file mode 100644
index 00000000000000..4bf0753f44d53e
--- /dev/null
+++ b/lib/bundler/templates/Executable.standalone
@@ -0,0 +1,14 @@
+#!/usr/bin/env <%= Bundler.settings[:shebang] || RbConfig::CONFIG["ruby_install_name"] %>
+#
+# This file was generated by Bundler.
+#
+# The application '<%= executable %>' is installed as part of a gem, and
+# this file is here to facilitate running it.
+#
+
+require "pathname"
+path = Pathname.new(__FILE__)
+$:.unshift File.expand_path "../<%= standalone_path %>", path.realpath
+
+require "bundler/setup"
+load File.expand_path "../<%= executable_path %>", path.realpath
diff --git a/lib/bundler/templates/Gemfile b/lib/bundler/templates/Gemfile
new file mode 100644
index 00000000000000..1afd2cce673bb2
--- /dev/null
+++ b/lib/bundler/templates/Gemfile
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+source "https://rubygems.org"
+
+git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
+
+# gem "rails"
diff --git a/lib/bundler/templates/gems.rb b/lib/bundler/templates/gems.rb
new file mode 100644
index 00000000000000..547cd6e8d9bb76
--- /dev/null
+++ b/lib/bundler/templates/gems.rb
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+# A sample gems.rb
+source "https://rubygems.org"
+
+git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
+
+# gem "rails"
diff --git a/lib/bundler/templates/newgem/CODE_OF_CONDUCT.md.tt b/lib/bundler/templates/newgem/CODE_OF_CONDUCT.md.tt
new file mode 100644
index 00000000000000..a3833d29d780ff
--- /dev/null
+++ b/lib/bundler/templates/newgem/CODE_OF_CONDUCT.md.tt
@@ -0,0 +1,74 @@
+# Contributor Covenant Code of Conduct
+
+## Our Pledge
+
+In the interest of fostering an open and welcoming environment, we as
+contributors and maintainers pledge to making participation in our project and
+our community a harassment-free experience for everyone, regardless of age, body
+size, disability, ethnicity, gender identity and expression, level of experience,
+nationality, personal appearance, race, religion, or sexual identity and
+orientation.
+
+## Our Standards
+
+Examples of behavior that contributes to creating a positive environment
+include:
+
+* Using welcoming and inclusive language
+* Being respectful of differing viewpoints and experiences
+* Gracefully accepting constructive criticism
+* Focusing on what is best for the community
+* Showing empathy towards other community members
+
+Examples of unacceptable behavior by participants include:
+
+* The use of sexualized language or imagery and unwelcome sexual attention or
+advances
+* Trolling, insulting/derogatory comments, and personal or political attacks
+* Public or private harassment
+* Publishing others' private information, such as a physical or electronic
+ address, without explicit permission
+* Other conduct which could reasonably be considered inappropriate in a
+ professional setting
+
+## Our Responsibilities
+
+Project maintainers are responsible for clarifying the standards of acceptable
+behavior and are expected to take appropriate and fair corrective action in
+response to any instances of unacceptable behavior.
+
+Project maintainers have the right and responsibility to remove, edit, or
+reject comments, commits, code, wiki edits, issues, and other contributions
+that are not aligned to this Code of Conduct, or to ban temporarily or
+permanently any contributor for other behaviors that they deem inappropriate,
+threatening, offensive, or harmful.
+
+## Scope
+
+This Code of Conduct applies both within project spaces and in public spaces
+when an individual is representing the project or its community. Examples of
+representing a project or community include using an official project e-mail
+address, posting via an official social media account, or acting as an appointed
+representative at an online or offline event. Representation of a project may be
+further defined and clarified by project maintainers.
+
+## Enforcement
+
+Instances of abusive, harassing, or otherwise unacceptable behavior may be
+reported by contacting the project team at <%= config[:email] %>. All
+complaints will be reviewed and investigated and will result in a response that
+is deemed necessary and appropriate to the circumstances. The project team is
+obligated to maintain confidentiality with regard to the reporter of an incident.
+Further details of specific enforcement policies may be posted separately.
+
+Project maintainers who do not follow or enforce the Code of Conduct in good
+faith may face temporary or permanent repercussions as determined by other
+members of the project's leadership.
+
+## Attribution
+
+This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
+available at [http://contributor-covenant.org/version/1/4][version]
+
+[homepage]: http://contributor-covenant.org
+[version]: http://contributor-covenant.org/version/1/4/
diff --git a/lib/bundler/templates/newgem/Gemfile.tt b/lib/bundler/templates/newgem/Gemfile.tt
new file mode 100644
index 00000000000000..4cd2e40f4f8a23
--- /dev/null
+++ b/lib/bundler/templates/newgem/Gemfile.tt
@@ -0,0 +1,4 @@
+source "https://rubygems.org"
+
+# Specify your gem's dependencies in <%= config[:name] %>.gemspec
+gemspec
diff --git a/lib/bundler/templates/newgem/LICENSE.txt.tt b/lib/bundler/templates/newgem/LICENSE.txt.tt
new file mode 100644
index 00000000000000..76ef4b0191c829
--- /dev/null
+++ b/lib/bundler/templates/newgem/LICENSE.txt.tt
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) <%= Time.now.year %> <%= config[:author] %>
+
+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 NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/lib/bundler/templates/newgem/README.md.tt b/lib/bundler/templates/newgem/README.md.tt
new file mode 100644
index 00000000000000..868a0afe67fa7b
--- /dev/null
+++ b/lib/bundler/templates/newgem/README.md.tt
@@ -0,0 +1,47 @@
+# <%= config[:constant_name] %>
+
+Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/<%= config[:namespaced_path] %>`. To experiment with that code, run `bin/console` for an interactive prompt.
+
+TODO: Delete this and the text above, and describe your gem
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+```ruby
+gem '<%= config[:name] %>'
+```
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install <%= config[:name] %>
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Development
+
+After checking out the repo, run `bin/setup` to install dependencies.<% if config[:test] %> Then, run `rake <%= config[:test].sub('mini', '').sub('rspec', 'spec') %>` to run the tests.<% end %> You can also run `bin/console` for an interactive prompt that will allow you to experiment.<% if config[:bin] %> Run `bundle exec <%= config[:name] %>` to use the gem in this directory, ignoring other installed copies of this gem.<% end %>
+
+To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
+
+## Contributing
+
+Bug reports and pull requests are welcome on GitHub at https://github.com/<%= config[:github_username] %>/<%= config[:name] %>.<% if config[:coc] %> This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.<% end %>
+<% if config[:mit] -%>
+
+## License
+
+The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
+<% end -%>
+<% if config[:coc] -%>
+
+## Code of Conduct
+
+Everyone interacting in the <%= config[:constant_name] %> project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/<%= config[:github_username] %>/<%= config[:name] %>/blob/master/CODE_OF_CONDUCT.md).
+<% end -%>
diff --git a/lib/bundler/templates/newgem/Rakefile.tt b/lib/bundler/templates/newgem/Rakefile.tt
new file mode 100644
index 00000000000000..099da6f3ecde55
--- /dev/null
+++ b/lib/bundler/templates/newgem/Rakefile.tt
@@ -0,0 +1,29 @@
+require "bundler/gem_tasks"
+<% if config[:test] == "minitest" -%>
+require "rake/testtask"
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << "test"
+ t.libs << "lib"
+ t.test_files = FileList["test/**/*_test.rb"]
+end
+
+<% elsif config[:test] == "rspec" -%>
+require "rspec/core/rake_task"
+
+RSpec::Core::RakeTask.new(:spec)
+
+<% end -%>
+<% if config[:ext] -%>
+require "rake/extensiontask"
+
+task :build => :compile
+
+Rake::ExtensionTask.new("<%= config[:underscored_name] %>") do |ext|
+ ext.lib_dir = "lib/<%= config[:namespaced_path] %>"
+end
+
+task :default => [:clobber, :compile, :<%= config[:test_task] %>]
+<% else -%>
+task :default => :<%= config[:test_task] %>
+<% end -%>
diff --git a/lib/bundler/templates/newgem/bin/console.tt b/lib/bundler/templates/newgem/bin/console.tt
new file mode 100644
index 00000000000000..a27f82430f9516
--- /dev/null
+++ b/lib/bundler/templates/newgem/bin/console.tt
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+
+require "bundler/setup"
+require "<%= config[:namespaced_path] %>"
+
+# You can add fixtures and/or initialization code here to make experimenting
+# with your gem easier. You can also use a different console, if you like.
+
+# (If you use this, don't forget to add pry to your Gemfile!)
+# require "pry"
+# Pry.start
+
+require "irb"
+IRB.start(__FILE__)
diff --git a/lib/bundler/templates/newgem/bin/setup.tt b/lib/bundler/templates/newgem/bin/setup.tt
new file mode 100644
index 00000000000000..dce67d860af47a
--- /dev/null
+++ b/lib/bundler/templates/newgem/bin/setup.tt
@@ -0,0 +1,8 @@
+#!/usr/bin/env bash
+set -euo pipefail
+IFS=$'\n\t'
+set -vx
+
+bundle install
+
+# Do any other automated setup that you need to do here
diff --git a/lib/bundler/templates/newgem/exe/newgem.tt b/lib/bundler/templates/newgem/exe/newgem.tt
new file mode 100644
index 00000000000000..a8339bb79f58b0
--- /dev/null
+++ b/lib/bundler/templates/newgem/exe/newgem.tt
@@ -0,0 +1,3 @@
+#!/usr/bin/env ruby
+
+require "<%= config[:namespaced_path] %>"
diff --git a/lib/bundler/templates/newgem/ext/newgem/extconf.rb.tt b/lib/bundler/templates/newgem/ext/newgem/extconf.rb.tt
new file mode 100644
index 00000000000000..8cfc828f94f93a
--- /dev/null
+++ b/lib/bundler/templates/newgem/ext/newgem/extconf.rb.tt
@@ -0,0 +1,3 @@
+require "mkmf"
+
+create_makefile(<%= config[:makefile_path].inspect %>)
diff --git a/lib/bundler/templates/newgem/ext/newgem/newgem.c.tt b/lib/bundler/templates/newgem/ext/newgem/newgem.c.tt
new file mode 100644
index 00000000000000..8177c4d202147d
--- /dev/null
+++ b/lib/bundler/templates/newgem/ext/newgem/newgem.c.tt
@@ -0,0 +1,9 @@
+#include "<%= config[:underscored_name] %>.h"
+
+VALUE rb_m<%= config[:constant_array].join %>;
+
+void
+Init_<%= config[:underscored_name] %>(void)
+{
+ rb_m<%= config[:constant_array].join %> = rb_define_module(<%= config[:constant_name].inspect %>);
+}
diff --git a/lib/bundler/templates/newgem/ext/newgem/newgem.h.tt b/lib/bundler/templates/newgem/ext/newgem/newgem.h.tt
new file mode 100644
index 00000000000000..c6e420b66ecb0f
--- /dev/null
+++ b/lib/bundler/templates/newgem/ext/newgem/newgem.h.tt
@@ -0,0 +1,6 @@
+#ifndef <%= config[:underscored_name].upcase %>_H
+#define <%= config[:underscored_name].upcase %>_H 1
+
+#include "ruby.h"
+
+#endif /* <%= config[:underscored_name].upcase %>_H */
diff --git a/lib/bundler/templates/newgem/gitignore.tt b/lib/bundler/templates/newgem/gitignore.tt
new file mode 100644
index 00000000000000..b1c9f9986cc4bb
--- /dev/null
+++ b/lib/bundler/templates/newgem/gitignore.tt
@@ -0,0 +1,20 @@
+/.bundle/
+/.yardoc
+/_yardoc/
+/coverage/
+/doc/
+/pkg/
+/spec/reports/
+/tmp/
+<%- if config[:ext] -%>
+*.bundle
+*.so
+*.o
+*.a
+mkmf.log
+<%- end -%>
+<%- if config[:test] == "rspec" -%>
+
+# rspec failure tracking
+.rspec_status
+<%- end -%>
diff --git a/lib/bundler/templates/newgem/lib/newgem.rb.tt b/lib/bundler/templates/newgem/lib/newgem.rb.tt
new file mode 100644
index 00000000000000..fae6337c3e6e69
--- /dev/null
+++ b/lib/bundler/templates/newgem/lib/newgem.rb.tt
@@ -0,0 +1,13 @@
+require "<%= config[:namespaced_path] %>/version"
+<%- if config[:ext] -%>
+require "<%= config[:namespaced_path] %>/<%= config[:underscored_name] %>"
+<%- end -%>
+
+<%- config[:constant_array].each_with_index do |c, i| -%>
+<%= " " * i %>module <%= c %>
+<%- end -%>
+<%= " " * config[:constant_array].size %>class Error < StandardError; end
+<%= " " * config[:constant_array].size %># Your code goes here...
+<%- (config[:constant_array].size-1).downto(0) do |i| -%>
+<%= " " * i %>end
+<%- end -%>
diff --git a/lib/bundler/templates/newgem/lib/newgem/version.rb.tt b/lib/bundler/templates/newgem/lib/newgem/version.rb.tt
new file mode 100644
index 00000000000000..389daf5048d796
--- /dev/null
+++ b/lib/bundler/templates/newgem/lib/newgem/version.rb.tt
@@ -0,0 +1,7 @@
+<%- config[:constant_array].each_with_index do |c, i| -%>
+<%= " " * i %>module <%= c %>
+<%- end -%>
+<%= " " * config[:constant_array].size %>VERSION = "0.1.0"
+<%- (config[:constant_array].size-1).downto(0) do |i| -%>
+<%= " " * i %>end
+<%- end -%>
diff --git a/lib/bundler/templates/newgem/newgem.gemspec.tt b/lib/bundler/templates/newgem/newgem.gemspec.tt
new file mode 100644
index 00000000000000..faf6f7bbc5e55b
--- /dev/null
+++ b/lib/bundler/templates/newgem/newgem.gemspec.tt
@@ -0,0 +1,55 @@
+<%- if RUBY_VERSION < "2.0.0" -%>
+# coding: utf-8
+<%- end -%>
+
+lib = File.expand_path("../lib", __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require "<%= config[:namespaced_path] %>/version"
+
+Gem::Specification.new do |spec|
+ spec.name = <%= config[:name].inspect %>
+ spec.version = <%= config[:constant_name] %>::VERSION
+ spec.authors = [<%= config[:author].inspect %>]
+ spec.email = [<%= config[:email].inspect %>]
+
+ spec.summary = %q{TODO: Write a short summary, because RubyGems requires one.}
+ spec.description = %q{TODO: Write a longer description or delete this line.}
+ spec.homepage = "TODO: Put your gem's website or public repo URL here."
+<%- if config[:mit] -%>
+ spec.license = "MIT"
+<%- end -%>
+
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
+ if spec.respond_to?(:metadata)
+ spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
+
+ spec.metadata["homepage_uri"] = spec.homepage
+ spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
+ spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
+ else
+ raise "RubyGems 2.0 or newer is required to protect against " \
+ "public gem pushes."
+ end
+
+ # Specify which files should be added to the gem when it is released.
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
+ end
+ spec.bindir = "exe"
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
+ spec.require_paths = ["lib"]
+<%- if config[:ext] -%>
+ spec.extensions = ["ext/<%= config[:underscored_name] %>/extconf.rb"]
+<%- end -%>
+
+ spec.add_development_dependency "bundler", "~> <%= config[:bundler_version] %>"
+ spec.add_development_dependency "rake", "~> 10.0"
+<%- if config[:ext] -%>
+ spec.add_development_dependency "rake-compiler"
+<%- end -%>
+<%- if config[:test] -%>
+ spec.add_development_dependency "<%= config[:test] %>", "~> <%= config[:test_framework_version] %>"
+<%- end -%>
+end
diff --git a/lib/bundler/templates/newgem/rspec.tt b/lib/bundler/templates/newgem/rspec.tt
new file mode 100644
index 00000000000000..34c5164d9b56c7
--- /dev/null
+++ b/lib/bundler/templates/newgem/rspec.tt
@@ -0,0 +1,3 @@
+--format documentation
+--color
+--require spec_helper
diff --git a/lib/bundler/templates/newgem/spec/newgem_spec.rb.tt b/lib/bundler/templates/newgem/spec/newgem_spec.rb.tt
new file mode 100644
index 00000000000000..c63b48783060af
--- /dev/null
+++ b/lib/bundler/templates/newgem/spec/newgem_spec.rb.tt
@@ -0,0 +1,9 @@
+RSpec.describe <%= config[:constant_name] %> do
+ it "has a version number" do
+ expect(<%= config[:constant_name] %>::VERSION).not_to be nil
+ end
+
+ it "does something useful" do
+ expect(false).to eq(true)
+ end
+end
diff --git a/lib/bundler/templates/newgem/spec/spec_helper.rb.tt b/lib/bundler/templates/newgem/spec/spec_helper.rb.tt
new file mode 100644
index 00000000000000..805cf57e0114ef
--- /dev/null
+++ b/lib/bundler/templates/newgem/spec/spec_helper.rb.tt
@@ -0,0 +1,14 @@
+require "bundler/setup"
+require "<%= config[:namespaced_path] %>"
+
+RSpec.configure do |config|
+ # Enable flags like --only-failures and --next-failure
+ config.example_status_persistence_file_path = ".rspec_status"
+
+ # Disable RSpec exposing methods globally on `Module` and `main`
+ config.disable_monkey_patching!
+
+ config.expect_with :rspec do |c|
+ c.syntax = :expect
+ end
+end
diff --git a/lib/bundler/templates/newgem/test/newgem_test.rb.tt b/lib/bundler/templates/newgem/test/newgem_test.rb.tt
new file mode 100644
index 00000000000000..f2af9f90e0bab0
--- /dev/null
+++ b/lib/bundler/templates/newgem/test/newgem_test.rb.tt
@@ -0,0 +1,11 @@
+require "test_helper"
+
+class <%= config[:constant_name] %>Test < Minitest::Test
+ def test_that_it_has_a_version_number
+ refute_nil ::<%= config[:constant_name] %>::VERSION
+ end
+
+ def test_it_does_something_useful
+ assert false
+ end
+end
diff --git a/lib/bundler/templates/newgem/test/test_helper.rb.tt b/lib/bundler/templates/newgem/test/test_helper.rb.tt
new file mode 100644
index 00000000000000..725e3e4647386f
--- /dev/null
+++ b/lib/bundler/templates/newgem/test/test_helper.rb.tt
@@ -0,0 +1,4 @@
+$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
+require "<%= config[:namespaced_path] %>"
+
+require "minitest/autorun"
diff --git a/lib/bundler/templates/newgem/travis.yml.tt b/lib/bundler/templates/newgem/travis.yml.tt
new file mode 100644
index 00000000000000..7a3381a889097a
--- /dev/null
+++ b/lib/bundler/templates/newgem/travis.yml.tt
@@ -0,0 +1,7 @@
+---
+sudo: false
+language: ruby
+cache: bundler
+rvm:
+ - <%= RUBY_VERSION %>
+before_install: gem install bundler -v <%= Bundler::VERSION %>
diff --git a/lib/bundler/ui.rb b/lib/bundler/ui.rb
new file mode 100644
index 00000000000000..8138b30d389e2b
--- /dev/null
+++ b/lib/bundler/ui.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Bundler
+ module UI
+ autoload :RGProxy, "bundler/ui/rg_proxy"
+ autoload :Shell, "bundler/ui/shell"
+ autoload :Silent, "bundler/ui/silent"
+ end
+end
diff --git a/lib/bundler/ui/rg_proxy.rb b/lib/bundler/ui/rg_proxy.rb
new file mode 100644
index 00000000000000..e2f98481db3328
--- /dev/null
+++ b/lib/bundler/ui/rg_proxy.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+require "bundler/ui"
+require "rubygems/user_interaction"
+
+module Bundler
+ module UI
+ class RGProxy < ::Gem::SilentUI
+ def initialize(ui)
+ @ui = ui
+ super()
+ end
+
+ def say(message)
+ @ui && @ui.debug(message)
+ end
+ end
+ end
+end
diff --git a/lib/bundler/ui/shell.rb b/lib/bundler/ui/shell.rb
new file mode 100644
index 00000000000000..16e3d1571373b0
--- /dev/null
+++ b/lib/bundler/ui/shell.rb
@@ -0,0 +1,146 @@
+# frozen_string_literal: true
+
+require "bundler/vendored_thor"
+
+module Bundler
+ module UI
+ class Shell
+ LEVELS = %w[silent error warn confirm info debug].freeze
+
+ attr_writer :shell
+
+ def initialize(options = {})
+ if options["no-color"] || !$stdout.tty?
+ Thor::Base.shell = Thor::Shell::Basic
+ end
+ @shell = Thor::Base.shell.new
+ @level = ENV["DEBUG"] ? "debug" : "info"
+ @warning_history = []
+ end
+
+ def add_color(string, *color)
+ @shell.set_color(string, *color)
+ end
+
+ def info(msg, newline = nil)
+ tell_me(msg, nil, newline) if level("info")
+ end
+
+ def confirm(msg, newline = nil)
+ tell_me(msg, :green, newline) if level("confirm")
+ end
+
+ def warn(msg, newline = nil)
+ return unless level("warn")
+ return if @warning_history.include? msg
+ @warning_history << msg
+
+ return tell_err(msg, :yellow, newline) if Bundler.feature_flag.error_on_stderr?
+ tell_me(msg, :yellow, newline)
+ end
+
+ def error(msg, newline = nil)
+ return unless level("error")
+ return tell_err(msg, :red, newline) if Bundler.feature_flag.error_on_stderr?
+ tell_me(msg, :red, newline)
+ end
+
+ def debug(msg, newline = nil)
+ tell_me(msg, nil, newline) if debug?
+ end
+
+ def debug?
+ level("debug")
+ end
+
+ def quiet?
+ level("quiet")
+ end
+
+ def ask(msg)
+ @shell.ask(msg)
+ end
+
+ def yes?(msg)
+ @shell.yes?(msg)
+ end
+
+ def no?
+ @shell.no?(msg)
+ end
+
+ def level=(level)
+ raise ArgumentError unless LEVELS.include?(level.to_s)
+ @level = level.to_s
+ end
+
+ def level(name = nil)
+ return @level unless name
+ unless index = LEVELS.index(name)
+ raise "#{name.inspect} is not a valid level"
+ end
+ index <= LEVELS.index(@level)
+ end
+
+ def trace(e, newline = nil, force = false)
+ return unless debug? || force
+ msg = "#{e.class}: #{e.message}\n#{e.backtrace.join("\n ")}"
+ tell_me(msg, nil, newline)
+ end
+
+ def silence(&blk)
+ with_level("silent", &blk)
+ end
+
+ def unprinted_warnings
+ []
+ end
+
+ private
+
+ # valimism
+ def tell_me(msg, color = nil, newline = nil)
+ msg = word_wrap(msg) if newline.is_a?(Hash) && newline[:wrap]
+ if newline.nil?
+ @shell.say(msg, color)
+ else
+ @shell.say(msg, color, newline)
+ end
+ end
+
+ def tell_err(message, color = nil, newline = nil)
+ return if @shell.send(:stderr).closed?
+
+ newline ||= message.to_s !~ /( |\t)\Z/
+ message = word_wrap(message) if newline.is_a?(Hash) && newline[:wrap]
+
+ color = nil if color && !$stderr.tty?
+
+ buffer = @shell.send(:prepare_message, message, *color)
+ buffer << "\n" if newline && !message.to_s.end_with?("\n")
+
+ @shell.send(:stderr).print(buffer)
+ @shell.send(:stderr).flush
+ end
+
+ def strip_leading_spaces(text)
+ spaces = text[/\A\s+/, 0]
+ spaces ? text.gsub(/#{spaces}/, "") : text
+ end
+
+ def word_wrap(text, line_width = @shell.terminal_width)
+ strip_leading_spaces(text).split("\n").collect do |line|
+ line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
+ end * "\n"
+ end
+
+ def with_level(level)
+ original = @level
+ @level = level
+ yield
+ ensure
+ @level = original
+ end
+ end
+ end
+end
diff --git a/lib/bundler/ui/silent.rb b/lib/bundler/ui/silent.rb
new file mode 100644
index 00000000000000..dca1b2ac86de5a
--- /dev/null
+++ b/lib/bundler/ui/silent.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+module Bundler
+ module UI
+ class Silent
+ attr_writer :shell
+
+ def initialize
+ @warnings = []
+ end
+
+ def add_color(string, color)
+ string
+ end
+
+ def info(message, newline = nil)
+ end
+
+ def confirm(message, newline = nil)
+ end
+
+ def warn(message, newline = nil)
+ @warnings |= [message]
+ end
+
+ def error(message, newline = nil)
+ end
+
+ def debug(message, newline = nil)
+ end
+
+ def debug?
+ false
+ end
+
+ def quiet?
+ false
+ end
+
+ def ask(message)
+ end
+
+ def yes?(msg)
+ raise "Cannot ask yes? with a silent shell"
+ end
+
+ def no?
+ raise "Cannot ask no? with a silent shell"
+ end
+
+ def level=(name)
+ end
+
+ def level(name = nil)
+ end
+
+ def trace(message, newline = nil, force = false)
+ end
+
+ def silence
+ yield
+ end
+
+ def unprinted_warnings
+ @warnings
+ end
+ end
+ end
+end
diff --git a/lib/bundler/uri_credentials_filter.rb b/lib/bundler/uri_credentials_filter.rb
new file mode 100644
index 00000000000000..ee3692268cfd17
--- /dev/null
+++ b/lib/bundler/uri_credentials_filter.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+module Bundler
+ module URICredentialsFilter
+ module_function
+
+ def credential_filtered_uri(uri_to_anonymize)
+ return uri_to_anonymize if uri_to_anonymize.nil?
+ uri = uri_to_anonymize.dup
+ uri = URI(uri.to_s) unless uri.is_a?(URI)
+ if uri.userinfo
+ # oauth authentication
+ if uri.password == "x-oauth-basic" || uri.password == "x"
+ # URI as string does not display with password if no user is set
+ oauth_designation = uri.password
+ uri.user = oauth_designation
+ end
+ uri.password = nil
+ end
+ return uri if uri_to_anonymize.is_a?(URI)
+ return uri.to_s if uri_to_anonymize.is_a?(String)
+ rescue URI::InvalidURIError # uri is not canonical uri scheme
+ uri
+ end
+
+ def credential_filtered_string(str_to_filter, uri)
+ return str_to_filter if uri.nil? || str_to_filter.nil?
+ str_with_no_credentials = str_to_filter.dup
+ anonymous_uri_str = credential_filtered_uri(uri).to_s
+ uri_str = uri.to_s
+ if anonymous_uri_str != uri_str
+ str_with_no_credentials = str_with_no_credentials.gsub(uri_str, anonymous_uri_str)
+ end
+ str_with_no_credentials
+ end
+ end
+end
diff --git a/lib/bundler/vendor/fileutils/lib/fileutils.rb b/lib/bundler/vendor/fileutils/lib/fileutils.rb
new file mode 100644
index 00000000000000..cc697408451573
--- /dev/null
+++ b/lib/bundler/vendor/fileutils/lib/fileutils.rb
@@ -0,0 +1,1638 @@
+# frozen_string_literal: true
+#
+# = fileutils.rb
+#
+# Copyright (c) 2000-2007 Minero Aoki
+#
+# This program is free software.
+# You can distribute/modify this program under the same terms of ruby.
+#
+# == module Bundler::FileUtils
+#
+# Namespace for several file utility methods for copying, moving, removing, etc.
+#
+# === Module Functions
+#
+# require 'bundler/vendor/fileutils/lib/fileutils'
+#
+# Bundler::FileUtils.cd(dir, options)
+# Bundler::FileUtils.cd(dir, options) {|dir| block }
+# Bundler::FileUtils.pwd()
+# Bundler::FileUtils.mkdir(dir, options)
+# Bundler::FileUtils.mkdir(list, options)
+# Bundler::FileUtils.mkdir_p(dir, options)
+# Bundler::FileUtils.mkdir_p(list, options)
+# Bundler::FileUtils.rmdir(dir, options)
+# Bundler::FileUtils.rmdir(list, options)
+# Bundler::FileUtils.ln(target, link, options)
+# Bundler::FileUtils.ln(targets, dir, options)
+# Bundler::FileUtils.ln_s(target, link, options)
+# Bundler::FileUtils.ln_s(targets, dir, options)
+# Bundler::FileUtils.ln_sf(target, link, options)
+# Bundler::FileUtils.cp(src, dest, options)
+# Bundler::FileUtils.cp(list, dir, options)
+# Bundler::FileUtils.cp_r(src, dest, options)
+# Bundler::FileUtils.cp_r(list, dir, options)
+# Bundler::FileUtils.mv(src, dest, options)
+# Bundler::FileUtils.mv(list, dir, options)
+# Bundler::FileUtils.rm(list, options)
+# Bundler::FileUtils.rm_r(list, options)
+# Bundler::FileUtils.rm_rf(list, options)
+# Bundler::FileUtils.install(src, dest, options)
+# Bundler::FileUtils.chmod(mode, list, options)
+# Bundler::FileUtils.chmod_R(mode, list, options)
+# Bundler::FileUtils.chown(user, group, list, options)
+# Bundler::FileUtils.chown_R(user, group, list, options)
+# Bundler::FileUtils.touch(list, options)
+#
+# The options parameter is a hash of options, taken from the list
+# :force, :noop, :preserve, and :verbose.
+# :noop means that no changes are made. The other three are obvious.
+# Each method documents the options that it honours.
+#
+# All methods that have the concept of a "source" file or directory can take
+# either one file or a list of files in that argument. See the method
+# documentation for examples.
+#
+# There are some `low level' methods, which do not accept any option:
+#
+# Bundler::FileUtils.copy_entry(src, dest, preserve = false, dereference = false)
+# Bundler::FileUtils.copy_file(src, dest, preserve = false, dereference = true)
+# Bundler::FileUtils.copy_stream(srcstream, deststream)
+# Bundler::FileUtils.remove_entry(path, force = false)
+# Bundler::FileUtils.remove_entry_secure(path, force = false)
+# Bundler::FileUtils.remove_file(path, force = false)
+# Bundler::FileUtils.compare_file(path_a, path_b)
+# Bundler::FileUtils.compare_stream(stream_a, stream_b)
+# Bundler::FileUtils.uptodate?(file, cmp_list)
+#
+# == module Bundler::FileUtils::Verbose
+#
+# This module has all methods of Bundler::FileUtils module, but it outputs messages
+# before acting. This equates to passing the :verbose flag to methods
+# in Bundler::FileUtils.
+#
+# == module Bundler::FileUtils::NoWrite
+#
+# This module has all methods of Bundler::FileUtils module, but never changes
+# files/directories. This equates to passing the :noop flag to methods
+# in Bundler::FileUtils.
+#
+# == module Bundler::FileUtils::DryRun
+#
+# This module has all methods of Bundler::FileUtils module, but never changes
+# files/directories. This equates to passing the :noop and
+# :verbose flags to methods in Bundler::FileUtils.
+#
+
+module Bundler::FileUtils
+
+ def self.private_module_function(name) #:nodoc:
+ module_function name
+ private_class_method name
+ end
+
+ #
+ # Returns the name of the current directory.
+ #
+ def pwd
+ Dir.pwd
+ end
+ module_function :pwd
+
+ alias getwd pwd
+ module_function :getwd
+
+ #
+ # Changes the current directory to the directory +dir+.
+ #
+ # If this method is called with block, resumes to the old
+ # working directory after the block execution finished.
+ #
+ # Bundler::FileUtils.cd('/', :verbose => true) # chdir and report it
+ #
+ # Bundler::FileUtils.cd('/') do # chdir
+ # # ... # do something
+ # end # return to original directory
+ #
+ def cd(dir, verbose: nil, &block) # :yield: dir
+ fu_output_message "cd #{dir}" if verbose
+ Dir.chdir(dir, &block)
+ fu_output_message 'cd -' if verbose and block
+ end
+ module_function :cd
+
+ alias chdir cd
+ module_function :chdir
+
+ #
+ # Returns true if +new+ is newer than all +old_list+.
+ # Non-existent files are older than any file.
+ #
+ # Bundler::FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \
+ # system 'make hello.o'
+ #
+ def uptodate?(new, old_list)
+ return false unless File.exist?(new)
+ new_time = File.mtime(new)
+ old_list.each do |old|
+ if File.exist?(old)
+ return false unless new_time > File.mtime(old)
+ end
+ end
+ true
+ end
+ module_function :uptodate?
+
+ def remove_trailing_slash(dir) #:nodoc:
+ dir == '/' ? dir : dir.chomp(?/)
+ end
+ private_module_function :remove_trailing_slash
+
+ #
+ # Creates one or more directories.
+ #
+ # Bundler::FileUtils.mkdir 'test'
+ # Bundler::FileUtils.mkdir %w( tmp data )
+ # Bundler::FileUtils.mkdir 'notexist', :noop => true # Does not really create.
+ # Bundler::FileUtils.mkdir 'tmp', :mode => 0700
+ #
+ def mkdir(list, mode: nil, noop: nil, verbose: nil)
+ list = fu_list(list)
+ fu_output_message "mkdir #{mode ? ('-m %03o ' % mode) : ''}#{list.join ' '}" if verbose
+ return if noop
+
+ list.each do |dir|
+ fu_mkdir dir, mode
+ end
+ end
+ module_function :mkdir
+
+ #
+ # Creates a directory and all its parent directories.
+ # For example,
+ #
+ # Bundler::FileUtils.mkdir_p '/usr/local/lib/ruby'
+ #
+ # causes to make following directories, if it does not exist.
+ #
+ # * /usr
+ # * /usr/local
+ # * /usr/local/lib
+ # * /usr/local/lib/ruby
+ #
+ # You can pass several directories at a time in a list.
+ #
+ def mkdir_p(list, mode: nil, noop: nil, verbose: nil)
+ list = fu_list(list)
+ fu_output_message "mkdir -p #{mode ? ('-m %03o ' % mode) : ''}#{list.join ' '}" if verbose
+ return *list if noop
+
+ list.map {|path| remove_trailing_slash(path)}.each do |path|
+ # optimize for the most common case
+ begin
+ fu_mkdir path, mode
+ next
+ rescue SystemCallError
+ next if File.directory?(path)
+ end
+
+ stack = []
+ until path == stack.last # dirname("/")=="/", dirname("C:/")=="C:/"
+ stack.push path
+ path = File.dirname(path)
+ end
+ stack.pop # root directory should exist
+ stack.reverse_each do |dir|
+ begin
+ fu_mkdir dir, mode
+ rescue SystemCallError
+ raise unless File.directory?(dir)
+ end
+ end
+ end
+
+ return *list
+ end
+ module_function :mkdir_p
+
+ alias mkpath mkdir_p
+ alias makedirs mkdir_p
+ module_function :mkpath
+ module_function :makedirs
+
+ def fu_mkdir(path, mode) #:nodoc:
+ path = remove_trailing_slash(path)
+ if mode
+ Dir.mkdir path, mode
+ File.chmod mode, path
+ else
+ Dir.mkdir path
+ end
+ end
+ private_module_function :fu_mkdir
+
+ #
+ # Removes one or more directories.
+ #
+ # Bundler::FileUtils.rmdir 'somedir'
+ # Bundler::FileUtils.rmdir %w(somedir anydir otherdir)
+ # # Does not really remove directory; outputs message.
+ # Bundler::FileUtils.rmdir 'somedir', :verbose => true, :noop => true
+ #
+ def rmdir(list, parents: nil, noop: nil, verbose: nil)
+ list = fu_list(list)
+ fu_output_message "rmdir #{parents ? '-p ' : ''}#{list.join ' '}" if verbose
+ return if noop
+ list.each do |dir|
+ begin
+ Dir.rmdir(dir = remove_trailing_slash(dir))
+ if parents
+ until (parent = File.dirname(dir)) == '.' or parent == dir
+ dir = parent
+ Dir.rmdir(dir)
+ end
+ end
+ rescue Errno::ENOTEMPTY, Errno::EEXIST, Errno::ENOENT
+ end
+ end
+ end
+ module_function :rmdir
+
+ #
+ # :call-seq:
+ # Bundler::FileUtils.ln(target, link, force: nil, noop: nil, verbose: nil)
+ # Bundler::FileUtils.ln(target, dir, force: nil, noop: nil, verbose: nil)
+ # Bundler::FileUtils.ln(targets, dir, force: nil, noop: nil, verbose: nil)
+ #
+ # In the first form, creates a hard link +link+ which points to +target+.
+ # If +link+ already exists, raises Errno::EEXIST.
+ # But if the :force option is set, overwrites +link+.
+ #
+ # Bundler::FileUtils.ln 'gcc', 'cc', verbose: true
+ # Bundler::FileUtils.ln '/usr/bin/emacs21', '/usr/bin/emacs'
+ #
+ # In the second form, creates a link +dir/target+ pointing to +target+.
+ # In the third form, creates several hard links in the directory +dir+,
+ # pointing to each item in +targets+.
+ # If +dir+ is not a directory, raises Errno::ENOTDIR.
+ #
+ # Bundler::FileUtils.cd '/sbin'
+ # Bundler::FileUtils.ln %w(cp mv mkdir), '/bin' # Now /sbin/cp and /bin/cp are linked.
+ #
+ def ln(src, dest, force: nil, noop: nil, verbose: nil)
+ fu_output_message "ln#{force ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if verbose
+ return if noop
+ fu_each_src_dest0(src, dest) do |s,d|
+ remove_file d, true if force
+ File.link s, d
+ end
+ end
+ module_function :ln
+
+ alias link ln
+ module_function :link
+
+ #
+ # :call-seq:
+ # Bundler::FileUtils.ln_s(target, link, force: nil, noop: nil, verbose: nil)
+ # Bundler::FileUtils.ln_s(target, dir, force: nil, noop: nil, verbose: nil)
+ # Bundler::FileUtils.ln_s(targets, dir, force: nil, noop: nil, verbose: nil)
+ #
+ # In the first form, creates a symbolic link +link+ which points to +target+.
+ # If +link+ already exists, raises Errno::EEXIST.
+ # But if the :force option is set, overwrites +link+.
+ #
+ # Bundler::FileUtils.ln_s '/usr/bin/ruby', '/usr/local/bin/ruby'
+ # Bundler::FileUtils.ln_s 'verylongsourcefilename.c', 'c', force: true
+ #
+ # In the second form, creates a link +dir/target+ pointing to +target+.
+ # In the third form, creates several symbolic links in the directory +dir+,
+ # pointing to each item in +targets+.
+ # If +dir+ is not a directory, raises Errno::ENOTDIR.
+ #
+ # Bundler::FileUtils.ln_s Dir.glob('/bin/*.rb'), '/home/foo/bin'
+ #
+ def ln_s(src, dest, force: nil, noop: nil, verbose: nil)
+ fu_output_message "ln -s#{force ? 'f' : ''} #{[src,dest].flatten.join ' '}" if verbose
+ return if noop
+ fu_each_src_dest0(src, dest) do |s,d|
+ remove_file d, true if force
+ File.symlink s, d
+ end
+ end
+ module_function :ln_s
+
+ alias symlink ln_s
+ module_function :symlink
+
+ #
+ # :call-seq:
+ # Bundler::FileUtils.ln_sf(*args)
+ #
+ # Same as
+ #
+ # Bundler::FileUtils.ln_s(*args, force: true)
+ #
+ def ln_sf(src, dest, noop: nil, verbose: nil)
+ ln_s src, dest, force: true, noop: noop, verbose: verbose
+ end
+ module_function :ln_sf
+
+ #
+ # Copies a file content +src+ to +dest+. If +dest+ is a directory,
+ # copies +src+ to +dest/src+.
+ #
+ # If +src+ is a list of files, then +dest+ must be a directory.
+ #
+ # Bundler::FileUtils.cp 'eval.c', 'eval.c.org'
+ # Bundler::FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6'
+ # Bundler::FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', :verbose => true
+ # Bundler::FileUtils.cp 'symlink', 'dest' # copy content, "dest" is not a symlink
+ #
+ def cp(src, dest, preserve: nil, noop: nil, verbose: nil)
+ fu_output_message "cp#{preserve ? ' -p' : ''} #{[src,dest].flatten.join ' '}" if verbose
+ return if noop
+ fu_each_src_dest(src, dest) do |s, d|
+ copy_file s, d, preserve
+ end
+ end
+ module_function :cp
+
+ alias copy cp
+ module_function :copy
+
+ #
+ # Copies +src+ to +dest+. If +src+ is a directory, this method copies
+ # all its contents recursively. If +dest+ is a directory, copies
+ # +src+ to +dest/src+.
+ #
+ # +src+ can be a list of files.
+ #
+ # # Installing Ruby library "mylib" under the site_ruby
+ # Bundler::FileUtils.rm_r site_ruby + '/mylib', :force
+ # Bundler::FileUtils.cp_r 'lib/', site_ruby + '/mylib'
+ #
+ # # Examples of copying several files to target directory.
+ # Bundler::FileUtils.cp_r %w(mail.rb field.rb debug/), site_ruby + '/tmail'
+ # Bundler::FileUtils.cp_r Dir.glob('*.rb'), '/home/foo/lib/ruby', :noop => true, :verbose => true
+ #
+ # # If you want to copy all contents of a directory instead of the
+ # # directory itself, c.f. src/x -> dest/x, src/y -> dest/y,
+ # # use following code.
+ # Bundler::FileUtils.cp_r 'src/.', 'dest' # cp_r('src', 'dest') makes dest/src,
+ # # but this doesn't.
+ #
+ def cp_r(src, dest, preserve: nil, noop: nil, verbose: nil,
+ dereference_root: true, remove_destination: nil)
+ fu_output_message "cp -r#{preserve ? 'p' : ''}#{remove_destination ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if verbose
+ return if noop
+ fu_each_src_dest(src, dest) do |s, d|
+ copy_entry s, d, preserve, dereference_root, remove_destination
+ end
+ end
+ module_function :cp_r
+
+ #
+ # Copies a file system entry +src+ to +dest+.
+ # If +src+ is a directory, this method copies its contents recursively.
+ # This method preserves file types, c.f. symlink, directory...
+ # (FIFO, device files and etc. are not supported yet)
+ #
+ # Both of +src+ and +dest+ must be a path name.
+ # +src+ must exist, +dest+ must not exist.
+ #
+ # If +preserve+ is true, this method preserves owner, group, and
+ # modified time. Permissions are copied regardless +preserve+.
+ #
+ # If +dereference_root+ is true, this method dereference tree root.
+ #
+ # If +remove_destination+ is true, this method removes each destination file before copy.
+ #
+ def copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false)
+ Entry_.new(src, nil, dereference_root).wrap_traverse(proc do |ent|
+ destent = Entry_.new(dest, ent.rel, false)
+ File.unlink destent.path if remove_destination && File.file?(destent.path)
+ ent.copy destent.path
+ end, proc do |ent|
+ destent = Entry_.new(dest, ent.rel, false)
+ ent.copy_metadata destent.path if preserve
+ end)
+ end
+ module_function :copy_entry
+
+ #
+ # Copies file contents of +src+ to +dest+.
+ # Both of +src+ and +dest+ must be a path name.
+ #
+ def copy_file(src, dest, preserve = false, dereference = true)
+ ent = Entry_.new(src, nil, dereference)
+ ent.copy_file dest
+ ent.copy_metadata dest if preserve
+ end
+ module_function :copy_file
+
+ #
+ # Copies stream +src+ to +dest+.
+ # +src+ must respond to #read(n) and
+ # +dest+ must respond to #write(str).
+ #
+ def copy_stream(src, dest)
+ IO.copy_stream(src, dest)
+ end
+ module_function :copy_stream
+
+ #
+ # Moves file(s) +src+ to +dest+. If +file+ and +dest+ exist on the different
+ # disk partition, the file is copied then the original file is removed.
+ #
+ # Bundler::FileUtils.mv 'badname.rb', 'goodname.rb'
+ # Bundler::FileUtils.mv 'stuff.rb', '/notexist/lib/ruby', :force => true # no error
+ #
+ # Bundler::FileUtils.mv %w(junk.txt dust.txt), '/home/foo/.trash/'
+ # Bundler::FileUtils.mv Dir.glob('test*.rb'), 'test', :noop => true, :verbose => true
+ #
+ def mv(src, dest, force: nil, noop: nil, verbose: nil, secure: nil)
+ fu_output_message "mv#{force ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if verbose
+ return if noop
+ fu_each_src_dest(src, dest) do |s, d|
+ destent = Entry_.new(d, nil, true)
+ begin
+ if destent.exist?
+ if destent.directory?
+ raise Errno::EEXIST, d
+ else
+ destent.remove_file if rename_cannot_overwrite_file?
+ end
+ end
+ begin
+ File.rename s, d
+ rescue Errno::EXDEV
+ copy_entry s, d, true
+ if secure
+ remove_entry_secure s, force
+ else
+ remove_entry s, force
+ end
+ end
+ rescue SystemCallError
+ raise unless force
+ end
+ end
+ end
+ module_function :mv
+
+ alias move mv
+ module_function :move
+
+ def rename_cannot_overwrite_file? #:nodoc:
+ /emx/ =~ RUBY_PLATFORM
+ end
+ private_module_function :rename_cannot_overwrite_file?
+
+ #
+ # Remove file(s) specified in +list+. This method cannot remove directories.
+ # All StandardErrors are ignored when the :force option is set.
+ #
+ # Bundler::FileUtils.rm %w( junk.txt dust.txt )
+ # Bundler::FileUtils.rm Dir.glob('*.so')
+ # Bundler::FileUtils.rm 'NotExistFile', :force => true # never raises exception
+ #
+ def rm(list, force: nil, noop: nil, verbose: nil)
+ list = fu_list(list)
+ fu_output_message "rm#{force ? ' -f' : ''} #{list.join ' '}" if verbose
+ return if noop
+
+ list.each do |path|
+ remove_file path, force
+ end
+ end
+ module_function :rm
+
+ alias remove rm
+ module_function :remove
+
+ #
+ # Equivalent to
+ #
+ # Bundler::FileUtils.rm(list, :force => true)
+ #
+ def rm_f(list, noop: nil, verbose: nil)
+ rm list, force: true, noop: noop, verbose: verbose
+ end
+ module_function :rm_f
+
+ alias safe_unlink rm_f
+ module_function :safe_unlink
+
+ #
+ # remove files +list+[0] +list+[1]... If +list+[n] is a directory,
+ # removes its all contents recursively. This method ignores
+ # StandardError when :force option is set.
+ #
+ # Bundler::FileUtils.rm_r Dir.glob('/tmp/*')
+ # Bundler::FileUtils.rm_r 'some_dir', :force => true
+ #
+ # WARNING: This method causes local vulnerability
+ # if one of parent directories or removing directory tree are world
+ # writable (including /tmp, whose permission is 1777), and the current
+ # process has strong privilege such as Unix super user (root), and the
+ # system has symbolic link. For secure removing, read the documentation
+ # of #remove_entry_secure carefully, and set :secure option to true.
+ # Default is :secure=>false.
+ #
+ # NOTE: This method calls #remove_entry_secure if :secure option is set.
+ # See also #remove_entry_secure.
+ #
+ def rm_r(list, force: nil, noop: nil, verbose: nil, secure: nil)
+ list = fu_list(list)
+ fu_output_message "rm -r#{force ? 'f' : ''} #{list.join ' '}" if verbose
+ return if noop
+ list.each do |path|
+ if secure
+ remove_entry_secure path, force
+ else
+ remove_entry path, force
+ end
+ end
+ end
+ module_function :rm_r
+
+ #
+ # Equivalent to
+ #
+ # Bundler::FileUtils.rm_r(list, :force => true)
+ #
+ # WARNING: This method causes local vulnerability.
+ # Read the documentation of #rm_r first.
+ #
+ def rm_rf(list, noop: nil, verbose: nil, secure: nil)
+ rm_r list, force: true, noop: noop, verbose: verbose, secure: secure
+ end
+ module_function :rm_rf
+
+ alias rmtree rm_rf
+ module_function :rmtree
+
+ #
+ # This method removes a file system entry +path+. +path+ shall be a
+ # regular file, a directory, or something. If +path+ is a directory,
+ # remove it recursively. This method is required to avoid TOCTTOU
+ # (time-of-check-to-time-of-use) local security vulnerability of #rm_r.
+ # #rm_r causes security hole when:
+ #
+ # * Parent directory is world writable (including /tmp).
+ # * Removing directory tree includes world writable directory.
+ # * The system has symbolic link.
+ #
+ # To avoid this security hole, this method applies special preprocess.
+ # If +path+ is a directory, this method chown(2) and chmod(2) all
+ # removing directories. This requires the current process is the
+ # owner of the removing whole directory tree, or is the super user (root).
+ #
+ # WARNING: You must ensure that *ALL* parent directories cannot be
+ # moved by other untrusted users. For example, parent directories
+ # should not be owned by untrusted users, and should not be world
+ # writable except when the sticky bit set.
+ #
+ # WARNING: Only the owner of the removing directory tree, or Unix super
+ # user (root) should invoke this method. Otherwise this method does not
+ # work.
+ #
+ # For details of this security vulnerability, see Perl's case:
+ #
+ # * http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CAN-2005-0448
+ # * http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CAN-2004-0452
+ #
+ # For fileutils.rb, this vulnerability is reported in [ruby-dev:26100].
+ #
+ def remove_entry_secure(path, force = false)
+ unless fu_have_symlink?
+ remove_entry path, force
+ return
+ end
+ fullpath = File.expand_path(path)
+ st = File.lstat(fullpath)
+ unless st.directory?
+ File.unlink fullpath
+ return
+ end
+ # is a directory.
+ parent_st = File.stat(File.dirname(fullpath))
+ unless parent_st.world_writable?
+ remove_entry path, force
+ return
+ end
+ unless parent_st.sticky?
+ raise ArgumentError, "parent directory is world writable, Bundler::FileUtils#remove_entry_secure does not work; abort: #{path.inspect} (parent directory mode #{'%o' % parent_st.mode})"
+ end
+ # freeze tree root
+ euid = Process.euid
+ File.open(fullpath + '/.') {|f|
+ unless fu_stat_identical_entry?(st, f.stat)
+ # symlink (TOC-to-TOU attack?)
+ File.unlink fullpath
+ return
+ end
+ f.chown euid, -1
+ f.chmod 0700
+ unless fu_stat_identical_entry?(st, File.lstat(fullpath))
+ # TOC-to-TOU attack?
+ File.unlink fullpath
+ return
+ end
+ }
+ # ---- tree root is frozen ----
+ root = Entry_.new(path)
+ root.preorder_traverse do |ent|
+ if ent.directory?
+ ent.chown euid, -1
+ ent.chmod 0700
+ end
+ end
+ root.postorder_traverse do |ent|
+ begin
+ ent.remove
+ rescue
+ raise unless force
+ end
+ end
+ rescue
+ raise unless force
+ end
+ module_function :remove_entry_secure
+
+ def fu_have_symlink? #:nodoc:
+ File.symlink nil, nil
+ rescue NotImplementedError
+ return false
+ rescue TypeError
+ return true
+ end
+ private_module_function :fu_have_symlink?
+
+ def fu_stat_identical_entry?(a, b) #:nodoc:
+ a.dev == b.dev and a.ino == b.ino
+ end
+ private_module_function :fu_stat_identical_entry?
+
+ #
+ # This method removes a file system entry +path+.
+ # +path+ might be a regular file, a directory, or something.
+ # If +path+ is a directory, remove it recursively.
+ #
+ # See also #remove_entry_secure.
+ #
+ def remove_entry(path, force = false)
+ Entry_.new(path).postorder_traverse do |ent|
+ begin
+ ent.remove
+ rescue
+ raise unless force
+ end
+ end
+ rescue
+ raise unless force
+ end
+ module_function :remove_entry
+
+ #
+ # Removes a file +path+.
+ # This method ignores StandardError if +force+ is true.
+ #
+ def remove_file(path, force = false)
+ Entry_.new(path).remove_file
+ rescue
+ raise unless force
+ end
+ module_function :remove_file
+
+ #
+ # Removes a directory +dir+ and its contents recursively.
+ # This method ignores StandardError if +force+ is true.
+ #
+ def remove_dir(path, force = false)
+ remove_entry path, force # FIXME?? check if it is a directory
+ end
+ module_function :remove_dir
+
+ #
+ # Returns true if the contents of a file +a+ and a file +b+ are identical.
+ #
+ # Bundler::FileUtils.compare_file('somefile', 'somefile') #=> true
+ # Bundler::FileUtils.compare_file('/dev/null', '/dev/urandom') #=> false
+ #
+ def compare_file(a, b)
+ return false unless File.size(a) == File.size(b)
+ File.open(a, 'rb') {|fa|
+ File.open(b, 'rb') {|fb|
+ return compare_stream(fa, fb)
+ }
+ }
+ end
+ module_function :compare_file
+
+ alias identical? compare_file
+ alias cmp compare_file
+ module_function :identical?
+ module_function :cmp
+
+ #
+ # Returns true if the contents of a stream +a+ and +b+ are identical.
+ #
+ def compare_stream(a, b)
+ bsize = fu_stream_blksize(a, b)
+ sa = String.new(capacity: bsize)
+ sb = String.new(capacity: bsize)
+ begin
+ a.read(bsize, sa)
+ b.read(bsize, sb)
+ return true if sa.empty? && sb.empty?
+ end while sa == sb
+ false
+ end
+ module_function :compare_stream
+
+ #
+ # If +src+ is not same as +dest+, copies it and changes the permission
+ # mode to +mode+. If +dest+ is a directory, destination is +dest+/+src+.
+ # This method removes destination before copy.
+ #
+ # Bundler::FileUtils.install 'ruby', '/usr/local/bin/ruby', :mode => 0755, :verbose => true
+ # Bundler::FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose => true
+ #
+ def install(src, dest, mode: nil, owner: nil, group: nil, preserve: nil,
+ noop: nil, verbose: nil)
+ if verbose
+ msg = +"install -c"
+ msg << ' -p' if preserve
+ msg << ' -m ' << mode_to_s(mode) if mode
+ msg << " -o #{owner}" if owner
+ msg << " -g #{group}" if group
+ msg << ' ' << [src,dest].flatten.join(' ')
+ fu_output_message msg
+ end
+ return if noop
+ uid = fu_get_uid(owner)
+ gid = fu_get_gid(group)
+ fu_each_src_dest(src, dest) do |s, d|
+ st = File.stat(s)
+ unless File.exist?(d) and compare_file(s, d)
+ remove_file d, true
+ copy_file s, d
+ File.utime st.atime, st.mtime, d if preserve
+ File.chmod fu_mode(mode, st), d if mode
+ File.chown uid, gid, d if uid or gid
+ end
+ end
+ end
+ module_function :install
+
+ def user_mask(target) #:nodoc:
+ target.each_char.inject(0) do |mask, chr|
+ case chr
+ when "u"
+ mask | 04700
+ when "g"
+ mask | 02070
+ when "o"
+ mask | 01007
+ when "a"
+ mask | 07777
+ else
+ raise ArgumentError, "invalid `who' symbol in file mode: #{chr}"
+ end
+ end
+ end
+ private_module_function :user_mask
+
+ def apply_mask(mode, user_mask, op, mode_mask) #:nodoc:
+ case op
+ when '='
+ (mode & ~user_mask) | (user_mask & mode_mask)
+ when '+'
+ mode | (user_mask & mode_mask)
+ when '-'
+ mode & ~(user_mask & mode_mask)
+ end
+ end
+ private_module_function :apply_mask
+
+ def symbolic_modes_to_i(mode_sym, path) #:nodoc:
+ mode = if File::Stat === path
+ path.mode
+ else
+ File.stat(path).mode
+ end
+ mode_sym.split(/,/).inject(mode & 07777) do |current_mode, clause|
+ target, *actions = clause.split(/([=+-])/)
+ raise ArgumentError, "invalid file mode: #{mode_sym}" if actions.empty?
+ target = 'a' if target.empty?
+ user_mask = user_mask(target)
+ actions.each_slice(2) do |op, perm|
+ need_apply = op == '='
+ mode_mask = (perm || '').each_char.inject(0) do |mask, chr|
+ case chr
+ when "r"
+ mask | 0444
+ when "w"
+ mask | 0222
+ when "x"
+ mask | 0111
+ when "X"
+ if FileTest.directory? path
+ mask | 0111
+ else
+ mask
+ end
+ when "s"
+ mask | 06000
+ when "t"
+ mask | 01000
+ when "u", "g", "o"
+ if mask.nonzero?
+ current_mode = apply_mask(current_mode, user_mask, op, mask)
+ end
+ need_apply = false
+ copy_mask = user_mask(chr)
+ (current_mode & copy_mask) / (copy_mask & 0111) * (user_mask & 0111)
+ else
+ raise ArgumentError, "invalid `perm' symbol in file mode: #{chr}"
+ end
+ end
+
+ if mode_mask.nonzero? || need_apply
+ current_mode = apply_mask(current_mode, user_mask, op, mode_mask)
+ end
+ end
+ current_mode
+ end
+ end
+ private_module_function :symbolic_modes_to_i
+
+ def fu_mode(mode, path) #:nodoc:
+ mode.is_a?(String) ? symbolic_modes_to_i(mode, path) : mode
+ end
+ private_module_function :fu_mode
+
+ def mode_to_s(mode) #:nodoc:
+ mode.is_a?(String) ? mode : "%o" % mode
+ end
+ private_module_function :mode_to_s
+
+ #
+ # Changes permission bits on the named files (in +list+) to the bit pattern
+ # represented by +mode+.
+ #
+ # +mode+ is the symbolic and absolute mode can be used.
+ #
+ # Absolute mode is
+ # Bundler::FileUtils.chmod 0755, 'somecommand'
+ # Bundler::FileUtils.chmod 0644, %w(my.rb your.rb his.rb her.rb)
+ # Bundler::FileUtils.chmod 0755, '/usr/bin/ruby', :verbose => true
+ #
+ # Symbolic mode is
+ # Bundler::FileUtils.chmod "u=wrx,go=rx", 'somecommand'
+ # Bundler::FileUtils.chmod "u=wr,go=rr", %w(my.rb your.rb his.rb her.rb)
+ # Bundler::FileUtils.chmod "u=wrx,go=rx", '/usr/bin/ruby', :verbose => true
+ #
+ # "a" :: is user, group, other mask.
+ # "u" :: is user's mask.
+ # "g" :: is group's mask.
+ # "o" :: is other's mask.
+ # "w" :: is write permission.
+ # "r" :: is read permission.
+ # "x" :: is execute permission.
+ # "X" ::
+ # is execute permission for directories only, must be used in conjunction with "+"
+ # "s" :: is uid, gid.
+ # "t" :: is sticky bit.
+ # "+" :: is added to a class given the specified mode.
+ # "-" :: Is removed from a given class given mode.
+ # "=" :: Is the exact nature of the class will be given a specified mode.
+
+ def chmod(mode, list, noop: nil, verbose: nil)
+ list = fu_list(list)
+ fu_output_message sprintf('chmod %s %s', mode_to_s(mode), list.join(' ')) if verbose
+ return if noop
+ list.each do |path|
+ Entry_.new(path).chmod(fu_mode(mode, path))
+ end
+ end
+ module_function :chmod
+
+ #
+ # Changes permission bits on the named files (in +list+)
+ # to the bit pattern represented by +mode+.
+ #
+ # Bundler::FileUtils.chmod_R 0700, "/tmp/app.#{$$}"
+ # Bundler::FileUtils.chmod_R "u=wrx", "/tmp/app.#{$$}"
+ #
+ def chmod_R(mode, list, noop: nil, verbose: nil, force: nil)
+ list = fu_list(list)
+ fu_output_message sprintf('chmod -R%s %s %s',
+ (force ? 'f' : ''),
+ mode_to_s(mode), list.join(' ')) if verbose
+ return if noop
+ list.each do |root|
+ Entry_.new(root).traverse do |ent|
+ begin
+ ent.chmod(fu_mode(mode, ent.path))
+ rescue
+ raise unless force
+ end
+ end
+ end
+ end
+ module_function :chmod_R
+
+ #
+ # Changes owner and group on the named files (in +list+)
+ # to the user +user+ and the group +group+. +user+ and +group+
+ # may be an ID (Integer/String) or a name (String).
+ # If +user+ or +group+ is nil, this method does not change
+ # the attribute.
+ #
+ # Bundler::FileUtils.chown 'root', 'staff', '/usr/local/bin/ruby'
+ # Bundler::FileUtils.chown nil, 'bin', Dir.glob('/usr/bin/*'), :verbose => true
+ #
+ def chown(user, group, list, noop: nil, verbose: nil)
+ list = fu_list(list)
+ fu_output_message sprintf('chown %s %s',
+ (group ? "#{user}:#{group}" : user || ':'),
+ list.join(' ')) if verbose
+ return if noop
+ uid = fu_get_uid(user)
+ gid = fu_get_gid(group)
+ list.each do |path|
+ Entry_.new(path).chown uid, gid
+ end
+ end
+ module_function :chown
+
+ #
+ # Changes owner and group on the named files (in +list+)
+ # to the user +user+ and the group +group+ recursively.
+ # +user+ and +group+ may be an ID (Integer/String) or
+ # a name (String). If +user+ or +group+ is nil, this
+ # method does not change the attribute.
+ #
+ # Bundler::FileUtils.chown_R 'www', 'www', '/var/www/htdocs'
+ # Bundler::FileUtils.chown_R 'cvs', 'cvs', '/var/cvs', :verbose => true
+ #
+ def chown_R(user, group, list, noop: nil, verbose: nil, force: nil)
+ list = fu_list(list)
+ fu_output_message sprintf('chown -R%s %s %s',
+ (force ? 'f' : ''),
+ (group ? "#{user}:#{group}" : user || ':'),
+ list.join(' ')) if verbose
+ return if noop
+ uid = fu_get_uid(user)
+ gid = fu_get_gid(group)
+ list.each do |root|
+ Entry_.new(root).traverse do |ent|
+ begin
+ ent.chown uid, gid
+ rescue
+ raise unless force
+ end
+ end
+ end
+ end
+ module_function :chown_R
+
+ begin
+ require 'etc'
+ rescue LoadError # rescue LoadError for miniruby
+ end
+
+ def fu_get_uid(user) #:nodoc:
+ return nil unless user
+ case user
+ when Integer
+ user
+ when /\A\d+\z/
+ user.to_i
+ else
+ Etc.getpwnam(user) ? Etc.getpwnam(user).uid : nil
+ end
+ end
+ private_module_function :fu_get_uid
+
+ def fu_get_gid(group) #:nodoc:
+ return nil unless group
+ case group
+ when Integer
+ group
+ when /\A\d+\z/
+ group.to_i
+ else
+ Etc.getgrnam(group) ? Etc.getgrnam(group).gid : nil
+ end
+ end
+ private_module_function :fu_get_gid
+
+ #
+ # Updates modification time (mtime) and access time (atime) of file(s) in
+ # +list+. Files are created if they don't exist.
+ #
+ # Bundler::FileUtils.touch 'timestamp'
+ # Bundler::FileUtils.touch Dir.glob('*.c'); system 'make'
+ #
+ def touch(list, noop: nil, verbose: nil, mtime: nil, nocreate: nil)
+ list = fu_list(list)
+ t = mtime
+ if verbose
+ fu_output_message "touch #{nocreate ? '-c ' : ''}#{t ? t.strftime('-t %Y%m%d%H%M.%S ') : ''}#{list.join ' '}"
+ end
+ return if noop
+ list.each do |path|
+ created = nocreate
+ begin
+ File.utime(t, t, path)
+ rescue Errno::ENOENT
+ raise if created
+ File.open(path, 'a') {
+ ;
+ }
+ created = true
+ retry if t
+ end
+ end
+ end
+ module_function :touch
+
+ private
+
+ module StreamUtils_
+ private
+
+ def fu_windows?
+ /mswin|mingw|bccwin|emx/ =~ RUBY_PLATFORM
+ end
+
+ def fu_copy_stream0(src, dest, blksize = nil) #:nodoc:
+ IO.copy_stream(src, dest)
+ end
+
+ def fu_stream_blksize(*streams)
+ streams.each do |s|
+ next unless s.respond_to?(:stat)
+ size = fu_blksize(s.stat)
+ return size if size
+ end
+ fu_default_blksize()
+ end
+
+ def fu_blksize(st)
+ s = st.blksize
+ return nil unless s
+ return nil if s == 0
+ s
+ end
+
+ def fu_default_blksize
+ 1024
+ end
+ end
+
+ include StreamUtils_
+ extend StreamUtils_
+
+ class Entry_ #:nodoc: internal use only
+ include StreamUtils_
+
+ def initialize(a, b = nil, deref = false)
+ @prefix = @rel = @path = nil
+ if b
+ @prefix = a
+ @rel = b
+ else
+ @path = a
+ end
+ @deref = deref
+ @stat = nil
+ @lstat = nil
+ end
+
+ def inspect
+ "\#<#{self.class} #{path()}>"
+ end
+
+ def path
+ if @path
+ File.path(@path)
+ else
+ join(@prefix, @rel)
+ end
+ end
+
+ def prefix
+ @prefix || @path
+ end
+
+ def rel
+ @rel
+ end
+
+ def dereference?
+ @deref
+ end
+
+ def exist?
+ begin
+ lstat
+ true
+ rescue Errno::ENOENT
+ false
+ end
+ end
+
+ def file?
+ s = lstat!
+ s and s.file?
+ end
+
+ def directory?
+ s = lstat!
+ s and s.directory?
+ end
+
+ def symlink?
+ s = lstat!
+ s and s.symlink?
+ end
+
+ def chardev?
+ s = lstat!
+ s and s.chardev?
+ end
+
+ def blockdev?
+ s = lstat!
+ s and s.blockdev?
+ end
+
+ def socket?
+ s = lstat!
+ s and s.socket?
+ end
+
+ def pipe?
+ s = lstat!
+ s and s.pipe?
+ end
+
+ S_IF_DOOR = 0xD000
+
+ def door?
+ s = lstat!
+ s and (s.mode & 0xF000 == S_IF_DOOR)
+ end
+
+ def entries
+ opts = {}
+ opts[:encoding] = ::Encoding::UTF_8 if fu_windows?
+ Dir.entries(path(), opts)\
+ .reject {|n| n == '.' or n == '..' }\
+ .map {|n| Entry_.new(prefix(), join(rel(), n.untaint)) }
+ end
+
+ def stat
+ return @stat if @stat
+ if lstat() and lstat().symlink?
+ @stat = File.stat(path())
+ else
+ @stat = lstat()
+ end
+ @stat
+ end
+
+ def stat!
+ return @stat if @stat
+ if lstat! and lstat!.symlink?
+ @stat = File.stat(path())
+ else
+ @stat = lstat!
+ end
+ @stat
+ rescue SystemCallError
+ nil
+ end
+
+ def lstat
+ if dereference?
+ @lstat ||= File.stat(path())
+ else
+ @lstat ||= File.lstat(path())
+ end
+ end
+
+ def lstat!
+ lstat()
+ rescue SystemCallError
+ nil
+ end
+
+ def chmod(mode)
+ if symlink?
+ File.lchmod mode, path() if have_lchmod?
+ else
+ File.chmod mode, path()
+ end
+ end
+
+ def chown(uid, gid)
+ if symlink?
+ File.lchown uid, gid, path() if have_lchown?
+ else
+ File.chown uid, gid, path()
+ end
+ end
+
+ def copy(dest)
+ lstat
+ case
+ when file?
+ copy_file dest
+ when directory?
+ if !File.exist?(dest) and descendant_directory?(dest, path)
+ raise ArgumentError, "cannot copy directory %s to itself %s" % [path, dest]
+ end
+ begin
+ Dir.mkdir dest
+ rescue
+ raise unless File.directory?(dest)
+ end
+ when symlink?
+ File.symlink File.readlink(path()), dest
+ when chardev?
+ raise "cannot handle device file" unless File.respond_to?(:mknod)
+ mknod dest, ?c, 0666, lstat().rdev
+ when blockdev?
+ raise "cannot handle device file" unless File.respond_to?(:mknod)
+ mknod dest, ?b, 0666, lstat().rdev
+ when socket?
+ raise "cannot handle socket" unless File.respond_to?(:mknod)
+ mknod dest, nil, lstat().mode, 0
+ when pipe?
+ raise "cannot handle FIFO" unless File.respond_to?(:mkfifo)
+ mkfifo dest, 0666
+ when door?
+ raise "cannot handle door: #{path()}"
+ else
+ raise "unknown file type: #{path()}"
+ end
+ end
+
+ def copy_file(dest)
+ File.open(path()) do |s|
+ File.open(dest, 'wb', s.stat.mode) do |f|
+ IO.copy_stream(s, f)
+ end
+ end
+ end
+
+ def copy_metadata(path)
+ st = lstat()
+ if !st.symlink?
+ File.utime st.atime, st.mtime, path
+ end
+ mode = st.mode
+ begin
+ if st.symlink?
+ begin
+ File.lchown st.uid, st.gid, path
+ rescue NotImplementedError
+ end
+ else
+ File.chown st.uid, st.gid, path
+ end
+ rescue Errno::EPERM, Errno::EACCES
+ # clear setuid/setgid
+ mode &= 01777
+ end
+ if st.symlink?
+ begin
+ File.lchmod mode, path
+ rescue NotImplementedError
+ end
+ else
+ File.chmod mode, path
+ end
+ end
+
+ def remove
+ if directory?
+ remove_dir1
+ else
+ remove_file
+ end
+ end
+
+ def remove_dir1
+ platform_support {
+ Dir.rmdir path().chomp(?/)
+ }
+ end
+
+ def remove_file
+ platform_support {
+ File.unlink path
+ }
+ end
+
+ def platform_support
+ return yield unless fu_windows?
+ first_time_p = true
+ begin
+ yield
+ rescue Errno::ENOENT
+ raise
+ rescue => err
+ if first_time_p
+ first_time_p = false
+ begin
+ File.chmod 0700, path() # Windows does not have symlink
+ retry
+ rescue SystemCallError
+ end
+ end
+ raise err
+ end
+ end
+
+ def preorder_traverse
+ stack = [self]
+ while ent = stack.pop
+ yield ent
+ stack.concat ent.entries.reverse if ent.directory?
+ end
+ end
+
+ alias traverse preorder_traverse
+
+ def postorder_traverse
+ if directory?
+ entries().each do |ent|
+ ent.postorder_traverse do |e|
+ yield e
+ end
+ end
+ end
+ ensure
+ yield self
+ end
+
+ def wrap_traverse(pre, post)
+ pre.call self
+ if directory?
+ entries.each do |ent|
+ ent.wrap_traverse pre, post
+ end
+ end
+ post.call self
+ end
+
+ private
+
+ $fileutils_rb_have_lchmod = nil
+
+ def have_lchmod?
+ # This is not MT-safe, but it does not matter.
+ if $fileutils_rb_have_lchmod == nil
+ $fileutils_rb_have_lchmod = check_have_lchmod?
+ end
+ $fileutils_rb_have_lchmod
+ end
+
+ def check_have_lchmod?
+ return false unless File.respond_to?(:lchmod)
+ File.lchmod 0
+ return true
+ rescue NotImplementedError
+ return false
+ end
+
+ $fileutils_rb_have_lchown = nil
+
+ def have_lchown?
+ # This is not MT-safe, but it does not matter.
+ if $fileutils_rb_have_lchown == nil
+ $fileutils_rb_have_lchown = check_have_lchown?
+ end
+ $fileutils_rb_have_lchown
+ end
+
+ def check_have_lchown?
+ return false unless File.respond_to?(:lchown)
+ File.lchown nil, nil
+ return true
+ rescue NotImplementedError
+ return false
+ end
+
+ def join(dir, base)
+ return File.path(dir) if not base or base == '.'
+ return File.path(base) if not dir or dir == '.'
+ File.join(dir, base)
+ end
+
+ if File::ALT_SEPARATOR
+ DIRECTORY_TERM = "(?=[/#{Regexp.quote(File::ALT_SEPARATOR)}]|\\z)"
+ else
+ DIRECTORY_TERM = "(?=/|\\z)"
+ end
+ SYSCASE = File::FNM_SYSCASE.nonzero? ? "-i" : ""
+
+ def descendant_directory?(descendant, ascendant)
+ /\A(?#{SYSCASE}:#{Regexp.quote(ascendant)})#{DIRECTORY_TERM}/ =~ File.dirname(descendant)
+ end
+ end # class Entry_
+
+ def fu_list(arg) #:nodoc:
+ [arg].flatten.map {|path| File.path(path) }
+ end
+ private_module_function :fu_list
+
+ def fu_each_src_dest(src, dest) #:nodoc:
+ fu_each_src_dest0(src, dest) do |s, d|
+ raise ArgumentError, "same file: #{s} and #{d}" if fu_same?(s, d)
+ yield s, d
+ end
+ end
+ private_module_function :fu_each_src_dest
+
+ def fu_each_src_dest0(src, dest) #:nodoc:
+ if tmp = Array.try_convert(src)
+ tmp.each do |s|
+ s = File.path(s)
+ yield s, File.join(dest, File.basename(s))
+ end
+ else
+ src = File.path(src)
+ if File.directory?(dest)
+ yield src, File.join(dest, File.basename(src))
+ else
+ yield src, File.path(dest)
+ end
+ end
+ end
+ private_module_function :fu_each_src_dest0
+
+ def fu_same?(a, b) #:nodoc:
+ File.identical?(a, b)
+ end
+ private_module_function :fu_same?
+
+ @fileutils_output = $stderr
+ @fileutils_label = ''
+
+ def fu_output_message(msg) #:nodoc:
+ @fileutils_output ||= $stderr
+ @fileutils_label ||= ''
+ @fileutils_output.puts @fileutils_label + msg
+ end
+ private_module_function :fu_output_message
+
+ # This hash table holds command options.
+ OPT_TABLE = {} #:nodoc: internal use only
+ (private_instance_methods & methods(false)).inject(OPT_TABLE) {|tbl, name|
+ (tbl[name.to_s] = instance_method(name).parameters).map! {|t, n| n if t == :key}.compact!
+ tbl
+ }
+
+ #
+ # Returns an Array of method names which have any options.
+ #
+ # p Bundler::FileUtils.commands #=> ["chmod", "cp", "cp_r", "install", ...]
+ #
+ def self.commands
+ OPT_TABLE.keys
+ end
+
+ #
+ # Returns an Array of option names.
+ #
+ # p Bundler::FileUtils.options #=> ["noop", "force", "verbose", "preserve", "mode"]
+ #
+ def self.options
+ OPT_TABLE.values.flatten.uniq.map {|sym| sym.to_s }
+ end
+
+ #
+ # Returns true if the method +mid+ have an option +opt+.
+ #
+ # p Bundler::FileUtils.have_option?(:cp, :noop) #=> true
+ # p Bundler::FileUtils.have_option?(:rm, :force) #=> true
+ # p Bundler::FileUtils.have_option?(:rm, :preserve) #=> false
+ #
+ def self.have_option?(mid, opt)
+ li = OPT_TABLE[mid.to_s] or raise ArgumentError, "no such method: #{mid}"
+ li.include?(opt)
+ end
+
+ #
+ # Returns an Array of option names of the method +mid+.
+ #
+ # p Bundler::FileUtils.options_of(:rm) #=> ["noop", "verbose", "force"]
+ #
+ def self.options_of(mid)
+ OPT_TABLE[mid.to_s].map {|sym| sym.to_s }
+ end
+
+ #
+ # Returns an Array of method names which have the option +opt+.
+ #
+ # p Bundler::FileUtils.collect_method(:preserve) #=> ["cp", "cp_r", "copy", "install"]
+ #
+ def self.collect_method(opt)
+ OPT_TABLE.keys.select {|m| OPT_TABLE[m].include?(opt) }
+ end
+
+ LOW_METHODS = singleton_methods(false) - collect_method(:noop).map(&:intern)
+ module LowMethods
+ private
+ def _do_nothing(*)end
+ ::Bundler::FileUtils::LOW_METHODS.map {|name| alias_method name, :_do_nothing}
+ end
+
+ METHODS = singleton_methods() - [:private_module_function,
+ :commands, :options, :have_option?, :options_of, :collect_method]
+
+ #
+ # This module has all methods of Bundler::FileUtils module, but it outputs messages
+ # before acting. This equates to passing the :verbose flag to
+ # methods in Bundler::FileUtils.
+ #
+ module Verbose
+ include Bundler::FileUtils
+ @fileutils_output = $stderr
+ @fileutils_label = ''
+ names = ::Bundler::FileUtils.collect_method(:verbose)
+ names.each do |name|
+ module_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{name}(*args, **options)
+ super(*args, **options, verbose: true)
+ end
+ EOS
+ end
+ private(*names)
+ extend self
+ class << self
+ public(*::Bundler::FileUtils::METHODS)
+ end
+ end
+
+ #
+ # This module has all methods of Bundler::FileUtils module, but never changes
+ # files/directories. This equates to passing the :noop flag
+ # to methods in Bundler::FileUtils.
+ #
+ module NoWrite
+ include Bundler::FileUtils
+ include LowMethods
+ @fileutils_output = $stderr
+ @fileutils_label = ''
+ names = ::Bundler::FileUtils.collect_method(:noop)
+ names.each do |name|
+ module_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{name}(*args, **options)
+ super(*args, **options, noop: true)
+ end
+ EOS
+ end
+ private(*names)
+ extend self
+ class << self
+ public(*::Bundler::FileUtils::METHODS)
+ end
+ end
+
+ #
+ # This module has all methods of Bundler::FileUtils module, but never changes
+ # files/directories, with printing message before acting.
+ # This equates to passing the :noop and :verbose flag
+ # to methods in Bundler::FileUtils.
+ #
+ module DryRun
+ include Bundler::FileUtils
+ include LowMethods
+ @fileutils_output = $stderr
+ @fileutils_label = ''
+ names = ::Bundler::FileUtils.collect_method(:noop)
+ names.each do |name|
+ module_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{name}(*args, **options)
+ super(*args, **options, noop: true, verbose: true)
+ end
+ EOS
+ end
+ private(*names)
+ extend self
+ class << self
+ public(*::Bundler::FileUtils::METHODS)
+ end
+ end
+
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo.rb b/lib/bundler/vendor/molinillo/lib/molinillo.rb
new file mode 100644
index 00000000000000..9e2867144fb485
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+require 'bundler/vendor/molinillo/lib/molinillo/compatibility'
+require 'bundler/vendor/molinillo/lib/molinillo/gem_metadata'
+require 'bundler/vendor/molinillo/lib/molinillo/errors'
+require 'bundler/vendor/molinillo/lib/molinillo/resolver'
+require 'bundler/vendor/molinillo/lib/molinillo/modules/ui'
+require 'bundler/vendor/molinillo/lib/molinillo/modules/specification_provider'
+
+# Bundler::Molinillo is a generic dependency resolution algorithm.
+module Bundler::Molinillo
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/compatibility.rb b/lib/bundler/vendor/molinillo/lib/molinillo/compatibility.rb
new file mode 100644
index 00000000000000..3eba8e4083fe7c
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/compatibility.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+module Bundler::Molinillo
+ # Hacks needed for old Ruby versions.
+ module Compatibility
+ module_function
+
+ if [].respond_to?(:flat_map)
+ # Flat map
+ # @param [Enumerable] enum an enumerable object
+ # @block the block to flat-map with
+ # @return The enum, flat-mapped
+ def flat_map(enum, &blk)
+ enum.flat_map(&blk)
+ end
+ else
+ # Flat map
+ # @param [Enumerable] enum an enumerable object
+ # @block the block to flat-map with
+ # @return The enum, flat-mapped
+ def flat_map(enum, &blk)
+ enum.map(&blk).flatten(1)
+ end
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/delegates/resolution_state.rb b/lib/bundler/vendor/molinillo/lib/molinillo/delegates/resolution_state.rb
new file mode 100644
index 00000000000000..bcacf35243bc01
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/delegates/resolution_state.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+module Bundler::Molinillo
+ # @!visibility private
+ module Delegates
+ # Delegates all {Bundler::Molinillo::ResolutionState} methods to a `#state` property.
+ module ResolutionState
+ # (see Bundler::Molinillo::ResolutionState#name)
+ def name
+ current_state = state || Bundler::Molinillo::ResolutionState.empty
+ current_state.name
+ end
+
+ # (see Bundler::Molinillo::ResolutionState#requirements)
+ def requirements
+ current_state = state || Bundler::Molinillo::ResolutionState.empty
+ current_state.requirements
+ end
+
+ # (see Bundler::Molinillo::ResolutionState#activated)
+ def activated
+ current_state = state || Bundler::Molinillo::ResolutionState.empty
+ current_state.activated
+ end
+
+ # (see Bundler::Molinillo::ResolutionState#requirement)
+ def requirement
+ current_state = state || Bundler::Molinillo::ResolutionState.empty
+ current_state.requirement
+ end
+
+ # (see Bundler::Molinillo::ResolutionState#possibilities)
+ def possibilities
+ current_state = state || Bundler::Molinillo::ResolutionState.empty
+ current_state.possibilities
+ end
+
+ # (see Bundler::Molinillo::ResolutionState#depth)
+ def depth
+ current_state = state || Bundler::Molinillo::ResolutionState.empty
+ current_state.depth
+ end
+
+ # (see Bundler::Molinillo::ResolutionState#conflicts)
+ def conflicts
+ current_state = state || Bundler::Molinillo::ResolutionState.empty
+ current_state.conflicts
+ end
+
+ # (see Bundler::Molinillo::ResolutionState#unused_unwind_options)
+ def unused_unwind_options
+ current_state = state || Bundler::Molinillo::ResolutionState.empty
+ current_state.unused_unwind_options
+ end
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/delegates/specification_provider.rb b/lib/bundler/vendor/molinillo/lib/molinillo/delegates/specification_provider.rb
new file mode 100644
index 00000000000000..ec9c770a2889fd
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/delegates/specification_provider.rb
@@ -0,0 +1,81 @@
+# frozen_string_literal: true
+
+module Bundler::Molinillo
+ module Delegates
+ # Delegates all {Bundler::Molinillo::SpecificationProvider} methods to a
+ # `#specification_provider` property.
+ module SpecificationProvider
+ # (see Bundler::Molinillo::SpecificationProvider#search_for)
+ def search_for(dependency)
+ with_no_such_dependency_error_handling do
+ specification_provider.search_for(dependency)
+ end
+ end
+
+ # (see Bundler::Molinillo::SpecificationProvider#dependencies_for)
+ def dependencies_for(specification)
+ with_no_such_dependency_error_handling do
+ specification_provider.dependencies_for(specification)
+ end
+ end
+
+ # (see Bundler::Molinillo::SpecificationProvider#requirement_satisfied_by?)
+ def requirement_satisfied_by?(requirement, activated, spec)
+ with_no_such_dependency_error_handling do
+ specification_provider.requirement_satisfied_by?(requirement, activated, spec)
+ end
+ end
+
+ # (see Bundler::Molinillo::SpecificationProvider#name_for)
+ def name_for(dependency)
+ with_no_such_dependency_error_handling do
+ specification_provider.name_for(dependency)
+ end
+ end
+
+ # (see Bundler::Molinillo::SpecificationProvider#name_for_explicit_dependency_source)
+ def name_for_explicit_dependency_source
+ with_no_such_dependency_error_handling do
+ specification_provider.name_for_explicit_dependency_source
+ end
+ end
+
+ # (see Bundler::Molinillo::SpecificationProvider#name_for_locking_dependency_source)
+ def name_for_locking_dependency_source
+ with_no_such_dependency_error_handling do
+ specification_provider.name_for_locking_dependency_source
+ end
+ end
+
+ # (see Bundler::Molinillo::SpecificationProvider#sort_dependencies)
+ def sort_dependencies(dependencies, activated, conflicts)
+ with_no_such_dependency_error_handling do
+ specification_provider.sort_dependencies(dependencies, activated, conflicts)
+ end
+ end
+
+ # (see Bundler::Molinillo::SpecificationProvider#allow_missing?)
+ def allow_missing?(dependency)
+ with_no_such_dependency_error_handling do
+ specification_provider.allow_missing?(dependency)
+ end
+ end
+
+ private
+
+ # Ensures any raised {NoSuchDependencyError} has its
+ # {NoSuchDependencyError#required_by} set.
+ # @yield
+ def with_no_such_dependency_error_handling
+ yield
+ rescue NoSuchDependencyError => error
+ if state
+ vertex = activated.vertex_named(name_for(error.dependency))
+ error.required_by += vertex.incoming_edges.map { |e| e.origin.name }
+ error.required_by << name_for_explicit_dependency_source unless vertex.explicit_requirements.empty?
+ end
+ raise
+ end
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb
new file mode 100644
index 00000000000000..677a8bd916509e
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb
@@ -0,0 +1,223 @@
+# frozen_string_literal: true
+
+require 'set'
+require 'tsort'
+
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/log'
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/vertex'
+
+module Bundler::Molinillo
+ # A directed acyclic graph that is tuned to hold named dependencies
+ class DependencyGraph
+ include Enumerable
+
+ # Enumerates through the vertices of the graph.
+ # @return [Array] The graph's vertices.
+ def each
+ return vertices.values.each unless block_given?
+ vertices.values.each { |v| yield v }
+ end
+
+ include TSort
+
+ # @!visibility private
+ alias tsort_each_node each
+
+ # @!visibility private
+ def tsort_each_child(vertex, &block)
+ vertex.successors.each(&block)
+ end
+
+ # Topologically sorts the given vertices.
+ # @param [Enumerable] vertices the vertices to be sorted, which must
+ # all belong to the same graph.
+ # @return [Array] The sorted vertices.
+ def self.tsort(vertices)
+ TSort.tsort(
+ lambda { |b| vertices.each(&b) },
+ lambda { |v, &b| (v.successors & vertices).each(&b) }
+ )
+ end
+
+ # A directed edge of a {DependencyGraph}
+ # @attr [Vertex] origin The origin of the directed edge
+ # @attr [Vertex] destination The destination of the directed edge
+ # @attr [Object] requirement The requirement the directed edge represents
+ Edge = Struct.new(:origin, :destination, :requirement)
+
+ # @return [{String => Vertex}] the vertices of the dependency graph, keyed
+ # by {Vertex#name}
+ attr_reader :vertices
+
+ # @return [Log] the op log for this graph
+ attr_reader :log
+
+ # Initializes an empty dependency graph
+ def initialize
+ @vertices = {}
+ @log = Log.new
+ end
+
+ # Tags the current state of the dependency as the given tag
+ # @param [Object] tag an opaque tag for the current state of the graph
+ # @return [Void]
+ def tag(tag)
+ log.tag(self, tag)
+ end
+
+ # Rewinds the graph to the state tagged as `tag`
+ # @param [Object] tag the tag to rewind to
+ # @return [Void]
+ def rewind_to(tag)
+ log.rewind_to(self, tag)
+ end
+
+ # Initializes a copy of a {DependencyGraph}, ensuring that all {#vertices}
+ # are properly copied.
+ # @param [DependencyGraph] other the graph to copy.
+ def initialize_copy(other)
+ super
+ @vertices = {}
+ @log = other.log.dup
+ traverse = lambda do |new_v, old_v|
+ return if new_v.outgoing_edges.size == old_v.outgoing_edges.size
+ old_v.outgoing_edges.each do |edge|
+ destination = add_vertex(edge.destination.name, edge.destination.payload)
+ add_edge_no_circular(new_v, destination, edge.requirement)
+ traverse.call(destination, edge.destination)
+ end
+ end
+ other.vertices.each do |name, vertex|
+ new_vertex = add_vertex(name, vertex.payload, vertex.root?)
+ new_vertex.explicit_requirements.replace(vertex.explicit_requirements)
+ traverse.call(new_vertex, vertex)
+ end
+ end
+
+ # @return [String] a string suitable for debugging
+ def inspect
+ "#{self.class}:#{vertices.values.inspect}"
+ end
+
+ # @param [Hash] options options for dot output.
+ # @return [String] Returns a dot format representation of the graph
+ def to_dot(options = {})
+ edge_label = options.delete(:edge_label)
+ raise ArgumentError, "Unknown options: #{options.keys}" unless options.empty?
+
+ dot_vertices = []
+ dot_edges = []
+ vertices.each do |n, v|
+ dot_vertices << " #{n} [label=\"{#{n}|#{v.payload}}\"]"
+ v.outgoing_edges.each do |e|
+ label = edge_label ? edge_label.call(e) : e.requirement
+ dot_edges << " #{e.origin.name} -> #{e.destination.name} [label=#{label.to_s.dump}]"
+ end
+ end
+
+ dot_vertices.uniq!
+ dot_vertices.sort!
+ dot_edges.uniq!
+ dot_edges.sort!
+
+ dot = dot_vertices.unshift('digraph G {').push('') + dot_edges.push('}')
+ dot.join("\n")
+ end
+
+ # @return [Boolean] whether the two dependency graphs are equal, determined
+ # by a recursive traversal of each {#root_vertices} and its
+ # {Vertex#successors}
+ def ==(other)
+ return false unless other
+ return true if equal?(other)
+ vertices.each do |name, vertex|
+ other_vertex = other.vertex_named(name)
+ return false unless other_vertex
+ return false unless vertex.payload == other_vertex.payload
+ return false unless other_vertex.successors.to_set == vertex.successors.to_set
+ end
+ end
+
+ # @param [String] name
+ # @param [Object] payload
+ # @param [Array] parent_names
+ # @param [Object] requirement the requirement that is requiring the child
+ # @return [void]
+ def add_child_vertex(name, payload, parent_names, requirement)
+ root = !parent_names.delete(nil) { true }
+ vertex = add_vertex(name, payload, root)
+ vertex.explicit_requirements << requirement if root
+ parent_names.each do |parent_name|
+ parent_vertex = vertex_named(parent_name)
+ add_edge(parent_vertex, vertex, requirement)
+ end
+ vertex
+ end
+
+ # Adds a vertex with the given name, or updates the existing one.
+ # @param [String] name
+ # @param [Object] payload
+ # @return [Vertex] the vertex that was added to `self`
+ def add_vertex(name, payload, root = false)
+ log.add_vertex(self, name, payload, root)
+ end
+
+ # Detaches the {#vertex_named} `name` {Vertex} from the graph, recursively
+ # removing any non-root vertices that were orphaned in the process
+ # @param [String] name
+ # @return [Array] the vertices which have been detached
+ def detach_vertex_named(name)
+ log.detach_vertex_named(self, name)
+ end
+
+ # @param [String] name
+ # @return [Vertex,nil] the vertex with the given name
+ def vertex_named(name)
+ vertices[name]
+ end
+
+ # @param [String] name
+ # @return [Vertex,nil] the root vertex with the given name
+ def root_vertex_named(name)
+ vertex = vertex_named(name)
+ vertex if vertex && vertex.root?
+ end
+
+ # Adds a new {Edge} to the dependency graph
+ # @param [Vertex] origin
+ # @param [Vertex] destination
+ # @param [Object] requirement the requirement that this edge represents
+ # @return [Edge] the added edge
+ def add_edge(origin, destination, requirement)
+ if destination.path_to?(origin)
+ raise CircularDependencyError.new([origin, destination])
+ end
+ add_edge_no_circular(origin, destination, requirement)
+ end
+
+ # Deletes an {Edge} from the dependency graph
+ # @param [Edge] edge
+ # @return [Void]
+ def delete_edge(edge)
+ log.delete_edge(self, edge.origin.name, edge.destination.name, edge.requirement)
+ end
+
+ # Sets the payload of the vertex with the given name
+ # @param [String] name the name of the vertex
+ # @param [Object] payload the payload
+ # @return [Void]
+ def set_payload(name, payload)
+ log.set_payload(self, name, payload)
+ end
+
+ private
+
+ # Adds a new {Edge} to the dependency graph without checking for
+ # circularity.
+ # @param (see #add_edge)
+ # @return (see #add_edge)
+ def add_edge_no_circular(origin, destination, requirement)
+ log.add_edge_no_circular(self, origin.name, destination.name, requirement)
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/action.rb b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/action.rb
new file mode 100644
index 00000000000000..c04c7eec9c783b
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/action.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+module Bundler::Molinillo
+ class DependencyGraph
+ # An action that modifies a {DependencyGraph} that is reversible.
+ # @abstract
+ class Action
+ # rubocop:disable Lint/UnusedMethodArgument
+
+ # @return [Symbol] The name of the action.
+ def self.action_name
+ raise 'Abstract'
+ end
+
+ # Performs the action on the given graph.
+ # @param [DependencyGraph] graph the graph to perform the action on.
+ # @return [Void]
+ def up(graph)
+ raise 'Abstract'
+ end
+
+ # Reverses the action on the given graph.
+ # @param [DependencyGraph] graph the graph to reverse the action on.
+ # @return [Void]
+ def down(graph)
+ raise 'Abstract'
+ end
+
+ # @return [Action,Nil] The previous action
+ attr_accessor :previous
+
+ # @return [Action,Nil] The next action
+ attr_accessor :next
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/add_edge_no_circular.rb b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/add_edge_no_circular.rb
new file mode 100644
index 00000000000000..9849aea2fee2b0
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/add_edge_no_circular.rb
@@ -0,0 +1,66 @@
+# frozen_string_literal: true
+
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/action'
+module Bundler::Molinillo
+ class DependencyGraph
+ # @!visibility private
+ # (see DependencyGraph#add_edge_no_circular)
+ class AddEdgeNoCircular < Action
+ # @!group Action
+
+ # (see Action.action_name)
+ def self.action_name
+ :add_vertex
+ end
+
+ # (see Action#up)
+ def up(graph)
+ edge = make_edge(graph)
+ edge.origin.outgoing_edges << edge
+ edge.destination.incoming_edges << edge
+ edge
+ end
+
+ # (see Action#down)
+ def down(graph)
+ edge = make_edge(graph)
+ delete_first(edge.origin.outgoing_edges, edge)
+ delete_first(edge.destination.incoming_edges, edge)
+ end
+
+ # @!group AddEdgeNoCircular
+
+ # @return [String] the name of the origin of the edge
+ attr_reader :origin
+
+ # @return [String] the name of the destination of the edge
+ attr_reader :destination
+
+ # @return [Object] the requirement that the edge represents
+ attr_reader :requirement
+
+ # @param [DependencyGraph] graph the graph to find vertices from
+ # @return [Edge] The edge this action adds
+ def make_edge(graph)
+ Edge.new(graph.vertex_named(origin), graph.vertex_named(destination), requirement)
+ end
+
+ # Initialize an action to add an edge to a dependency graph
+ # @param [String] origin the name of the origin of the edge
+ # @param [String] destination the name of the destination of the edge
+ # @param [Object] requirement the requirement that the edge represents
+ def initialize(origin, destination, requirement)
+ @origin = origin
+ @destination = destination
+ @requirement = requirement
+ end
+
+ private
+
+ def delete_first(array, item)
+ return unless index = array.index(item)
+ array.delete_at(index)
+ end
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/add_vertex.rb b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/add_vertex.rb
new file mode 100644
index 00000000000000..0a1e08255bccd5
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/add_vertex.rb
@@ -0,0 +1,62 @@
+# frozen_string_literal: true
+
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/action'
+module Bundler::Molinillo
+ class DependencyGraph
+ # @!visibility private
+ # (see DependencyGraph#add_vertex)
+ class AddVertex < Action # :nodoc:
+ # @!group Action
+
+ # (see Action.action_name)
+ def self.action_name
+ :add_vertex
+ end
+
+ # (see Action#up)
+ def up(graph)
+ if existing = graph.vertices[name]
+ @existing_payload = existing.payload
+ @existing_root = existing.root
+ end
+ vertex = existing || Vertex.new(name, payload)
+ graph.vertices[vertex.name] = vertex
+ vertex.payload ||= payload
+ vertex.root ||= root
+ vertex
+ end
+
+ # (see Action#down)
+ def down(graph)
+ if defined?(@existing_payload)
+ vertex = graph.vertices[name]
+ vertex.payload = @existing_payload
+ vertex.root = @existing_root
+ else
+ graph.vertices.delete(name)
+ end
+ end
+
+ # @!group AddVertex
+
+ # @return [String] the name of the vertex
+ attr_reader :name
+
+ # @return [Object] the payload for the vertex
+ attr_reader :payload
+
+ # @return [Boolean] whether the vertex is root or not
+ attr_reader :root
+
+ # Initialize an action to add a vertex to a dependency graph
+ # @param [String] name the name of the vertex
+ # @param [Object] payload the payload for the vertex
+ # @param [Boolean] root whether the vertex is root or not
+ def initialize(name, payload, root)
+ @name = name
+ @payload = payload
+ @root = root
+ end
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/delete_edge.rb b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/delete_edge.rb
new file mode 100644
index 00000000000000..1d9f4b327d10d3
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/delete_edge.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/action'
+module Bundler::Molinillo
+ class DependencyGraph
+ # @!visibility private
+ # (see DependencyGraph#delete_edge)
+ class DeleteEdge < Action
+ # @!group Action
+
+ # (see Action.action_name)
+ def self.action_name
+ :delete_edge
+ end
+
+ # (see Action#up)
+ def up(graph)
+ edge = make_edge(graph)
+ edge.origin.outgoing_edges.delete(edge)
+ edge.destination.incoming_edges.delete(edge)
+ end
+
+ # (see Action#down)
+ def down(graph)
+ edge = make_edge(graph)
+ edge.origin.outgoing_edges << edge
+ edge.destination.incoming_edges << edge
+ edge
+ end
+
+ # @!group DeleteEdge
+
+ # @return [String] the name of the origin of the edge
+ attr_reader :origin_name
+
+ # @return [String] the name of the destination of the edge
+ attr_reader :destination_name
+
+ # @return [Object] the requirement that the edge represents
+ attr_reader :requirement
+
+ # @param [DependencyGraph] graph the graph to find vertices from
+ # @return [Edge] The edge this action adds
+ def make_edge(graph)
+ Edge.new(
+ graph.vertex_named(origin_name),
+ graph.vertex_named(destination_name),
+ requirement
+ )
+ end
+
+ # Initialize an action to add an edge to a dependency graph
+ # @param [String] origin_name the name of the origin of the edge
+ # @param [String] destination_name the name of the destination of the edge
+ # @param [Object] requirement the requirement that the edge represents
+ def initialize(origin_name, destination_name, requirement)
+ @origin_name = origin_name
+ @destination_name = destination_name
+ @requirement = requirement
+ end
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/detach_vertex_named.rb b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/detach_vertex_named.rb
new file mode 100644
index 00000000000000..385dcbdd064bd0
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/detach_vertex_named.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/action'
+module Bundler::Molinillo
+ class DependencyGraph
+ # @!visibility private
+ # @see DependencyGraph#detach_vertex_named
+ class DetachVertexNamed < Action
+ # @!group Action
+
+ # (see Action#name)
+ def self.action_name
+ :add_vertex
+ end
+
+ # (see Action#up)
+ def up(graph)
+ return [] unless @vertex = graph.vertices.delete(name)
+
+ removed_vertices = [@vertex]
+ @vertex.outgoing_edges.each do |e|
+ v = e.destination
+ v.incoming_edges.delete(e)
+ if !v.root? && v.incoming_edges.empty?
+ removed_vertices.concat graph.detach_vertex_named(v.name)
+ end
+ end
+
+ @vertex.incoming_edges.each do |e|
+ v = e.origin
+ v.outgoing_edges.delete(e)
+ end
+
+ removed_vertices
+ end
+
+ # (see Action#down)
+ def down(graph)
+ return unless @vertex
+ graph.vertices[@vertex.name] = @vertex
+ @vertex.outgoing_edges.each do |e|
+ e.destination.incoming_edges << e
+ end
+ @vertex.incoming_edges.each do |e|
+ e.origin.outgoing_edges << e
+ end
+ end
+
+ # @!group DetachVertexNamed
+
+ # @return [String] the name of the vertex to detach
+ attr_reader :name
+
+ # Initialize an action to detach a vertex from a dependency graph
+ # @param [String] name the name of the vertex to detach
+ def initialize(name)
+ @name = name
+ end
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/log.rb b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/log.rb
new file mode 100644
index 00000000000000..8582dd19c16c63
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/log.rb
@@ -0,0 +1,126 @@
+# frozen_string_literal: true
+
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/add_edge_no_circular'
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/add_vertex'
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/delete_edge'
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/detach_vertex_named'
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/set_payload'
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/tag'
+
+module Bundler::Molinillo
+ class DependencyGraph
+ # A log for dependency graph actions
+ class Log
+ # Initializes an empty log
+ def initialize
+ @current_action = @first_action = nil
+ end
+
+ # @!macro [new] action
+ # {include:DependencyGraph#$0}
+ # @param [Graph] graph the graph to perform the action on
+ # @param (see DependencyGraph#$0)
+ # @return (see DependencyGraph#$0)
+
+ # @macro action
+ def tag(graph, tag)
+ push_action(graph, Tag.new(tag))
+ end
+
+ # @macro action
+ def add_vertex(graph, name, payload, root)
+ push_action(graph, AddVertex.new(name, payload, root))
+ end
+
+ # @macro action
+ def detach_vertex_named(graph, name)
+ push_action(graph, DetachVertexNamed.new(name))
+ end
+
+ # @macro action
+ def add_edge_no_circular(graph, origin, destination, requirement)
+ push_action(graph, AddEdgeNoCircular.new(origin, destination, requirement))
+ end
+
+ # {include:DependencyGraph#delete_edge}
+ # @param [Graph] graph the graph to perform the action on
+ # @param [String] origin_name
+ # @param [String] destination_name
+ # @param [Object] requirement
+ # @return (see DependencyGraph#delete_edge)
+ def delete_edge(graph, origin_name, destination_name, requirement)
+ push_action(graph, DeleteEdge.new(origin_name, destination_name, requirement))
+ end
+
+ # @macro action
+ def set_payload(graph, name, payload)
+ push_action(graph, SetPayload.new(name, payload))
+ end
+
+ # Pops the most recent action from the log and undoes the action
+ # @param [DependencyGraph] graph
+ # @return [Action] the action that was popped off the log
+ def pop!(graph)
+ return unless action = @current_action
+ unless @current_action = action.previous
+ @first_action = nil
+ end
+ action.down(graph)
+ action
+ end
+
+ extend Enumerable
+
+ # @!visibility private
+ # Enumerates each action in the log
+ # @yield [Action]
+ def each
+ return enum_for unless block_given?
+ action = @first_action
+ loop do
+ break unless action
+ yield action
+ action = action.next
+ end
+ self
+ end
+
+ # @!visibility private
+ # Enumerates each action in the log in reverse order
+ # @yield [Action]
+ def reverse_each
+ return enum_for(:reverse_each) unless block_given?
+ action = @current_action
+ loop do
+ break unless action
+ yield action
+ action = action.previous
+ end
+ self
+ end
+
+ # @macro action
+ def rewind_to(graph, tag)
+ loop do
+ action = pop!(graph)
+ raise "No tag #{tag.inspect} found" unless action
+ break if action.class.action_name == :tag && action.tag == tag
+ end
+ end
+
+ private
+
+ # Adds the given action to the log, running the action
+ # @param [DependencyGraph] graph
+ # @param [Action] action
+ # @return The value returned by `action.up`
+ def push_action(graph, action)
+ action.previous = @current_action
+ @current_action.next = action if @current_action
+ @current_action = action
+ @first_action ||= action
+ action.up(graph)
+ end
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/set_payload.rb b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/set_payload.rb
new file mode 100644
index 00000000000000..37286d104a3995
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/set_payload.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/action'
+module Bundler::Molinillo
+ class DependencyGraph
+ # @!visibility private
+ # @see DependencyGraph#set_payload
+ class SetPayload < Action # :nodoc:
+ # @!group Action
+
+ # (see Action.action_name)
+ def self.action_name
+ :set_payload
+ end
+
+ # (see Action#up)
+ def up(graph)
+ vertex = graph.vertex_named(name)
+ @old_payload = vertex.payload
+ vertex.payload = payload
+ end
+
+ # (see Action#down)
+ def down(graph)
+ graph.vertex_named(name).payload = @old_payload
+ end
+
+ # @!group SetPayload
+
+ # @return [String] the name of the vertex
+ attr_reader :name
+
+ # @return [Object] the payload for the vertex
+ attr_reader :payload
+
+ # Initialize an action to add set the payload for a vertex in a dependency
+ # graph
+ # @param [String] name the name of the vertex
+ # @param [Object] payload the payload for the vertex
+ def initialize(name, payload)
+ @name = name
+ @payload = payload
+ end
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/tag.rb b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/tag.rb
new file mode 100644
index 00000000000000..d6ad16e07a268e
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/tag.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph/action'
+module Bundler::Molinillo
+ class DependencyGraph
+ # @!visibility private
+ # @see DependencyGraph#tag
+ class Tag < Action
+ # @!group Action
+
+ # (see Action.action_name)
+ def self.action_name
+ :tag
+ end
+
+ # (see Action#up)
+ def up(_graph)
+ end
+
+ # (see Action#down)
+ def down(_graph)
+ end
+
+ # @!group Tag
+
+ # @return [Object] An opaque tag
+ attr_reader :tag
+
+ # Initialize an action to tag a state of a dependency graph
+ # @param [Object] tag an opaque tag
+ def initialize(tag)
+ @tag = tag
+ end
+ end
+ end
+end
diff --git a/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/vertex.rb b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/vertex.rb
new file mode 100644
index 00000000000000..7ecdc4b65a1a60
--- /dev/null
+++ b/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/vertex.rb
@@ -0,0 +1,136 @@
+# frozen_string_literal: true
+
+module Bundler::Molinillo
+ class DependencyGraph
+ # A vertex in a {DependencyGraph} that encapsulates a {#name} and a
+ # {#payload}
+ class Vertex
+ # @return [String] the name of the vertex
+ attr_accessor :name
+
+ # @return [Object] the payload the vertex holds
+ attr_accessor :payload
+
+ # @return [Array