Phx.gen.auth - Could not find "use Mix.Config" or "import Config"

Trying to use phx.gen.auth to get started on the authentication framework for an existing project. Running phx.gen.auth on a brand new project works as expected but the existing project returns the error:

** (Mix) Could not find “use Mix.Config” or “import Config” in “./config/test.exs”

and ends the program. The test.exs file at that location has use Mix.Config as the first line, but this issue still happens.

Why is the use Mix.Config line being missed and how would I be able to finish running phx.gen.auth?

@ChrisMD2 Could you please share your config/test.exs or provide a minimal repo in order to reproduce issue?

You said that for new project it works as expected. Therefore based on your description we only know that this is “existing project” and nothing more. It’s not possible to guess what you could do wrong. :slight_smile:

I’m not sure what I can share about this project at the moment, but the new project was my attempt at reproducing the error. I wonder if anyone else has encountered this error.

So what I have that I can share is this project https://github.com/chrismd2/training_srvr-minimal which had code taken out but will still produce the error.

I made clone, called deps.get task, deps.compile, compile and everything worked. I quickly changed only db credentials to match my current setup and I was able to call ecto.setup without a problem.

After that I have called: mix phx.gen.auth Accounts User users command (which is first example from documentation) and everything worked as expected. I was again in need to update db credentials for test environment and right after that mix test returned only this one error:

$ mix test
Compiling 40 files (.ex)
warning: variable "result" is unused (if the variable is not meant to be used, prefix it with an underscore)
  lib/training_srvr_web/controllers/upload_controller.ex:43: TrainingSrvrWeb.UploadController.save_image/2

Generated training_srvr app
....................................
== Compilation error in file test/training_srvr/videos_test.exs ==
** (CompileError) test/training_srvr/videos_test.exs:133: def video_frames_image_classes_fixture/1 defines defaults multiple times. Elixir allows defaults to be declared once per definition. Instead of:

    def foo(:first_clause, b \\ :default) do ... end
    def foo(:second_clause, b \\ :default) do ... end

one should write:

    def foo(a, b \\ :default)
    def foo(:first_clause, b) do ... end
    def foo(:second_clause, b) do ... end

    test/training_srvr/videos_test.exs:133: (module)
    (stdlib 3.13) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir 1.10.4) lib/kernel/parallel_compiler.ex:396: Kernel.ParallelCompiler.require_file/2
    (elixir 1.10.4) lib/kernel/parallel_compiler.ex:306: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

If that go so far I guess that configuration was good.

Did you called something else? Maybe different phx.gen.auth call? If it’s not that I would re-check your Elixir installation. I have latest Elixir installed using asdf version manager.

Ok, I deleted and cloned the repository to be sure. Then I upgraded Elixir from 1.10.3 to 1.10.4. Followed the steps you described for mix deps.get, deps.compile, compile, ecto.setup, and phx.gen.auth Accounts User users. This is where I get the same “… Could not find …” error. Is it possible that I am getting this error due to database credentials? I’m new to Elixir and Phoenix but I could imagine there is a setting from the initial setup on the creator’s machine that I need to change for mine so the generator will work.

First of all for database credentials we have different errors, so no need to worry about it. If I remember correctly files in config directory (except releases.exs and runtime.exs) are evaluated at compile time and database connection happens on runtime (when repo is started in your app supervisor tree), so there is no way to reach database credentials error without fixing compile-time problems like for example syntax errors.

That’s really weird and now I’m really curious what happen. I took a look at source code of this library and found something interesting …


As we can see it raises error not for missing use Mix.Config or import Config, but for those strings: use Mix.Config\n or import Config\n instead. That may be a weak point. For me it works, but what if you for example you have added some comments like:
import Config # my comment is here

or when you have \r\n as newline (that’s how Windows handles them).

You said that you have deleted and cloned again repository, so everything should be good, but anyway just for sure can you please cd to your (minimal) project directory, run iex, type:

File.read!("config/test.exs")

and paste whole output as-is?

1 Like

Commenting the end of the line didn’t work. Here is the output from the iex File.read!\1:

“use Mix.Config #This line is a nuisance\r\n\r\n# Configure your database\r\n#\r\n# The MIX_TEST_PARTITION environment variable can be used\r\n# to provide built-in test partitioning in CI environment.\r\n# Run mix help test for more information.\r\nconfig :training_srvr, TrainingSrvr.Repo,\r\n username: “postgres”,\r\n password: “postgres”,\r\n database: “training_srvr_test#{System.get_env(“MIX_TEST_PARTITION”)}”,\r\n hostname: “localhost”,\r\n pool: Ecto.Adapters.SQL.Sandbox\r\n\r\n# We don’t run a server during test. If one is required,\r\n# you can enable the server option below.\r\nconfig :training_srvr, TrainingSrvrWeb.Endpoint,\r\n http: [port: 4002],\r\n server: false\r\n\r\n# Print only warnings and
errors during test\r\nconfig :logger, level: :warn\r\n”

Try saving the config with linux line endings instead of the windows ones.

1 Like

Sorry, I was not precise …

I said that this task is not only checking mentioned strings, but also \n (newline character) right after them.

That was rhetorical question with providing an example which should not work. Same goes if you have anything else before \n (newline character) for example:

# this would work:
import Config\n(…)
# as well as this:
use Mix Config\n(…)
# that would not compile, but in fact mentioned check could pass:
crazy_import Config\n(…)
# that would NOT work:
import Config\r(…)
# as well as:
use Mix.Config # some comment\n(…)

Thanks a lot for your help Eiji. It turned out to be the \r\n imposed by the operating system. I split the output of the read into a list of strings separated at all occurrences of \r and rewrote the list back into test.exs.