-
Notifications
You must be signed in to change notification settings - Fork 3.9k
fix(memory-sources): use the native directory chooser and never store a bare folder name #5832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
M3gA-Mind
wants to merge
1
commit into
tinyhumansai:main
from
M3gA-Mind:fix/5831-native-folder-picker
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| //! Native directory chooser for memory-source configuration (#5831). | ||
| //! | ||
| //! The folder memory-source used to be picked with an | ||
| //! `<input type="file" webkitdirectory>` in the renderer. That element does | ||
| //! not expose a filesystem path: Chromium only carries one on the | ||
| //! non-standard `File.path` attribute, and only when the renderer has | ||
| //! filesystem-aware integration. Outside that renderer the handler fell back | ||
| //! to `webkitRelativePath.split('/')[0]` — the chosen directory's **name** | ||
| //! with its location discarded — and stored it. The source then looked | ||
| //! configured and could never sync, failing once per cycle forever with | ||
| //! `folder does not exist: docs`. | ||
| //! | ||
| //! A native dialog has no such gap: it returns an absolute path in every | ||
| //! renderer, on every platform. | ||
| //! | ||
| //! ## Why `rfd` and not `tauri-plugin-dialog` | ||
| //! | ||
| //! This mirrors [`crate::artifact_commands::save_artifact_via_dialog`] | ||
| //! (#3162), which is already in this shell and already talks to the OS | ||
| //! dialog APIs through `rfd`. Reusing it means **no new dependency, no new | ||
| //! plugin, and no capability-allowlist entry** — the crate is declared in | ||
| //! `Cargo.toml` with `default-features = false` plus `xdg-portal`, which is | ||
| //! what keeps Linux off GTK. Adding the dialog plugin for one command would | ||
| //! widen the dependency graph for a capability the shell already has. | ||
| //! | ||
| //! ## Trust boundary | ||
| //! | ||
| //! Deliberately none. Unlike the artifact commands — which re-validate that | ||
| //! a renderer-supplied path sits inside the artifacts tree, because there | ||
| //! the renderer *supplies* the path — this command takes no input and | ||
| //! returns only what the user chose in an OS-owned dialog. The renderer | ||
| //! cannot steer it at a directory, and picking a folder to index is the | ||
| //! user's decision to make anywhere on their disk. | ||
|
|
||
| /// Open the OS-native directory chooser and return the absolute path of the | ||
| /// directory the user selected. | ||
| /// | ||
| /// Returns: | ||
| /// - `Ok(Some(path))` — the absolute path chosen. | ||
| /// - `Ok(None)` — the user dismissed the dialog. Not an error; the caller | ||
| /// leaves the field untouched. | ||
| /// - `Err(_)` — the dialog could not run (for example, no xdg-desktop | ||
| /// portal on a headless Linux host), or it somehow yielded a relative | ||
| /// path. The caller surfaces this rather than storing anything. | ||
| #[tauri::command] | ||
| pub async fn pick_directory_via_dialog() -> Result<Option<String>, String> { | ||
| // On Linux this is the xdg-desktop portal (no GTK link); on macOS and | ||
| // Windows the system panel. The await resolves when the user picks or | ||
| // cancels. | ||
| let handle = rfd::AsyncFileDialog::new().pick_folder().await; | ||
|
|
||
| let Some(dir) = handle else { | ||
| log::info!("[directory_picker] pick_directory_via_dialog cancelled by user"); | ||
| return Ok(None); | ||
| }; | ||
|
|
||
| let path = dir.path().to_path_buf(); | ||
|
|
||
| // The OS dialogs all hand back absolute paths, so this is a belt-and-braces | ||
| // check rather than a branch we expect to take. It exists because a | ||
| // relative value reaching the store is the entire defect this command was | ||
| // written to remove: an error here is recoverable and visible, whereas a | ||
| // stored relative path is neither. | ||
| if !path.is_absolute() { | ||
| return Err(format!( | ||
| "the directory chooser returned a non-absolute path: {}", | ||
| path.display() | ||
| )); | ||
| } | ||
|
|
||
| log::info!( | ||
| "[directory_picker] pick_directory_via_dialog chose {}", | ||
| path.display() | ||
| ); | ||
| Ok(Some(path.display().to_string())) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
app/src/components/intelligence/__tests__/folderPicker.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /** | ||
| * #5831 — the folder picker must never hand back a value that cannot work | ||
| * as a path. | ||
| * | ||
| * The defect these tests pin: when `File.path` was unavailable the handler | ||
| * fell back to `webkitRelativePath.split('/')[0]`, storing the chosen | ||
| * directory's NAME with its location discarded. A source built from that | ||
| * value can never sync, and it fails once per cycle forever rather than once, | ||
| * visibly, at the moment of choosing. | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { directoryPathFromPickedFiles, pickDirectoryNatively } from '../folderPicker'; | ||
|
|
||
| const hoisted = vi.hoisted(() => ({ invoke: vi.fn(), isTauri: vi.fn() })); | ||
|
|
||
| vi.mock('../../../utils/tauriCommands/common', () => ({ | ||
| safeInvoke: hoisted.invoke, | ||
| isTauri: hoisted.isTauri, | ||
| })); | ||
|
|
||
| /** A `webkitdirectory` file entry, with `path` present only when asked for. */ | ||
| function pickedFile(relativePath: string, absolutePath?: string): File { | ||
| const file = new File(['x'], relativePath.split('/').pop() ?? 'f.md'); | ||
| Object.defineProperty(file, 'webkitRelativePath', { value: relativePath }); | ||
| if (absolutePath !== undefined) { | ||
| Object.defineProperty(file, 'path', { value: absolutePath }); | ||
| } | ||
| return file; | ||
| } | ||
|
|
||
| function fileList(...files: File[]): FileList { | ||
| const indexed: Record<number, File> = {}; | ||
| files.forEach((file, i) => { | ||
| indexed[i] = file; | ||
| }); | ||
| return { | ||
| ...indexed, | ||
| length: files.length, | ||
| item: (i: number) => files[i] ?? null, | ||
| } as unknown as FileList; | ||
| } | ||
|
|
||
| describe('directoryPathFromPickedFiles', () => { | ||
| it('refuses to produce a path when the renderer does not expose File.path', () => { | ||
| // THE REGRESSION. The old handler returned 'docs' here. A source stored | ||
| // with that value produced `folder does not exist: docs` on every sync, | ||
| // indefinitely, and no absolute path is recoverable from this input. | ||
| const result = directoryPathFromPickedFiles(fileList(pickedFile('docs/readme.md'))); | ||
|
|
||
| expect(result).toEqual({ ok: false, reason: 'no-absolute-path' }); | ||
| }); | ||
|
|
||
| it('never returns the bare directory name for any nesting depth', () => { | ||
| for (const relative of ['docs/a.md', 'docs/deep/b.md', 'docs/deep/deeper/c.md']) { | ||
| const result = directoryPathFromPickedFiles(fileList(pickedFile(relative))); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| // Belt and braces: assert the specific bad value can never come back, | ||
| // not merely that this input failed. | ||
| expect(JSON.stringify(result)).not.toContain('"docs"'); | ||
| } | ||
| }); | ||
|
|
||
| it('derives the containing directory when File.path is present', () => { | ||
| const result = directoryPathFromPickedFiles( | ||
| fileList(pickedFile('notes/readme.md', '/Users/you/notes/readme.md')) | ||
| ); | ||
|
|
||
| expect(result).toEqual({ ok: true, path: '/Users/you' }); | ||
| }); | ||
|
|
||
| it('derives a Windows directory without mangling the separators', () => { | ||
| const result = directoryPathFromPickedFiles( | ||
| fileList(pickedFile('notes/readme.md', 'C:\\Users\\you\\notes\\readme.md')) | ||
| ); | ||
|
|
||
| // `webkitRelativePath` is forward-slashed while the absolute path is not, | ||
| // so the relative portion does not appear verbatim and the whole path is | ||
| // kept. That is a usable absolute path, which is the property that matters. | ||
| expect(result).toEqual({ ok: true, path: 'C:\\Users\\you\\notes\\readme.md' }); | ||
| }); | ||
|
|
||
| it('treats an empty selection as a cancellation rather than an error', () => { | ||
| expect(directoryPathFromPickedFiles(null)).toEqual({ ok: false, reason: 'cancelled' }); | ||
| expect(directoryPathFromPickedFiles(fileList())).toEqual({ ok: false, reason: 'cancelled' }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('pickDirectoryNatively', () => { | ||
| beforeEach(() => { | ||
| hoisted.invoke.mockReset(); | ||
| hoisted.isTauri.mockReset(); | ||
| }); | ||
|
|
||
| it('reports unavailable outside the desktop shell, without invoking', async () => { | ||
| hoisted.isTauri.mockReturnValue(false); | ||
|
|
||
| await expect(pickDirectoryNatively()).resolves.toEqual({ ok: false, reason: 'unavailable' }); | ||
| expect(hoisted.invoke).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns the absolute path the native dialog chose', async () => { | ||
| hoisted.isTauri.mockReturnValue(true); | ||
| hoisted.invoke.mockResolvedValue('/Users/you/notes'); | ||
|
|
||
| await expect(pickDirectoryNatively()).resolves.toEqual({ ok: true, path: '/Users/you/notes' }); | ||
| expect(hoisted.invoke).toHaveBeenCalledWith('pick_directory_via_dialog'); | ||
| }); | ||
|
|
||
| it('treats a null return as a cancellation', async () => { | ||
| hoisted.isTauri.mockReturnValue(true); | ||
| hoisted.invoke.mockResolvedValue(null); | ||
|
|
||
| await expect(pickDirectoryNatively()).resolves.toEqual({ ok: false, reason: 'cancelled' }); | ||
| }); | ||
|
|
||
| it('falls back to unavailable when the dialog cannot run', async () => { | ||
| // Headless Linux with no xdg-desktop portal lands here, and wants the | ||
| // fallback input rather than an error. | ||
| hoisted.isTauri.mockReturnValue(true); | ||
| hoisted.invoke.mockRejectedValue(new Error('no portal')); | ||
|
|
||
| await expect(pickDirectoryNatively()).resolves.toEqual({ ok: false, reason: 'unavailable' }); | ||
| }); | ||
|
|
||
| it('refuses a blank path rather than storing it', async () => { | ||
| hoisted.isTauri.mockReturnValue(true); | ||
| hoisted.invoke.mockResolvedValue(' '); | ||
|
|
||
| await expect(pickDirectoryNatively()).resolves.toEqual({ | ||
| ok: false, | ||
| reason: 'no-absolute-path', | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
Do not log the selected absolute path.
Line 72 writes the selected path to the application log. The path can contain user names and sensitive directory names. Log only that a directory was selected, or redact the path.
🤖 Prompt for AI Agents