rseletsk's avatar

Running a CLI build results in runtime error

Have a CLI which built successfully on WSL via appname app:build This results is appname executable in the builds directory In the builds directory and running appname get runtime error: Database file at path ....appname/database/database.sqlite] does not exist But, appname is not a directory since is an executable

0 likes
3 replies
LaryAI's avatar
Level 58

Certainly! Here’s a clear solution for the issue posted on Laracasts:


Solution:

The error message indicates that your CLI application is trying to find the database file at a path relative to the running executable. After building your CLI (which is now a standalone executable), the working directory may be set to the builds directory (where the binary is), but the database file path is often hardcoded or relative to the project root (e.g., database/database.sqlite).

Why this happens:

  • When running a Laravel-based CLI app (built with e.g. Laravel Zero or Symfony Console), you may use relative paths for resources. But after compiling, the working directory/context might change.
  • appname is now an executable file and not a directory, so paths like ../database/database.sqlite may no longer resolve as expected.

How to solve:

  1. Ensure the database file is where the app expects it:
    Place the database.sqlite file in the expected path relative to where you're running the executable, or provide an absolute path.

  2. Change working directory handling:
    Update your code to compute the database path dynamically, based on the runtime location of the executable.

For example, in PHP:

$basePath = dirname($_SERVER['PHP_SELF']); // path to where the binary is
$databasePath = $basePath . '/database/database.sqlite';

if (!file_exists($databasePath)) {
    throw new Exception("Database file at path [$databasePath] does not exist");
}

Or, if you want to always use a path relative to the original source directory (not the executable), set a config or environment variable so your app knows where to look for the database file.

  1. Provide a command-line flag or environment variable:
    Modify your CLI to allow specifying a database path:
php appname --db=/absolute/path/to/database.sqlite

Or set an environment variable:

export DB_PATH=/absolute/path/to/database.sqlite

And in your code, check for it:

$databasePath = getenv('DB_PATH') ?: ($basePath . '/database/database.sqlite');
  1. Copy the database to the build directory:
    If you want a quick fix, try copying your database folder (with database.sqlite inside) into your builds directory:
cp -r database builds/

Summary:
Adjust how you reference the database file path in your code, or copy the database file to the correct place in your builds output directory. For maximum portability, use config/env variables to specify the database location, falling back to a relative path as needed.

Let me know if you’d like a more specific example based on your CLI framework or bootstrap code!

rseletsk's avatar

Runtime error is: Database file at path [phar: ... /database/database.sqlite] does not exist

rseletsk's avatar

Resolved this issue via setting DB_DATABASE

Please or to participate in this conversation.