nautechsystems/nautilus_trader · error
Could not calculate schema dir from current directory path o
Error message
Could not calculate schema dir from current directory path or SCHEMA_DIR env variable
What it means
init_postgres locates the SQL schema directory either from the SCHEMA_DIR env variable or by searching the current working directory path for the nautilus repo name. If SCHEMA_DIR is unset and the current directory path does not contain the repository name, the library bails with this error because it cannot determine where the schema SQL files live.
Source
Thrown at crates/infrastructure/src/sql/pg.rs:223
/// # Errors
///
/// Returns an error if the `SCHEMA_DIR` environment variable is not set and the repository
/// cannot be located in the current directory path.
///
/// # Panics
///
/// Panics if the current working directory cannot be determined or contains invalid UTF-8.
fn get_schema_dir() -> anyhow::Result<String> {
std::env::var("SCHEMA_DIR").or_else(|_| {
let nautilus_git_repo_name = "nautilus_trader";
let binding = std::env::current_dir().unwrap();
let current_dir = binding.to_str().unwrap();
match current_dir.find(nautilus_git_repo_name){
Some(index) => {
let schema_path = current_dir[0..index + nautilus_git_repo_name.len()].to_string() + "/schema/sql";
Ok(schema_path)
}
None => anyhow::bail!("Could not calculate schema dir from current directory path or SCHEMA_DIR env variable")
}
})
}
/// Initializes the Postgres database by creating schema, roles, and executing SQL files from `schema_dir`.
///
/// # Errors
///
/// Returns an error if any SQL execution or file system operation fails.
///
/// # Panics
///
/// Panics if `schema_dir` is missing and cannot be determined or if other unwraps fail.
pub async fn init_postgres(
pg: &PgPool,
database: String,
password: String,
schema_dir: Option<String>,View on GitHub (pinned to 18893faf8b)
Solutions
- Set the SCHEMA_DIR environment variable to the absolute path of the schema directory (e.g. export SCHEMA_DIR=/path/to/nautilus/schema/sql) before running the command
- Run the command from inside the nautilus repository checkout so the current directory path contains the repo name
- Verify the repository was cloned with its standard name; if renamed, either rename it back or set SCHEMA_DIR explicitly
- In deployments, bake the schema directory path into the container image and pass it via SCHEMA_DIR
Example fix
// before (fails when run outside the repo) $ ./target/release/nautilus database init // after $ export SCHEMA_DIR=/home/dev/nautilus/schema/sql $ ./target/release/nautilus database init
Defensive patterns
Strategy: validation
Validate before calling
let schema_dir = std::env::var("SCHEMA_DIR").ok().unwrap_or_else(|| {
let cwd = std::env::current_dir().unwrap();
let name = "nautilus"; // repo name the library searches for
match cwd.to_str().unwrap().find(name) {
Some(i) => cwd.to_str().unwrap()[0..i + name.len()].to_string() + "/schema/sql",
_ => panic!("set SCHEMA_DIR or run from inside the repo"),
}
}); Prevention
- Always export SCHEMA_DIR in deployment scripts and CI before invoking database commands
- Run database init/drop commands from the repository root or a subdirectory of the checkout
- In containers, copy the schema/sql directory into the image and set SCHEMA_DIR at build time
When it happens
Trigger: Calling init_postgres (or run_database_command init) when: (1) the SCHEMA_DIR env variable is not set, and (2) the process current working directory is not inside a directory whose path contains the nautilus git repository name (e.g. running the binary from /usr/local/bin or from the repo root when the repo was cloned under a renamed folder).
Common situations: Running the database init command from outside the repository checkout; a renamed or non-standard clone directory; Docker containers where only the compiled binary is present; forgetting to export SCHEMA_DIR in CI or deployment scripts.
Understand the failure class
Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.
Related errors
- Failed to set work_mem: {e}
- Error executing statement {sql_statement} with error: {e:?}
- Error dropping role {database}: {e:?}
- Failed to load order events: {e}
- Failed to start COPY operation: {e}
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/8cbbc951afd630c1.
Report an issue: GitHub.