Skip to content

Commit 2b424ca

Browse files
authored
Deal with relative paths in mix cmd, closes #14787 (#14788)
1 parent f089571 commit 2b424ca

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/mix/lib/mix/tasks/cmd.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,19 @@ defmodule Mix.Tasks.Cmd do
7575
end
7676

7777
if apps == [] or Mix.Project.config()[:app] in apps do
78+
path = hd(args)
79+
rest = tl(args)
80+
81+
path =
82+
if String.contains?(path, ["/", "\\"]) and Path.type(path) != :absolute do
83+
Path.expand(path, Keyword.get(opts, :cd, "."))
84+
else
85+
path
86+
end
87+
7888
cmd_opts = Keyword.take(opts, [:cd])
7989

80-
case Mix.shell().cmd({hd(args), tl(args)}, cmd_opts) do
90+
case Mix.shell().cmd({path, rest}, cmd_opts) do
8191
0 -> :ok
8292
status -> exit(status)
8393
end

lib/mix/test/mix/tasks/cmd_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ defmodule Mix.Tasks.CmdTest do
1414
assert_received {:mix_shell, :run, ["hello world\n"]}
1515
end
1616

17+
@tag :unix
18+
test "supports relative paths" do
19+
in_tmp("cmd-relative", fn ->
20+
File.mkdir_p!("priv")
21+
File.write!("priv/world.sh", "#!/bin/sh\necho world")
22+
File.chmod!("priv/world.sh", 0o755)
23+
24+
Mix.Task.run("cmd", ["priv/world.sh"])
25+
assert_received {:mix_shell, :run, ["world\n"]}
26+
27+
Mix.Task.run("cmd", ["--cd", "priv", "./world.sh"])
28+
assert_received {:mix_shell, :run, ["world\n"]}
29+
end)
30+
end
31+
1732
test "runs the command for each app" do
1833
in_fixture("umbrella_dep/deps/umbrella", fn ->
1934
Mix.Project.in_project(:umbrella, ".", fn _ ->

0 commit comments

Comments
 (0)