Module start code not running automatically in iex shell

I’m a newcomer to Elixir and trying the tutorial exercises in the official site. I’m up to this page:
https://elixir-lang.org/getting-started/mix-otp/supervisor-and-application.html
I’ve entered the code as requested, but it does not seem to be running the start function in the main module when I run the iex shell (with iex.bat -S mix (the .bat is needed because I’m doing this in Windows)).

PS C:\Users\dan\Documents\Projects\elixir\kv> iex.bat -S mix
Interactive Elixir (1.13.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> KV.Registry.create(KV.Registry, “shopping”)
:ok
iex(2)> KV.Registry.lookup(KV.Registry, “shopping”)
** (exit) exited in: GenServer.call(KV.Registry, {:lookup, “shopping”}, 5000)
** (EXIT) no process: the process is not alive or there’s no process currently associated with the given name, possibly because its application isn’t started
(elixir 1.13.4) lib/gen_server.ex:1019: GenServer.call/3
iex(2)>

By the example text on the page, this should have resulted in a response, not an error.

It did start up the application:

iex(2)> Application.start(:kv)
{:error, {:already_started, :kv}}

but not the supervisor process.

When I manually start the supervisor it works:

iex(3)> KV.Supervisor.start_link(name: KV.Supervisor)
{:ok, #PID<0.152.0>}
iex(4)> KV.Registry.create(KV.Registry, “shopping”)
:ok
iex(5)> KV.Registry.lookup(KV.Registry, “shopping”)
{:ok, #PID<0.156.0>}

Also, when I create a test for it:

defmodule KVTest do
use ExUnit.Case
doctest KV

test “spawns registry” do
assert KV.Registry.lookup(KV.Registry, “shopping”) == :error

KV.Registry.create(KV.Registry, "shopping")
assert {:ok, bucket} = KV.Registry.lookup(KV.Registry, "shopping")

KV.Bucket.put(bucket, "milk", 1)
assert KV.Bucket.get(bucket, "milk") == 1

end
end

that test passes, so it’s starting the supervisor then, just not when I invoke the shell.

What am I missing?

Then another problem on the next page:

The test:

test “are temporary workers” do
assert Supervisor.child_spec(KV.Bucket, []).restart == :temporary
end

fails with this error:

  1. test are temporary workers (KV.BucketTest)
    test/kv/bucket_test.exs:9
    ** (KeyError) key :restart not found in: %{id: KV.Bucket, start: {KV.Bucket, :start_link, [[]]}}. Did you mean:

      * :start
    

    code: assert Supervisor.child_spec(KV.Bucket, []).restart == :temporary
    stacktrace:
    test/kv/bucket_test.exs:10: (test)

A bit later, when the tutorial has me launch the observer GUI, the GUI comes up fine (the text warns that it sometimes doesn’t work, but it does for me), but “kv” doesn’t show up in the list of applications in the Applications tab. It’s like the iex interface fails to recognize it as an application at all.

There are a lot of moving parts, hard to say whats going on. Can you push your project to github?

1 Like

Here you are:

Never mind… I just noticed a file in my project with unsaved changes in the editor, and when I saved it everything worked. Sometimes the answer is really simple! Thanks for offering to help, and I’m still learning lots of stuff about Elixir, Visual Studio Code, GitHub, and related things… I have many years of programming experience, just not in those environments so I’m a little shaky there. Thanks anyway!

1 Like

Something I ought to be familiar with from my long experience is that tech development includes a whole lot of banging your head against the wall for hours about something only to find out the solution is really trivial…

If you’ve learned to enable autosave, its worth it :slight_smile:

1 Like