# Stage: Builtin completion #qp2, Autocompletion Works Locally but Fails Codecrafters Tests

**URL:** <https://forum.codecrafters.io/t/stage-builtin-completion-qp2-autocompletion-works-locally-but-fails-codecrafters-tests/5525>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [February 26, 2025, 11:18am UTC](https://forum.codecrafters.io/t/stage-builtin-completion-qp2-autocompletion-works-locally-but-fails-codecrafters-tests/5525 "2025-02-26T11:18:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Md-Talim](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/md-talim/32/5433_2.png) [@Md-Talim](https://forum.codecrafters.io/u/Md-Talim)\
**Post date:** [February 26, 2025, 11:18am UTC](https://forum.codecrafters.io/t/stage-builtin-completion-qp2-autocompletion-works-locally-but-fails-codecrafters-tests/5525/1 "2025-02-26T11:18:43Z")

</div>

I’m implementing the **builtin command autocompletion** stage for my shell project. Locally, autocompletion for `echo` and `exit` works as expected, but the Codecrafters tests fail.

#### **What I Have Implemented**

- When the user types `ech` and presses `<TAB>`, it completes to `echo␣`.
- When the user types `exi` and presses `<TAB>`, it completes to `exit␣`.
- The logic ensures autocompletion happens **only if there is exactly one match**.

#### **Issue**

- The same code works fine on my local machine but does not pass the Codecrafters tests.
- I’m not sure what the test framework expects differently.
- The failure message does not clearly indicate what’s wrong.

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/7/746be5b11839444a332301f05f71fb265ae376e5.png)

#### **Questions**

1. Does Codecrafters expect a specific way of handling output that differs from regular terminal behavior?
2. Is there any specific way to simulate user input/output to debug this issue?
3. Could there be a difference in how `<TAB>` is handled in the test environment?

Any insights from others who have passed this stage would be really helpful. Thanks!

Here’s the code in java:

```java
package shell;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import shell.command.*;
import shell.environment.Environment;
import shell.environment.SystemEnvironment;
import shell.io.Redirection;
import shell.parser.Parser;
import shell.process.ProcessExecutor;
import shell.process.SystemProcessExecutor;
import shell.terminal.Termios;

public class Shell {
    private final Map<String, Command> builtinCommands;
    private final Environment environment;
    private final ProcessExecutor processExecutor;
    private boolean shouldExit;

    public Shell() {
        this.environment = new SystemEnvironment();
        this.processExecutor = new SystemProcessExecutor();
        this.builtinCommands = new HashMap<>();
        this.shouldExit = false;

        initializeBuiltinCommands();
    }

    private void initializeBuiltinCommands() {
        builtinCommands.put("exit", new ExitCommand(() -> shouldExit = true));
        builtinCommands.put("echo", new EchoCommand());
        builtinCommands.put("pwd", new PwdCommand(environment));
        builtinCommands.put("cd", new CdCommand(environment));
        builtinCommands.put("type", new TypeCommand(builtinCommands, environment));
    }

    private void executeCommand(String input) {
        try {
            Parser parser = new Parser(input);
            List<String> args = parser.parse();
            Redirection redirection = parser.getRedirection();

            if (args.isEmpty()) {
                return;
            }

            String commandName = args.get(0);
            Command command = builtinCommands.getOrDefault(commandName,
                    new ExternalCommand(commandName, processExecutor));
            command.execute(args, redirection);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    private String autocomplete(String input) {
        List<String> matches = builtinCommands.keySet()
                .stream()
                .filter(command -> command.startsWith(input))
                .sorted()
                .toList();

        if (matches.size() == 1) {
            return matches.get(0) + " ";
        }

        return null;
    }

    private String read() {
        Termios.enableRawMode();
        final var line = new StringBuilder();

        try {
            while (true) {
                int key = System.in.read();

                if (key == -1 || key == 4) { // CTRL + D (EOF)
                    return null;
                } else if (key == '\n') { // ENTER KEY
                    System.out.println();
                    return line.toString();
                } else if (key == '\t') { // HANDLE TAB KEY
                    String completion = autocomplete(line.toString());
                    if (completion != null) {
                        System.out.print("\r$ " + completion);
                        line.setLength(0);
                        line.append(completion);
                    }
                } else if (key == 127) { // BACKSPACE KEY
                    if (line.length() > 0) {
                        line.setLength(line.length() - 1);
                        System.out.print("\b \b"); // Move cursor back, erase character
                    }
                } else {
                    line.append((char) key);
                    System.out.print((char) key);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            Termios.disableRawMode();
        }

        return "";
    }

    public void run() {
        while (true) {
            System.out.print("$ ");
            final String line = read();

            if (line == null) {
                break;
            }

            executeCommand(line);

            if (shouldExit) {
                break;
            }
        }
    }
}

```

See the full code on Github here (feat/auto-completions branch): [https://github.com/Md-Talim/codecrafters-shell-java/tree/feat/auto-completions](https://github.com/Md-Talim/codecrafters-shell-java/tree/feat/auto-completions)

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [February 26, 2025, 12:54pm UTC](https://forum.codecrafters.io/t/stage-builtin-completion-qp2-autocompletion-works-locally-but-fails-codecrafters-tests/5525/3 "2025-02-26T12:54:03Z")

</div>

I’ll take a look and get back to you by the end of the week.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [February 28, 2025, 9:47am UTC](https://forum.codecrafters.io/t/stage-builtin-completion-qp2-autocompletion-works-locally-but-fails-codecrafters-tests/5525/5 "2025-02-28T09:47:41Z")

</div>

@Md-Talim 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.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [March 9, 2025, 3:59pm UTC](https://forum.codecrafters.io/t/stage-builtin-completion-qp2-autocompletion-works-locally-but-fails-codecrafters-tests/5525/7 "2025-03-09T15:59:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [March 14, 2025, 3:59pm UTC](https://forum.codecrafters.io/t/stage-builtin-completion-qp2-autocompletion-works-locally-but-fails-codecrafters-tests/5525/8 "2025-03-14T15:59:30Z")

</div>

Closed on March 14, 2025, 3:59pm UTC

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