Skip to content

Commit 78b4611

Browse files
committed
fix(workspace-binding): reject duplicate folder with a clear message
create/update_workspace_binding now pre-check for an existing mapping on the same normalized root and return a readable error ("A mapping already exists for …") instead of tripping the opaque SQLite UNIQUE(workspace_root) error. Signed-off-by: Mohammod Al Amin Ashik <maa.ashik00@gmail.com>
1 parent 2520db2 commit 78b4611

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

apps/desktop/src-tauri/src/commands/workspace_binding.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ pub async fn create_workspace_binding(
194194
let feature_set_ids = validate_fs_list(&input)?;
195195
let normalized = normalize_and_validate(&input.workspace_root)?;
196196

197+
// Reject a duplicate folder up front with a readable message. The schema
198+
// already enforces `UNIQUE(workspace_root)`, but that surfaces an opaque
199+
// SQLite constraint error — this gives the UI something a user can act on.
200+
let existing = state
201+
.workspace_binding_repository
202+
.list()
203+
.await
204+
.map_err(|e| e.to_string())?;
205+
if existing.iter().any(|b| b.workspace_root == normalized) {
206+
return Err(format!(
207+
"A mapping already exists for {normalized}. Edit the existing mapping instead of adding a second one."
208+
));
209+
}
210+
197211
let binding = WorkspaceBinding::new_multi(normalized.clone(), space_id, feature_set_ids);
198212

199213
state
@@ -233,6 +247,23 @@ pub async fn update_workspace_binding(
233247
let feature_set_ids = validate_fs_list(&input)?;
234248
let normalized = normalize_and_validate(&input.workspace_root)?;
235249

250+
// If the edit moved the folder onto a path another mapping already owns,
251+
// reject with a readable message rather than tripping the DB UNIQUE
252+
// constraint. Exclude this binding's own row.
253+
let all = state
254+
.workspace_binding_repository
255+
.list()
256+
.await
257+
.map_err(|e| e.to_string())?;
258+
if all
259+
.iter()
260+
.any(|b| b.id != id_uuid && b.workspace_root == normalized)
261+
{
262+
return Err(format!(
263+
"Another mapping already uses {normalized}. Pick a different folder."
264+
));
265+
}
266+
236267
let existing = state
237268
.workspace_binding_repository
238269
.get(&id_uuid)

0 commit comments

Comments
 (0)