Skip to content

Commit 6b039e2

Browse files
fix(complete): keep a partial command expanding to its full name
Registering the requested alias made it a suggestion of its own, so `webpack b<TAB>` offered `build` and `b` and the shell, having two candidates, left the word at their common prefix instead of writing `build` out. The last word is the one being typed, so only the words before it name the command an alias has to resolve. Co-authored-by: AmirSa12 <amirhosseinpr184@gmail.com>
1 parent df97606 commit 6b039e2

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,8 +2643,10 @@ class WebpackCLI {
26432643
};
26442644
}
26452645

2646-
// The command being completed, e.g. `b` in `complete -- b --mode=`.
2647-
const requested = words.find((word) => word !== "--" && !word.startsWith("-"));
2646+
// The command already typed, e.g. `b` in `complete -- b --mode=`. The last word
2647+
// is still being written — `complete -- b` is completing `b` itself, which must
2648+
// stay a prefix of `build` so the shell expands it instead of matching an alias.
2649+
const requested = words.slice(0, -1).find((word) => word !== "--" && !word.startsWith("-"));
26482650

26492651
for (const command of this.program.commands) {
26502652
const tabCommand = completion.commands.get(command.name());

test/complete/complete.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ describe("complete", () => {
7070
expect(suggestions).toEqual(expect.arrayContaining(["json", "markdown"]));
7171
});
7272

73+
it("should expand a partial command to a single suggestion", async () => {
74+
// `webpack b<TAB>` has to offer `build` alone, so the shell writes it out;
75+
// suggesting the alias `b` next to it would leave the word as typed.
76+
for (const [typed, command] of [
77+
["b", "build"],
78+
["w", "watch"],
79+
["s", "serve"],
80+
["i", "info"],
81+
]) {
82+
const { exitCode, stderr, stdout } = await run(__dirname, ["complete", "--", typed]);
83+
84+
expect(exitCode).toBe(0);
85+
expect(stderr).toBeFalsy();
86+
expect(parseCompletions(stdout)).toEqual([command]);
87+
}
88+
});
89+
7390
it("should suggest the same options for a command alias", async () => {
7491
const { stdout: canonical } = await run(__dirname, ["complete", "--", "build", "--"]);
7592

0 commit comments

Comments
 (0)