{"record":{"id":"8cbbc951afd630c1","repo":"nautechsystems/nautilus_trader","slug":"could-not-calculate-schema-dir-from-current-direct","errorCode":null,"errorMessage":"Could not calculate schema dir from current directory path or SCHEMA_DIR env variable","messagePattern":"Could not calculate schema dir from current directory path or SCHEMA_DIR env variable","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/pg.rs","lineNumber":223,"sourceCode":"/// # Errors\n///\n/// Returns an error if the `SCHEMA_DIR` environment variable is not set and the repository\n/// cannot be located in the current directory path.\n///\n/// # Panics\n///\n/// Panics if the current working directory cannot be determined or contains invalid UTF-8.\nfn get_schema_dir() -> anyhow::Result<String> {\n    std::env::var(\"SCHEMA_DIR\").or_else(|_| {\n        let nautilus_git_repo_name = \"nautilus_trader\";\n        let binding = std::env::current_dir().unwrap();\n        let current_dir = binding.to_str().unwrap();\n        match current_dir.find(nautilus_git_repo_name){\n            Some(index) => {\n                let schema_path = current_dir[0..index + nautilus_git_repo_name.len()].to_string() + \"/schema/sql\";\n                Ok(schema_path)\n            }\n            None => anyhow::bail!(\"Could not calculate schema dir from current directory path or SCHEMA_DIR env variable\")\n        }\n    })\n}\n\n/// Initializes the Postgres database by creating schema, roles, and executing SQL files from `schema_dir`.\n///\n/// # Errors\n///\n/// Returns an error if any SQL execution or file system operation fails.\n///\n/// # Panics\n///\n/// Panics if `schema_dir` is missing and cannot be determined or if other unwraps fail.\npub async fn init_postgres(\n    pg: &PgPool,\n    database: String,\n    password: String,\n    schema_dir: Option<String>,","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/pg.rs#L205-L241","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before (fails when run outside the repo)\n$ ./target/release/nautilus database init\n// after\n$ export SCHEMA_DIR=/home/dev/nautilus/schema/sql\n$ ./target/release/nautilus database init","handlingStrategy":"validation","validationCode":"let schema_dir = std::env::var(\"SCHEMA_DIR\").ok().unwrap_or_else(|| {\n    let cwd = std::env::current_dir().unwrap();\n    let name = \"nautilus\"; // repo name the library searches for\n    match cwd.to_str().unwrap().find(name) {\n        Some(i) => cwd.to_str().unwrap()[0..i + name.len()].to_string() + \"/schema/sql\",\n        _ => panic!(\"set SCHEMA_DIR or run from inside the repo\"),\n    }\n});","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["database","postgres","configuration","path-resolution"],"backgroundTag":"missing-env-var","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}