Search code examples
rusttauri

fs::create_dir_all() doesn't throw an error, but also does not work


I'm new to Rust, so maybe I'm not understanding this correctly. I am creating a multiplatform application using Tauri + Yew. In my backend code when I'm setting up the app, I wanted to create a directory in the $DOCUMENT directory, for projects made inside my application.

The result for fs::create_dir_all("$DOCUMENT/QueryIOProjects"); is Ok() and it prints Projects Directory created at /Users/username/Documents/QueryIOProjects, but I cant see any folder in Finder or via terminal. To debug, I used the read_dir method to print all the DirEntrys,which prints out only one output dirEntry = QueryIOProjects, leaving me even more confused.

src-tauri/src/main.rs:

    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .menu(menu)
        .on_menu_event(menu_item_handler)
        .setup(|app| {
            let path_str = match path::parse(
                &app.config(),
                app.package_info(),
                &app.env(),
                "$DOCUMENT/QueryIOProjects",
            ) {
                Ok(path) => path.to_string_lossy().to_string(),
                Err(_) => "Path creation failed".to_string(),
            };
            {
                let result = fs::create_dir_all("$DOCUMENT/QueryIOProjects");
                match result {
                    Ok(_) => println!("Projects Directory created at {}", path_str),
                    Err(_) => println!("Failed to create project directory at {}", path_str),
                }
            }
            {
                let result = fs::read_dir("$DOCUMENT/");
                match result {
                    Ok(readDir) => {
                        readDir.for_each(|dirEntryResult| match dirEntryResult {
                            Ok(dirEntry) => println!(
                                "dirEntry = {}",
                                dirEntry
                                    .file_name()
                                    .into_string()
                                    .expect("dir entry string conversion failed")
                            ),
                            Err(err) => println!("error in dirEntry"),
                        });
                    }
                    Err(_) => println!("Failed to read directory at {}", "$DOCUMENT/"),
                }
            }
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");

tauri.conf.json:

{
  "tauri": {
    "allowlist": {
      "all": false,
      "shell": {
        "all": false,
        "open": true
      },
      "fs": {
        "scope": [
          "$DOCUMENT",
          "$DOCUMENT/*"
        ],
        "all": true,
        "readFile": true,
        "writeFile": true,
        "readDir": true,
        "copyFile": true,
        "createDir": true,
        "removeFile": true,
        "renameFile": true,
        "exists": true
      },
      "path": {
        "all": true
      },
      "dialog": {
        "open": true,
        "save": true
      }
    }
  }
}

Solution

  • You are creating the folder at the literal path "$DOCUMENT/QueryIOProjects", not path_str. Somewhere on your computer is a folder called, literally, $DOCUMENT.