Skip to content
Open
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
14 changes: 14 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ jobs:
! bazelisk test //:failing_test
! bazelisk test //:broken_test
! bazelisk test //:empty_test
- name: WARNINGS AS ERRORS FAIL
working-directory: test
run: |
# "! cmd" does not stop a "bash -e" step, so test the status
# explicitly.
for app in unused undef io_warn; do
if bazelisk build //warnings:$app 2> warnings_$app.log; then
echo "//warnings:$app built despite --warnings-as-errors"
exit 1
fi
cat warnings_$app.log
grep -F "Compilation failed due to warnings while using the --warnings-as-errors option" \
warnings_$app.log
done
- name: UPLOAD TEST LOGS
if: always()
uses: actions/upload-artifact@v4
Expand Down
20 changes: 18 additions & 2 deletions private/elixir_bytecode.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,26 @@ fi
{env}

{setup}
set -x
diagnostics=$(mktemp)
status=0
${{ABS_ELIXIR_HOME}}/bin/elixirc \\
-o {out_dir} \\
{elixirc_opts} \\
{srcs}
{srcs} 2>"$diagnostics" || status=$?
cat "$diagnostics" >&2
# Released Elixir ignores --warnings-as-errors in elixirc and exits 0 on
# warnings. Fail here instead. Once elixirc fails by itself, status is
# not 0 and this check does nothing. The launcher's warning about a
# latin1 file name encoding is not a compiler warning, so it is skipped.
if [ "$status" = 0 ] && [ -n "{warnings_as_errors}" ] && awk '
/^[[:space:]]*warning:/ && !/native name encoding of latin1/ {{ found = 1 }}
END {{ exit !found }}
' "$diagnostics"; then
echo "Compilation failed due to warnings while using the --warnings-as-errors option" >&2
status=1
fi
rm -f "$diagnostics"
exit "$status"
""".format(
maybe_install_erlang = maybe_install_erlang(ctx),
erlang_home = erlang_home,
Expand All @@ -73,6 +88,7 @@ ${{ABS_ELIXIR_HOME}}/bin/elixirc \\
out_dir = ebin.path,
elixirc_opts = " ".join([shell.quote(opt) for opt in ctx.attr.elixirc_opts]),
srcs = " ".join([f.path for f in ctx.files.srcs]),
warnings_as_errors = "1" if "--warnings-as-errors" in ctx.attr.elixirc_opts else "",
)

inputs = depset(
Expand Down
29 changes: 29 additions & 0 deletions test/warnings/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@rules_elixir//private:elixir_bytecode.bzl", "elixir_bytecode")

# Each source has one warning. With --warnings-as-errors the build must
# fail; .github/workflows/test.yaml checks that, so these are manual.
# unused: an unused variable, a compile-time warning
# undef: a call to an undefined remote function, a warning the
# compiler emits after the module is compiled
# io_warn: IO.warn at compile time, printed without indentation
[
elixir_bytecode(
name = name,
srcs = [name + ".ex"],
dest = name,
elixirc_opts = ["--warnings-as-errors"],
tags = ["manual"],
)
for name in [
"unused",
"undef",
"io_warn",
]
]

# The same warning as :unused, without --warnings-as-errors. It builds.
elixir_bytecode(
name = "allowed",
srcs = ["unused.ex"],
dest = "allowed",
)
7 changes: 7 additions & 0 deletions test/warnings/io_warn.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Warnings.IoWarn do
# A warning printed by code that runs at compile time. Unlike the
# compiler's own warnings, it is not indented.
IO.warn("a warning from IO.warn at compile time")

def f, do: :ok
end
3 changes: 3 additions & 0 deletions test/warnings/undef.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Warnings.Undef do
def f, do: NoSuchModule.nope()
end
6 changes: 6 additions & 0 deletions test/warnings/unused.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule Warnings.Unused do
def f(x) do
y = 1
x
end
end