Skip to content

Commit

Permalink
Detected version change on call to package
Browse files Browse the repository at this point in the history
Another belt to wear with the suspenders. When we build a package that
overwrites another package, if the version has changed, we delete the
old compiled code and the old package and start compilation from
scratch. This definitely prevents old beam files from ending up in the
new package.
  • Loading branch information
scohen committed Aug 15, 2023
1 parent 1ee0c31 commit a14f066
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions apps/server/lib/mix/tasks/package.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ defmodule Mix.Tasks.Package do
@execute_permisson 0o755

def run(args) do
Mix.Task.run(:compile)
{opts, _, _} = OptionParser.parse(args, @options)
default_path = Path.join([Mix.Project.build_path(), "package", "lexical"])
package_root = Keyword.get(opts, :path, default_path)

rebuild_on_version_change(package_root)

Mix.Task.run(:compile)
Mix.Shell.IO.info("Assembling build in #{package_root}")
File.mkdir_p!(package_root)

Expand All @@ -103,6 +105,21 @@ defmodule Mix.Tasks.Package do
end
end

defp rebuild_on_version_change(package_root) do
%{elixir: elixir_current, erlang: erlang_current} = Versions.current()

with {:ok, %{elixir: elixir_compiled, erlang: erlang_compiled}} <-
Versions.read(priv_path(package_root)) do
if elixir_compiled != elixir_current or erlang_compiled != erlang_current do
Code.put_compiler_option(:ignore_module_conflict, true)
Mix.Shell.IO.error("The version of elixir or erlang has changed. Forcing recompilation.")
File.rm_rf!(package_root)
Mix.Task.clear()
Mix.Task.run(:clean, ~w(--deps))
end
end
end

defp prepare(package_root) do
scratch_directory = Path.join(package_root, "scratch")
File.mkdir(scratch_directory)
Expand Down Expand Up @@ -246,7 +263,7 @@ defmodule Mix.Tasks.Package do
@priv_apps [:remote_control]

defp copy_priv_files(package_root) do
priv_dest_dir = Path.join(package_root, "priv")
priv_dest_dir = priv_path(package_root)

Enum.each(@priv_apps, fn app_name ->
case priv_dir(app_name) do
Expand All @@ -261,7 +278,7 @@ defmodule Mix.Tasks.Package do

defp write_vm_versions(package_root) do
package_root
|> Path.join("priv")
|> priv_path()
|> Versions.write()
end

Expand Down Expand Up @@ -316,4 +333,8 @@ defmodule Mix.Tasks.Package do
path
end
end

defp priv_path(package_root) do
Path.join(package_root, "priv")
end
end

0 comments on commit a14f066

Please sign in to comment.