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 2 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
4 changes: 3 additions & 1 deletion lib/mix/lib/mix/tasks/release.init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ defmodule Mix.Tasks.Release.Init do
#!/bin/sh
set -e

SELF=$(readlink "$0" || true)
SELF=$(readlink -f "$0" || true)
if [ -z "$SELF" ]; then SELF="$0"; fi
if [ "${SELF#/}" = "$SELF" ]; then SELF="$(cd "$(dirname "$0")" && pwd -P)/$(basename "$SELF")"; fi
SELF=$(cd "$(dirname "$SELF")" && pwd -P)/$(basename "$SELF")
RELEASE_ROOT="$(CDPATH='' cd "$(dirname "$SELF")/.." && pwd -P)"
export RELEASE_ROOT
RELEASE_NAME="${RELEASE_NAME:-"<%= @release.name %>"}"
Expand Down
54 changes: 54 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,60 @@ 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")

script_name = "release_test"

script_name =
if match?({:win32, _}, :os.type()), do: "#{script_name}.bat", else: script_name

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

try do
script = Path.absname(script_name)
{hello_world, 0} = System.cmd(script, ["eval", "IO.puts :hello_world"])
assert String.trim_trailing(hello_world) == "hello_world"
after
File.rm!(script_name)
end
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")

script_name = "release_test"

script_name =
if match?({:win32, _}, :os.type()), do: "#{script_name}.bat", else: script_name

File.mkdir!("bin")

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

try do
script = Path.absname(Path.join("bin", script_name))
{hello_world, 0} = System.cmd(script, ["eval", "IO.puts :hello_world"])
assert String.trim_trailing(hello_world) == "hello_world"
after
File.rm_rf!("bin")
end
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