Skip to content

Commit 2be5d6c

Browse files
committed
Add config option controlling sleep after mix task returns
1 parent 37359e8 commit 2be5d6c

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ Below is a list of configuration options supported by the ElixirLS Debugger. Con
336336
<dt>debugInterpretModulesPatterns</dt><dd>A list of globs specifying modules that should be interpreted</dd>
337337
<dt>projectDir</dt><dd>An absolute path to the directory where `mix.exs` is located - In VSCode, `${workspaceRoot}` can be used.</dd>
338338
<dt>excludeModules</dt><dd>A list of modules that should not be interpreted</dd>
339+
<dt>exitAfterTaskReturns</dt><dd>Should the debug session stop when mix task returns. Tasks that return early while the code continues running asynchronously require `false` setting. Defaults to `true`.</dd>
339340
</dl>
340341

341342
## Troubleshooting

apps/elixir_ls_debugger/lib/debugger/server.ex

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,7 @@ defmodule ElixirLS.Debugger.Server do
744744
:int.auto_attach([:break], build_attach_mfa(:breakpoint_reached))
745745
end
746746

747-
task = state.config["task"]
748-
args = state.config["taskArgs"]
749-
{_pid, task_ref} = spawn_monitor(fn -> launch_task(task, args) end)
747+
{_pid, task_ref} = spawn_monitor(fn -> launch_task(state.config) end)
750748

751749
{%{}, %{state | task_ref: task_ref}}
752750
end
@@ -1577,27 +1575,35 @@ defmodule ElixirLS.Debugger.Server do
15771575
end
15781576
end
15791577

1580-
defp launch_task(task, args) do
1578+
defp launch_task(config) do
15811579
# This fixes a race condition in the tests and likely improves reliability when using the
15821580
# debugger as well.
15831581
Process.sleep(100)
15841582

1583+
task = config["task"]
1584+
args = config["taskArgs"]
1585+
15851586
if args != [] do
15861587
Output.debugger_console("Running mix #{task} #{Enum.join(args, " ")}\n")
15871588
else
15881589
Output.debugger_console("Running mix #{task}\n")
15891590
end
15901591

1591-
Mix.Task.run(task, args)
1592+
res = Mix.Task.run(task, args)
15921593

1593-
Output.debugger_console(
1594-
"Mix.Task.run returned, sleeping.\nNote that debugger needs to be stopped manually.\n"
1595-
)
1594+
Output.debugger_console("Mix.Task.run returned:\n#{inspect(res)}\n")
15961595

1597-
# Starting from Elixir 1.9 Mix.Task.run will return so we need to sleep our
1598-
# process so that the code keeps running (Note: process is expected to be
1599-
# killed by stopping the debugger)
1600-
Process.sleep(:infinity)
1596+
if Map.get(config, "exitAfterTaskReturns", true) do
1597+
Output.debugger_console(
1598+
"Exiting.\nIf this behavior is undesired consider setting `sleepAfterTaskReturns` in launch config.\n"
1599+
)
1600+
else
1601+
# Starting from Elixir 1.9 Mix.Task.run will return so some task require sleeping
1602+
# process so that the code can keep running (Note: process is expected to be
1603+
# killed by stopping the debugger)
1604+
Output.debugger_console("Sleeping. The debugger will need to be stopped manually.\n")
1605+
Process.sleep(:infinity)
1606+
end
16011607
end
16021608

16031609
# Interpreting modules defined in .exs files requires that we first load the file and save any

0 commit comments

Comments
 (0)