Working locally but failing submit #ue6

I’m stuck on Stage #ue6.

Here are my logs:

[tester::#ZV2] ✓ Prompt line matches "$ wc grape-"
[tester::#ZV2] Pressed "<TAB>" (expecting autocomplete to "wc grape-90.txt" followed by a space)
[your-program] $ wc grape-grape-90.txt 
[tester::#ZV2] ^ Line does not match expected value.
[tester::#ZV2] Expected: "$ wc grape-90.txt "
[tester::#ZV2] Received: "$ wc grape-grape-90.txt "

Locally when I run it, It is working correctly.

Python Completer code:

builtin=["exit", "echo", "type", "pwd","cd"]
autocomplete_list=builtin.copy()
for path in os.environ.get("PATH", "").split(os.pathsep):
    if os.path.exists(path):
        autocomplete_list.extend(os.listdir(path))

def completer(text,state):
    buffer = readline.get_line_buffer()
    last_word = buffer.split()[-1] if buffer else ""
    prev_path = Path(last_word)
    if not prev_path.is_dir():
        prev_path = Path()
    if not text:
        options=([cmd for cmd in os.listdir(prev_path)])
    else:
        if '/' not in text and '\\' not in text:
            options = [cmd for cmd in autocomplete_list if cmd.startswith(text)]
            options.extend([cmd for cmd in os.listdir() if cmd.startswith(text)])
        else:
            path = prev_path/Path(text)
            prefix = ".\\" if not path.is_absolute else ""
            if path.is_dir():
                options = [prefix+str(path/cmd) for cmd in os.listdir(path)]
            else:
                path_dir = path.parent.absolute()
                path_text = path.name
                options = [cmd for cmd in os.listdir(path_dir) if cmd.startswith(path_text)]
    if state < len(options):
        option = Path(options[state])
        if option.is_dir():
            return str(option)+"/"
        return options[state]+" "
    return None

What am I here?

Hey @Gautam1608, looks like you’ve got past this stage. Do you happen to remember what was wrong? Would love to see if we can improve the tester / instructions.

I… dont know haha.

So 1st thing I did is to stop using powershell(default terminal in vscode).

I change it to git bash.

That meant I didnt need to worry about windows and posix, so I changed the completion to posix output:

def completer(text,state):
    if not text:
        options=([cmd for cmd in os.listdir()])
    else:
        if '/' not in text and '\\' not in text:
            options = [cmd for cmd in autocomplete_list if cmd.startswith(text)]
            options.extend([cmd for cmd in os.listdir() if cmd.startswith(text)])
        else:
            path = Path(text)
            prefix = "./" if not path.is_absolute() else ""
            if path.is_dir():
                options = [prefix+(path/cmd).as_posix() for cmd in os.listdir(path)]
            else:
                path_dir = path.parent.absolute() if path.parent.absolute().is_dir() else Path()
                path_text = path.name
                options = [(path.parent/cmd).as_posix() for cmd in os.listdir(path_dir) if cmd.startswith(path_text)]
    if state < len(options):
        option = Path(options[state])
        if option.is_dir():
            return option.as_posix()+"/"
        return options[state]+" "
    return None

I am not a pro, and I have written so much code that I am feeling a little lost myself.

The changes I made was to remove the readline.get_line_buffer() as in bash text is the full “word” unlike powershell, which I still dont know what text it was giving me.

I believe something here solved the problem.

Although I am facing a similar problem yet again:

[tester::#LC6] Running tests for Stage #LC6 (Filename Completion - Directory completion)
[tester::#LC6] [working_dir] - rat/
[tester::#LC6] [working_dir]   - pig/ (empty)
[tester::#LC6] Running ./your_program.sh
[tester::#LC6] ✓ Received prompt ($ )
[tester::#LC6] Typed "ls" followed by a <SPACE>
[tester::#LC6] ✓ Prompt line matches "$ ls "
[tester::#LC6] Pressed "<TAB>" (expecting autocomplete to "ls rat/")
[tester::#LC6] ✓ Prompt line matches "$ ls rat/"
[tester::#LC6] Pressed "<TAB>" (expecting autocomplete to "ls rat/pig/")
[your-program] $ ls rat/rat/
[tester::#LC6] ^ Line does not match expected value.
[tester::#LC6] Expected: "$ ls rat/pig/"
[tester::#LC6] Received: "$ ls rat/rat/"
[tester::#LC6] Test failed                                                                                                                                                                                      

But the code locally is working fine:

$ ls
app codecrafters.yml grape-90.txt pig pyproject.toml README.md uv.lock your_program.sh
$ cd pig
$ ls
owl
$ cd ..
$ cd pig/owl/
$

I did the exercise step for step. I am not able to replicate the failure locally.

FYI I typed: cd pig/ and it gave me cd pig/owl/

This time it should not be posix and windows issue, atleast thats what i believe.

Edit: I think this is important and I forgot to mention, readline doesnt work for windows.

So I am using the below import to resolve this. Took me good few tries to come up with this idea and I dont even know if it works or not.

try:
    import readline
except ImportError:
    try:
        from pyreadline3 import Readline
        readline = Readline()
        def input(txt):
            readline.readline(txt)
    except ImportError:
        readline = None

@Gautam1608 To make sure any earlier stages are not broken, you can use our CLI to test against previous stages by running:

codecrafters test --previous

This is a handy way to catch regressions.