Skip to content

Commit 50c55af

Browse files
committed
✨ feat: enhance search command with improved messaging for workspace root and dependencies, and refine relative path calculation for packages
1 parent bdd7c1c commit 50c55af

4 files changed

Lines changed: 59 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/), and this
1616

1717
- Refactored workspace dependencies architecture for better code reusability
1818

19+
### Fixed
20+
21+
- Fixed workspace root path calculation to ensure proper relative paths for search functionality
22+
1923
## [0.2.1] - 2025-07-23
2024

2125
### Added

src/commands.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,6 @@ export function registerCommands(context: vscode.ExtensionContext) {
310310

311311
// Get all paths (package + dependencies)
312312
const allPaths = await getPackageAndDependencyPaths(selected.package.name);
313-
if (allPaths.length === 0) {
314-
vscode.window.showInformationMessage(`No paths found for package "${selected.package.name}".`);
315-
return;
316-
}
317313

318314
// Format paths for search (comma-space separated)
319315
const searchPaths = allPaths.join(', ');
@@ -325,9 +321,19 @@ export function registerCommands(context: vscode.ExtensionContext) {
325321
triggerSearch: false, // Don't auto-search, let user enter search term
326322
});
327323

328-
vscode.window.showInformationMessage(
329-
`Opened search in ${allPaths.length} locations: ${selected.package.name} and its ${allPaths.length - 1} dependencies.`
330-
);
324+
// Show appropriate message based on whether workspace root is included
325+
let message: string;
326+
if (selected.package.isRoot && allPaths.length === 0) {
327+
message = `Opened search in workspace root (${selected.package.name}) - no workspace dependencies found.`;
328+
} else if (selected.package.isRoot) {
329+
message = `Opened search in workspace root (${selected.package.name}) and its ${allPaths.length} dependencies.`;
330+
} else {
331+
const totalLocations = allPaths.length;
332+
const dependencyCount = allPaths.length - 1;
333+
message = `Opened search in ${totalLocations} locations: ${selected.package.name} and its ${dependencyCount} dependencies.`;
334+
}
335+
336+
vscode.window.showInformationMessage(message);
331337
} catch (error) {
332338
logError('Failed to search in package and workspace dependencies', error);
333339
vscode.window.showErrorMessage('Failed to search in package and workspace dependencies');

src/package-scanner.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,27 @@ export async function loadPackageInfo(packageJsonUri: vscode.Uri): Promise<{ nam
8282

8383
const packageDir = Utils.dirname(packageJsonUri);
8484
const workspaceFolder = vscode.workspace.getWorkspaceFolder(packageDir);
85-
const relativePath = workspaceFolder ? vscode.workspace.asRelativePath(packageDir, false) : packageDir.path;
85+
86+
let relativePath: string;
87+
if (workspaceFolder) {
88+
// Use asRelativePath and ensure it's actually relative
89+
const vsCodeRelativePath = vscode.workspace.asRelativePath(packageDir, false);
90+
// If asRelativePath returns an absolute path (starts with / or drive letter), compute manually
91+
if (vsCodeRelativePath.startsWith('/') || (vsCodeRelativePath.length > 1 && vsCodeRelativePath[1] === ':')) {
92+
// Manually compute relative path from workspace folder to package directory
93+
const workspacePath = workspaceFolder.uri.fsPath;
94+
const packagePath = packageDir.fsPath;
95+
if (packagePath.startsWith(workspacePath)) {
96+
relativePath = packagePath.substring(workspacePath.length + 1).replace(/\\/g, '/');
97+
} else {
98+
relativePath = vsCodeRelativePath;
99+
}
100+
} else {
101+
relativePath = vsCodeRelativePath;
102+
}
103+
} else {
104+
relativePath = packageDir.path;
105+
}
86106

87107
log(`Package validation successful: ${validatedPackage.name} at ${relativePath}`);
88108
return {

src/pnpm-workspace.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,22 @@ export async function scanWorkspacePackages(): Promise<WorkspacePackage[]> {
4646
const rootInfo = await loadPackageInfo(rootPackageJson);
4747
if (rootInfo) {
4848
log(`Found workspace root package: ${rootInfo.name}`);
49+
50+
// For workspace root, determine the correct relative path
51+
const workspaceFolder = vscode.workspace.getWorkspaceFolder(workspaceRoot);
52+
let rootPath: string;
53+
if (workspaceFolder && workspaceFolder.uri.toString() === workspaceRoot.toString()) {
54+
// If this workspace root is the same as VS Code's workspace folder root, use "."
55+
rootPath = '.';
56+
} else {
57+
// Otherwise, use the relative path from the current workspace folder
58+
rootPath = workspaceFolder ? vscode.workspace.asRelativePath(workspaceRoot, false) : rootInfo.path;
59+
}
60+
61+
log(`Workspace root path determined as: "${rootPath}"`);
4962
packages.push({
5063
name: rootInfo.name,
51-
path: rootInfo.path,
64+
path: rootPath,
5265
uri: workspaceRoot,
5366
isRoot: true,
5467
});
@@ -168,9 +181,13 @@ export async function getPackageAndDependencyPaths(packageName: string): Promise
168181

169182
const allPaths: string[] = [];
170183

171-
// Add the target package path first
172-
allPaths.push(targetPackage.path);
173-
log(`Added target package path: ${targetPackage.path}`);
184+
// Add the target package path first, but skip if it's the workspace root (current directory)
185+
if (targetPackage.path && targetPackage.path !== '.') {
186+
allPaths.push(targetPackage.path);
187+
log(`Added target package path: ${targetPackage.path}`);
188+
} else {
189+
log(`Skipping target package path (workspace root): ${targetPackage.path}`);
190+
}
174191

175192
// Get workspace dependency paths
176193
const dependencyPaths = await getWorkspaceDependencyPathsFromPackage(packageName, packages);

0 commit comments

Comments
 (0)