Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/cli/src/ui/commands/cdCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,23 @@ describe('cdCommand', () => {
});
});

it('resolves a Windows-style home-relative path from the home directory', async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] This test only covers the error path (directory not found). No success test verifies that cd ~\existingDir actually calls relocateWorkingDirectory. Consider adding a success-path test mirroring the existing ../next test pattern:

it('moves to a Windows-style home-relative directory', async () => {
  const homeSub = path.join(os.homedir(), `qwen-cd-ok-${process.pid}`);
  fs.mkdirSync(homeSub);
  try {
    const result = await cdCommand.action?.(context, `~\\qwen-cd-ok-${process.pid}`);
    expect(relocateWorkingDirectory).toHaveBeenCalledWith(homeSub, context);
  } finally {
    fs.rmSync(homeSub, { recursive: true });
  }
});

— qwen3.7-max via Qwen Code /review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. This is covered now by the success-path Windows-style home-relative /cd test, and the targeted cdCommand.test.ts -t "Windows-style home-relative" run passed: 2 passed, 18 skipped.

const missingName = `qwen-cd-missing-${process.pid}-${Date.now()}`;
const expectedPath = path.normalize(path.join(os.homedir(), missingName));

const result = (await cdCommand.action?.(
context,
`~\\${missingName}`,
)) as MessageActionReturn;

expect(result).toEqual({
type: 'message',
messageType: 'error',
content: `Couldn't find a directory at ${expectedPath}.`,
});
expect(relocateWorkingDirectory).not.toHaveBeenCalled();
});

it('moves to a path with escaped spaces', async () => {
const spacedDir = path.join(tmpDir, 'space dir');
fs.mkdirSync(spacedDir);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/ui/commands/cdCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function resolveCdPath(input: string, baseDir: string): string {
return path.normalize(os.homedir());
}

if (input.startsWith('~/')) {
if (input.startsWith('~/') || input.startsWith('~\\')) {
return path.normalize(path.join(os.homedir(), input.slice(2)));
}

Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/utils/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@ describe('resolvePath', () => {
expect(result).toBe(path.join(homeDir, 'documents/file.txt'));
});

it('expands Windows-style tilde-prefixed paths to home directory', () => {
const homeDir = os.homedir();
const result = resolvePath('/some/base', '~\\documents\\file.txt');
expect(result).toBe(path.join(homeDir, 'documents\\file.txt'));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] Test validates broken behavior on POSIX. Both the implementation and path.join(homeDir, 'documents\\file.txt') produce the same malformed path (/home/user/documents\file.txt) on macOS/Linux — a path with a literal backslash in the filename. The test passes on POSIX but for the wrong reason.

A correct assertion would expose the bug by expecting proper path joining:

Suggested change
expect(result).toBe(path.join(homeDir, 'documents\\file.txt'));
expect(result).toBe(path.join(homeDir, 'documents', 'file.txt'));

This assertion will FAIL on POSIX with the current implementation (exposing the bug) and PASS after applying the split(/[\/\\]+/) fix.

— qwen3.7-max via Qwen Code /review

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] This test encodes a POSIX-incorrect result. On macOS/Linux, path.join(homeDir, 'documents\\file.txt') produces a path with literal backslashes in the filename (e.g. /home/user/documents\file.txt), not a nested directory path. On Windows, path.join normalizes backslashes so the result happens to be correct.

The codebase already has a more robust pattern in channels/base/src/paths.ts and cli/src/utils/resolvePath.ts that splits on both separators before joining:

path.join(homeDir, ...relativePath.slice(2).split(/[/\\]+/).filter(Boolean))

This normalizes separators correctly on all platforms. Consider adopting this pattern in both resolvePath() (paths.ts:332) and resolveCdPath() (cdCommand.ts:44), and updating the test assertion accordingly.

— qwen3.7-max via Qwen Code /review

});

it('uses baseDir when provided for relative paths', () => {
const baseDir = '/custom/base';
const result = resolvePath(baseDir, './relative/path');
Expand Down Expand Up @@ -619,6 +625,9 @@ describe('resolveAndValidatePath', () => {
expect(resolveAndValidatePath(configWithHome, '~/project')).toBe(
homeSubdir,
);
expect(resolveAndValidatePath(configWithHome, '~\\project')).toBe(
homeSubdir,
);
expect(resolveAndValidatePath(configWithHome, '~')).toBe(fakeHome);
} finally {
homedirSpy.mockRestore();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export function resolvePath(

if (relativePath === '~') {
return homeDir;
} else if (relativePath.startsWith('~/')) {
} else if (relativePath.startsWith('~/') || relativePath.startsWith('~\\')) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] POSIX backslash normalization bug. On macOS/Linux, path.join(homeDir, relativePath.slice(2)) does NOT convert backslashes — they become literal filename characters. Input ~\src\components resolves to /home/user/src\components (broken) instead of /home/user/src/components. The same bug exists in cdCommand.ts:43.

The codebase already has 6+ correct implementations using .split(/[\/\\]+/) before path.join (e.g., packages/cli/src/utils/resolvePath.ts:19-25, packages/core/src/config/storage.ts:59-67). Adopt the proven pattern:

Suggested change
} else if (relativePath.startsWith('~/') || relativePath.startsWith('~\\')) {
} else if (relativePath.startsWith('~/') || relativePath.startsWith('~\\')) {
return path.join(homeDir, ...relativePath.slice(2).split(/[\/\\]+/).filter(Boolean));

Apply the same fix in cdCommand.ts:43:

if (input.startsWith('~/') || input.startsWith('~\\')) {
  const segments = input.slice(2).split(/[\/\\]+/).filter(Boolean);
  return path.normalize(path.join(os.homedir(), ...segments));
}

— qwen3.7-max via Qwen Code /review

return path.join(homeDir, relativePath.slice(2));
} else if (path.isAbsolute(relativePath)) {
return relativePath;
Expand Down
Loading