Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(desktop): expand ~ when opening local files and folders (#15284)

Innei980c2e74d819
diff --git a/apps/desktop/src/main/controllers/LocalFileCtr.ts b/apps/desktop/src/main/controllers/LocalFileCtr.ts
index 43c995d5e4c..9683c90abbc 100644
--- a/apps/desktop/src/main/controllers/LocalFileCtr.ts
+++ b/apps/desktop/src/main/controllers/LocalFileCtr.ts
@@ -248,14 +248,15 @@ export default class LocalFileCtr extends ControllerModule {
error?: string;
success: boolean;
}> {
- logger.debug('Attempting to open file:', { filePath });
+ const resolvedPath = expandTilde(filePath) ?? filePath;
+ logger.debug('Attempting to open file:', { filePath: resolvedPath });
try {
- await shell.openPath(filePath);
- logger.debug('File opened successfully:', { filePath });
+ await shell.openPath(resolvedPath);
+ logger.debug('File opened successfully:', { filePath: resolvedPath });
return { success: true };
} catch (error) {
- logger.error(`Failed to open file ${filePath}:`, error);
+ logger.error(`Failed to open file ${resolvedPath}:`, error);
return { error: (error as Error).message, success: false };
}
}
@@ -265,8 +266,13 @@ export default class LocalFileCtr extends ControllerModule {
error?: string;
success: boolean;
}> {
- const folderPath = isDirectory ? targetPath : path.dirname(targetPath);
- logger.debug('Attempting to open folder:', { folderPath, isDirectory, targetPath });
+ const resolvedTarget = expandTilde(targetPath) ?? targetPath;
+ const folderPath = isDirectory ? resolvedTarget : path.dirname(resolvedTarget);
+ logger.debug('Attempting to open folder:', {
+ folderPath,
+ isDirectory,
+ targetPath: resolvedTarget,
+ });
try {
await shell.openPath(folderPath);
diff --git a/apps/desktop/src/main/controllers/__tests__/LocalFileCtr.test.ts b/apps/desktop/src/main/controllers/__tests__/LocalFileCtr.test.ts
index c18a7fd814b..d6c5f75234c 100644
--- a/apps/desktop/src/main/controllers/__tests__/LocalFileCtr.test.ts
+++ b/apps/desktop/src/main/controllers/__tests__/LocalFileCtr.test.ts
@@ -143,6 +143,17 @@ describe('LocalFileCtr', () => {
expect(result).toEqual({ success: false, error: 'Failed to open' });
});
+
+ it('should expand a leading ~ to the user home directory', async () => {
+ const os = await import('node:os');
+ const path = await import('node:path');
+ vi.mocked(mockShell.openPath).mockResolvedValue('');
+
+ const result = await localFileCtr.handleOpenLocalFile({ path: '~/git/work/file.txt' });
+
+ expect(result).toEqual({ success: true });
+ expect(mockShell.openPath).toHaveBeenCalledWith(path.join(os.homedir(), 'git/work/file.txt'));
+ });
});
describe('handleOpenLocalFolder', () => {
@@ -158,6 +169,20 @@ describe('LocalFileCtr', () => {
expect(mockShell.openPath).toHaveBeenCalledWith('/test/folder');
});
+ it('should expand a leading ~ when opening a directory', async () => {
+ const os = await import('node:os');
+ const path = await import('node:path');
+ vi.mocked(mockShell.openPath).mockResolvedValue('');
+
+ const result = await localFileCtr.handleOpenLocalFolder({
+ path: '~/git/work',
+ isDirectory: true,
+ });
+
+ expect(result).toEqual({ success: true });
+ expect(mockShell.openPath).toHaveBeenCalledWith(path.join(os.homedir(), 'git/work'));
+ });
+
it('should open parent directory when isDirectory is false', async () => {
vi.mocked(mockShell.openPath).mockResolvedValue('');