Showing posts with label elixir. Show all posts
Showing posts with label elixir. Show all posts

Friday, March 18, 2022

Elixir: Emacs lsp-mode: Unchecked dependencies for environment test

Source: https://github.com/elixir-lsp/vscode-elixir-ls/issues/132#issuecomment-743332723
Sometimes, the lsp-mode of Emacs remains stubbornly stuck on an error like:
MIX_ENV: test
MIX_TARGET: host
Unchecked dependencies for environment test:
* decimal (Hex package)
  the dependency does not match the requirement "~> 1.0", got "2.0.0"

18:56:16.378 [error] Process #PID<0.122.0> raised an exception
** (Mix.Error) Can't continue due to errors on dependencies
    (mix 1.13.1) lib/mix.ex:515: Mix.raise/2
    (mix 1.13.1) lib/mix/tasks/deps.loadpaths.ex:34: Mix.Tasks.Deps.Loadpaths.run/1
    (mix 1.13.1) lib/mix/task.ex:397: anonymous fn/3 in Mix.Task.run_task/3
    (mix 1.13.1) lib/mix/tasks/loadpaths.ex:40: Mix.Tasks.Loadpaths.run/1
    (mix 1.13.1) lib/mix/task.ex:397: anonymous fn/3 in Mix.Task.run_task/3
    (mix 1.13.1) lib/mix/tasks/compile.ex:123: Mix.Tasks.Compile.run/1
    (mix 1.13.1) lib/mix/task.ex:397: anonymous fn/3 in Mix.Task.run_task/3
    (language_server 0.8.2) lib/language_server/build.ex:200: ElixirLS.LanguageServer.Build.compile/0

In order to get out of this situation, do
> rm -rf .elixir_ls/build

Tuesday, March 8, 2022

Elixir: lsp-mode: No mixfile found in project. To use a subdirectory, set `elixirLS.projectDir` in your settings.

I'm using a mono-repo on GitHub, and this is the error message I get in Emacs/*lsp-log* buffer when adding a new sub-directory in the repo.
In order to fix that, you can update ~/.emacs.d/.lsp-session-v1 to manually add the sub-directory.
> vi ~/.emacs.d/.lsp-session-v1

#s(lsp-session ("/Users/jerome/src/repo" "/Users/jerome/src/boss/sub-directory...

And then bounce Emacs.
Source: https://github.com/elixir-lsp/elixir-ls/issues/364

Wednesday, December 22, 2021

Elixir: clean deps

Source: How to remove unused deps from mix.lock
This removes the unused files, as well as cleans mix.lock.
> mix deps.clean --unused --unlock

Tuesday, December 21, 2021

Elixir: rename_keys

Elixir equivalent of Clojure rename_keys()
  @doc """
  Returns the map with the keys in kmap renamed to the values in kmap.
  """
  def rename_keys(map, kmap) when is_map(map) and is_map(kmap) do
    for {key, value} <- map,
        into: %{},
        do: if(new_key = Map.get(kmap, key), do: {new_key, value}, else: {key, value})
  end

Elixir: if key exists: update, otherwise: assoc

Remember Clojure: if key exists: update, otherwise: assoc? Let's do that with Elixir.
  @doc """
  If `key` exists in `map`, updates its value (assumed to be a list) by concatenating `value`.
  If `key` does not exist in `map`, creates `{key => [value]}` in `map`.
  ## Examples

    iex(35)> Tap.Worker.update_conj!(%{a: [1]}, :a, 2)
    %{a: [1, 2]}

    iex(36)> Tap.Worker.update_conj!(%{a: [1]}, :b, 2)
    %{a: [1], b: [2]}

  """
  @spec update_conj!(map, any, any) :: map
  def update_conj!(map, key, value) when is_map(map) do
    Map.update(map, key, [value], fn
      existing_values when is_list(existing_values) ->
        existing_values ++ [value]

      existing_value ->
        raise(ArgumentError, "Value is not a list: #{inspect(existing_value)}")
    end)
  end

Elixir: map to struct

Courtesy of Jose Valim: (https://groups.google.com/forum/#!msg/elixir-lang-talk/6geXOLUeIpI/L9einu4EEAAJ).
  def to_struct(kind, attrs) do
    struct = struct(kind)

    Logger.debug("attrs = #{inspect(attrs)}")

    Enum.reduce(Map.to_list(struct), struct, fn {k, _}, acc ->
      case Map.fetch(attrs, k) do
        {:ok, v} -> %{acc | k => v}
        :error -> acc
      end
    end)
  end

Wednesday, September 15, 2021

Elixir: convert map to keywords list

iex(20)> Enum.reduce(%{"code" => 33, "kind" => 2, "servid" => 13}, [], fn {k, v}, acc -> acc ++ [{String.to_atom(k), v}] end)
[code: 33, kind: 2, servid: 13]
or
iex(23)> for {k, v} <- %{"code" => 33, "kind" => 2, "servid" => 13}, do: {String.to_atom(k), v}
[code: 33, kind: 2, servid: 13]

Tuesday, September 7, 2021

elixir-ls: /usr/local/bin/elixir: line 231: exec: erl: not found

Source: Elixir on Emacs: exec erl not found
If after installing elixir-ls in emacs, you get this error reported in *elixir-ls::stderr* buffer, then install exec-path-from-shell by using M-x package-list-packages.