Ruby

Ruby

Made by DeepSource

Avoid using git to declare files in gemspec RB-E1004

Anti-pattern
Major

Avoid using git ls-files to produce lists of files. Downstreams often need to build your package in an environment that does not have git (on purpose). Instead, use some pure Ruby alternatives, like Dir or Dir.glob.

Adding git as a dependency for a Ruby gem is not recommended, since it is possible that the environments in which the gems are built do not have git installed. For example, packages in Debian are built in a clean environment (sbuild, schroot, etc) so in those environments the build fails with the following error:

Invalid gemspec in [<gem_name>.gemspec]: No such file or directory - git

Source packages often consist of released tarballs, which are extracted during build. So even if we install git beforehand as a build dependency, it would still fail as the package source tree is not a git repo. Even when the package is maintained in git, it is uploaded as tarballs to the archive without any version control information.

Therefore, the only solution for this is to remove the usage of git and use some plain Ruby alternatives like Dir or Dir.glob or even Rake::FileList.

Bad practice

Gem::Specification.new do |spec|
  spec.files         = `git ls-files -- lib/`.split("
")
  spec.test_files    = `git ls-files -- test/{functional,unit}/*`.split("
")
  spec.executables   = `git ls-files -- bin/*`.split("
").map{ |f| File.basename(f) }
end

Recommended

Gem::Specification.new do |spec|
  spec.files         = Dir.glob("lib/**/*")
  spec.test_files    = Dir.glob("test/{functional,test}/*")
  spec.executables   = Dir.glob("bin/*").map{ |f| File.basename(f) }
end