igniter icon indicating copy to clipboard operation
igniter copied to clipboard

add_new_child/3 with after: inserts one child too late

Open pshoukry opened this issue 2 months ago • 0 comments

Igniter.Project.Application.add_new_child/3 with after: inserts the child one place too late when a non-matching child follows the last match.

Igniter 0.8.4; skip_after/2 is unchanged on main at b9454fe7bdf8.

# mix run repro.exs, in a `mix new` project with {:igniter, "== 0.8.4"}
files = %{
  "mix.exs" => """
  defmodule Test.MixProject do
    use Mix.Project
    def project, do: [app: :test, version: "0.1.0", deps: []]
    def application, do: [mod: {Test.Application, []}]
  end
  """,
  "lib/test/application.ex" => """
  defmodule Test.Application do
    use Application

    def start(_type, _args) do
      children = [Test.Repo, TestWeb.Endpoint]
      Supervisor.start_link(children, strategy: :one_for_one)
    end
  end
  """
}

[files: files]
|> Igniter.Test.test_project()
|> Igniter.Project.Application.add_new_child(NewChild, after: [Test.Repo])
|> Igniter.Test.puts_diff()

Expected, per the docs ("the earliest item in the list that we can, skipping any modules in the after option"):

children = [Test.Repo, NewChild, TestWeb.Endpoint]

Actual:

children = [Test.Repo, TestWeb.Endpoint, NewChild]

In a phx.new app this starts a child that needs the repo after the Endpoint, so requests can arrive before it runs.

Cause: in skip_after/2, after a match it recurses on Zipper.right(zipper) with nested?: true. When that recursion finds no further match, it returns {:after, zipper} for the item right of the last match, so insert_child/3 inserts after that item. The last match being the final item (Zipper.right/1 returns nil) already returns {:after, match} and is correct.

Returning {:before, zipper} in the nested :error branch fixes both cases we tried locally. We did not run Igniter's own suite with it.

add_new_child(NewChild, after: [Test.Repo])
# [Test.Repo, TestWeb.Endpoint]             => [Test.Repo, NewChild, TestWeb.Endpoint]
add_new_child(NewChild, after: [Test.Repo, Test.Other])
# [Test.Repo, Test.Other, TestWeb.Endpoint] => [Test.Repo, Test.Other, NewChild, TestWeb.Endpoint]

pshoukry avatar Oct 02 '26 15:10 pshoukry