electron: [Bug]: showItemInFolder hangs macOS Finder for about a minute

Preflight Checklist

Electron Version

25.0.1

What operating system are you using?

macOS

Operating System Version

macOS Ventura 13.4

What arch are you using?

x64

Last Known Working Electron version

24.4.0

Expected Behavior

import { shell } from "electron";

shell.showItemInFolder("/path/to/file.ext");

This should open the folder and highlight the file in it

Actual Behavior

In renderer process it leads to Finder’s “Not Responding” state for about a minute, and then it show the file. Meanwhile in main process it works as expected

Testcase Gist URL

https://gist.github.com/Nesvet/8700b509a57043453b1e6e027cc0c85e

Additional Information

No response

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 9
  • Comments: 15 (10 by maintainers)

Commits related to this issue

Most upvoted comments

We define the function in main process now and invoke it from renderer. This can be done through https://www.npmjs.com/package/@electron/remote or IPC.

I see… I’l do this from now on. I’ve disabled remote myself for security reasons so am using IPC, but it’s working great and would consider this issue closed.

The code for those wondering:

In main:

const { ipcMain, shell } = require('electron');
ipcMain.on('showItemInFolder', function(event, fullPath) {
    shell.showItemInFolder(fullPath);
});

In renderer:

const { ipcRenderer } = require('electron');
ipcRenderer.send('showItemInFolder', fullPath);

Because it’s easy for devs to miss this even if electron produces security warnings, I’d suggest that electron should throw a top-level error if these kinds of API calls are made in a sandboxed environment, if it doesn’t already.

Yes this is still happening. Calling showItemInFolder from render process hangs Finder, my solution: Use IPC Messages to expose it and call it from main process:

// preload.js
const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('shell', {
  showItemInFolder: async (fullPath) => {
    try {
      await ipcRenderer.invoke('show-item-in-folder', fullPath);
    } catch (error) {
      console.error('Error showing item in folder:', error);
      throw error;
    }
  },
});

handle it in main process:

import { app, BrowserWindow, ipcMain  } from 'electron';

// ...

const handleIpcShowItemInFolder = async (e, fullPath) => {
  shell.showItemInFolder(fullPath);
}

// ...

app.whenReady().then(() => {
  createWindow();

  ipcMain.handle('show-item-in-folder', handleIpcShowItemInFolder);

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
});

and call it in render.js or wherever you need it:

// ...
window.shell.showItemInFolder(filePathWhitoutProtocol);

If this function no longer works in the renderer and is not intending to be fixed, maybe it should just be removed?

Hey folks, just stumbled across this one as well – I have a non-sandboxed renderer process and other methods in the shell module can be called successfully (eg: require('electron').shell.openPath('...')). It seems this bug is specific to showItemInFolder, and hangs the Finder for 60s. Until this is fixed, I think calling it from the main process only is the best bet.

Sidenote: I’m upgrading an Electron app from v24 => v30 and this is one of the only regressions I’ve found - impressive considering I’m behind 20+ months of updates!

We define the function in main process now and invoke it from renderer. This can be done through https://www.npmjs.com/package/@electron/remote or IPC.