Skip to content

Format: .formatter.exs with import_deps results in errors #415

Open
@sascha-wolf

Description

@sascha-wolf

This issue is closely related to elixir-lang/elixir#7327.

Current behaviour

Let's assume I'm in a project with a .formatter.exs file looking like this:

[
  import_deps: [:some_dependency_with_formatting_rules],
  inputs: [
    # Some files here ...
  ],
  locals_without_parens: [
    # Some configurations ...
  ]
]

Now I'm working on a file in lib/my_cool_module.ex and then execute elixir-format. This results in the following error:

** (Mix) Unknown dependency :some_dependency_with_formatting_rules given to :import_deps in the formatter configuration. The dependency is not listed in your mix.exs for environment :dev

The dependency is available in dev and included in the root mix.exs. The issue stems from the fact that mix format tries find the dependencies in the current folder - which is the buffer's default-directory - instead of looking for them in the .formatter.exs folder. Due to that it's unable to find the dependencies and raises this error.

Possible fix

Due to mix format looking for a .formatter.exs file in the current folder by default, it might make sense to allow the user to control the execution directory for elixir-format, this would also make a --dot-formatter configuration obsolete.

In addition to that a sensible default for this folder could be the projectile root folder, as this usually contains the .formatter.exs.

Alternative fix

This could be fixed on the mix level.

I've actually submitted elixir-lang/elixir#7328 a while ago, which would have fixed this issue. Sadly it got rejected due to having to cd into the .formatter.exs folder when loading the dependencies.

Instead it was decided that mix format should raise a more "descriptive" error, which you can see above.

Workaround

As a workaround I point my elixir-format-elixir-path to a bash script with the following content:

if git rev-parse --is-inside-work-tree 1>/dev/null 2>/dev/null; then
  cd "$(git rev-parse --absolute-git-dir | xargs dirname)"
fi

/path/to/my/elixir $@

This script checks if I'm in a git repository and then cds into the root folder of the repository. It basically is a hacked version of the suggested fix above.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions