Skip to content

Fix relative path resolution in "mix release" for symlinked scripts #13796

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 6 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 14 additions & 2 deletions lib/mix/lib/mix/tasks/release.init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,20 @@ defmodule Mix.Tasks.Release.Init do
#!/bin/sh
set -e

SELF=$(readlink "$0" || true)
if [ -z "$SELF" ]; then SELF="$0"; fi
readlink_f () {
original_dir=$(pwd)
cd "$(dirname "$1")" > /dev/null
filename="$(basename "$1")"
if [ -h "$filename" ]; then
result=$(readlink_f "$(readlink "$filename")")
else
result="$(pwd -P)/$filename"
fi
cd "$original_dir" > /dev/null
echo "$result"
}

SELF=$(readlink_f "$0")
RELEASE_ROOT="$(CDPATH='' cd "$(dirname "$SELF")/.." && pwd -P)"
export RELEASE_ROOT
RELEASE_NAME="${RELEASE_NAME:-"<%= @release.name %>"}"
Expand Down
36 changes: 36 additions & 0 deletions lib/mix/test/mix/tasks/release_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,42 @@ defmodule Mix.Tasks.ReleaseTest do
end)
end

test "works properly with an absolute symlink to release" do
in_fixture("release_test", fn ->
Mix.Project.in_project(:release_test, ".", fn _ ->
Mix.Task.run("release")

File.ln_s!(
"_build/dev/rel/release_test/bin/release_test",
"release_test"
)

script = Path.absname("release_test")
{hello_world, 0} = System.cmd(script, ["eval", "IO.puts :hello_world"])
assert String.trim_trailing(hello_world) == "hello_world"
end)
end)
end

test "works properly with a relative symlink to release" do
in_fixture("release_test", fn ->
Mix.Project.in_project(:release_test, ".", fn _ ->
Mix.Task.run("release")

File.mkdir!("bin")

File.ln_s!(
"../_build/dev/rel/release_test/bin/release_test",
"bin/release_test"
)

script = Path.absname("bin/release_test")
{hello_world, 0} = System.cmd(script, ["eval", "IO.puts :hello_world"])
assert String.trim_trailing(hello_world) == "hello_world"
end)
end)
end

defp open_port(command, args, env \\ []) do
env = for {k, v} <- env, do: {to_charlist(k), to_charlist(v)}
Port.open({:spawn_executable, to_charlist(command)}, [:hide, args: args, env: env])
Expand Down
Loading