Skip to content

Commit 32383dd

Browse files
committed
fixup! Updates Sentry.Sources to support multiple source code root paths
1 parent 5ca7f87 commit 32383dd

File tree

6 files changed

+115
-15
lines changed

6 files changed

+115
-15
lines changed

lib/sentry/config.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ defmodule Sentry.Config do
9090
path = get_config(:root_source_code_path)
9191

9292
cond do
93+
not is_nil(path) and not is_nil(paths) ->
94+
raise ArgumentError, """
95+
:root_source_code_path and :root_source_code_paths can't be configured at the \
96+
same time.
97+
98+
:root_source_code_path is deprecated. Set :root_source_code_paths instead.
99+
"""
100+
93101
not is_nil(paths) ->
94102
paths
95103

lib/sentry/sources.ex

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,11 @@ defmodule Sentry.Sources do
6565
@type source_map :: %{String.t() => file_map}
6666

6767
def load_files do
68-
root_paths = Config.root_source_code_paths()
69-
path_pattern = Config.source_code_path_pattern()
70-
exclude_patterns = Config.source_code_exclude_patterns()
71-
72-
Enum.reduce(root_paths, %{}, fn root_path, acc1 ->
73-
Path.join(root_path, path_pattern)
74-
|> Path.wildcard()
75-
|> exclude_files(exclude_patterns)
76-
|> Enum.reduce(acc1, fn path, acc2 ->
77-
key = Path.relative_to(path, root_path)
78-
value = source_to_lines(File.read!(path))
79-
80-
Map.put(acc2, key, value)
81-
end)
82-
end)
68+
Enum.reduce(
69+
Config.root_source_code_paths(),
70+
%{},
71+
&load_files_for_root_path/2
72+
)
8373
end
8474

8575
@doc """
@@ -128,6 +118,37 @@ defmodule Sentry.Sources do
128118
end)
129119
end
130120

121+
defp load_files_for_root_path(root_path, files) do
122+
root_path
123+
|> find_files_for_root_path()
124+
|> Enum.reduce(files, fn path, acc ->
125+
key = Path.relative_to(path, root_path)
126+
127+
if Map.has_key?(acc, key) do
128+
raise RuntimeError, """
129+
Found two source files in different source root paths with the same relative \
130+
path: #{key}
131+
132+
This means that both source files would be reported to Sentry as the same \
133+
file. Please rename one of them to avoid this.
134+
"""
135+
else
136+
value = source_to_lines(File.read!(path))
137+
138+
Map.put(acc, key, value)
139+
end
140+
end)
141+
end
142+
143+
defp find_files_for_root_path(root_path) do
144+
path_pattern = Config.source_code_path_pattern()
145+
exclude_patterns = Config.source_code_exclude_patterns()
146+
147+
Path.join(root_path, path_pattern)
148+
|> Path.wildcard()
149+
|> exclude_files(exclude_patterns)
150+
end
151+
131152
defp exclude_files(file_names, []), do: file_names
132153

133154
defp exclude_files(file_names, [exclude_pattern | rest]) do

test/config_test.exs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,46 @@ defmodule Sentry.ConfigTest do
7272
assert ["test", "dev"] == Config.included_environments()
7373
end
7474
end
75+
76+
describe "root_source_code_paths" do
77+
test "raises error if :root_source_code_path and :root_source_code_paths are set" do
78+
modify_env(:sentry, root_source_code_path: "/test")
79+
modify_env(:sentry, root_source_code_paths: ["/test"])
80+
81+
expected_error_message = """
82+
:root_source_code_path and :root_source_code_paths can't be configured at the \
83+
same time.
84+
85+
:root_source_code_path is deprecated. Set :root_source_code_paths instead.
86+
"""
87+
88+
assert_raise ArgumentError, expected_error_message, fn ->
89+
Config.root_source_code_paths()
90+
end
91+
end
92+
93+
test "raises error if :root_source_code_path and :root_source_code_paths are not set" do
94+
delete_env(:sentry, [:root_source_code_path, :root_source_code_paths])
95+
96+
expected_error_message = ":root_source_code_paths must be configured"
97+
98+
assert_raise ArgumentError, expected_error_message, fn ->
99+
Config.root_source_code_paths()
100+
end
101+
end
102+
103+
test "returns :root_source_code_path if it's set" do
104+
modify_env(:sentry, root_source_code_path: "/test")
105+
modify_env(:sentry, root_source_code_paths: nil)
106+
107+
assert Config.root_source_code_paths() == ["/test"]
108+
end
109+
110+
test "returns :root_source_code_paths if it's set" do
111+
modify_env(:sentry, root_source_code_path: nil)
112+
modify_env(:sentry, root_source_code_paths: ["/test"])
113+
114+
assert Config.root_source_code_paths() == ["/test"]
115+
end
116+
end
75117
end

test/sources_test.exs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ defmodule Sentry.SourcesTest do
2727
}
2828
} = Sentry.Sources.load_files()
2929
end
30+
31+
test "raises error when two files have the same relative path" do
32+
Application.put_env(:sentry, :root_source_code_paths, [
33+
File.cwd!() <> "/test/support/example-umbrella-app-with-conflict/apps/app_a",
34+
File.cwd!() <> "/test/support/example-umbrella-app-with-conflict/apps/app_b"
35+
])
36+
37+
expected_error_message = """
38+
Found two source files in different source root paths with the same relative \
39+
path: lib/module_a.ex
40+
41+
This means that both source files would be reported to Sentry as the same \
42+
file. Please rename one of them to avoid this.
43+
"""
44+
45+
assert_raise RuntimeError, expected_error_message, fn ->
46+
Sentry.Sources.load_files()
47+
end
48+
end
3049
end
3150

3251
test "exception makes call to Sentry API" do
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule ModuleA do
2+
def test do
3+
"test a"
4+
end
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule ModuleB do
2+
def test do
3+
"test b"
4+
end
5+
end

0 commit comments

Comments
 (0)