Skip to content

Commit b326114

Browse files
authored
Merge pull request #1200 from elixir-lsp/codex/normalize-directory-path-in-path_in_dir
Fix trailing slash handling in path_in_dir? and add tests
2 parents 5d6639f + 85ec365 commit b326114

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

apps/language_server/lib/language_server/source_file/path.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ defmodule ElixirLS.LanguageServer.SourceFile.Path do
147147

148148
# This function expects absolute paths with universal separators
149149
def path_in_dir?(file, dir) do
150+
dir = if dir == "/", do: "/", else: String.trim_trailing(dir, "/")
151+
150152
case String.starts_with?(file, dir) do
151153
true ->
152154
# Get the grapheme after the directory in the file path

apps/language_server/test/source_file/path_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,18 @@ defmodule ElixirLS.LanguageServer.SourceFile.PathTest do
191191
end
192192
end
193193
end
194+
195+
describe "path_in_dir?/2" do
196+
test "matches regardless of trailing slash" do
197+
assert path_in_dir?("/foo/bar.ex", "/foo")
198+
assert path_in_dir?("/foo/bar.ex", "/foo/")
199+
assert path_in_dir?("c:/foo/bar.ex", "c:/foo")
200+
assert path_in_dir?("c:/foo/bar.ex", "c:/foo/")
201+
end
202+
203+
test "non-matching paths" do
204+
refute path_in_dir?("/foobar/baz.ex", "/foo")
205+
refute path_in_dir?("/foobar/baz.ex", "/foo/")
206+
end
207+
end
194208
end

0 commit comments

Comments
 (0)