plugins-workspace icon indicating copy to clipboard operation
plugins-workspace copied to clipboard

[persisted-scope] v2.0.0-alpha - Not persisted after restart ?

Open Vexcited opened this issue 2 years ago • 7 comments

Hi!

I have an issue where the scope isn't getting restored after restarting the application. Wonder if that plugin supports it. If it doesn't, I'd have to have all the * variations in my fs allow scope but I don't want this for security reasons.

Here's what happening in my main.rs file, nothing special here.

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_persisted_scope::init())
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_dialog::init())
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Then, in my app, I open a directory using the following piece of code :

import { open } from '@tauri-apps/plugin-dialog';
import { documentDir } from '@tauri-apps/api/path';

const selectedFolderPath = await open({
  multiple: false,
  directory: true,
  recursive: true,
  defaultPath: await documentDir()
});

Then I save the path inside localStorage. When the app gets restarted, it reads that value from localStorage and tries to open back the folder with the following function (also used when opening for the first time the folder through open) :

import { readDir } from "@tauri-apps/plugin-fs";

// I kinda liked the API of FileEntry in 1.x, so I tried to reimplement it.
export interface FileEntry {
  name: string;
  path: string;
  children?: Array<FileEntry>;
}

// Since I'm on Windows, I hardwritten this until I find another better way.
const sep = "\\";

export const listRecursivelyFilesInside = async (absolutePath: string): Promise<Array<FileEntry>> => {
  const entries = await readDir(absolutePath);
  const output: Array<FileEntry> = [];
  
  for (const entry of entries) {
    if (entry.isDirectory) {
      const dir = absolutePath + sep + entry.name;
      const entries = await listRecursivelyFilesInside(dir);
    
      output.push({
        name: entry.name,
        path: dir,
        children: entries
      });
    }
    else {
      output.push({
        name: entry.name,
        path: absolutePath + sep + entry.name
      });
    }
  }

  return output;
}

But whenever I restart the app, I get hit by a Error: "forbidden path: C:\\...". Happens on development and production.

Any idea ? Thanks !

Vexcited avatar Dec 22 '23 21:12 Vexcited

Is this still an issue in rc? If so, could you upload a minimal reproduction repo for us to try? I'm a bit confused as to why we only got a single report for this issue so i assume this is something app specific.

FabianLars avatar Aug 07 '24 14:08 FabianLars

@FabianLars I have the same issue when opening a file asociated. Can be reproduced by sending the file path to the FE without using dialogs and executing readTextFile / readFile

lsegurado avatar Sep 02 '24 13:09 lsegurado

Hmm, yeah the File Association feature does not extend the scope as far as i know. Or did you use the FsScope methods yourself and that's not saved?

FabianLars avatar Sep 02 '24 13:09 FabianLars

I have no idea about those methods 😂. How are associated files expected to be handled? In this example there is no file manipulation

lsegurado avatar Sep 02 '24 13:09 lsegurado

Hmmm, okay we either need to make it behave like the dialog api (auto adding the paths to the scope) or add the scope stuff in the example here https://github.com/tauri-apps/tauri/blob/dev/examples/file-associations/src-tauri/src/main.rs (scopes must be modified in rust).

Edit: I'll update the example in a few. Makes more sense than typing it out twice.

FabianLars avatar Sep 02 '24 13:09 FabianLars

Okay, this is a bit messed up but here it is: https://github.com/tauri-apps/tauri/pull/10864/files - for you the interesting parts are the lines that are commented out :/ (the fs_scope() stuff)

FabianLars avatar Sep 02 '24 13:09 FabianLars

Works perfectly that way. Thanks!

lsegurado avatar Sep 02 '24 13:09 lsegurado