fix: re-authorize recent project directories before opening

This commit is contained in:
Weilin Cai 2026-04-07 16:02:23 +08:00
parent 0f2635ab7a
commit 037e033f35
3 changed files with 40 additions and 0 deletions

View file

@ -7,6 +7,7 @@ mod uv;
mod zotero;
use std::path::Path;
use tauri_plugin_fs::FsExt;
use tauri::{Emitter, Manager, WebviewUrl, WebviewWindowBuilder};
/// Entry point for the `--tectonic-compile` subprocess mode.
@ -198,6 +199,21 @@ fn create_new_window(app: tauri::AppHandle) -> Result<(), String> {
Ok(())
}
#[tauri::command]
fn allow_project_directory(app: tauri::AppHandle, root_path: String) -> Result<(), String> {
let fs_scope = app.fs_scope();
fs_scope
.allow_directory(&root_path, true)
.map_err(|e| format!("Failed to allow project directory: {}", e))?;
let asset_scope = app.state::<tauri::scope::Scopes>();
asset_scope
.allow_directory(&root_path, true)
.map_err(|e| format!("Failed to allow project assets: {}", e))?;
Ok(())
}
// --- Debug logging from JS (survives white-screen crashes) ---
#[tauri::command]
@ -349,6 +365,7 @@ pub fn run() {
})
.invoke_handler(tauri::generate_handler![
create_new_window,
allow_project_directory,
detect_editors,
open_in_editor,
js_log,

View file

@ -1,4 +1,6 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import { invoke } from "@tauri-apps/api/core";
import { readDir, readTextFile } from "@tauri-apps/plugin-fs";
import { writeTextFile } from "@tauri-apps/plugin-fs";
import {
useDocumentStore,
@ -42,6 +44,7 @@ function makeFile(overrides: Partial<ProjectFile> = {}): ProjectFile {
describe("useDocumentStore", () => {
beforeEach(() => {
vi.clearAllMocks();
clearPdfBytesCache();
useDocumentStore.setState({
projectRoot: "/project",
@ -83,6 +86,24 @@ describe("useDocumentStore", () => {
});
});
describe("openProject", () => {
it("re-authorizes the project directory before scanning", async () => {
vi.mocked(invoke).mockResolvedValue(undefined);
vi.mocked(readDir).mockResolvedValue([
{ name: "main.tex", isDirectory: false },
] as any);
vi.mocked(readTextFile).mockResolvedValue("\\documentclass{article}");
await useDocumentStore
.getState()
.openProject("E:/overleaf-cache/论文项目");
expect(invoke).toHaveBeenCalledWith("allow_project_directory", {
rootPath: "E:/overleaf-cache/论文项目",
});
});
});
describe("insertAtCursor", () => {
it("inserts text at cursor position", () => {
useDocumentStore.getState().insertAtCursor(", Beautiful");

View file

@ -1,4 +1,5 @@
import { create } from "zustand";
import { invoke } from "@tauri-apps/api/core";
import {
scanProjectFolder,
readTexFileContent,
@ -266,6 +267,7 @@ export const useDocumentStore = create<DocumentState>()((set, get) => ({
openProject: async (rootPath: string) => {
log.info(`Opening project: ${rootPath}`);
await invoke("allow_project_directory", { rootPath });
const { files: fsFiles, folders: fsFolders } =
await scanProjectFolder(rootPath);
const projectFiles: ProjectFile[] = [];