Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix phx.routes mix task when docs return multiple annotations #5606

Merged
merged 4 commits into from
Oct 24, 2023

Conversation

dinocosta
Copy link
Contributor

I'm not entirely sure how to reproduce this, not even how it actually happens, but I'm currently working on a codebase where running mix phx.routes --info [ROUTE] raises an exception with the following message:

Function: :index
** (ArgumentError) cannot convert the given list to a string.

To be converted to a string, a list must either be empty or only
contain the following elements:

  * strings
  * integers representing Unicode code points
  * a list containing one of these three elements

Please check the given list or call inspect/1 to get the list representation, got:

[generated: true, location: 32]

    (elixir 1.15.5) lib/list.ex:1084: List.to_string/1
    (phoenix 1.7.8) lib/mix/tasks/phx.routes.ex:109: Mix.Tasks.Phx.Routes.get_url_info/2
    (mix 1.15.5) lib/mix/task.ex:447: anonymous fn/3 in Mix.Task.run_task/5
    (mix 1.15.5) lib/mix/cli.ex:92: Mix.CLI.run_task/2
    /Users/dino/.asdf/installs/elixir/1.15.5-otp-26/bin/mix:2: (file)

After reviewing the code for mix phx.routes I noticed that it relies on Code.fetch_docs/1 to find the line number for a specific function in a module's file.

However, it assumes that Code.fetch_docs/1 will always return the function's location as the second element of the tuple, which is not always the case, as Code.fetch_docs/1 can return a list of annotations - :erl_anno.anno() - as a Keyword list. When that's the case, the line number is actually the value for the :location key.

As such, this Pull Request updates the Mix.Tasks.Phx.Routes.get_line_number/2 to make sure that both situations are supported.

The `phx.routes` mix task relies on `Code.fetch_docs/1` to find the line
number for a specific function in a module's file. However, it assumes
that `Code.fetch_docs/1` will always return the line number as the
second element of the tuple, which is not always the case, as
`Code.fetch_docs/1` can return a list of annotations as a Keyword list.
When that's the case, the line number is actually the value for the
`:location` key.
As such, this commit updates the code in `phx.routes` to make sure that
both situations are supported.
@josevalim josevalim merged commit 3fd5984 into phoenixframework:main Oct 24, 2023
4 checks passed
@josevalim
Copy link
Member

It is not a list of annotations but it may include other metadata. :)

@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@dinocosta dinocosta deleted the fix-phx-routes-line-number branch October 24, 2023 08:45
@dinocosta
Copy link
Contributor Author

Can't believe I moved all of that code around and, at the end of the day, I only needed to call :erl_anno.line/1, today I learned! 😅

Thank you for reviewing, improving and merging this @josevalim 💜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants