Skip to content
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

Getting Error: spawn ENOTDIR with activeWindow() #185

Open
sidh01 opened this issue May 27, 2024 · 27 comments
Open

Getting Error: spawn ENOTDIR with activeWindow() #185

sidh01 opened this issue May 27, 2024 · 27 comments

Comments

@sidh01
Copy link

sidh01 commented May 27, 2024

No description provided.

@Ladvace
Copy link

Ladvace commented Sep 3, 2024

same problem here, did you find a solution?

@qualwebs
Copy link

qualwebs commented Sep 10, 2024

@Ladvace @sidh01 Anyone found the solution?

@Ladvace
Copy link

Ladvace commented Sep 10, 2024

nope, I get this error on macos, not sure if it's supposed to ask for permissions or the user need to set it manually (didn't try this yet)

@qualwebs
Copy link

@Ladvace The app is working fine using electron start but not with the build, Not sure if the terminal has the permissions.

BTW, what you are building with this?

@Ladvace
Copy link

Ladvace commented Sep 10, 2024

same for me, you mean what I'm using to build it?

@qualwebs
Copy link

No, I mean what purpose you're using this package?

@Ladvace
Copy link

Ladvace commented Sep 11, 2024

how is this related to he problem?

@Nantris
Copy link

Nantris commented Sep 12, 2024

Filing a bug report for a native dependency without even specifying the OS is just asking to never get it addressed.

@qualwebs
Copy link

@Nantris It's in MacOS(Silicon chip), and happening when creating build, Dev mode with electron is working fine.

@Nantris
Copy link

Nantris commented Sep 12, 2024

@qualwebs did you see this? #99 (comment)

If this fixes the issue, there probably needs to be a note in the README for macOS too.

@Ladvace
Copy link

Ladvace commented Sep 12, 2024

@qualwebs did you see this? #99 (comment)

If this fixes the issue, there probably needs to be a note in the README for macOS too.

I just tried this and it doesn't seem to fix it to me

@Nantris
Copy link

Nantris commented Sep 12, 2024

I'm planning to try this myself on macOS tomorrow, though not Silicon. Any other findings at all that might be helpful towards debugging? I wonder what version of macOS you're on as well and if your application is signed?

@qualwebs
Copy link

@Nantris
My macOS version is 14.6.1.
It works perfectly fine with dev mode in electron but not after build.

Also, Can I fetch browser URLs in linux and windows? if not, why and if that can be achieved?

@Nantris
Copy link

Nantris commented Sep 12, 2024

It works for me on macOS 14.5 - but specifically I tested with the optional permissions disabled.

I suspect the app needs to be signed and notarized to work. I can't answer about URLs because we don't need them, sorry.

Can't speak to whether this works on Silicon.

@trinhxyz
Copy link

@Nantris - Related to this issue: #189

@timeowilliams
Copy link

timeowilliams commented Sep 22, 2024

@qualwebs @sidh01 @Ladvace

I had the same error:

Error getting active window: Error: spawn ENOTDIR
    at ChildProcess.spawn (node:internal/child_process:421:11)
    at spawn (node:child_process:776:9)
    at execFile (node:child_process:351:17)
    at node:child_process:243:21
    at activeWindow (file:///Applications/deepfocus.app/Contents/Resources/app.asar/node_modules/get-windows/lib/macos.js:38:25)
    at activeWindow (file:///Applications/deepfocus.app/Contents/Resources/app.asar/node_modules/get-windows/index.js:18:10)
    at async Timeout._onTimeout (file:///Applications/deepfocus.app/Contents/Resources/app.asar/out/main/index.js:220:26) {
  errno: -20,
  code: 'ENOTDIR',
  syscall: 'spawn'
}

I use an M1 Macbook Pro (16GB) & Sonoma 14.5

What worked for me was modifying the node-modules directly and changing this file:

node_modules/get-windows/lib/macos.js

Here's the code:

import path from "node:path";
import { promisify } from "node:util";
import childProcess from "node:child_process";
import { fileURLToPath } from "node:url";
import fs from "node:fs";
import process from "node:process";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const execFile = promisify(childProcess.execFile);

// Detect if running in packaged mode using process.resourcesPath
const isPackaged = process.resourcesPath !== process.cwd();
console.log("isPackaged:", isPackaged);

// Set the binary path dynamically for both development and packaged (production) mode
let binary = isPackaged
	? path.join(
			process.resourcesPath,
			"app.asar.unpacked",
			"node_modules",
			"get-windows",
			"main",
		) // Packaged environment
	: path.join(__dirname, "../main"); // Development environment

console.log("Binary path:", binary);

// Ensure the binary exists
if (!fs.existsSync(binary)) {
	console.error("Binary file does not exist:", binary);
	binary = path.join(__dirname, "../main");
}

const parseMac = (stdout) => {
	try {
		return JSON.parse(stdout);
	} catch (error) {
		console.error(error);
		throw new Error("Error parsing window data");
	}
};

const getArguments = (options) => {
	const args = [];
	if (options?.accessibilityPermission === false) {
		args.push("--no-accessibility-permission");
	}
	if (options?.screenRecordingPermission === false) {
		args.push("--no-screen-recording-permission");
	}
	return args;
};

export async function activeWindow(options) {
	const { stdout } = await execFile(binary, getArguments(options));
	return parseMac(stdout);
}

export function activeWindowSync(options) {
	const stdout = childProcess.execFileSync(binary, getArguments(options), {
		encoding: "utf8",
	});
	return parseMac(stdout);
}

export async function openWindows(options) {
	const { stdout } = await execFile(binary, [
		...getArguments(options),
		"--open-windows-list",
	]);
	return parseMac(stdout);
}

export function openWindowsSync(options) {
	const stdout = childProcess.execFileSync(
		binary,
		[...getArguments(options), "--open-windows-list"],
		{ encoding: "utf8" },
	);
	return parseMac(stdout);
}

Note: some of the logic is meant for use with building an Electron app, but shouldn't be an issue.

Afterwards, I can get the activeWindow info with no issues.
Time spent on Getting Error: spawn ENOTDIR with activeWindow() · Issue #185 · sindresorhus/get-windows: 2m 0s

I'm considering raising a PR if this is a more common issue.

@trinhxyz
Copy link

@timeowilliams - thanks for the tip, could you raise the PR though, I'm curious to see what the fix is? Also are you using electron-forge?

@timeowilliams
Copy link

@trinhxyz :

https://github.com/Tech-Nest-Ventures/get-windows/blob/main/lib/macos.js

Something similar to this. I use electron-builder right now in combination with https://electron-vite.org/

@Ladvace
Copy link

Ladvace commented Sep 23, 2024

I'm using electron-builder with https://electron-vite.org/ on Macos M1 too

@Ladvace
Copy link

Ladvace commented Sep 24, 2024

I just tried to replace node_modules/get-windows/lib/macos.js as @timeowilliams did but I just get the following error:

App threw an error during load
Error: Binary file not found

because path.join(process.resourcesPath, 'app.asar.unpacked', 'node_modules', 'get-windows', 'main') doesn't exist

@Ladvace
Copy link

Ladvace commented Sep 24, 2024

actually building it, I don't get the error, I just get this:

stdout: 'get-windows requires the accessibility permission in “System Settings › Privacy & Security › Accessibility”.\n',

@Ladvace
Copy link

Ladvace commented Sep 24, 2024

I can confirm that giving the right permission and the changes from @timeowilliams, it works!

@timeowilliams
Copy link

@trinhxyz ,

Ended up straying away quite a bit from this repo, but if you're interested in just using the dependency without having to mess with the code/just install, feel free to use this dependency:

https://github.com/Tech-Nest-Ventures/get-windows

^ Specifically optimized for MacOS at this time.

@Ladvace
Copy link

Ladvace commented Sep 25, 2024

one thing I forgot to mention is that it worked fine in production, but for some reason isPackaged was true even when runned in dev (from the @timeowilliams code), so I got an error in that case, I didn't dig much into it, but expect for that, it was working fine

@timeowilliams
Copy link

@Ladvace ,

100%! Was using the wrong method.

Please change this in node_modules/get-windows/lib/macos.js

const isPackaged = process.main?.filename.indexOf("app.asar") !== undefined;

to this:

const isPackaged = process.resourcesPath !== process.cwd();

@Ladvace
Copy link

Ladvace commented Sep 25, 2024

seems working great now!

@sindresorhus
Copy link
Owner

#192 (comment)

Repository owner locked as resolved and limited conversation to collaborators Oct 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants