sindresorhus/alfy

Using console log in global scope crashes the execution

OlegGulevskyy opened this issue · 3 comments

Any time any console logs are attempted, it crashes the execution context.
Here is minimum reproducable code:

import alfy from "alfy";

const data = await alfy.fetch("https://jsonplaceholder.typicode.com/posts");
// alfy.log("DATA", data);

// alfy.log("HELLO");

const items = alfy
  .inputMatches(data, "title")
  .map((el) => ({ title: el.title, subtitle: el.subtitle, arg: el.id }));

alfy.output(items);

Uncomment any console log line and I am getting this crash

[18:37:21.238] TEST_WF[Script Filter] Queuing argument 'r'
[18:37:21.648] TEST_WF[Script Filter] Script with argv 'r' finished
[18:37:21.651] TEST_WF[Script Filter] {
	"items": [
		{
			"title": "HELLO\n",
			"subtitle": "Press ⌘L to see the full error and ⌘C to copy it.",
			"valid": false,
			"text": {
				"copy": "```\nHELLO\n```\n\n-\nTEST_WF undefined\nAlfred 5.0\ndarwin 21.6.0",
				"largetype": "HELLO"
			},
			"icon": {
				"path": "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns"
			}
		}
	]
}
{
	"items": [
		{
			"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
			"arg": 1
		},
		{
			"title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
			"arg": 3
		},
...shortened 
   ]
}
[18:37:21.659] ERROR: TEST_WF[Script Filter] JSON error: Garbage at end around line 17, column 0. in JSON:
{
	"items": [
		{
			"title": "HELLO\n",
			"subtitle": "Press ⌘L to see the full error and ⌘C to copy it.",
			"valid": false,
			"text": {
				"copy": "```\nHELLO\n```\n\n-\nTEST_WF undefined\nAlfred 5.0\ndarwin 21.6.0",
				"largetype": "HELLO"
			},
			"icon": {
				"path": "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns"
			}
		}
	]
}
{
	"items": [
		{
			"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
			"arg": 1
		},
...shortened
	]
}

alfy.log simply passes it to console.error. Alfred documents that you can output to stderr for debugging.

https://www.alfredforum.com/topic/9170-alfred-3-tip-use-stderr-to-output-to-the-debug-console/

hoto commented

I also don't understand how do you "console debug" anything using alfy without effectively breaking the workflow execution.
ATM it's either you get the debug logs or you get proper workflow execution, never both.

Any of the alfy/console logs will break the execution of the workflow:

import alfy from 'alfy'

alfy.log("my alfy.log()")
alfy.error("my alfy.error()")
console.info("my console.info()")
console.debug("my console.debug()")
console.error("my console.error()")

alfy.output(
    [
        {
            title: "My title"
        }
    ]
)

You will see the various log messages and My title being displayed in debug console, but the workflow execution will be broken, it crashes as alfred cannot parse the output.
Using any of the above logs crashes the exectuion of the workflow.

The big question here is how do you add debug log but at the same time don't crash the workflow execution?

I'm also confused as to how this is supposed to work. I understand that alfy.log() is simply a wrapper around console.error(); however, when I try to use it, I end up seeing two items arrays getting printed to the console, which doesn't seem right.

FYI... If I comment out the following line in Alfy, everything works as expected:

// hookStderr(alfy.error);

Alfy seems to be trying to do two conflicting things with regards to stderr:

#86 (comment)