pantajoe / vscode-elixir-credo

VSCode support for Elixir Linter 'Credo'.

Home Page:https://marketplace.visualstudio.com/items?itemName=pantajoe.vscode-elixir-credo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

“.credo.exs file does not exist” in Umbrella project

Fl4m3Ph03n1x opened this issue · comments

Background

I have an umbrella app with the following structure:

:.
├───mix.exs
├───.mix.lock
├───.credo.exs
├───apps
│   └───app1
...

When working with my umbrella project, I usually open the root folder of the project and work in the desired application from there.

My .credo.exs. file is the default one generated by mix credo gen.config and I have {:credo, "~> 1.6", only: [:dev, :test], runtime: false}, in my root's mix.exs file.

I have also installed credo and its dependencies using:

mix archive.install hex credo
mix archive.install hex bunt
mix archive.install hex jason

So I am sure I have all the dependencies installed.

I also launch VSCode with code . using Windows cmd to make sure I get all the ENV vars from the shell loaded.

Problem

The issue here is that no matter what I do, I always get the .credo.exs file does not exist. Ignoring .... no matter what I do.

To try and fix my issues I read this post:

https://elixirforum.com/t/configure-credo-to-use-global-credo-exs-file/2707

However this wont do it for me. Not only do I still get the error once I get inside the individual applications, it also does not fit with my workflow, as I mostly work with the root directory open.

Questions

How can I fix this?

Thanks for submitting this issue! Apologies for the late reply, but I'm trying to pick this project up again!

Just to clarify, have to you tried to create a .vscode/settings.json and set the option elixir.credo.configurationFile to an absolute path?

I didnt know this was an option! I will let you know once I try !
Happy to have you back !

When I added the complete path it fixed the issue:

settings.json for user:

{
    "workbench.colorTheme": "Catppuccin Mocha",
    "workbench.iconTheme": "catppuccin-mocha",
    "editor.fontFamily": "Fira Code",
    "editor.fontLigatures": true,
    "editor.formatOnSave": true,
    "redhat.telemetry.enabled": true,
    "editor.rulers": [
        120
    ],
    "workbench.colorCustomizations": {
        "editorRuler.foreground": "#009999"
    },
    "elixir.credo.configurationFile": "/home/user/workspace/my_app/.credo.exs"
}

This held true for both the User settings and Workspace settings.

Perhaps there is an issue when credo is building the path to find the file?

Okay, great that we have a workaround for this issue! 👍🏽

I think, the main problem is that the extension searchs for the credo configuration inside the folder of the mix project of the active elixir file. And since in each directory, there is a mix.exs file, it assumes that the project folder has been found and tries to look for the credo.exs there instead of one directory further up.

const projectFolder = documentUri ? CredoUtils.getProjectFolder(documentUri) : undefined
const found = projectFolder
? [path.join(projectFolder, configFilePath), path.join(projectFolder, 'config', configFilePath)].filter(
(fullPath: string) => fs.existsSync(fullPath),
)
: []

If that is the case, then perhaps the mix.exs file itself can help.
Here is an example of a mix.exs file from a typical Phoenix application:

  def project do
    [
      app: :my_app,
      version: "0.1.0",
      elixir: "~> 1.16",
      elixirc_paths: elixirc_paths(Mix.env()),
      start_permanent: Mix.env() == :prod,
      aliases: aliases(),
      deps: deps(),
      compilers: [:yecc] ++ Mix.compilers()
      ]
    ]
  end

And this is the mix.exs file from an umbrella app:

  def project do
    [
      app: :auction_house,
      version: "3.0.1",
      build_path: "../../_build",
      config_path: "../../config/config.exs",
      deps_path: "../../deps",
      lockfile: "../../mix.lock",
      elixir: "~> 1.16",
      elixirc_paths: elixirc_paths(Mix.env()),
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      test_coverage: [tool: ExCoveralls],
      preferred_cli_env: preferred_cli_env(),
      aliases: aliases()
    ]
  end

Notice we have specific fields, such as build_path, config_path, deps_path, etc.

Perhaps we can leverage this? If no .credo.exs file is found in the current directory, we could try to follow the build_path, for example.

I would love to get this fixed, and happy to even contribute if needed. How about we use the lockfile path? I'm not sure if people tend to change any of these configs from the defaults, but I would assume the lockfile is at least always at the project root, where the umbrella mix.exs is as well.

Hm, I don't think parsing the mix.exs is something we should do. Instead, Since the structure of elixir umbrella apps is always:

  • parent_folder/
    • mix.exs
    • credo.exs / config/credo.exs
    • apps/
      • app1/
        • mix.exs
      • app2/
        • mix.exs

We just have to change the detection of this piece right here:

const projectFolder = documentUri ? CredoUtils.getProjectFolder(documentUri) : undefined
const found = projectFolder
? [path.join(projectFolder, configFilePath), path.join(projectFolder, 'config', configFilePath)].filter(
(fullPath: string) => fs.existsSync(fullPath),
)
: []

It has to support that the detected mix project folder is part of an apps directory that is an elixir project (i.e., has an own mix.exs).

I've drafted a PR that should fix that, I just have to find some time to fix and extend the tests: #473