Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
54 changes: 52 additions & 2 deletions src/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ import {
platformSpecificBinaryName,
platformSpecificNodePackageName,
} from "./constants";
import { config, fileExists, getLspBin, safeSpawnSync } from "./utils";
import {
config,
fileExists,
getLspBin,
safeSpawnSync,
searchForFileRecursively,
} from "./utils";

export default class Locator {
private get globalNodeModulesPaths(): Record<string, Uri | undefined> {
Expand Down Expand Up @@ -126,7 +132,8 @@ export default class Locator {
(await this.findBiomeInNodeModules()) ??
(await this.findBiomeInGlobalNodeModules()) ??
(await this.findBiomeInYarnPnp()) ??
(await this.findBiomeInPath());
(await this.findBiomeInPath()) ??
(await this.findBiomeInSubDir());

return await this.unshim(biome);
}
Expand Down Expand Up @@ -442,4 +449,47 @@ export default class Locator {

return undefined;
}

private async findBiomeInSubDir(): Promise<Uri | undefined> {
const folder = this.biome.workspaceFolder;
if (!folder) {
return;
}

this.biome.logger.debug("🔍 Looking for Biome binary in sub directories");

const skipDirectories = [
".git",
"node_modules",
"dist",
"build",
"coverage",
"target", // Rust
"vendor", // Go/PHP
".next",
".nuxt",
"out",
"temp",
];

const configPath = await searchForFileRecursively(
"biome.json",
folder.uri,
skipDirectories,
);
if (!configPath) {
this.biome.logger.debug("🔍 No Biome binary found in sub directories");
return;
}

const biome =
(await this.findBiomeInNodeModules(configPath)) ??
(await this.findBiomeInYarnPnp());

if (!biome) {
this.biome.logger.debug("🔍 No Biome binary found in sub directories");
}

return biome;
}
}
42 changes: 42 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,45 @@ export const safeSpawnSync = (

return output;
};

export const searchForFileRecursively = async (
file: string,
dirUri: Uri,
skipDirectories: string[] = [],
maxDepth: number = 3,
): Promise<Uri | undefined> => {
if (maxDepth <= 0) {
return;
}

try {
const entries = await workspace.fs.readDirectory(dirUri);

const filePath = Uri.joinPath(dirUri, file);
if (await fileExists(filePath)) {
return dirUri;
}

for (const [name, type] of entries) {
if (type !== FileType.Directory || skipDirectories.includes(name)) {
continue;
}

const subDirUri = Uri.joinPath(dirUri, name);
const filePath = await searchForFileRecursively(
file,
subDirUri,
skipDirectories,
maxDepth - 1,
);

if (filePath) {
return filePath;
}
}
} catch (_) {
// Directory might not be readable, continue silently
}

return;
};