lexical-lsp / lexical

Lexical is a next-generation elixir language server
779 stars 77 forks source link

Expected warnings in exs files #685

Open sodapopcan opened 3 months ago

sodapopcan commented 3 months ago

For certain special-case files, both .exs files in my case, some syntax is legit that isn't normally considered so.

For example:

In .iex.exs, it's very common to import, alias, or require modules without actually using them. In priv/repo/seeds.exs in a Phoenix project, it's pretty much guaranteed you're going to use your Repo to populate your db. In both cases, Lexical gives warnings. In the former they "unsed import/alias/require" warnings, and in the latter it's "could not look up Ecto repo". An example seeds file would look like:

unless MyApp.Accounts.get_user_by_email("admin@example.com") do
  MyApp.Accounts.register_user(%{
    name: "Bob",
    email: "admin@example.com",
    password: "Password12345"
  })
end

Of course the warnings in .iex.exs could be silenced by adding warn: false to everything, but that is not ideal (though also not the end of the world). I'm not sure if anything can be done about the repo case. I have noticed wrapping everything in if Mix.env() != :test silences it.

Thanks!

scohen commented 2 months ago

I've been thinking about this, and this seems like it'd be a project-level configuration option, though I don't quite know how we'd implement it. You'd likely need to figure out which errors you'd like to suppress in which files, maybe using a wildcard pattern, but this strategy seems version-specific, as the error messages change slightly from elixir version to version.

I don't see a path forward with the repo case however.

sodapopcan commented 2 months ago

That makes sense to me. AFAIC this is a nice-to-have so I thought I would report. It's not the end of the world and there are ways around it. Thanks for giving it some thought, though :)