Skip to content

Commit 73dda9a

Browse files
authored
Fix reading of config in "mix sentry.package_source_code" (#653)
1 parent 61e3c8b commit 73dda9a

File tree

5 files changed

+23
-67
lines changed

5 files changed

+23
-67
lines changed

lib/mix/tasks/sentry.package_source_code.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ defmodule Mix.Tasks.Sentry.PackageSourceCode do
5656
def run(args) do
5757
{opts, _args} = OptionParser.parse!(args, strict: @switches)
5858

59+
# We can read the config here because this task depends on the `app.config` task.
60+
config = Application.get_all_env(:sentry)
61+
5962
{elapsed, source_map} =
6063
:timer.tc(fn ->
61-
case Sources.load_files() do
64+
case Sources.load_files(config) do
6265
{:ok, source_map} -> source_map
6366
{:error, message} -> Mix.raise(message)
6467
end

lib/sentry/config.ex

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,6 @@ defmodule Sentry.Config do
400400
@spec enable_source_code_context?() :: boolean()
401401
def enable_source_code_context?, do: fetch!(:enable_source_code_context)
402402

403-
@spec root_source_code_paths() :: [Path.t()]
404-
def root_source_code_paths, do: fetch!(:root_source_code_paths)
405-
406-
@spec source_code_path_pattern() :: String.t()
407-
def source_code_path_pattern, do: fetch!(:source_code_path_pattern)
408-
409-
@spec source_code_exclude_patterns() :: [Regex.t()]
410-
def source_code_exclude_patterns, do: fetch!(:source_code_exclude_patterns)
411-
412403
@spec context_lines() :: pos_integer()
413404
def context_lines, do: fetch!(:context_lines)
414405

lib/sentry/sources.ex

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ defmodule Sentry.Sources do
6868
:persistent_term.get(@source_code_map_key, nil)
6969
end
7070

71-
@spec load_files([Path.t()] | nil) :: {:ok, source_map()} | {:error, message :: String.t()}
72-
def load_files(root_paths \\ nil) do
73-
root_paths = root_paths || Config.root_source_code_paths()
74-
path_pattern = Config.source_code_path_pattern()
75-
exclude_patterns = Config.source_code_exclude_patterns()
71+
@spec load_files(keyword()) :: {:ok, source_map()} | {:error, message :: String.t()}
72+
def load_files(config \\ []) when is_list(config) do
73+
config = Sentry.Config.validate!(config)
7674

77-
root_paths
75+
path_pattern = Keyword.fetch!(config, :source_code_path_pattern)
76+
exclude_patterns = Keyword.fetch!(config, :source_code_exclude_patterns)
77+
78+
config
79+
|> Keyword.fetch!(:root_source_code_paths)
7880
|> Enum.reduce(%{}, &load_files_for_root_path(&1, &2, path_pattern, exclude_patterns))
7981
|> Map.new(fn {path, %{lines: lines}} -> {path, lines} end)
8082
catch

test/sentry_test.exs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -78,52 +78,4 @@ defmodule SentryTest do
7878

7979
assert log =~ "Sentry: unable to parse exception"
8080
end
81-
82-
test "reports source code context", %{bypass: bypass} do
83-
parent_pid = self()
84-
ref = make_ref()
85-
86-
put_test_config(
87-
enable_source_code_context: true,
88-
root_source_code_paths: [File.cwd!()],
89-
source_code_path_pattern: "{lib,test}/*.{ex,exs}"
90-
)
91-
92-
set_mix_shell(Mix.Shell.Quiet)
93-
94-
assert :ok = Mix.Task.rerun("sentry.package_source_code")
95-
96-
assert {:loaded, _source_map} = Sentry.Sources.load_source_code_map_if_present()
97-
98-
Bypass.expect(bypass, fn conn ->
99-
{:ok, body, conn} = Plug.Conn.read_body(conn)
100-
101-
event = decode_event_from_envelope!(body)
102-
send(parent_pid, {ref, event})
103-
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
104-
end)
105-
106-
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
107-
108-
assert {:ok, "340"} =
109-
Sentry.capture_exception(%RuntimeError{message: "oops"},
110-
stacktrace: stacktrace,
111-
result: :sync
112-
)
113-
114-
assert_receive {^ref, event}
115-
116-
[exception] = event.exception
117-
assert exception["type"] == "RuntimeError"
118-
assert exception["value"] == "oops"
119-
120-
assert [%{"function" => "Process.info/2"}, interesting_frame | _rest] =
121-
Enum.reverse(exception["stacktrace"]["frames"])
122-
123-
assert interesting_frame["context_line"] =~ "Process.info(self(), :current_stacktrace)"
124-
assert Enum.at(interesting_frame["pre_context"], 0) =~ "Plug.Conn.resp(conn"
125-
assert Enum.at(interesting_frame["pre_context"], 1) =~ "end)"
126-
assert Enum.at(interesting_frame["post_context"], 0) == ""
127-
assert Enum.at(interesting_frame["post_context"], 1) =~ "assert {:ok, "
128-
end
12981
end

test/sources_test.exs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Sentry.SourcesTest do
22
use Sentry.Case, async: true
33

4-
describe "load_files/0" do
4+
describe "load_files/1" do
55
test "loads files" do
66
paths = [
77
File.cwd!() <> "/test/fixtures/example-umbrella-app/apps/app_a",
@@ -24,7 +24,11 @@ defmodule Sentry.SourcesTest do
2424
4 => " end",
2525
5 => "end"
2626
}
27-
}} = Sentry.Sources.load_files(paths)
27+
}} =
28+
Sentry.Sources.load_files(
29+
root_source_code_paths: paths,
30+
source_code_exclude_patterns: []
31+
)
2832
end
2933

3034
test "raises error when two files have the same relative path" do
@@ -33,7 +37,11 @@ defmodule Sentry.SourcesTest do
3337
File.cwd!() <> "/test/fixtures/example-umbrella-app-with-conflict/apps/app_b"
3438
]
3539

36-
assert {:error, message} = Sentry.Sources.load_files(paths)
40+
assert {:error, message} =
41+
Sentry.Sources.load_files(
42+
root_source_code_paths: paths,
43+
source_code_exclude_patterns: []
44+
)
3745

3846
assert message == """
3947
Found two source files in different source root paths with the same relative path:

0 commit comments

Comments
 (0)