diff --git a/src/extension.ts b/src/extension.ts index 0565fea..58c9a2b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -149,15 +149,6 @@ export async function activate( registerBuildifierFormatter(); - // if this is a multi-root project, create a listener to refresh the symlinked project root directory on file add/remove - if (ProjectViewManager.isMultiRoot()) { - const w = workspace.createFileSystemWatcher( - new RelativePattern(workspaceRoot, '*') - ); - w.onDidCreate((_e) => ProjectViewManager.syncWorkspaceRoot()); - w.onDidDelete((_e) => ProjectViewManager.syncWorkspaceRoot()); - } - // trigger a refresh of the tree view when any task get executed tasks.onDidStartTask((_) => BazelRunTargetProvider.instance.refresh()); tasks.onDidEndTask((_) => BazelRunTargetProvider.instance.refresh()); diff --git a/src/projectViewManager.ts b/src/projectViewManager.ts index ea50106..ecc2e53 100644 --- a/src/projectViewManager.ts +++ b/src/projectViewManager.ts @@ -1,13 +1,4 @@ -import { - existsSync, - mkdirSync, - readdirSync, - rmSync, - statSync, - symlinkSync, - writeFileSync, -} from 'fs'; -import { homedir } from 'os'; +import { writeFileSync } from 'fs'; import { sep } from 'path'; import { commands, @@ -24,8 +15,17 @@ import { getVscodeConfig, getWorkspaceRoot } from './util'; export namespace ProjectViewManager { const workspaceRoot = getWorkspaceRoot(); - const workspaceRootName = workspaceRoot.split('/').reverse()[0]; - const projectRootSymlinks = `${homedir}${sep}${workspaceRootName}`; + const workspaceRootName = getWorkspaceRootName(); + + function getWorkspaceRootName(): string { + const name = workspaceRoot.split('/').reverse()[0]; + if (!name || name.trim() === '') { + throw new Error( + `Invalid workspace root path, cannot extract name: ${workspaceRoot}` + ); + } + return name; + } export function isMultiRoot(): boolean { return !!workspace.workspaceFile; @@ -93,10 +93,6 @@ export namespace ProjectViewManager { async function getDisplayFolders(): Promise { let displayFolders = new Set(['.eclipse']); // TODO bubble this out to a setting - if (isMultiRoot()) { - syncWorkspaceRoot(); - displayFolders.add(projectRootSymlinks); - } try { const bazelProjectFile = await getBazelProjectFile(); if (bazelProjectFile.directories.includes('.')) { @@ -149,22 +145,21 @@ export namespace ProjectViewManager { function updateMultiRootProjectView( displayFolders: string[] ): Thenable { + // 1. workspaceRoot as the first workspace folder, so ${workspaceFolder} resolves to the real project path + // 2. Other subdirectories serve as quick access entries + const workspaceFoldersToAdd = [ + { uri: Uri.file(workspaceRoot), name: workspaceRootName }, + ...displayFolders + .filter((f) => f !== '.') // Exclude '.' since workspaceRoot already represents the root directory + .map((f) => ({ + uri: Uri.file(`${workspaceRoot}/${f}`), + name: f.replaceAll(sep, ' ⇾ '), + })), + ]; workspace.updateWorkspaceFolders( 0, workspace.workspaceFolders?.length, - ...displayFolders.map((f) => { - if (f === projectRootSymlinks) { - return { - uri: Uri.file(projectRootSymlinks), - name: workspaceRootName, - }; - } else { - return { - uri: Uri.file(`${workspaceRoot}/${f}`), - name: f.replaceAll(sep, ' ⇾ '), - }; - } - }) + ...workspaceFoldersToAdd ); return Promise.resolve(displayFolders); } @@ -258,24 +253,4 @@ export namespace ProjectViewManager { function rootDirOnly(dirs: string[]): string[] { return dirs.map((d) => d.split('/')[0]); } - - export function syncWorkspaceRoot() { - if (existsSync(projectRootSymlinks)) { - rmSync(projectRootSymlinks, { recursive: true }); // delete - } - - mkdirSync(projectRootSymlinks); - - readdirSync(workspaceRoot).forEach((f) => { - const fpath = `${workspaceRoot}${sep}${f}`; - if (existsSync(fpath)) { - const stats = statSync(fpath); - if (stats.isFile()) { - symlinkSync(fpath, `${projectRootSymlinks}${sep}${f}`); - } - } - }); - - commands.executeCommand('workbench.files.action.refreshFilesExplorer'); - } } diff --git a/src/util.ts b/src/util.ts index 20a28e5..81dc533 100644 --- a/src/util.ts +++ b/src/util.ts @@ -19,7 +19,11 @@ derive_targets_from_directories: true export function getWorkspaceRoot(): string { if (workspace.workspaceFile) { - return dirname(workspace.workspaceFile.path); + const workspaceFilePath = dirname(workspace.workspaceFile.path); + if (workspaceFilePath.endsWith('.vscode')) { + return dirname(workspaceFilePath); + } + return workspaceFilePath; } else { if (workspace.workspaceFolders && workspace.workspaceFolders.length > 0) { return workspace.workspaceFolders[0].uri.path;