{"record":{"id":"a063824d3b414f28","repo":"diesel-rs/diesel","slug":"can-t-get-file-name-from-path-path","errorCode":null,"errorMessage":"Can't get file name from path `{path:?}`","messagePattern":"Can't get file name from path `(.+?)`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"diesel_migrations/migrations_macros/src/embed_migrations.rs","lineNumber":42,"sourceCode":"}\n\nfn migration_literals_from_path(\n    path: &Path,\n) -> Result<Vec<proc_macro2::TokenStream>, Box<dyn Error>> {\n    let mut migrations = migrations_directories(path)?.collect::<Result<Vec<_>, _>>()?;\n\n    migrations.sort_by_key(DirEntry::path);\n\n    Ok(migrations\n        .into_iter()\n        .map(|e| migration_literal_from_path(&e.path()))\n        .collect())\n}\n\nfn migration_literal_from_path(path: &Path) -> proc_macro2::TokenStream {\n    let name = path\n        .file_name()\n        .unwrap_or_else(|| panic!(\"Can't get file name from path `{path:?}`\"))\n        .to_string_lossy();\n    if version_from_string(&name).is_none() {\n        panic!(\n            \"Invalid migration directory: the directory's name should be \\\n             <timestamp>_<name_of_migration>, and it should contain \\\n             up.sql and optionally down.sql.\"\n        );\n    }\n    let up_sql_path = path.join(\"up.sql\");\n    let up_sql_path = up_sql_path.to_str();\n    let down_sql_path = path.join(\"down.sql\");\n    let metadata = TomlMetadata::read_from_file(&path.join(\"metadata.toml\")).unwrap_or_default();\n    let run_in_transaction = metadata.run_in_transaction;\n\n    let down_sql = match down_sql_path.metadata() {\n        Err(e) if e.kind() == std::io::ErrorKind::NotFound => quote! { None },\n        _ => {\n            let down_sql_path = down_sql_path.to_str();","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/diesel-rs/diesel/blob/6fa6ed01b24b24248ab2a611698d0a7c6a2e9120/diesel_migrations/migrations_macros/src/embed_migrations.rs#L24-L60","documentation":"This panic comes from diesel's `embed_migrations!` proc macro when building migration entries at compile time. `migration_literal_from_path` calls `Path::file_name()` on each entry in the migrations directory; `file_name()` returns None for paths ending in `..` or a filesystem root, so the macro panics instead of producing a migration name. It means the macro was given a path component from which no file/directory name can be extracted.","triggerScenarios":"Calling `embed_migrations!` (or `embed_migrations!(\"path\")`) with a path that resolves to `.`/`..` segments, a trailing path that reduces to a root, or a directory entry yielded by `read_dir` whose path has no final component.","commonSituations":"Pointing the macro at `\".\"` or `\"migrations/..\"`; build scripts or IDE-provided relative paths with `..` components; misconfigured MIGRATION_DIRECTORY env-style variables; moving the macros crate call so the implicit migrations path no longer exists as expected.","solutions":["Pass a concrete, normalized migrations directory path to `embed_migrations!`, e.g. `embed_migrations!(\"migrations\")`, not `.` or paths containing `..`.","Verify at `CARGO_MANIFEST_DIR` level that the directory exists and its entries are real subdirectories (e.g. `migrations/2023-01-01-000000_create_users`).","Run `diesel migration generate <name>` to create correctly shaped migration directories instead of hand-creating them.","If the path is computed dynamically in a build script, canonicalize it (remove `.`/`..`) before embedding."],"exampleFix":"// before\nembed_migrations!(\"./migrations/..\");\n// after\nembed_migrations!(\"migrations\");","handlingStrategy":"validation","validationCode":"let dir = std::path::Path::new(\"migrations\");\nassert!(dir.is_dir(), \"migrations dir missing\");\nassert!(dir.file_name().is_some(), \"path must end in a real directory name, not '.' or '..'\");","typeGuard":"fn has_file_name(p: &std::path::Path) -> bool { p.file_name().is_some() }","tryCatchPattern":null,"preventionTips":["Always pass a literal, normalized migrations directory to embed_migrations!.","Never use `.` or `..` components in the macro argument.","Generate migrations with `diesel migration generate` instead of hand-crafting folders."],"tags":["compile-time","proc-macro","migrations","path"],"backgroundTag":"invalid-argument-format","analyzedSha":"6fa6ed01b24b24248ab2a611698d0a7c6a2e9120","analyzedAt":"2026-09-07T01:50:13.074Z","contentChangedAt":"2026-09-07T01:50:13.074Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}