Skip to content

Commit

Permalink
Start matching pathname directory prefixes more rigorously
Browse files Browse the repository at this point in the history
Fixes #1174

The simplistic `start_with?` check was not sufficient to match that a
given file/folder was under a given folder, since we were not explicitly
checking for full folder name matches. For example, if
`path = "/foo/bar-gem/baz"` and `folder = "/foo/bar"`, then
`path.start_with?(folder)` would return `true`, but `path` is not under
the `folder` directory.

This commit fixes the problem by matching `folder` to any of the parent
directories of `path` by using the `Pathname#ascend` method to get all
parent directory prefixes of a `path`.
  • Loading branch information
paracycle committed Sep 16, 2022
1 parent 26b01f4 commit e5fdc2a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/tapioca/gemfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def contains_path?(path)
if default_gem?
files.any? { |file| file.to_s == to_realpath(path) }
else
to_realpath(path).start_with?(full_gem_path) || has_parent_gemspec?(path)
path_in_dir?(to_realpath(path), full_gem_path) || has_parent_gemspec?(path)
end
end

Expand Down
14 changes: 12 additions & 2 deletions lib/tapioca/helpers/gem_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ module GemHelper
sig { params(gemfile_dir: String, full_gem_path: String).returns(T::Boolean) }
def gem_in_app_dir?(gemfile_dir, full_gem_path)
!gem_in_bundle_path?(to_realpath(full_gem_path)) &&
full_gem_path.start_with?(to_realpath(gemfile_dir))
path_in_dir?(to_realpath(gemfile_dir), full_gem_path)
end

sig { params(full_gem_path: String).returns(T::Boolean) }
def gem_in_bundle_path?(full_gem_path)
full_gem_path.start_with?(Bundler.bundle_path.to_s, Bundler.app_cache.to_s)
path_in_dir?(full_gem_path, Bundler.bundle_path) ||
path_in_dir?(full_gem_path, Bundler.app_cache)
end

sig { params(path: T.any(String, Pathname)).returns(String) }
Expand All @@ -22,5 +23,14 @@ def to_realpath(path)
path_string = File.realpath(path_string) if File.exist?(path_string)
path_string
end

private

sig { params(path: T.any(Pathname, String), dir: T.any(Pathname, String)).returns(T::Boolean) }
def path_in_dir?(path, dir)
dir = Pathname.new(dir)
path = Pathname.new(path)
path.ascend.any?(dir)
end
end
end

0 comments on commit e5fdc2a

Please sign in to comment.