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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ Adds a third way to supply Elixir: an already-compiled distribution.
moving a repository between the source and prebuilt paths is a `MODULE.bazel`
edit and nothing else.

### Fixed

- **Relative paths in compiled modules** (`private/elixir_bytecode.bzl`).
`elixirc` expands every source to an absolute path and stores it in the debug
info and docs chunks of each `.beam`, so the output changed with the sandbox
directory. `elixir_bytecode` now makes those paths relative to the execroot and
compiles with `ERL_COMPILER_OPTIONS=deterministic`.

## 1.2.0

First release from <https://github.com/bazelverse/rules_elixir>.
Expand Down
30 changes: 30 additions & 0 deletions private/elixir_bytecode.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ if [ -n "{erl_libs_path}" ]; then
export ERL_LIBS={erl_libs_path}
fi

# Keeps the absolute source path and the compile options out of the CInf chunk.
export ERL_COMPILER_OPTIONS=deterministic

{env}

{setup}
Expand All @@ -63,6 +66,33 @@ ${{ABS_ELIXIR_HOME}}/bin/elixirc \\
-o {out_dir} \\
{elixirc_opts} \\
{srcs}

# elixirc expands every source to an absolute path, which lands in the debug
# info (Dbgi) and the docs (Docs) of each module. The path holds the sandbox
# directory, so make it relative to the execroot.
"${{ABS_ELIXIR_HOME}}"/bin/elixir -e '
relative = fn path -> Path.relative_to_cwd(to_string(path)) end
rewrite = fn
~c"Dbgi", {{:debug_info_v1, backend, {{:elixir_v1, %{{file: file}} = map, specs}}}} ->
{{:debug_info_v1, backend, {{:elixir_v1, %{{map | file: relative.(file)}}, specs}}}}
~c"Docs", {{:docs_v1, anno, language, format, moduledoc, %{{source_path: path}} = meta, docs}} ->
{{:docs_v1, anno, language, format, moduledoc, %{{meta | source_path: String.to_charlist(relative.(path))}}, docs}}
_, term -> term
end
for beam <- Path.wildcard(Path.join(hd(System.argv()), "*.beam")) do
{{:ok, _module, chunks}} = :beam_lib.all_chunks(String.to_charlist(beam))
chunks =
for {{id, data}} <- chunks do
if id in [~c"Dbgi", ~c"Docs"] do
{{id, :erlang.term_to_binary(rewrite.(id, :erlang.binary_to_term(data)), [:compressed])}}
else
{{id, data}}
end
end
{{:ok, binary}} = :beam_lib.build_module(chunks)
File.write!(beam, binary)
end
' {out_dir}
""".format(
maybe_install_erlang = maybe_install_erlang(ctx),
erlang_home = erlang_home,
Expand Down
12 changes: 12 additions & 0 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ sh_test(
toolchains = ["@rules_elixir//tools:elixir_vars"],
)

sh_test(
name = "beam_paths_test",
size = "small",
srcs = ["beam_paths_test.sh"],
data = [":ebin"],
env = select({
"@erlang_config//:erlang_internal": ELIXIR_VARS_ENV_MAP_INTERNAL,
"//conditions:default": ELIXIR_VARS_ENV_MAP,
}),
toolchains = ["@rules_elixir//tools:elixir_vars"],
)

# Below tests are intentially failing, and checked by
# .github/workflows/test.yaml

Expand Down
40 changes: 40 additions & 0 deletions test/beam_paths_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#! /usr/bin/env bash
set -exo pipefail

if [[ -n "$ERLANG_RELEASE_TAR_SHORT_PATH" ]]; then
mkdir -p $(dirname "$OTP_INSTALL_PATH")
if mkdir "$OTP_INSTALL_PATH"; then
tar --extract \
--directory "$OTP_INSTALL_PATH" \
--file "$ERLANG_RELEASE_TAR_SHORT_PATH"
fi
fi

PATH="$ELIXIR_HOME/bin:$ERLANG_HOME/bin:$PATH"

beam=$(find -L . -name Elixir.Basic.beam | head -n 1)
test -n "$beam"

elixir -e '
chunk = fn beam, id ->
{:ok, _module, chunks} = :beam_lib.all_chunks(String.to_charlist(beam))
{_, data} = List.keyfind!(chunks, id, 0)
:erlang.binary_to_term(data)
end

beam = hd(System.argv())

{:debug_info_v1, _, {:elixir_v1, %{file: file}, _}} = chunk.(beam, ~c"Dbgi")
if Path.type(to_string(file)) != :relative, do: raise("absolute path in Dbgi: #{file}")

case chunk.(beam, ~c"Docs") do
{:docs_v1, _, _, _, _, %{source_path: path}, _} ->
if Path.type(to_string(path)) != :relative, do: raise("absolute path in Docs: #{path}")

_ ->
:ok
end

{:ok, {_, [compile_info: info]}} = :beam_lib.chunks(String.to_charlist(beam), [:compile_info])
if Keyword.has_key?(info, :source), do: raise("source in CInf: #{info[:source]}")
' "$beam"