diff --git a/CHANGELOG.md b/CHANGELOG.md index ff860e2..8223ad6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 . diff --git a/private/elixir_bytecode.bzl b/private/elixir_bytecode.bzl index 2359411..33aea87 100644 --- a/private/elixir_bytecode.bzl +++ b/private/elixir_bytecode.bzl @@ -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} @@ -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, diff --git a/test/BUILD.bazel b/test/BUILD.bazel index caac24f..82932a4 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -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 diff --git a/test/beam_paths_test.sh b/test/beam_paths_test.sh new file mode 100755 index 0000000..9624f6f --- /dev/null +++ b/test/beam_paths_test.sh @@ -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"