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

Formatter not loading plugins in subdirectory consistently when run from umbrella root #12880

Closed
lukaszsamson opened this issue Aug 22, 2023 · 6 comments

Comments

@lukaszsamson
Copy link
Contributor

Elixir and Erlang/OTP versions

Erlang/OTP 25 [erts-13.2.2.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]

Elixir 1.15.4 (compiled with Erlang/OTP 25)

Operating system

macOS, linux

Current behavior

Repro:

  1. git clone [email protected]:lukaszsamson/elixir_bug_formatter_plugin.git
  2. cd elixir_bug_formatter_plugin
  3. mix deps.get
  4. mix format --check-formatted
** (Mix) mix format failed due to --check-formatted.
The following files are not formatted:

/dir/elixir_bug_formatter_plugin/apps/backoffice/lib/backoffice/templates/some/index.html.heex
  1. mix clean
  2. mix format --check-formatted
    no error
  3. cd apps/backoffice
  4. mix format --check-formatted
** (Mix) mix format failed due to --check-formatted.
The following files are not formatted:

/dir/elixir_bug_formatter_plugin/apps/backoffice/lib/backoffice/templates/some/index.html.heex

It works consistently if I add

plugins: [Phoenix.LiveView.HTMLFormatter],

to root .formatter.exs

Expected behavior

https://hexdocs.pm/mix/main/Mix.Tasks.Format.html#module-formatting-options state about subdirectories:

Configuration between .formatter.exs are not shared nor inherited.

Nothing about interaction between subdirectories and plugins is stated in plugins section.

My expectation is that running formatter in a subdirectory should be equivalent with running it in root. Moreover, it should behave consistently and not depend on wether mix clean has been run.

@josevalim
Copy link
Member

I am afraid you have not push the reproduction repo yet. :)

@lukaszsamson
Copy link
Contributor Author

oh, I accidentally made it private. Should work now

@lukaszsamson
Copy link
Contributor Author

Possibly another manifestation of the problem. When running from the root

mix format
mix format failed for file: apps/backend/lib/backend/components/core_components.ex
** (UndefinedFunctionError) function Phoenix.LiveView.HTMLFormatter.format/2 is undefined (module Phoenix.LiveView.HTMLFormatter is not available)
    Phoenix.LiveView.HTMLFormatter.format("<div>...</div>\n", [file: "/myapp/apps/backend/lib/backend/components/core_components.ex", line: 9, sigil: :H, modifiers: [], opening_delimiter: "\"\"\"", import_deps: [:ecto, :ecto_sql, :phoenix, :absinthe], plugins: [Phoenix.LiveView.HTMLFormatter, Absinthe.Formatter], inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}", "{lib,priv}/**/*.{gql,graphql}"]])
    (elixir 1.15.4) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3
    (elixir 1.15.4) lib/code/formatter.ex:1356: Code.Formatter.maybe_sigil_to_algebra/4
    (elixir 1.15.4) lib/code/formatter.ex:490: Code.Formatter.quoted_to_algebra/3
    (elixir 1.15.4) lib/code/formatter.ex:579: anonymous fn/3 in Code.Formatter.block_args_to_algebra/4
    (elixir 1.15.4) lib/code/formatter.ex:1987: Code.Formatter.each_quoted_to_algebra_without_comments/4
    (elixir 1.15.4) lib/code/formatter.ex:1973: Code.Formatter.quoted_to_algebra_with_comments/6
    (elixir 1.15.4) lib/code/formatter.ex:584: Code.Formatter.block_args_to_algebra/4

when running from app/backend it works

root .formatter

[
  inputs: ["mix.exs", "config/*.exs", "rel/*.exs", ".formatter.exs", ".dialyzer_ignore.exs"],
  subdirectories: ["apps/*"]
]

umbrella child .formatter

[
  import_deps: [:ecto, :ecto_sql, :phoenix, :absinthe],
  plugins: [Phoenix.LiveView.HTMLFormatter, Absinthe.Formatter],
  inputs: [
    "*.{heex,ex,exs}",
    "{config,lib,test}/**/*.{heex,ex,exs}",
    "{lib,priv}/**/*.{gql,graphql}"
  ]
]

@lukaszsamson
Copy link
Contributor Author

lukaszsamson commented Aug 23, 2023

@josevalim I built and installed latest version from 1.15 branch and got this

mix format                                
mix format failed for file: apps/backend/lib/backend/components/core_components.ex
** (BadFunctionError) function #Function<32.116260509/2 in Mix.Tasks.Format> is invalid, likely because it points to an old version of the code
    (elixir 1.15.4) lib/code/formatter.ex:1356: Code.Formatter.maybe_sigil_to_algebra/4
    (elixir 1.15.4) lib/code/formatter.ex:490: Code.Formatter.quoted_to_algebra/3
    (elixir 1.15.4) lib/code/formatter.ex:579: anonymous fn/3 in Code.Formatter.block_args_to_algebra/4
    (elixir 1.15.4) lib/code/formatter.ex:1987: Code.Formatter.each_quoted_to_algebra_without_comments/4
    (elixir 1.15.4) lib/code/formatter.ex:1973: Code.Formatter.quoted_to_algebra_with_comments/6
    (elixir 1.15.4) lib/code/formatter.ex:584: Code.Formatter.block_args_to_algebra/4
    (elixir 1.15.4) lib/code/formatter.ex:1892: Code.Formatter.clauses_to_algebra/4
    (elixir 1.15.4) lib/code/formatter.ex:1268: Code.Formatter.do_end_block_to_algebra/5

It stopped crashing after mix clean --deps

@josevalim
Copy link
Member

That makes sense. We cannot cache anonymous functions. :( I will take another look.

@josevalim josevalim reopened this Aug 23, 2023
@josevalim
Copy link
Member

The issue you saw wouldn’t happen in practice because the Elixir module changed but in practice that would be a new version which would clear all cache.

Still, I changed the code that handles sigiles to avoid caching anonymous functions. :)

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

No branches or pull requests

2 participants