__DIR__ strange output

I’m stuck on #EI0.

My current approach is to use the __DIR__ magic PHP constant. When I run the program, pwd outputs /home/astlaw83/www/html/codecrafters-shell-php, which is correct. When I run codecrafters submit, it outputs /app/app.

I can’t really debug this as I don’t know how it is getting /app in the first place or how to replicate it.

Here are my logs:

[your-program] $ type pwd
[your-program] pwd is a shell builtin
[tester::#EI0] ✓ Received expected response
[your-program] $ pwd
[your-program] /app/app
[tester::#EI0] ^ Line does not match expected value.
[tester::#EI0] Expected: "/app"
[tester::#EI0] Received: "/app/app"
[your-program] $ 
[tester::#EI0] Test failed

Here are the main parts of the code:

$commands = ['exit', 'echo', 'type', 'pwd'];

// run the shell forever
while (true) {
    fwrite(STDOUT, '$ ');

    // get the user's command
    $input = rtrim(fgets(STDIN), "\r\n");

    // stop the shell if the command is 'exit'
    if ($input === 'exit') break;

    // find the command
    $commandFound = false;
    foreach ($commands as $command) {
        $inputSplit = splitCommand($input);

        // check if the command is built-in
        if ($inputSplit[0] === $command) {
            // call the command's function passing the input without the command
            $functionName = "_$command";
            $functionName(substr($input, strlen($command) + 1));

            // exit loop
            $commandFound = true;
            break;
        } else {
            // check the command is an exec in PATH
            $execPath = checkPath($inputSplit[0]);

            // run exec if found
            if ($execPath) {
                system(implode(' ', $inputSplit));

                // exit loop
                $commandFound = true;
                break;
            }
        }
    }

    // fallback if command not found
    if (!$commandFound) fwrite(STDOUT, "$input: command not found" . PHP_EOL);
    unset($commandFound);
}

// ...

function _pwd(string $input): void {
    fwrite(STDOUT, __DIR__ . PHP_EOL);
}

Hi, thanks for your post!

I’m currently out of the office and will be back on March 23. I’ll get back to you as soon as possible once I return.

Not sure why you are using __DIR__, that seems to to the source file itself. The process may run from another folder. Have you tried getcwd??

Yes, I looked at some other solutions, and it seems like I took the wrong approach. Not so used to system PHP commands, so getcwd did not spring to mind. Thanks for the clarification on what __DIR__ points to. :+1: