Build your shell "The pwd builtin"'s testcases seems bugged #ei0

I’m stuck on Stage # #ei0.

First I thought I am loosing my mind, but the logs clearly show the testing process is incorrect

I am currently in the pwd builtin section, which is fairly easy task but my code is getting stuck in the type testcase which I have already passed before. I then hard coded the paths and tried to check if there is my_exe is expected in them, but the path my program shows seems to be the correct one and the expected path doesn’t have the my_exe file. Moreover, I have no idea why I am suddenly getting error in this testcase which I have already passed before

Any help is appreciated

Here are my logs:

remote: [your-program] $ type mkdir
remote: [tester::#MG5] ✓ Received expected response
remote: [your-program] $ type my_exe
remote: [your-program] my_exe is /tmp/quz/my_exe
remote: [tester::#MG5] Output does not match expected value.
remote: [tester::#MG5] Expected: "my_exe is /tmp/bar/my_exe"
remote: [tester::#MG5] Received: "my_exe is /tmp/quz/my_exe"
remote: [your-program] True
remote: [your-program] False
remote: [your-program] False
remote: [your-program] False
remote: [your-program] $
remote: [tester::#MG5] Assertion failed.
remote: [tester::#MG5] Test failed`

And here’s a snippet of my relevant code:

        elif split_command[0] == "type":
            PATH = os.environ.get("PATH")
            PATH_SPLIT = PATH.split(":") #separated by : in codecrafter's server
            command_path = 0
            for path in PATH_SPLIT:
                # print("currently in:",str(path) + "/" + str(split_command[1]))
                if os.path.isfile(str(path) + "/" + str(split_command[1])):
                    # print(str(path) + "/" + str(split_command[1]))
                    command_path = str(path) + "/" + str(split_command[1])
            if split_command[1] in ['echo','type','exit','pwd']:
                print(split_command[1],"is a shell builtin")
            elif split_command[1] == "my_exe":
                print("my_exe is",command_path)
                print(os.path.isfile(command_path))
                print(os.path.isfile("/tmp/bar/my_exe"))
                print(os.path.isfile("/tmp/qux/my_exe"))
                print(os.path.isfile("/tmp/foo/my_exe"))
            elif command_path:
                print(split_command[1],"is",command_path)
            else:
                print(split_command[1],": not found", sep="")

1 Like

Hi @Shreayan, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Here’s the github link:
https://github.com/Shreayan/codecrafters/blob/main/main.py

Hi, I am following up to my last message. Did you get the github link?

Hey, I’m having the same problem in C. What’s interesting, the bug is floating: the test runner throws an error from time to time, I’d say 50% cases or more

repo: GitHub - avfirsov/shell-in-c

Test logs:

[tester::#MG5] Running tests for Stage #MG5 (The type builtin: executable files)
[tester::#MG5] [setup] export PATH=/tmp/foo:$PATH
[tester::#MG5] [setup] export PATH=/tmp/qux:$PATH
[tester::#MG5] [setup] Available executables:
[tester::#MG5] [setup] - my_exe
[tester::#MG5] Running ./your_program.sh
[your-program] $ type cat
[your-program] cat is /bin/cat
[tester::#MG5] ✓ Received expected response
[your-program] $ type cp
[your-program] cp is /bin/cp
[tester::#MG5] ✓ Received expected response
[your-program] $ type mkdir
[your-program] mkdir is /bin/mkdir
[tester::#MG5] ✓ Received expected response
[your-program] $ type my_exe
[your-program] my_exe is /tmp/foo/my_exe
[tester::#MG5] Output does not match expected value.
[tester::#MG5] Expected: "my_exe is /tmp/qux/my_exe"
[tester::#MG5] Received: "my_exe is /tmp/foo/my_exe"

That’s reassuring to hear, btw do you get the error 50% of the time or did I understand that incorrectly?

Yeah, I had 2 tries with the exact same code: first try failed. Then I only added new line, keeping other code the same and it worked!

lmaoo, someone fix this!!

Hi @Shreayan, please note that my_exe can exist in multiple locations, and the first one found in PATH should be selected. However, if you loop through PATH without an early stop, later entries can overwrite the correct path.

Hi @avfirsov, it seems your code is also potentially overwriting the correct path.

1 Like

well thanks for the clarification, but the problem statement fails to do a clear job explaining that we must print the first occurrence where my_exe is found. But thanks for your time tho

@Shreayan Thanks for highlighting the issue! The instructions could definitely be improved.

Another heads-up: You might also need to check file permissions and ensure that only files with execute permission are considered valid executables.

1 Like

okay! will definitely keep that in mind : )

1 Like

Thanx a lot!

1 Like

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

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