Skip to content

[bug] readDir and createDir don't work in App Dir root level #3580

@Janaka-Steph

Description

@Janaka-Steph

Describe the bug

fs.readDir and fs.createDir don't work in App Dir root level in 1.0.0-rc, but was working fine in beta.
Those methods work only on a sub-directory of the specified BaseDirectory.

Use case: Checking if App dir exists, creating it otherwise, and write a file in it.

async setItem(key: string, value: any) {
    if ('__TAURI__' in window) {
      try {
        await fs.readDir('', { dir: fs.Dir.App });
      } catch (err) {
        await fs.createDir('', { dir: fs.Dir.App, recursive: true });
      }
      await fs.writeFile(
          { path: getFileNameForKey(key), contents: JSON.stringify(value) },
          { dir: fs.Dir.App }
        )
    }
  }

Here readDir with throw with path not allowed on the configured scope: /Users/stephane/Library/Application Support/tdex-dashboard/, even though it is not a scope issue.

Calling writeFile without checking if app dir exists obviously results in No such file or directory (os error 2) error.

Instead or in addition to fixing readDir/createDir, writeFile could create folder if it doesn' exist.

https://discord.com/channels/616186924390023171/731495028677148753/948156122261495808

Reproduction

No response

Expected behavior

No response

Platform and versions

▶ cargo tauri info

Operating System - Mac OS, version 12.2.1 X64

Node.js environment
  Node.js - 16.14.0
  @tauri-apps/cli - 1.0.0-rc.5
  @tauri-apps/api - 1.0.0-rc.1

Global packages
  npm - 8.1.4
  pnpm - Not installed
  yarn - 1.22.17

Rust environment
  rustup - 1.24.3
  rustc - 1.58.1
  cargo - 1.58.0
  toolchain - stable-aarch64-apple-darwin 

App directory structure
/node_modules
/public
/scripts
/.github
/src-tauri
/build
/.git
/.idea
/src

App
  tauri - 1.0.0-rc.3
  tauri-build - 1.0.0-rc.3
  tao - 0.6.2
  wry - 0.13.2
  build-type - bundle
  CSP - default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'
  distDir - ../build
  devPath - http://localhost:3003/
  framework - React
  bundler - Webpack

Stack trace

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions