From ed1976e31ef7d0c41804224411614c9781344943 Mon Sep 17 00:00:00 2001 From: Ajinkya Vidwans Date: Fri, 13 Feb 2026 14:48:28 +0530 Subject: [PATCH] fix: Fix import for records with duplicate names --- .../actions/local-sync/common-utils.ts | 17 ++++++--- src/renderer/actions/local-sync/fs-manager.ts | 37 ++++++++----------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/renderer/actions/local-sync/common-utils.ts b/src/renderer/actions/local-sync/common-utils.ts index 91150b55..7adc418a 100644 --- a/src/renderer/actions/local-sync/common-utils.ts +++ b/src/renderer/actions/local-sync/common-utils.ts @@ -344,7 +344,8 @@ export function isNewEntityName(name: string, baseString: string): boolean { const escapedBase = baseString.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // Pattern: exact match OR base string followed by one or more digits - const pattern = new RegExp(`^${escapedBase}(\\d+)?$`); + // Case-insensitive to align with case-insensitive filesystems (macOS/Windows) + const pattern = new RegExp(`^${escapedBase}(\\d+)?$`, 'i'); return pattern.test(name); } @@ -358,17 +359,23 @@ export function isNewEntityName(name: string, baseString: string): boolean { * @returns The next available name variant (e.g., 'Untitled1', 'Untitled2') */ export function getAlternateName(baseName: string, existingNames: Set): string { - if(!existingNames.has(baseName)) { + // Treat existing names in a case-insensitive way to avoid clashes on + // case-insensitive filesystems while preserving the original casing + const lowerExisting = new Set(Array.from(existingNames).map((n) => n.toLowerCase())); + const baseLower = baseName.toLowerCase(); + + if (!lowerExisting.has(baseLower)) { return baseName; } + let counter = 1; let candidateName = `${baseName}${counter}`; - - while (existingNames.has(candidateName)) { + + while (lowerExisting.has(candidateName.toLowerCase())) { counter++; candidateName = `${baseName}${counter}`; } - + return candidateName; } diff --git a/src/renderer/actions/local-sync/fs-manager.ts b/src/renderer/actions/local-sync/fs-manager.ts index 22671f1d..27278c31 100644 --- a/src/renderer/actions/local-sync/fs-manager.ts +++ b/src/renderer/actions/local-sync/fs-manager.ts @@ -787,10 +787,11 @@ export class FsManager { id: collectionId || getIdFromPath(this.rootPath), type: "folder", }); + const sanitizedName = sanitizeFsResourceName(content.name); const newName = getNewNameIfQuickCreate({ - name: sanitizeFsResourceName(content.name), - baseName: "Untitled request", + name: sanitizedName, + baseName: sanitizedName, parentPath: getNormalizedPath(parentFolderResource.path), }); @@ -1302,16 +1303,10 @@ export class FsManager { return folderCreationResult; } - let newName = getNewNameIfQuickCreate({ - name: sanitizeFsResourceName(environmentName), - baseName: "New Environment", - parentPath: getNormalizedPath(environmentFolderPath), - }); - - // re-used to handle name conflicts by appending a number when an environment with the same name already exists during import - newName = getNewNameIfQuickCreate({ - name: sanitizeFsResourceName(environmentName), - baseName: sanitizeFsResourceName(environmentName), + const sanitizedEnvName = sanitizeFsResourceName(environmentName); + const newName = getNewNameIfQuickCreate({ + name: sanitizedEnvName, + baseName: sanitizedEnvName, parentPath: getNormalizedPath(environmentFolderPath), }); @@ -1487,16 +1482,16 @@ export class FsManager { throw new Error(`Could not find path for id ${collection.collectionId}`); } - //Figure out an alternate name only for the root collection being imported - if (!collection.collectionId.length) { - const newName = getNewNameIfQuickCreate({ - name: sanitizeFsResourceName(collection.name), - baseName: collection.name, - parentPath: getNormalizedPath(parentPath), - }); + const sanitizedName = sanitizeFsResourceName(collection.name); - collection.name = newName; - } + // Handle name conflicts for imported/duplicated collections (root and nested) + const newName = getNewNameIfQuickCreate({ + name: sanitizedName, + baseName: sanitizedName, + parentPath: getNormalizedPath(parentPath), + }); + + collection.name = newName; const path = appendPath( parentPath,