How to correctly specify files when building gems
I recently found one of my docker images had 19MB of test files in the Ruby gems directory. When building a docker image, you want it as small as possible.
I wondered why some gems in there had tests included, and others did not? Here’s what I discovered…
When building a gem, it looks in your gemname.gemspec
file for a list of files to include.
The default is a command that includes every file. It’s better to be specific about what files you include, instead of the default shotgun approach
In your gemspec
file:
BAD
s.files = `git ls-files -z`.split("\0")
GOOD
s.files = Dir.glob("{lib}/**/*") + %w(LICENSE README.md CHANGELOG.md)
Tweet