Skip to content

Bundler.bin_path writes to the filesystem when it should be an attribute reader #9197

Closed

Description

@p-datadog

Describe the problem as clearly as you can

Bundler.bin_path looks like a simple attribute reader, is documented as such, but performs filesystem writes:

def bin_path
@bin_path ||= begin
path = Bundler.settings[:bin] || "bin"
path = Pathname.new(path).expand_path(root).expand_path
mkdir_p(path)
path
end
end

    # Returns absolute location of where binstubs are installed to.
    def bin_path
      @bin_path ||= begin
        path = Bundler.settings[:bin] || "bin"
        path = Pathname.new(path).expand_path(root).expand_path
        mkdir_p(path)
        path
      end
    end

The writes fail on read-only filesystems, and writing to filesystem is not expected behavior when one is trying to find out "where binstubs are installed to".

More information: DataDog/dd-trace-rb#5137

Did you try upgrading rubygems & bundler?

Problematic code is in master

Post steps to reproduce the problem

big# modprobe zram
big# zramctl -s 1g -f
/dev/zram0
big# mkfs.ext2 /dev/zram0 
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done                            
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 35fbb153-fd4e-4637-b912-5df653bbf4bb
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done

big# mount -o ro /dev/zram0 /mnt/tmp 
big# su - sandbox
big% bundle config bin /mnt/tmp/bin
big% echo 'source "https://rubygems.org"' >Gemfile
big% irb -rbundler                                
irb(main):001:0> Bundler.bin_path
/usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler/shared_helpers.rb:117:in `rescue in filesystem_access': There was an error accessing `/mnt/tmp/bin`. (Bundler::GenericSystemCallError)
The underlying system error is Errno::EROFS: Read-only file system @ dir_s_mkdir - /mnt/tmp/bin
        from /usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler/shared_helpers.rb:102:in `filesystem_access'
        from /usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler.rb:109:in `bin_path'
        from (irb):1:in `<main>'
        from /usr/lib/ruby/gems/3.0.0/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
        from /usr/bin/irb:23:in `load'
        from /usr/bin/irb:23:in `<main>'
/usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler/vendor/fileutils/lib/fileutils.rb:250:in `mkdir': Read-only file system @ dir_s_mkdir - /mnt/tmp/bin (Errno::EROFS)
        from /usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler/vendor/fileutils/lib/fileutils.rb:250:in `fu_mkdir'
        from /usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler/vendor/fileutils/lib/fileutils.rb:228:in `block (2 levels) in mkdir_p'
        from /usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler/vendor/fileutils/lib/fileutils.rb:226:in `reverse_each'
        from /usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler/vendor/fileutils/lib/fileutils.rb:226:in `block in mkdir_p'
        from /usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler/vendor/fileutils/lib/fileutils.rb:211:in `each'
        from /usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler/vendor/fileutils/lib/fileutils.rb:211:in `mkdir_p'
        from /usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler.rb:109:in `block in bin_path'
        from /usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler/shared_helpers.rb:103:in `filesystem_access'
        from /usr/share/rubygems-integration/all/gems/bundler-2.3.5/lib/bundler.rb:109:in `bin_path'
        from (irb):1:in `<main>'
        from /usr/lib/ruby/gems/3.0.0/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
        from /usr/bin/irb:23:in `load'
        from /usr/bin/irb:23:in `<main>'

Which command did you run?

What were you expecting to happen?

Bundler.bin_path should return the bin path and not perform any filesystem writes.

What happened instead?

Bundler writes (tries to write) to the filesystem.

Replace this with the actual result you got. Paste the output of your command here.

If not included with the output of your command, run bundle env and paste the output below

Not relevant

Activity

  1. hsbt commented on Dec 16, 2025

    @hsbt
    Member

    This mkdir_p is come from the first commit at d183011

    Should we handle Errno::EROFS and ignore it?

  2. p-datadog commented on Dec 16, 2025

    @p-datadog
    Author

    Writing to the filesystem is not expected behavior for an attribute reader.

    In DataDog/dd-trace-rb#5146 we duplicate bundler's implementation of bin_path with the filesystem write removed, because:

    (Note that while we could've kept calling Bundler.bin_path, wrapping it with the rescue, this would still produce the side-effect of creating the folder and it seems weird to me to end up in this situation of "turning on the profiler causes bin/ to be created". This is why I chose to reimplement the behavior, minus folder creation)

    Having the write means bundler will modify filesystem on what is expected to be a configuration read.

    If you add a rescue clause to swallow exceptions, it will be very hard to find out that bundler is causing these unexpected filesystem writes (for the users who do have a writable filesystem).

  3. p-datadog commented on Dec 16, 2025

    @p-datadog
    Author

    I suggest that the directory creation should be done in d183011#diff-b2555ca22e7e45e4842e20b8477290f64940ded0e96f7cc96f01ebbc24bbe1cbR49-R54 , right before

            File.open "#{Bundler.bin_path}/#{executable}", 'w', 0755 do |f|
    
    

    which is where the directory is actually needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions