Homebrew vendored `ruby` not use it's own GEM_* variables?

Good day, everyone. I’m experiencing some difficulties with brew, it’s rubygems and vendored rubies.
At first, I noticed, that there are several rubies within Homebrew:

 λ ls -la /usr/local/Homebrew/Library/Homebrew/vendor/
total 16
drwxr-xr-x    6 yumitsu  admin   204 Apr 13 18:51 .
drwxr-xr-x  108 yumitsu  admin  3672 Jul  2 11:44 ..
drwxr-xr-x    4 yumitsu  admin   136 Apr 13 18:51 bundle #
drwxr-xr-x    4 yumitsu  admin   136 Jun 18 02:11 portable-ruby #=>expanding to 2.3.7/bin/ruby
λ ls -la /usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/
total 24
drwxr-xr-x  6 yumitsu  admin  204 Apr 13 18:51 .
drwxr-xr-x  4 yumitsu  admin  136 Apr 13 18:51 ..
drwxr-xr-x  9 yumitsu  admin  306 Jul  2 13:46 2.3.0
lrwxr-xr-x  1 yumitsu  admin    5 Apr 13 18:51 2.4.0 -> 2.3.0
lrwxr-xr-x  1 yumitsu  admin    5 Apr 13 18:51 2.5.0 -> 2.3.0
lrwxr-xr-x  1 yumitsu  admin    5 Apr 13 18:51 2.6.0 -> 2.3.0

I should to mention that I use stable rvm for ruby version management.
So, problem occurs when I try to use brew bump-formula-pr: brew trying to install gem dependencies such as nokogiri, and that’s what happens:

Terminal output:

Installing nokogiri 1.10.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/2.3.0/gems/nokogiri-1.10.3/ext/nokogiri
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/bin/ruby -r
./siteconf20190702-98294-hbgyy3.rb extconf.rb --use-system-libraries
--with-libxml2-config=/usr/local/opt/libxml2/lib/pkgconfig --with-lixml2-dir=/usr/local/opt/libxml2
--with-libxslt-dir=/usr/local/opt/libxslt
checking if the C compiler accepts  -I
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/libxml2... no
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
pkg-config could not be used to find libxml-2.0
Please install either `pkg-config` or the pkg-config gem per

    gem install pkg-config -v "~> 1.1"

pkg-config could not be used to find libxslt
Please install either `pkg-config` or the pkg-config gem per

    gem install pkg-config -v "~> 1.1"

pkg-config could not be used to find libexslt
Please install either `pkg-config` or the pkg-config gem per

    gem install pkg-config -v "~> 1.1"

ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is
installed.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/bin/$(RUBY_BASE_NAME)
        --help
        --clean
        --use-system-libraries
        --with-zlib-dir
        --without-zlib-dir
        --with-zlib-include
        --without-zlib-include=${zlib-dir}/include
        --with-zlib-lib
        --without-zlib-lib=${zlib-dir}/lib
        --with-xml2-dir
        --without-xml2-dir
        --with-xml2-include
        --without-xml2-include=${xml2-dir}/include
        --with-xml2-lib
        --without-xml2-lib=${xml2-dir}/lib
        --with-libxml-2.0-config
        --without-libxml-2.0-config
        --with-pkg-config
        --without-pkg-config
        --with-xslt-dir
        --without-xslt-dir
        --with-xslt-include
        --without-xslt-include=${xslt-dir}/include
        --with-xslt-lib
        --without-xslt-lib=${xslt-dir}/lib
        --with-libxslt-config
        --without-libxslt-config
        --with-pkg-config
        --without-pkg-config
        --with-exslt-dir
        --without-exslt-dir
        --with-exslt-include
        --without-exslt-include=${exslt-dir}/include
        --with-exslt-lib
        --without-exslt-lib=${exslt-dir}/lib
        --with-libexslt-config
        --without-libexslt-config
        --with-pkg-config
        --without-pkg-config

To see why this extension failed to compile, please check the mkmf.log which can be found here:

/usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/2.3.0/extensions/universal-darwin-9/2.3.0-static/nokogiri-1.10.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/2.3.0/gems/nokogiri-1.10.3 for inspection.
Results logged to
/usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/2.3.0/extensions/universal-darwin-9/2.3.0-static/nokogiri-1.10.3/gem_make.out

An error occurred while installing nokogiri (1.10.3), and Bundler cannot continue.

(mkmf.log, grepped only errors)

"cc -I/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0 -I/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/ruby/backward -I/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0 -I.  -I/usr/local/opt/portable-readline/include -I/usr/local/opt/portable-libyaml/include -I/usr/local/opt/portable-openssl/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT    -pipe  -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/libxml2  -arch x86_64 -arch i386  -Werror -c conftest.c"
In file included from conftest.c:1:
In file included from /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/ruby.h:33:
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/ruby/ruby.h:929:1: error: attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration [-Werror,-Wignored-attributes]
DEPRECATED_TYPE(("RClass is internal use only"),
^
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0/ruby/config.h:128:49: note: expanded from macro 'DEPRECATED_TYPE'
#define DEPRECATED_TYPE(mesg,x) __attribute__ ((deprecated mesg)) x
                                                ^
--
--
2: 
3: int main() {return 0;}
/* end */

"cc -I/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0 -I/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/ruby/backward -I/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0 -I.  -I/usr/local/opt/portable-readline/include -I/usr/local/opt/portable-libyaml/include -I/usr/local/opt/portable-openssl/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT    -pipe -Wno-error=unused-command-line-argument-hard-error-in-future  -arch x86_64 -arch i386  -Werror -c conftest.c"
error: unknown warning option '-Werror=unused-command-line-argument-hard-error-in-future'; did you mean '-Werror=unused-command-line-argument'? [-Werror,-Wunknown-warning-option]
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main() {return 0;}
--
--
^
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0/ruby/config.h:128:49: note: expanded from macro 'DEPRECATED_TYPE'
#define DEPRECATED_TYPE(mesg,x) __attribute__ ((deprecated mesg)) x
                                                ^
1 warning generated.
clang: error: linker command failed with exit code 1 (use -v to see invocation)
checked program was:
/* begin */
 1: #include "ruby.h"
 2: 
 3: #include <stdlib.h>
--
--
DEPRECATED_TYPE(("RClass is internal use only"),
^
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0/ruby/config.h:128:49: note: expanded from macro 'DEPRECATED_TYPE'
#define DEPRECATED_TYPE(mesg,x) __attribute__ ((deprecated mesg)) x
                                                ^
conftest.c:15:57: error: use of undeclared identifier 'xsltParseStylesheetDoc'
int t(void) { void ((*volatile p)()); p = (void ((*)()))xsltParseStylesheetDoc; return !p; }
                                                        ^
1 warning and 1 error generated.
checked program was:
/* begin */
--
--
DEPRECATED_TYPE(("RClass is internal use only"),
^
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0/ruby/config.h:128:49: note: expanded from macro 'DEPRECATED_TYPE'
#define DEPRECATED_TYPE(mesg,x) __attribute__ ((deprecated mesg)) x
                                                ^
conftest.c:13:57: error: use of undeclared identifier 'xmlHasFeature'
int t(void) { void ((*volatile p)()); p = (void ((*)()))xmlHasFeature; return !p; }
                                                        ^
1 warning and 1 error generated.
checked program was:
/* begin */
--
--
DEPRECATED_TYPE(("RClass is internal use only"),
^
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0/ruby/config.h:128:49: note: expanded from macro 'DEPRECATED_TYPE'
#define DEPRECATED_TYPE(mesg,x) __attribute__ ((deprecated mesg)) x
                                                ^
conftest.c:13:57: error: use of undeclared identifier 'xmlFirstElementChild'
int t(void) { void ((*volatile p)()); p = (void ((*)()))xmlFirstElementChild; return !p; }
                                                        ^
1 warning and 1 error generated.
checked program was:
/* begin */
--
--
DEPRECATED_TYPE(("RClass is internal use only"),
^
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0/ruby/config.h:128:49: note: expanded from macro 'DEPRECATED_TYPE'
#define DEPRECATED_TYPE(mesg,x) __attribute__ ((deprecated mesg)) x
                                                ^
conftest.c:13:57: error: use of undeclared identifier 'xmlRelaxNGSetParserStructuredErrors'
int t(void) { void ((*volatile p)()); p = (void ((*)()))xmlRelaxNGSetParserStructuredErrors; return !p; }
                                                        ^
1 warning and 1 error generated.
checked program was:
/* begin */
--
--
DEPRECATED_TYPE(("RClass is internal use only"),
^
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0/ruby/config.h:128:49: note: expanded from macro 'DEPRECATED_TYPE'
#define DEPRECATED_TYPE(mesg,x) __attribute__ ((deprecated mesg)) x
                                                ^
conftest.c:13:57: error: use of undeclared identifier 'xmlRelaxNGSetParserStructuredErrors'
int t(void) { void ((*volatile p)()); p = (void ((*)()))xmlRelaxNGSetParserStructuredErrors; return !p; }
                                                        ^
1 warning and 1 error generated.
checked program was:
/* begin */
--
--
DEPRECATED_TYPE(("RClass is internal use only"),
^
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0/ruby/config.h:128:49: note: expanded from macro 'DEPRECATED_TYPE'
#define DEPRECATED_TYPE(mesg,x) __attribute__ ((deprecated mesg)) x
                                                ^
conftest.c:13:57: error: use of undeclared identifier 'xmlRelaxNGSetValidStructuredErrors'
int t(void) { void ((*volatile p)()); p = (void ((*)()))xmlRelaxNGSetValidStructuredErrors; return !p; }
                                                        ^
1 warning and 1 error generated.
checked program was:
/* begin */
--
--
DEPRECATED_TYPE(("RClass is internal use only"),
^
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0/ruby/config.h:128:49: note: expanded from macro 'DEPRECATED_TYPE'
#define DEPRECATED_TYPE(mesg,x) __attribute__ ((deprecated mesg)) x
                                                ^
conftest.c:13:57: error: use of undeclared identifier 'xmlSchemaSetValidStructuredErrors'
int t(void) { void ((*volatile p)()); p = (void ((*)()))xmlSchemaSetValidStructuredErrors; return !p; }
                                                        ^
1 warning and 1 error generated.
checked program was:
/* begin */
--
--
DEPRECATED_TYPE(("RClass is internal use only"),
^
/usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/include/ruby-2.3.0/universal-darwin9.0/ruby/config.h:128:49: note: expanded from macro 'DEPRECATED_TYPE'
#define DEPRECATED_TYPE(mesg,x) __attribute__ ((deprecated mesg)) x
                                                ^
conftest.c:13:57: error: use of undeclared identifier 'xmlSchemaSetParserStructuredErrors'
int t(void) { void ((*volatile p)()); p = (void ((*)()))xmlSchemaSetParserStructuredErrors; return !p; }
                                                        ^
1 warning and 1 error generated.
checked program was:
/* begin */

I believe it’s because of $PATH or $GEM_* variables. Or the problem is in brew libxml2 package. I tried to fix it by setting bundler config build.nokogiri to

--with-libxml2-config=/usr/local/opt/libxml2/bin/xml2-config
--with-libxml2-dir=/usr/local/opt/libxml2 

Any helpful thoughts would be appreciated.

For now, i fixed it by installing libxml2 both to /usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/2.3.0/gems and $HOME/.gem/ruby/2.3.0/gems.

Well, now everything became clear.
In ./Library/Homebrew/utils/ruby.sh

if [[ -z "$HOMEBREW_DEVELOPER" ]]
  then
    unset HOMEBREW_RUBY_PATH
  fi

It looks like you can use your own version of ruby with $HOMEBREW_DEVELOPER variable set.
Good news, will try.

Please don’t use your own version of Ruby with Homebrew, it shouldn’t be necessary. Use Homebrew’s built-in portable Ruby or the Ruby that comes with your macOS system.

If you provide some brew doctor and brew config output we may be able to help you figure out why nokogiri is failing.

For now I can only guess why it failed. I think something messed up my $PATH, as well as $GEM_HOME and $GEM_PATH vars, so from time to time brew used different gem locations - sometimes it used $HOME/.gem/ruby/2.3.0/gems, sometimes it was /usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/2.3.0/gems.

After I manually specified $GEM_HOME and $GEM_PATH before install, nokogiri installed just fine.