MCPcopy Create free account
hub / github.com/Dart-Code/Dart-Code / locateBestProjectRoot

Function locateBestProjectRoot

src/shared/vscode/project.ts:5–27  ·  view source on GitHub ↗
(folder: string, allowOutsideWorkspace = false)

Source from the content-addressed store, hash-verified

3import { hasPackageMapFile, hasPubspec } from "../utils/fs";
4
5export function locateBestProjectRoot(folder: string, allowOutsideWorkspace = false): string | undefined {
6 // TODO(dantup): The term "Project" is a bit ambiguous, and some places that call here really want "pub roots" (eg. the package, or if it's
7 // part of a Pub Workspace, the pub workspace root), and some want the packages (eg. the things that have a pubspec.yaml and a name).
8 // Perhaps consider a GLOSSARY.md and define terms to use consistently (and match don't use "Project" at all?).
9
10 // TODO(dantup): Review places where allowOutsideWorkspace is effectively false, because opening sub-folders
11 // of Pub Workspaces is probably not uncommon.
12 if (!folder)
13 return undefined;
14
15 if (!allowOutsideWorkspace && (!isWithinWorkspace(folder) && workspace.workspaceFolders?.length)) {
16 return undefined;
17 }
18
19 let dir = folder;
20 while (dir !== path.dirname(dir)) {
21 if (hasPubspec(dir) || hasPackageMapFile(dir))
22 return dir;
23 dir = path.dirname(dir);
24 }
25
26 return undefined;
27}
28
29export function isWithinWorkspace(file: string) {
30 return !!workspace.getWorkspaceFolder(Uri.file(file));

Callers 13

findSuiteNodeFunction · 0.90
getOrCreateSuiteMethod · 0.90
isInsideFlutterProjectFunction · 0.90
runProjectTaskFunction · 0.90
getFolderToRunCommandInFunction · 0.90
getProjectSelectionFunction · 0.90
guessBestEntryPointMethod · 0.90
provideCodeActionsMethod · 0.90
runTestsMethod · 0.90

Calls 3

hasPubspecFunction · 0.90
hasPackageMapFileFunction · 0.90
isWithinWorkspaceFunction · 0.85

Tested by

no test coverage detected