igniter icon indicating copy to clipboard operation
igniter copied to clipboard

configure_runtime_env/5 with {:code, quoted} crashes formatter (CaseClauseError) when the key already exists

Open sorax opened this issue 2 weeks ago • 0 comments

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

AI Policy

  • [x] I agree to follow this project's AI Policy, or I agree that AI was not used while creating this issue.

Versions

  • igniter 0.8.5
  • rewrite 1.3.0
  • sourceror 1.13.0
  • ash 3.37.6
  • Elixir 1.20.5 / Erlang/OTP 29

Operating system

macOS (Darwin 27.2)

Current Behavior

Igniter.Project.Config.configure_runtime_env/5 crashes when the value is {:code, quoted} from a plain quote, the key already exists in config/runtime.exs, and the existing value is a multi-line call. The crash happens in the Elixir formatter. If the key doesn't exist yet, the same call works fine.

This happens in practice with mix igniter.install ash_state_machine (or any installer that triggers ash.install) when an earlier ash.install already wrote keyset_signing_secret. On the second run, ash.install calls configure_runtime_env(:prod, :ash, [:keyset_signing_secret], {:code, quote do :crypto.mac(:hmac, ...) end}), and it crashes.

Error

** (CaseClauseError) no case clause matching:

    :hmac

    (rewrite 1.3.0) lib/rewrite/dot_formatter.ex:725: Rewrite.DotFormatter.format_quoted!/4
    (rewrite 1.3.0) lib/rewrite/source/ex.ex:169: Rewrite.Source.Ex.update_quoted/3
    (rewrite 1.3.0) lib/rewrite/source/ex.ex:157: Rewrite.Source.Ex.handle_update/4
    (rewrite 1.3.0) lib/rewrite/source.ex:501: Rewrite.Source.update/4
    (igniter 0.8.5) lib/igniter.ex:1755: Igniter.apply_func_with_zipper/3
    (igniter 0.8.5) lib/igniter.ex:570: Igniter.update_elixir_file/4

Rewrite.DotFormatter.format_quoted/4 rescues the error and raises it again, so the trace above hides where it really happens. Here is the original stacktrace:

(elixir 1.20.5) lib/code/formatter.ex:2446: Code.Formatter.force_args?/2
(elixir 1.20.5) lib/code/formatter.ex:1283: Code.Formatter.call_args_to_algebra_no_blocks/6
(elixir 1.20.5) lib/code/formatter.ex:1221: Code.Formatter.call_args_to_algebra/6
(elixir 1.20.5) lib/code/formatter.ex:1114: Code.Formatter.remote_to_algebra/3
(elixir 1.20.5) lib/code/formatter.ex:611: Code.Formatter.quoted_to_algebra/3

Analysis

Code.Formatter.force_args?/2 assumes every argument is a 3-tuple with metadata ({_, meta, _}), as in formatter/Sourceror AST where literals are wrapped in {:__block__, meta, [literal]}. The quoted code has a bare :hmac atom. When Igniter puts it in place of the existing node, the call apparently keeps line/closing metadata from the original multi-line call. That sends the formatter into force_args?/2, which then hits the bare atom.

If the key doesn't exist yet, the code is inserted without that metadata and formats correctly.

Possibly related: #338 (raw quote AST in add_dep).

Reproduction

runtime = """
import Config

if config_env() == :prod do
  config :my_app,
    secret:
      :crypto.mac(
        :hmac,
        :sha256,
        System.get_env("SECRET_KEY_BASE"),
        "salt"
      )
end
"""

code =
  quote do
    :crypto.mac(:hmac, :sha256, System.get_env("SECRET_KEY_BASE"), "salt")
  end

Igniter.Test.test_project(files: %{"config/runtime.exs" => runtime})
|> Igniter.Project.Config.configure_runtime_env(:prod, :my_app, [:secret], {:code, code})
|> Igniter.Test.diff(only: "config/runtime.exs")
|> IO.puts()

Expected Behavior

The existing value is replaced with the new code and formatted correctly, the same as when the key doesn't exist yet.

Possible fix

Normalize {:code, quoted} to formatter-compatible AST before replacing an existing value, for example by round-tripping it through Sourceror.to_string/1 and Sourceror.parse_string!/1, or by dropping the metadata from the original node when replacing it.

sorax avatar Nov 16 '26 13:11 sorax