I’m stuck on Stage Stage #QJ0 (Executing a quoted executable), TypeScript
Im getting the following results during your autotests:
remote: [your-program] $ ‘exe with space’ /tmp/ant/f1 remote: [your-program] /bin/sh: exe: not found remote: [tester::#QJ0] ^ Line does not match expected value. remote: [tester::#QJ0] Expected: “blueberry mango.” remote: [tester::#QJ0] Received: “/bin/sh: exe: not found”
But Im pretty sure my code correctly resolve the name of the executable as exe with space.
My logs on my PC:
$ ‘exe with space’ /tmp/pig/f1 exe with space: command not found $ echo ‘exe with space’ /tmp/pig/f1 exe with space /tmp/pig/f1 $
Also I incorporated some console.log() inside the functions which responsible for resolving the path to search for the executable and tried to send it to the platform autotest and heres the result below:
remote: [your-program] $ ‘exe with space’ /tmp/pig/f1 remote: [your-program] COMMAND NAME RECEIVED: exe with space remote: [tester::#QJ0] ^ Line does not match expected value. remote: [tester::#QJ0] Expected: “strawberry banana.” remote: [tester::#QJ0] Received: “COMMAND NAME RECEIVED: exe with space” remote: [your-program] FULL PATH TESTED: /tmp/pig/exe with space remote: [your-program] /bin/sh: exe: not found remote: [your-program] remote: [your-program] Error: Command failed: exe with space ‘/tmp/pig/f1’ remote: [your-program] /bin/sh: exe: not found remote: [your-program] remote: [your-program] $ remote: [tester::#QJ0] Assertion failed. remote: [tester::#QJ0] Test failed
This here /tmp/pig/exe with space is the string i send to check if it is executable. Does it seem like legitimate full path? Or is it somehow ill-formed? Is it possible something wrong with autotests or should I dig further? Need some help) thx in advance.
Well this output: /bin/sh: exe: not found suggest your code runs this line: console.log(`${splitCommand[0]}: command not found`); from one of the two else branches in your main loop. This implies splitCommand[0] is exe and not exe with space.
Again later in your debugging we see: FULL PATH TESTED: /tmp/pig/exe with space followed by /bin/sh: exe: not found which suggest again either parsing mistake or not outputting same expression as you try to run.
Above is actually a red herring, I think, because it is an execution issue, likely due to below.
When you try to run the command you use: const result = await commandExecuteWithPromise(executableName, args, fullPath); where executableName = splitCommand[0], and args is the rest of the splitCommand pieces. Then inside the code to execute you do: fullCommand = [fullCommand, ...fullCommandArray].join(' ');.
At this point it doesn’t even matter whether splitCommand[0] is exe with space or the first three are ["exe", "with", "space"] because joining them by ' ' gives the same start of fullCommand = "exe with space .....
You pass that to exec from child_process which decribes the argument as:
command The command to run, with space-separated arguments.
And it has a shell argument which defaults to /bin/sh.
First is philosophical: your challenge is to implement a shell but you are delegating the work to another shell, /bin/sh. You probably want to use execFile instead.
Now everything is clear and why we see the error /bin/sh: exe: not found. You are asking Node.js to run /bin/sh with the argument string exe with space ... that you constructed earlier with .join(' '). Just like if you opened /bin/sh (or bash) yourself and typed that. What likely happens is that node.js splits that into argv[] = {"exe", "with", "spaces", "...", "..."} for the invocation of /bin/sh. The system shell when run then “reparses” that and sees you wan to run exe with arguments withspaces.... But it can’t find a command named exe so the /bin/sh shell reports: /bin/sh: exe: not found.
Just…wow) for a user who just recently started to figuring out programming sounds like almost hacking :DDD thanks a lot for taking time and helping me dear sir). I passed the tests on the platform. But if I may, can I kindly ask You to elaborate a lil bit more on how exactly You figured out its not ‘exec’ but ‘execFile’ is the right command to use in this particular case? I mean i can understand that from /bin/sh being popped somwhere in the logs You already can say that my code tries to use another shell to do its work. But how one can tell that from the links You mentioned in Your reply? I mean can You put it maybe in more simpler(broader?) terms so I could be able to tell the difference at least on a higher level, and maybe later on I will read something more fundamental to be able to make same analysis in the future.
I guess I could tell the difference form the ‘exec’ description from the node.js documentation:
…spawns a shell then executes the command within that shell …command string passed to the exec function is processed directly by the shell… (docs)
and for ‘execFile’ description:
The child_process.execFile() function is similar … except that it does not spawn a shell by default. Rather, the specified executable file is spawned directly… (docs)
Well actually that was the last part that clicked. You start with what you know for sure, and then proceed with likely causes when investigating.
First I wrongly concluded that it was your error handling that outputted the “not found” message because you have similar line (a likely cause). When I started to manually evaluate the expressions in my head as if you parsed it both right, and wrongly, I realized that it wasn’t your console.log with not found that was being shown.
Kept going down until I reached exec. Based on the string joining I concluded that it goes a too-simple space-separated command string. Here I was surprised since most of these APIs in other languages typically take a list of strings and not a single combined string so I looked at the docs for exec and it was only then I realized that exec was a “shell-spawner”. Then it both made sense where the error was coming from and why /bin/sh was in the output, the shell was reporting its own name in the error.
As for how I found execFile, well if present, such a function would usually be in the same package as other kinds of process launching. I don’t work with Node.js, but the function description looked correct.
I can see your thought flow now, I guess being able to expect that there must be something else besides just “shell-spawner” also comes with a bit of experience on the matter))
Anyway, thanks again for Your help and guidance, I will remember it and hope it will help me deal with such issues in the future)