bson_ext gem install fails on macOS

Viewed 3536

I'm stuck with install bson_ext gem with 1.5.1 version. I've tried almost everything what I found on web but nothing helps. I tried to reinstall ruby, rvm and so on.

I am using:

  • RVM
  • MacOs Catalina 10.15.6
  • ruby 2.6.5

and after bundle install I got log like this:

Fetching bson_ext 1.5.1
Installing bson_ext 1.5.1 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/lukasz/.rvm/gems/ruby-2.6.5/gems/bson_ext-1.5.1/ext/cbson
/Users/lukasz/.rvm/rubies/ruby-2.6.5/bin/ruby -I /Users/lukasz/.rvm/rubies/ruby-2.6.5/lib/ruby/site_ruby/2.6.0 -r ./siteconf20201001-15946-1sze5ll.rb extconf.rb
checking for asprintf()... yes
checking for ruby/st.h... yes
checking for ruby/regex.h... yes
checking for ruby/encoding.h... yes
creating Makefile

current directory: /Users/lukasz/.rvm/gems/ruby-2.6.5/gems/bson_ext-1.5.1/ext/cbson
make "DESTDIR=" clean

current directory: /Users/lukasz/.rvm/gems/ruby-2.6.5/gems/bson_ext-1.5.1/ext/cbson
make "DESTDIR="
compiling bson_buffer.c
compiling cbson.c
cbson.c:118:36: warning: passing 'char *' to parameter of type 'const unsigned char *' converts between pointers to integer types with different sign [-Wpointer-sign]
    result_t status = check_string(RSTRING_PTR(string), RSTRING_LEN(string),
                                   ^~~~~~~~~~~~~~~~~~~
/Users/lukasz/.rvm/rubies/ruby-2.6.5/include/ruby-2.6.0/ruby/ruby.h:1003:5: note: expanded from macro 'RSTRING_PTR'
    (!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./encoding_helpers.h:26:44: note: passing argument to parameter 'string' here
result_t check_string(const unsigned char* string, const int length,
                                           ^
cbson.c:118:57: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32]
    result_t status = check_string(RSTRING_PTR(string), RSTRING_LEN(string),
                      ~~~~~~~~~~~~                      ^~~~~~~~~~~~~~~~~~~
/Users/lukasz/.rvm/rubies/ruby-2.6.5/include/ruby-2.6.0/ruby/ruby.h:1000:6: note: expanded from macro 'RSTRING_LEN'
     RSTRING_EMBED_LEN(str) : \
     ^~~~~~~~~~~~~~~~~~~~~~
/Users/lukasz/.rvm/rubies/ruby-2.6.5/include/ruby-2.6.0/ruby/ruby.h:996:6: note: expanded from macro 'RSTRING_EMBED_LEN'
     (long)((RBASIC(str)->flags >> RSTRING_EMBED_LEN_SHIFT) & \
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cbson.c:118:57: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32]
    result_t status = check_string(RSTRING_PTR(string), RSTRING_LEN(string),
                      ~~~~~~~~~~~~                      ^~~~~~~~~~~~~~~~~~~
/Users/lukasz/.rvm/rubies/ruby-2.6.5/include/ruby-2.6.0/ruby/ruby.h:1001:28: note: expanded from macro 'RSTRING_LEN'
     RSTRING(str)->as.heap.len)
     ~~~~~~~~~~~~~~~~~~~~~~^~~
cbson.c:128:45: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32]
    SAFE_WRITE(buffer, RSTRING_PTR(string), RSTRING_LEN(string));
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
/Users/lukasz/.rvm/rubies/ruby-2.6.5/include/ruby-2.6.0/ruby/ruby.h:1000:6: note: expanded from macro 'RSTRING_LEN'
     RSTRING_EMBED_LEN(str) : \
     ^
/Users/lukasz/.rvm/rubies/ruby-2.6.5/include/ruby-2.6.0/ruby/ruby.h:996:6: note: expanded from macro 'RSTRING_EMBED_LEN'
     (long)((RBASIC(str)->flags >> RSTRING_EMBED_LEN_SHIFT) & \
     ^
cbson.c:68:46: note: expanded from macro 'SAFE_WRITE'
    if (bson_buffer_write((buffer), (data), (size)) != 0)                    \
        ~~~~~~~~~~~~~~~~~                    ^~~~
cbson.c:128:45: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32]
    SAFE_WRITE(buffer, RSTRING_PTR(string), RSTRING_LEN(string));
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
/Users/lukasz/.rvm/rubies/ruby-2.6.5/include/ruby-2.6.0/ruby/ruby.h:1001:28: note: expanded from macro 'RSTRING_LEN'
     RSTRING(str)->as.heap.len)
                           ^
cbson.c:68:46: note: expanded from macro 'SAFE_WRITE'
    if (bson_buffer_write((buffer), (data), (size)) != 0)                    \
        ~~~~~~~~~~~~~~~~~                    ^~~~
cbson.c:303:20: warning: unused variable 'values' [-Wunused-variable]
            VALUE* values;
                   ^
cbson.c:602:18: error: implicit declaration of function 'bson_buffer_get_max_size' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if (length > bson_buffer_get_max_size(buffer)) {
                 ^
cbson.c:602:18: note: did you mean 'bson_buffer_set_max_size'?
./bson_buffer.h:34:6: note: 'bson_buffer_set_max_size' declared here
void bson_buffer_set_max_size(bson_buffer_t buffer, int max_size);
     ^
6 warnings and 1 error generated.
make: *** [cbson.o] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/lukasz/.rvm/gems/ruby-2.6.5/gems/bson_ext-1.5.1 for inspection.
Results logged to /Users/lukasz/.rvm/gems/ruby-2.6.5/extensions/x86_64-darwin-19/2.6.0/bson_ext-1.5.1/gem_make.out

An error occurred while installing bson_ext (1.5.1), and Bundler cannot continue.
Make sure that `gem install bson_ext -v '1.5.1' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  bson_ext

Anyone has idea what can I try else? In addition I can say that bson_ext with other version like 1.9.1 are installing successfully. But in this project I have to use 1.5.1.

5 Answers

I found a better solution, when you install the gem add this at the end

-- --with-cflags="-Wno-error=implicit-function-declaration"

This worked for me on MacOS 11.2.

gem install bson_ext -v '1.5.1' --source 'https://rubygems.org/' -- --with-cflags="-Wno-error=implicit-function-declaration"

1.5.1 is 9 years old, so there's no reason to believe that it should work. And, indeed, it also fails for me with the same error (Big Sur / 2.7.1 and 2.8.0).

That being said, if you 100% insist on doing this, and have no alternatives such as changing employers and/or a promising career in software archaeology to fall back on, try this:

Go to the folder indicated in the error message, /Users/lukasz/.rvm/gems/ruby-2.6.5/gems/bson_ext-1.5.1 for inspection.

From there, go to ext/cbson and edit the file bson_buffer.h.

You need to insert one line, near to the top of the file:

/* A buffer */
typedef struct bson_buffer* bson_buffer_t;
/* A position in the buffer */
typedef int bson_buffer_position;

/***** THE FOLLOWING IS THE LINE YOU NEED TO INSERT ****/
int bson_buffer_get_max_size(bson_buffer_t buffer); 

 /* Allocate and return a new buffer.
 * Return NULL on allocation failure. */
bson_buffer_t bson_buffer_new(void);

run make in that folder. It should work with just a few warnings.

After that, you still need to install it. Check gem help install for details on the process. This is following its instructions:

@ in bson_ext-1.5.1 $ gem spec ../../cache/bson_ext-1.5.1.gem --ruby > ../../specifications/bson_ext-1.5.1.gemspec

And, indeed:

@ in bson_ext-1.5.1 $ gem list bson_ext
*** LOCAL GEMS ***
bson_ext (1.5.1)

Note that this was in the "original" folder again.

Even after that, just doing requite 'bson_ext' does not work for me and require 'bson' complains about it missing. According to that error message, you might succeed by moving–and I want to be very specific here–something, somewhere. No guarantee this will actually work. Probably not.

Installing 1.3.1 version worked for me

Are you sure you need exactly that version of that gem or it's just default required in your project? If not you can just explicitly set latest/installed version of gem in Gemfile like:

gem list bson_ext                                                                         

*** LOCAL GEMS ***

bson_ext (1.12.5)
# Gemfile
source 'https://rubygems.org'

gem 'bcrypt'
gem 'bson_ext', '~> 1.12.5'

This worked form me under MacOS BigSur when by default bundle install was trying to install bson_ext -v '1.5.1', and exited with error like your:

     RSTRING(str)->as.heap.len)
                           ^
cbson.c:68:46: note: expanded from macro 'SAFE_WRITE'
    if (bson_buffer_write((buffer), (data), (size)) != 0)                    \
        ~~~~~~~~~~~~~~~~~                    ^~~~
cbson.c:303:20: warning: unused variable 'values' [-Wunused-variable]
            VALUE* values;
                   ^
cbson.c:602:18: error: implicit declaration of function 'bson_buffer_get_max_size' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if (length > bson_buffer_get_max_size(buffer)) {
                 ^
cbson.c:602:18: note: did you mean 'bson_buffer_set_max_size'?
./bson_buffer.h:34:6: note: 'bson_buffer_set_max_size' declared here
void bson_buffer_set_max_size(bson_buffer_t buffer, int max_size);
     ^
6 warnings and 1 error generated.
make: *** [cbson.o] Error 1

make failed, exit code 2
Related