Test Broken by move to 1.14, I can't find the specific change that causes this

This test:

defmodule ThrowawayTest do
  use ExUnit.Case

  {:module, _name, bytecode, _exports} =
    defmodule TestModule do
      @type my_type() :: :a | :b | :c | :d
      def hello, do: 1
    end

  @bytecode bytecode

  test "generates the expected types" do
    assert {:ok, [type: {:my_type, _, []}]} = Code.Typespec.fetch_types(@bytecode)
  end
end

works in 1.13.4-otp-25
but is broken in 1.14.0-otp-25

It appears that nested module are no longer written to disk.

Is this a known change in the 1.14 release or a side effect?

1 Like

What error do you get?

1 Like

Code.Typespec.fetch_types(@bytecode) returns :error

I suspect that the inner module is no longer written to disk.

Just shooting in the dark: does this post help?

I have a fix for the problem (move to a real .ex module and using Code.compile_file/2, itā€™s more that I canā€™t trace the source of the problem to the 1.14 release notes.

This is a minimal reconstruction of the issue, not the full test file that I am working with.

This is strange, because @bytecode is an actual bytecode of a module. In this form, module doesnā€™t have to be persisted

Yet the test still fails.

Its easy to test with an asdf setup.

$ elixir x.ex
{:ok,
 [
   type: {:my_type,
    {:type, 5, :union,
     [{:atom, 0, :a}, {:atom, 0, :b}, {:atom, 0, :c}, {:atom, 0, :d}]}, []}
 ]}
$ cat x.ex
defmodule ThrowawayTest do

  {:module, _name, bytecode, _exports} =
    defmodule TestModule do
      @type my_type() :: :a | :b | :c | :d
      def hello, do: 1
    end

  @bytecode bytecode

  def test do
    Code.Typespec.fetch_types(@bytecode)
  end

end

IO.inspect ThrowawayTest.test
$ elixir --version
Erlang/OTP 24 [erts-12.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Elixir 1.14.0 (compiled with Erlang/OTP 24)

For me it works correctly.

And in ExUnit test this works correctly too

$ mix test
Compiling 1 file (.ex)
Generated xxx app
.

Finished in 0.03 seconds (0.00s async, 0.03s sync)
1 test, 0 failures

Randomized with seed 488666
$ cat test/xxx_test.exs
defmodule ThrowawayTest do
  use ExUnit.Case

  {:module, _name, bytecode, _exports} =
    defmodule TestModule do
      @type my_type() :: :a | :b | :c | :d
      def hello, do: 1
    end

  @bytecode bytecode

  test "generates the expected types" do
    assert {:ok, [type: {:my_type, _, []}]} = Code.Typespec.fetch_types(@bytecode)
  end
end

This is in an .exs file

Yeah, .exs works correctly too.
Iā€™ve shown it here: Test Broken by move to 1.14, I can't find the specific change that causes this - #9 by hissssst

I am using otp 25

This works correctly on both OTP 25 and OTP 24 for me

FWIW Code.Typespec is a private module (@moduledoc false) so thereā€™s no backwards compatibility guarantee.

3 Likes

What do you suggest instead of? because I can use this code in my iex and ex file without any problem, but inside elixir test file it returns :error

{:module, name, bytecode_test, _exports} =
      defmodule TestOne do
      end
Code.Typespec.fetch_types(bytecode_test)

MacOs last version

Erlang/OTP 26 [erts-14.0.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]

Elixir 1.15.1 (compiled with Erlang/OTP 26)

@hst337 Please test it inside your unit test mix test in a elixir project, for example test this project GitHub - ejpcmac/typed_struct: An Elixir library for defining structs with a type without writing boilerplate code., this project uses this function inside its test

To clarify this doesnā€™t just work in test, it works everywhere else

Update:

I check it for Protocol and it works, but for module it just returns :error


The problem is because of test_elixirc_options: [debug_info: true]

you can see this issue