Running external programs causes unexpected results

I’m stuck on Stage #IP1.

I’ve tried to run the code on Windows command prompt.

Here are my logs:

remote: [compile] Moved ./.codecrafters/run.sh → ./your_program.sh
remote: [compile] Compilation successful.
remote:
remote: Debug = true
remote:
remote: [tester::#IP1] Running tests for Stage #IP1 (Run a program)
remote: [tester::#IP1] Running ./your_program.sh
remote: [your-program] $ custom_exe_9788 Maria
remote: [your-program] Program was passed 2 args (including program name).
remote: [your-program] Arg #0 (program name): /tmp/banana/banana/apple/custom_exe_9788
remote: [tester::#IP1] Output does not match expected value.
remote: [tester::#IP1] Expected: "Arg #0 (program name): custom_exe_9788"
remote: [tester::#IP1] Received: "Arg #0 (program name): /tmp/banana/banana/apple/custom_exe_9788"
remote: [your-program] Arg #1: Maria
remote: [your-program] Program Signature: 7761159136
remote: [your-program] $
remote: [tester::#IP1] Assertion failed.
remote: [tester::#IP1] Test failed

And here’s my code:


import os
import subprocess
import sys
from typing import Optional
def locate_executable(command) -> Optional[str]:
    path = os.environ.get("PATH", "")
    for directory in path.split(":"):
        file_path = os.path.join(directory, command)
        if os.path.isfile(file_path) and os.access(file_path, os.X_OK):
            return file_path

def handle_exit(args):
    sys.exit(int(args[0]) if args else 0)

def handle_echo(args):
    print(" ".join(args))

def handle_type(args):
    if args[0] in builtins:
        print(f"{args[0]} is a shell builtin")
    elif executable := locate_executable(command):
        # Print argument details
        print(f"Program was passed {len(args) + 1} args (including program name).")
        print(f"Arg #0 (program name): {os.path.basename(executable)}")  # Use basename here
        for i, arg in enumerate(args, start=1):
            print(f"Arg #{i}: {arg}")

        # Generate and print program signature
        #program_signature = generate_program_signature(command, args)
        #print(f"Program Signature: {program_signature}")

        # Execute the command
        subprocess.run([executable, *args])

    else:
        print(f"{args[0]} not found")

builtins = {
    "exit": handle_exit,
    "echo": handle_echo,
    "type": handle_type,
}

def main():
    while True:
        sys.stdout.write("$ ")
        sys.stdout.flush()
        command, *args = input().split(" ")
        if command in builtins:
            builtins[command](args)
            continue
        elif executable := locate_executable(command):
            subprocess.run([executable, *args])
        else:
            print(f"{command}: command not found")
        sys.stdout.flush()

if __name__ == "__main__":
    main()

Hi @phantom611, there might be multiple issues at play. Let’s tackle the first two that I noticed:

  1. There is no need to print argument details yourself, because our tester will verify it for you.

  1. You might need to pass command (just the command name) instead of executable (full path + command name) to subprocess.run:

1 Like

I had the same issue and suggestion 2. was what fixed it for me - I was misled by the code example here from Rohit, maybe that could be changed to avoid similar inquiries in the future.

2 Likes

Will keep this thread open until we have retired outdated code examples.