From bc1721b982d90a21e60a9c06e5c9ec08a2cbf09c Mon Sep 17 00:00:00 2001 From: Michal Kuratczyk Date: Mon, 17 Feb 2025 11:56:30 +0100 Subject: [PATCH] CLI: Don't use regex as module attributes When trying to use OTP28.0-rc1, Elixir fails to compile these modules because a module attribute cannot be a regex. It is not yet clear whether it's something to be fixed in Elixir for OTP28 compatibility or something that accidentally worked in the past, but either way, using a string as an attribute is equally good and works all OTP versions, including OTP28.0-rc1. ``` == Compilation error in file lib/rabbitmq/cli/core/command_modules.ex == ** (ArgumentError) cannot inject attribute @commands_ns into function/macro because cannot escape #Reference<0.2201422310.1333657602.13657>. The supported values are: lists, tuples, maps, atoms, numbers, bitstrings, PIDs and remote functions in the format &Mod.fun/arity (elixir 1.18.2) lib/kernel.ex:3729: Kernel.do_at/5 (elixir 1.18.2) expanding macro: Kernel.@/1 lib/rabbitmq/cli/core/command_modules.ex:133: RabbitMQ.CLI.Core.CommandModules.make_module_map/2 ``` (cherry picked from commit 7e8ecc96dba998694e2ef12f874946d0762e9426) --- deps/rabbitmq_cli/lib/rabbitmq/cli/core/command_modules.ex | 6 +++--- deps/rabbitmq_cli/lib/rabbitmq/cli/core/os_pid.ex | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/command_modules.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/command_modules.ex index ddba5a31a797..c3a2f14523f2 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/command_modules.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/command_modules.ex @@ -11,7 +11,7 @@ defmodule RabbitMQ.CLI.Core.CommandModules do import RabbitMQ.CLI.Core.CodePath - @commands_ns ~r/RabbitMQ.CLI.(.*).Commands/ + @commands_ns ~S"RabbitMQ.CLI.(.*).Commands" def module_map(opts \\ %{}) do Application.get_env(:rabbitmqctl, :commands) || load(opts) @@ -130,7 +130,7 @@ defmodule RabbitMQ.CLI.Core.CommandModules do end defp make_module_map(modules, scope) when modules != nil do - commands_ns = Regex.recompile!(@commands_ns) + commands_ns = Regex.compile!(@commands_ns) modules |> Enum.filter(fn mod -> @@ -212,7 +212,7 @@ defmodule RabbitMQ.CLI.Core.CommandModules do defp command_scopes(cmd) do case CommandBehaviour.scopes(cmd) do nil -> - Regex.recompile!(@commands_ns) + Regex.compile!(@commands_ns) |> Regex.run(to_string(cmd), capture: :all_but_first) |> List.first() |> to_snake_case diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/os_pid.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/os_pid.ex index 7340ae05713c..72b6636b0fa8 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/os_pid.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/os_pid.ex @@ -7,7 +7,7 @@ defmodule RabbitMQ.CLI.Core.OsPid do @external_process_check_interval 1000 - @pid_regex ~r/^\s*(?\d+)/ + @pid_regex ~S"^\s*(?\d+)" # # API @@ -27,7 +27,7 @@ defmodule RabbitMQ.CLI.Core.OsPid do def read_pid_from_file(pidfile_path, should_wait) do case {:file.read_file(pidfile_path), should_wait} do {{:ok, contents}, _} -> - pid_regex = Regex.recompile!(@pid_regex) + pid_regex = Regex.compile!(@pid_regex) case Regex.named_captures(pid_regex, contents)["pid"] do # e.g. the file is empty