The error {"no such file or directory", "cowboy_dirindex.app"} indicates that Erlang's application loader can't find the .app file for your application, cowboy_dirindex (which is my_index_server in our example).
This is a common issue when using Rebar3. Here's what's happening:
Rebar3 Project Name: When you ran rebar3 new app my_index_server, Rebar3 created a skeleton project named my_index_server. This name is used in various places, including the .app.src file (more on this below).
.app.src File: Inside src/, there's a file named my_index_server.app.src. This is a template for the .app file that Rebar3 will generate during compilation. The .app file is crucial because it tells Erlang about your application: its name, version, dependencies, and modules.
Application Name Mismatch: The error suggests that somewhere in your code or configuration, Erlang is looking for an application named cowboy_dirindex, but your actual application is named my_index_server.
Fix:
You need to ensure consistency in your application name across all relevant files. Since our project is named my_index_server, let's stick with that.
Check src/my_index_server.app.src:
This file should exist and look something like this:
Important: Verify that {application, my_index_server, ...} matches your project name.
rebar.config:
Ensure that the relx section (if you have one) or any other part of rebar.config refers to my_index_server, not cowboy_dirindex. However, in our simple case, we don't have a relx release configuration, so this step is skipped.
application.erl:
Double-check that the -module(application). and the application:start(my_index_server). command in the Erlang shell match your application name. We've already done this correctly in our example:
erlang
-module(application).
-behaviour(application).
And you start it with:
erlang
application:start(my_index_server).
Clean and Recompile:
Sometimes, Rebar3's caches can get out of sync. Run:
application:ensure_all_started(my_index_server) is a safer way to start your application and all its dependencies (cowboy in this case). It checks if cowboy is already started and starts it if necessary, then starts my_index_server. Using application:start(my_index_server) directly might fail if cowboy isn't already running.
If You Accidentally Named It cowboy_dirindex:
If you meant to name your application cowboy_dirindex but Rebar3 created my_index_server, here's a quick rename guide:
Rename the directory: mv my_index_server cowboy_dirindex
Edit rebar.config:
Change {project_app_dirs, ["my_index_server"]}. to {project_app_dirs, ["cowboy_dirindex"]}. (if it exists).
Verify {deps, ...} and other sections are correct.
Rename src/my_index_server.app.src to src/cowboy_dirindex.app.src.
Inside src/cowboy_dirindex.app.src, change {application, my_index_server, ...} to {application, cowboy_dirindex, ...}.
Run rebar3 clean -a and rebar3 compile.
Start with application:ensure_all_started(cowboy_dirindex). in the Erlang shell.
By following these steps, you should resolve the {"no such file or directory", "cowboy_dirindex.app"} error and get your Cowboy-based directory index server up and running!
rebar.config:{apps, [cowboy_dirindex]} instructs Rebar3 to look for an application named cowboy_dirindex.
Erlang OTP Convention: For an application named cowboy_dirindex, Erlang expects the main application module to be cowboy_dirindex.erl, implementing the application behaviour.
.app File: When Rebar3 compiles your project, it generates a cowboy_dirindex.app file (in _build/default/lib/cowboy_dirindex/ebin/). This .app file contains:
The crucial part is {mod,{cowboy_dirindex,[]}}. This tells Erlang: "When starting cowboy_dirindex, call the start/2 function in the cowboy_dirindex module."
By renaming cowboy_dirindex_app.erl to cowboy_dirindex.erl, you align your module name with the application name specified in rebar.config and the OTP conventions.
Additional Tips:
Consistency: Always ensure your:
rebar.config{apps, [AppName]} matches
src/AppName.erl (the application module) matches
src/AppName.app.src (if you have one) contains {application, AppName, ...}
_app Suffix: Only use the _app suffix in the module name if your application name already ends with _app (e.g., my_app_app would be correct for an app named my_app_app). For cowboy_dirindex, the correct module is simply cowboy_dirindex.
relx Releases: If you were building a release with relx, the release section in rebar.config would also need to reference cowboy_dirindex correctly.
Now, run:
bash
rebar3 shell
Your cowboy_dirindex application should start correctly, and you can verify it with: