Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defp deps do

{:sentry, "~> 13.0"},
{:jason, "~> 1.4"},
{:finch, "~> 0.17.0"}
{:finch, "~> 0.21"}
]
end
```
Expand Down
16 changes: 13 additions & 3 deletions lib/mix/tasks/sentry.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if Code.ensure_loaded?(Igniter) do
def info(_argv, _composing_task) do
%Igniter.Mix.Task.Info{
group: :sentry,
adds_deps: [{:jason, "~> 1.2"}, {:hackney, ">= 1.8.0 and < 5.0.0"}],
adds_deps: [{:jason, "~> 1.4"}, {:finch, "~> 0.21"}],
example: __MODULE__.Docs.example(),
schema: [dsn: :string],
defaults: [dsn: "<your_dsn>"]
Expand All @@ -63,7 +63,7 @@ if Code.ensure_loaded?(Igniter) do
"prod.exs",
:sentry,
[:environment_name],
{:code, quote(do: Mix.env())}
{:code, quote(do: config_env())}
)
|> Igniter.Project.Config.configure(
"prod.exs",
Expand Down Expand Up @@ -138,14 +138,24 @@ if Code.ensure_loaded?(Igniter) do
end

defp setup_endpoint(igniter, endpoint) do
# Sentry.PlugCapture is only recommended for Cowboy; on Bandit it can

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The has_dep?(:plug_cowboy) check reads from the on-disk mix.exs, not in-memory state. If sentry.install is composed after add_dep without applying changes, the check will fail.
Severity: LOW

Suggested Fix

The has_dep? check should be made aware of pending changes in the igniter state. Alternatively, if this composition pattern is unsupported, the documentation for Igniter should be updated to clarify that changes must be applied before running dependent tasks.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: lib/mix/tasks/sentry.install.ex#L141

Potential issue: The `sentry.install` task uses
`Igniter.Project.Deps.has_dep?(:plug_cowboy)` to decide whether to add
`Sentry.PlugCapture`. This function checks the on-disk `mix.exs` file, not the in-memory
state of an `igniter` process. If a developer programmatically composes tasks by first
adding `:plug_cowboy` with `add_dep` and then immediately running `sentry.install`
without first applying the changes to disk, the check will incorrectly return `false`.
This results in `Sentry.PlugCapture` being omitted from the configuration, even though
the project will have the `:plug_cowboy` dependency.

Did we get this right? 👍 / 👎 to inform future reviews.

# result in duplicate errors.
uses_cowboy? = Igniter.Project.Deps.has_dep?(igniter, :plug_cowboy)

Igniter.Project.Module.find_and_update_module!(igniter, endpoint, fn zipper ->
zipper
|> Igniter.Code.Common.within(&add_plug_capture/1)
|> maybe_add_plug_capture(uses_cowboy?)
|> Igniter.Code.Common.within(&add_plug_context/1)
|> then(&{:ok, &1})
end)
end

defp maybe_add_plug_capture(zipper, true) do
Igniter.Code.Common.within(zipper, &add_plug_capture/1)
end

defp maybe_add_plug_capture(zipper, false), do: zipper

defp add_plug_capture(zipper) do
with :error <- Igniter.Code.Module.move_to_use(zipper, Sentry.PlugCapture),
{:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Phoenix.Endpoint) do
Expand Down
29 changes: 24 additions & 5 deletions test/mix/sentry.install_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,19 @@ defmodule Mix.Tasks.Sentry.InstallTest do
]
end

test "installation adds jason and hackney dependencies", %{igniter: igniter} do
test "installation adds jason and finch dependencies", %{igniter: igniter} do
igniter
|> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn"])
|> assert_creates("config/prod.exs", """
import Config

config :sentry,
dsn: "test_dsn",
environment_name: Mix.env(),
environment_name: config_env(),
enable_source_code_context: true,
root_source_code_paths: [File.cwd!()]
""")
|> assert_has_patch("lib/test_web/endpoint.ex", """
+ | use Sentry.PlugCapture
""")
|> assert_has_patch("lib/test_web/endpoint.ex", """
+ | plug(Sentry.PlugContext)
""")
|> assert_has_patch("lib/test/application.ex", """
Expand All @@ -62,6 +59,28 @@ defmodule Mix.Tasks.Sentry.InstallTest do
""")
end

test "installation does not add Sentry.PlugCapture without plug_cowboy", %{igniter: igniter} do
endpoint =
igniter
|> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn"])
|> apply_igniter!()
|> then(& &1.assigns[:test_files]["lib/test_web/endpoint.ex"])

refute endpoint =~ "Sentry.PlugCapture"
end

test "installation adds Sentry.PlugCapture when the project uses plug_cowboy", %{
igniter: igniter
} do
igniter
|> Igniter.Project.Deps.add_dep({:plug_cowboy, "~> 2.7"})
|> apply_igniter!()
|> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn"])
|> assert_has_patch("lib/test_web/endpoint.ex", """
+ | use Sentry.PlugCapture
""")
end

test "installation is idempotent", %{igniter: igniter} do
igniter
|> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn"])
Expand Down