Skip to content

feat: return compiler diagnostics from reload #6

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

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: CI
on:
pull_request:
push:
branches: main

jobs:
tests:
runs-on: ubuntu-latest
name: Test (${{matrix.elixir}}/${{matrix.otp}})

strategy:
fail-fast: false
matrix:
elixir: [1.17.x, 1.18.x]
otp: [26.x, 27.x]

steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
id: install
with:
otp-version: ${{matrix.otp}}
elixir-version: ${{matrix.elixir}}
- uses: actions/cache@v4
id: cache
with:
path: |
deps
key: ${{ runner.os }}-mix-${{steps.install.outputs.otp-version}}-${{steps.install.outputs.elixir-version}}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-mix-${{steps.install.outputs.otp-version}}-${{steps.install.outputs.elixir-version}}-

- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: mix deps.get

- name: Run Tests
run: mix test

formatter:
runs-on: ubuntu-latest
name: Formatter (${{matrix.elixir}}/${{matrix.otp}})

strategy:
matrix:
otp: [27.x]
elixir: [1.17.x]

steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
id: install
with:
otp-version: ${{matrix.otp}}
elixir-version: ${{matrix.elixir}}
- uses: actions/cache@v4
id: cache
with:
path: |
deps
_build
key: ${{ runner.os }}-mix-${{steps.install.outputs.otp-version}}-${{steps.install.outputs.elixir-version}}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-mix-${{steps.install.outputs.otp-version}}-${{steps.install.outputs.elixir-version}}-

- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: mix deps.get

- name: Run Formatter
run: mix format --check-formatted
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ web_dev_utils-*.tar

# Temporary files, for example, from tests.
/tmp/

/test/support/editable.ex
13 changes: 7 additions & 6 deletions lib/web_dev_utils/code_reloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ defmodule WebDevUtils.CodeReloader do
{:ok, :nostate}
end

def handle_call(:reload, from, state) do
froms = all_waiting([from])
mix_compile(Code.ensure_loaded(Mix.Task))
Enum.each(froms, &GenServer.reply(&1, :ok))
def handle_call(:reload, _from, state) do
froms = all_waiting([])
result = mix_compile(Code.ensure_loaded(Mix.Task))
Enum.each(froms, &GenServer.reply(&1, result))

{:noreply, state}
{:reply, result, state}
end

defp all_waiting(acc) do
Expand All @@ -43,10 +43,11 @@ defmodule WebDevUtils.CodeReloader do

defp mix_compile({:error, _reason}) do
Logger.error("Could not find Mix")
:error
end

defp mix_compile({:module, Mix.Task}) do
Mix.Task.reenable("compile.elixir")
Mix.Task.run("compile.elixir")
Mix.Task.run("compile.elixir", ["--return-errors"])
end
end
5 changes: 5 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule WebDevUtils.MixProject do
"Library to enable awesome local development for websites and web applications",
version: "0.2.0",
elixir: "~> 1.14",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
package: package(),
deps: deps()
Expand All @@ -23,6 +24,10 @@ defmodule WebDevUtils.MixProject do
]
end

# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]

# Run "mix help deps" to learn about dependencies.
defp deps do
[
Expand Down
Empty file added test/support/.keep
Empty file.
47 changes: 47 additions & 0 deletions test/web_dev_utils/code_reloader_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule WebDevUtils.CodeReloaderTest do
use ExUnit.Case, async: true
import ExUnit.CaptureIO

alias WebDevUtils.CodeReloader

setup do
on_exit(fn ->
File.rm("test/support/editable.ex")
end)

:ok
end

test "noops when nothing has changed" do
start_supervised!(CodeReloader)

assert {_, []} = CodeReloader.reload()
assert {:noop, []} == CodeReloader.reload()
end

test "recompiles code" do
File.write!("test/support/editable.ex", """
defmodule Editable do
end
""")

start_supervised!(CodeReloader)

capture_io(:stderr, fn ->
assert {:ok, []} == CodeReloader.reload()
end)
end

test "recompiles code and something fails" do
File.write!("test/support/editable.ex", """
defmodule Editable d
end
""")

start_supervised!(CodeReloader)

capture_io(:stderr, fn ->
assert {:error, [%Mix.Task.Compiler.Diagnostic{}]} = CodeReloader.reload()
end)
end
end
8 changes: 0 additions & 8 deletions test/web_dev_utils_test.exs

This file was deleted.