# Randomly getting CompilationError on tests

**URL:** <https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298>\
**Category:** Questions\
**Tags:** exunit\
**Created:** [October 15, 2018, 2:51pm UTC](https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298 "2018-10-15T14:51:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![victorolinasc](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/victorolinasc/32/10667_2.png) [@victorolinasc](https://forum.elixirforum.com/u/victorolinasc)\
**Post date:** [October 15, 2018, 2:51pm UTC](https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298/1 "2018-10-15T14:51:36Z")

</div>

In a project I am working, I am randomly getting compilation errors while running tests. The stack trace is like this (changed file names):

```elixir
......................
== Compilation error in file test/some_test.exs ==
** (UndefinedFunctionError) function File.regular?/1 is undefined or private
    (elixir) File.regular?("~/dev/some_project//test/some_test.exs")
    (elixir) lib/code.ex:1144: Code.find_file/2
    (elixir) lib/code.ex:754: Code.require_file/2
    (elixir) lib/kernel/parallel_compiler.ex:209: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6

```

It complains about `File.regular?/1` that is part of the core library. In my `test_helper.exs` I have:

```elixir
File.mkdir_p("/tmp/junit-reports")
ExUnit.configure(formatters: [JUnitFormatter, ExUnit.CLIFormatter])
ExUnit.start()

```

So I thought that it could be it (the first line there). I’ve added `{:module, File} = Code.ensure_compiled(File)` but it did not help.

Not sure where to look next. I am on:

- Erlang 21.1 (asdf)
- Elixir 1.7.3-otp-21 (asdf)
- Linux arm64 (Fedora 28)

Has anyone suffered a similar issue? I’d appreciate some help as this is killing productivity. I think it might have to do with the language server but can’t be 100% sure.

**Edited** it is AMD64 not ARM64… my bad =/

---

<div class="post-metadata">

**Author:** ![aseigo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/aseigo/32/10674_2.png) [@aseigo](https://forum.elixirforum.com/u/aseigo)\
**Post date:** [October 15, 2018, 7:25pm UTC](https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298/2 "2018-10-15T19:25:10Z")

</div>

None of those functions call into `File.regular?/1`. The only one I could find in ExUnit was `ExUnit.FailuresManifest.write!/2` which is called from `ExUnit.RunnerStats` in a `handle_cast` implementation that takes a tuple starting with `:suite_finished` … but that shouldn’t crap out during running tests, if anything it would do so during building Elixir itself …

I see that you are building on arm64 .. could it be that this is a deeper issue like running out of memory when running specific test suites on a low-mem (relatively?) system? If not that, perhaps something arm64 specific. Are you using a self-built OTP / Elixir stack?

---

<div class="post-metadata">

**Author:** ![josevalim](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/32/1787_2.png) [@josevalim](https://forum.elixirforum.com/u/josevalim)\
**Post date:** [October 15, 2018, 7:42pm UTC](https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298/3 "2018-10-15T19:42:26Z")

</div>

Are any of your tests using `File.cd!` or similar? If so, this means the test is running and changing the directory while the test suite is still loading files. You have to set these tests to `async: false` (or remove the async config, which is false by default).

---

<div class="post-metadata">

**Author:** ![victorolinasc](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/victorolinasc/32/10667_2.png) [@victorolinasc](https://forum.elixirforum.com/u/victorolinasc)\
**Post date:** [October 15, 2018, 8:51pm UTC](https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298/4 "2018-10-15T20:51:00Z")

</div>

I have more than 16GB of fre RAM and plenty of disk space so I guess that is not it…

Also it is AMD64 not arm… sorry for my mistake. Should have written x64 that is easier.

---

<div class="post-metadata">

**Author:** ![victorolinasc](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/victorolinasc/32/10667_2.png) [@victorolinasc](https://forum.elixirforum.com/u/victorolinasc)\
**Post date:** [October 15, 2018, 8:53pm UTC](https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298/5 "2018-10-15T20:53:14Z")

</div>

I will double check that. My guess is that the language server trie to compile the files too but what you say makes sense.

JUnitFormatter creates a report when the suite ends but that’s about it. I create the dir on `test_helper.exs` and JUnitFormatter writes the file when it ends.

I really think we are not using `File.cd!` in our test suite. Be right back when I am 100% positive on that.

**EDITED** : 100% sure we are not using a single call to `File.` in our test base (just ran `grep -rnw '.' -e 'File.'` in test folder)

---

<div class="post-metadata">

**Author:** ![josevalim](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/32/1787_2.png) [@josevalim](https://forum.elixirforum.com/u/josevalim)\
**Post date:** [October 15, 2018, 9:20pm UTC](https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298/6 "2018-10-15T21:20:42Z")

</div>

> [@victorolinasc](#):
>
> I will double check that. My guess is that the language server trie to compile the files too but what you say makes sense.

Compilation would definitely trigger this, especially in an umbrella project. But isn’t that supposed to be happening in a separate process? If you can by any chance isolate this, I would be glad to take a look.

---

<div class="post-metadata">

**Author:** ![victorolinasc](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/victorolinasc/32/10667_2.png) [@victorolinasc](https://forum.elixirforum.com/u/victorolinasc)\
**Post date:** [October 15, 2018, 9:31pm UTC](https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298/7 "2018-10-15T21:31:31Z")

</div>

I will switch the language server off and try to simulate the error. At least that will exclude that possibility. I think its that because on CI it doesn’t fail. 🙂

(should have mentioned it before)

Be right back with results …

**EDITED** nope. It’s not the language server… Can reproduce it even without the language server and without JUnitFormatter configured.

It shows on different test files each time and I’ve marked them with async: false but it didn’t help. It looks like it happens before running.

---

<div class="post-metadata">

**Author:** ![victorolinasc](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/victorolinasc/32/10667_2.png) [@victorolinasc](https://forum.elixirforum.com/u/victorolinasc)\
**Post date:** [October 15, 2018, 9:49pm UTC](https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298/8 "2018-10-15T21:49:09Z")

</div>

Found it… god damnit!

One endpoint of our API replies with the content of a static file. Like a constant response that we cache at application startup…

In our tests someone used the `Mock` library to mock the `File.read/1` call. That test was async…

The code is like this:

```elixir
    with_mock File, read: fn _filename -> {:ok, "12345"} end do
      assert FileLoader.load("priv://dir/file") == "12345"
    end

```

My earlier grep couldn’t find it. So I started running tests with `mix test --trace` which showed me the bad test… Should have done that before posting here! Sorry.

I hate mocks… marking it as `async: false` seems to have solved it. Thanks for your help and support!

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [October 16, 2018, 6:44pm UTC](https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298/9 "2018-10-16T18:44:09Z")

</div>

> [@victorolinasc](#):
>
> I hate mocks… marking it as `async: false` seems to have solved it. Thanks for your help and support!

In place of mocking you can always pass module names into functions just as general usage, that’s what I do and it let’s you have a lot more control as well. 🙂

This is like the Witness pattern for note. 🙂

---

<div class="post-metadata">

**Author:** ![victorolinasc](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/victorolinasc/32/10667_2.png) [@victorolinasc](https://forum.elixirforum.com/u/victorolinasc)\
**Post date:** [October 16, 2018, 7:18pm UTC](https://forum.elixirforum.com/t/randomly-getting-compilationerror-on-tests/17298/10 "2018-10-16T19:18:06Z")

</div>

Totally agree. Will refactor it (eventually) to avoid this. This is classical technical debt being carried over which is not the language’s fault. We, the dev team, all learned something from it though 🙂
