Struggling to understand what's causing this error

I’m stuck on Stage #NI6. The problem appears to be that my code is adding a newline character before returning the prompt, however I can’t find where this character is being added.

I’ve tried testing locally on my machine, however there doesn’t appear to be a newline character when I run the cat command with multiple files. I also played with how the cat output is returned to the terminal, but that didn’t make a difference. I would appreciate if someone could look over my code and point me in the right direction.

Here are my logs:

remote: [tester::#NI6] Running tests for Stage #NI6 (Quoting - Single quotes)
remote: [tester::#NI6] [setup] export PATH=/tmp/grape/mango/grape:$PATH
remote: [tester::#NI6] Running ./your_program.sh
remote: [tester::#NI6] [setup] echo -n "mango blueberry." > "/tmp/quz/f   51"
remote: [tester::#NI6] [setup] echo -n "pineapple strawberry." > "/tmp/quz/f   12"
remote: [tester::#NI6] [setup] echo -n "raspberry grape." > "/tmp/quz/f   63"
remote: [your-program] $ echo 'example hello'
remote: [your-program] example hello
remote: [your-program] $ echo hello     script
remote: [your-program] hello script
remote: [tester::#NI6] ✓ Received expected response
remote: [your-program] $ echo 'shell     world' 'script''example'
remote: [your-program] shell     world scriptexample
remote: [tester::#NI6] ✓ Received expected response
remote: [your-program] $ cat '/tmp/quz/f   51' '/tmp/quz/f   12' '/tmp/quz/f   63'
remote: [your-program] mango blueberry.pineapple strawberry.raspberry grape.
remote: [tester::#NI6] Expected prompt ("$ ") but received ""
remote: [your-program] $
remote: [tester::#NI6] Assertion failed.
remote: [tester::#NI6] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

And here’s a snippet of my code:

function prompt() {
  rl.question("$ ", (answer) => {
    const args = parser(answer);
    switch (args[0]) {
      case 'exit':
        if (args.length < 2 || args[1] !== '0') break;
        exit(0);
      case 'echo':
        if (answer.includes('\'')) {
          console.log(answer.substring(answer.indexOf('\''), answer.lastIndexOf('\'')).replaceAll('\'', ''));
        } else {
          console.log(...args.splice(1, args.length));
        }
        break;
      case 'type':
        if (builtin.includes(args[1])) {
          console.log(`${args[1]} is a shell builtin`);
        }
        else {
          const fileName = getFilePath(args[1]);
          if (fileName === null) {
            console.log(`${args[1]}: not found`);
          }
          else {
            console.log(`${args[1]} is ${fileName}`);
          }
        }
        break;
      case 'pwd':
        console.log(process.env.PWD);
        break;
      case 'cd':
        if (args[1] == '~') process.env.PWD = process.env.HOME;
        else if (fs.existsSync(buildPathToDirectory(args[1]))) {
          process.env.PWD = buildPathToDirectory(args[1]);
        }
        else if (fs.existsSync(args[1])) {
          process.env.PWD = `${args[1]}`;
        }
        else console.log(`${args[0]}: ${args[1]}: No such file or directory`);
        break;
      case 'cat':
        let output = "";
        for (let i = 1; i < args.length; i++) {
          if (fs.existsSync(args[i])) output += fs.readFileSync(args[i], 'utf-8');
        }
        output === "" ? output : console.log(output);
        break;
      default:
        const filePath = getFilePath(args[0]);
        if (filePath === null) console.log(`${args[0]}: command not found`);
        else {
          try {
            spawnSync(`${args[0]}`, args.slice(1), { shell: true, stdio: 'inherit' });
          } catch (err) {
            console.error(`Error executing command: ${err.message}`);
            console.error(err.stack);
          }
        }
        break;
    }
    prompt();
  });
}

Link to full code on GitHub: https://github.com/Nicholas-EG/Code-Crafters-Shell/blob/main/main.js

Can you try using process.stdout.write instead of console.log ? I suspect console.log is adding a newline behind the scenes.

edit:
docs don’t lie :grin:

Prints to stdout with newline.

2 Likes

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