Stuck on Run a program #ip1

I’m stuck on Run a program #ip1.

I’ve tried the approaches used in the examples as well but no luck.

Here are my logs:

[tester::#IP1] Running ./your_program.sh
[your-program] $ custom_exe_5725 Emily Maria Maria
[your-program] custom_exe_5725 Emily Maria Maria: not found
[tester::#IP1] ^ Line does not match expected value.
[tester::#IP1] Expected: "Program was passed 4 args (including program name)."
[tester::#IP1] Received: "custom_exe_5725 Emily Maria Maria: not found"
[your-program] $ 
[tester::#IP1] Assertion failed.
[tester::#IP1] Test failed

And here’s a snippet of my code:

 const directories = process.env.PATH.split(path.delimiter);
  let [command, ...cmdArgs] = args.split(" ");

  for (const directory of directories) {
    const execPath = path.join(directory, command);
    
    try {
      fs.accessSync(execPath, fs.constants.X_OK);
      if (execute) {
        // execCommand(execPath, command, cmdArgs);
        // console.log(execPath, command, cmdArgs);  
        // "/tmp/ant/custom_exe_5866 custom_exe_5866 [ 'James', 'David' ]"
        const child = spawn(execPath, cmdArgs, {
          stdio: "inherit",
          argv0: command,
        });

        child.stdout.on("data", (data) => {
          console.log(data);
        });

        child.stderr.on("data", (data) => {
          console.log(data);
        });

        child.on("close", () => {
          console.log("close");
        });
      } else {
        console.log(`${command} is ${execPath}`);
      }
      return;
    } catch (e) {
    }
  }

  console.log(`${args}: not found`);

No matter what I do it seems the spawn method just doesn’t work, I’m probably making a very silly mistake here somewhere but it’s been a week now and I’m stuck here. Any help is greatly appreciated!!

There is not much to go on at the moment. All we can see is that the return is probably not taken since you emit the output seen in the log at bottom of this function.

Since return is not taken, that makes me question whether spawn might also throw and you just continue the loop. Also spawn supposedly has a callback named option you might also be able to get error information from.

(post deleted by author)

I couldn’t find any callback option unfortunately. I tried to log the errors in the catch block to see if there’s any error thrown by spawn but the only errors were file not found errors which are expected. I had also uncommented the console log at line 2 in the snippet, but that never got logged. After the file not found errors it just goes back to the “not found” log at the end.

[tester::#IP1] Running ./your_program.sh
[your-program] $ custom_exe_3070 Alice David
[tester::#IP1] ^ Line does not match expected value.
[tester::#IP1] Expected: "$ custom_exe_3070 Alice David"
[tester::#IP1] Received: "    at emitKeys (node:internal/readline/utils:371:14)"
[your-program]     at emitKeys.next (<anonymous>)
[your-program] Error: ENOENT: no such file or directory, access '/usr/local/sbin/custom_exe_3070'
[your-program]     at Object.accessSync (node:fs:253:11)
[your-program]     at pathCheck (/app/app/main.js:51:10)
[your-program]     at Interface.<anonymous> (/app/app/main.js:37:7)
[your-program]     at Interface.emit (node:events:519:28)
[your-program]     at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
[your-program]     at [_line] [as _line] (node:internal/readline/interface:887:18)
[your-program]     at [_ttyWrite] [as _ttyWrite] (node:internal/readline/interface:1274:24)
[your-program]     at ReadStream.onkeypress (node:internal/readline/interface:264:20)
[your-program]     at ReadStream.emit (node:events:519:28)
[your-program]     at emitKeys (node:internal/readline/utils:371:14) {
[your-program]   errno: -2,
[your-program]   code: 'ENOENT',
[your-program]   syscall: 'access',
[your-program]   path: '/usr/local/sbin/custom_exe_3070'
[your-program] }
[your-program] Error: ENOENT: no such file or directory, access '/usr/local/bin/custom_exe_3070'
[your-program]     at Object.accessSync (node:fs:253:11)
[your-program]     at pathCheck (/app/app/main.js:51:10)
[your-program]     at Interface.<anonymous> (/app/app/main.js:37:7)
[your-program]     at Interface.emit (node:events:519:28)
[your-program]     at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
[your-program]     at [_line] [as _line] (node:internal/readline/interface:887:18)
[your-program]     at [_ttyWrite] [as _ttyWrite] (node:internal/readline/interface:1274:24)
[your-program]     at ReadStream.onkeypress (node:internal/readline/interface:264:20)
[your-program]     at ReadStream.emit (node:events:519:28)
[your-program]     at emitKeys (node:internal/readline/utils:371:14) {
[your-program]   errno: -2,
[your-program]   code: 'ENOENT',
[your-program]   syscall: 'access',
[your-program]   path: '/usr/local/bin/custom_exe_3070'
[your-program] }
[your-program] Error: ENOENT: no such file or directory, access '/usr/sbin/custom_exe_3070'
[your-program]     at Object.accessSync (node:fs:253:11)
[your-program]     at pathCheck (/app/app/main.js:51:10)
[your-program]     at Interface.<anonymous> (/app/app/main.js:37:7)
[your-program]     at Interface.emit (node:events:519:28)
[your-program]     at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
[your-program]     at [_line] [as _line] (node:internal/readline/interface:887:18)
[your-program]     at [_ttyWrite] [as _ttyWrite] (node:internal/readline/interface:1274:24)
[your-program]     at ReadStream.onkeypress (node:internal/readline/interface:264:20)
[your-program]     at ReadStream.emit (node:events:519:28)
[your-program]     at emitKeys (node:internal/readline/utils:371:14) {
[your-program]   errno: -2,
[your-program]   code: 'ENOENT',
[your-program]   syscall: 'access',
[your-program]   path: '/usr/sbin/custom_exe_3070'
[your-program] }
[your-program] Error: ENOENT: no such file or directory, access '/usr/bin/custom_exe_3070'
[your-program]     at Object.accessSync (node:fs:253:11)
[your-program]     at pathCheck (/app/app/main.js:51:10)
[your-program]     at Interface.<anonymous> (/app/app/main.js:37:7)
[your-program]     at Interface.emit (node:events:519:28)
[your-program]     at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
[your-program]     at [_line] [as _line] (node:internal/readline/interface:887:18)
[your-program]     at [_ttyWrite] [as _ttyWrite] (node:internal/readline/interface:1274:24)
[your-program]     at ReadStream.onkeypress (node:internal/readline/interface:264:20)
[your-program]     at ReadStream.emit (node:events:519:28)
[your-program]     at emitKeys (node:internal/readline/utils:371:14) {
[your-program]   errno: -2,
[your-program]   code: 'ENOENT',
[your-program]   syscall: 'access',
[your-program]   path: '/usr/bin/custom_exe_3070'
[your-program] }
[your-program] Error: ENOENT: no such file or directory, access '/sbin/custom_exe_3070'
[your-program]     at Object.accessSync (node:fs:253:11)
[your-program]     at pathCheck (/app/app/main.js:51:10)
[your-program]     at Interface.<anonymous> (/app/app/main.js:37:7)
[your-program]     at Interface.emit (node:events:519:28)
[your-program]     at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
[your-program]     at [_line] [as _line] (node:internal/readline/interface:887:18)
[your-program]     at [_ttyWrite] [as _ttyWrite] (node:internal/readline/interface:1274:24)
[your-program]     at ReadStream.onkeypress (node:internal/readline/interface:264:20)
[your-program]     at ReadStream.emit (node:events:519:28)
[your-program]     at emitKeys (node:internal/readline/utils:371:14) {
[your-program]   errno: -2,
[your-program]   code: 'ENOENT',
[your-program]   syscall: 'access',
[your-program]   path: '/sbin/custom_exe_3070'
[your-program] }
[your-program] Error: ENOENT: no such file or directory, access '/bin/custom_exe_3070'
[your-program]     at Object.accessSync (node:fs:253:11)
[your-program]     at pathCheck (/app/app/main.js:51:10)
[your-program]     at Interface.<anonymous> (/app/app/main.js:37:7)
[your-program]     at Interface.emit (node:events:519:28)
[your-program]     at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
[your-program]     at [_line] [as _line] (node:internal/readline/interface:887:18)
[your-program]     at [_ttyWrite] [as _ttyWrite] (node:internal/readline/interface:1274:24)
[your-program]     at ReadStream.onkeypress (node:internal/readline/interface:264:20)
[your-program]     at ReadStream.emit (node:events:519:28)
[your-program]     at emitKeys (node:internal/readline/utils:371:14) {
[your-program]   errno: -2,
[your-program]   code: 'ENOENT',
[your-program]   syscall: 'access',
[your-program]   path: '/bin/custom_exe_3070'
[your-program] }
[your-program] custom_exe_3070 Alice David: not found
[your-program] $
[tester::#IP1] Assertion failed.
[tester::#IP1] Test failed

What’s even more funny is that when I remove the console log from the catch block, I get the output for console.log(execPath, command, cmdArgs);

Solved. I was missing the fact that I have to wait for the custom exe to run, even though I had already tried this using promise earlier. One of the solutions I saw hadn’t used any async logic at all.

const pathCheck = async (args, execute = false) => {
  const directories = process.env.PATH.split(path.delimiter);
  let [command, ...cmdArgs] = args.split(" ");

  for (const directory of directories) {
    const execPath = path.join(directory, command);

    try {
      fs.accessSync(execPath, fs.constants.X_OK);
      if (execute) {
        const child = spawn(execPath, cmdArgs, {
          stdio: "inherit",
          argv0: command,
        });

        await new Promise((resolve) => {
          child.on("close", resolve);
        });
      } else {
        console.log(`${command} is ${execPath}`);
      }
      return;
    } catch (e) {}
  }

  console.log(`${args}: not found`);
};
1 Like

Nice catch.

I assumed that happened where you returned to before. Weird you got the console log “not found” message though at the bottom though since you return.

That is still a mystery honestly, still makes no sense to me.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.