Skip to content

Commit abe678d

Browse files
b84330808claude
andcommitted
fix(prompts): accept both path separators on every OS
Per review: treat a trailing "/" or "\" as a separator regardless of platform, and move the Windows cases into path.test.ts behind skipIf. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f1f8d7a commit abe678d

3 files changed

Lines changed: 42 additions & 75 deletions

File tree

‎packages/prompts/src/path.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync, lstatSync, readdirSync } from 'node:fs';
2-
import { dirname, join, sep } from 'node:path';
2+
import { dirname, join } from 'node:path';
33
import type { Validate } from '@clack/core';
44
import { runValidation } from '@clack/core';
55
import { autocomplete } from './autocomplete.js';
@@ -87,8 +87,8 @@ export const path = (opts: PathOptions) => {
8787

8888
try {
8989
let searchPath: string;
90-
// `sep` is `\` on Windows, where both separators are valid
91-
const hasTrailingSeparator = userInput.endsWith('/') || userInput.endsWith(sep);
90+
// Accept both separators on every OS: Windows users type `\`
91+
const hasTrailingSeparator = userInput.endsWith('/') || userInput.endsWith('\\');
9292

9393
if (!existsSync(userInput)) {
9494
searchPath = dirname(userInput);

‎packages/prompts/test/path-win32.test.ts‎

Lines changed: 0 additions & 72 deletions
This file was deleted.

‎packages/prompts/test/path.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,45 @@ describe.each(['true', 'false'])('text (isCI = %s)', (isCI) => {
237237
expect(value).toBe('/tmp/foo');
238238
});
239239

240+
// Windows path semantics: node:path is win32 there, and memfs maps C:\\tmp to /tmp.
241+
test.skipIf(process.platform !== 'win32')(
242+
'directory mode traverses into child when trailing backslash entered',
243+
async () => {
244+
const result = prompts.path({
245+
message: 'foo',
246+
initialValue: 'C:\\tmp',
247+
directory: true,
248+
input,
249+
output,
250+
});
251+
252+
input.emit('keypress', '\\', { name: '\\' });
253+
input.emit('keypress', '', { name: 'return' });
254+
// cancel instead of hanging if return did not submit
255+
input.emit('keypress', '', { name: 'escape' });
256+
257+
expect(await result).toBe('C:\\tmp\\foo');
258+
}
259+
);
260+
261+
test.skipIf(process.platform !== 'win32')(
262+
'directory mode lists children of an initial value ending in a backslash',
263+
async () => {
264+
const result = prompts.path({
265+
message: 'foo',
266+
initialValue: 'C:\\tmp\\',
267+
directory: true,
268+
input,
269+
output,
270+
});
271+
272+
input.emit('keypress', '', { name: 'return' });
273+
input.emit('keypress', '', { name: 'escape' });
274+
275+
expect(await result).toBe('C:\\tmp\\foo');
276+
}
277+
);
278+
240279
test('directory mode can navigate from initial directory to child directory', async () => {
241280
const result = prompts.path({
242281
message: 'foo',

0 commit comments

Comments
 (0)