From 436d4388dd63f13b05ec4d90d303b1fa787d6beb Mon Sep 17 00:00:00 2001 From: Robert Lillack Date: Tue, 13 Feb 2024 19:32:11 +0100 Subject: [PATCH] feat: add Yarn PnP support (#134) Co-authored-by: Nicolas Hedger --- src/main.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/main.ts b/src/main.ts index ffac0d56..6c696118 100644 --- a/src/main.ts +++ b/src/main.ts @@ -397,6 +397,41 @@ async function getWorkspaceDependency( outputChannel: OutputChannel, ): Promise { for (const workspaceFolder of workspace.workspaceFolders ?? []) { + // Check for Yarn PnP and try resolving the Biome binary without a node_modules + // folder first. + for (const ext of ["cjs", "js"]) { + const pnpFile = Uri.joinPath(workspaceFolder.uri, `.pnp.${ext}`); + if (!(await fileExists(pnpFile))) { + continue; + } + + outputChannel.appendLine( + `Looks like a Yarn PnP workspace: ${workspaceFolder.uri.fsPath}`, + ); + try { + const pnpApi = require( + Uri.joinPath(workspaceFolder.uri, ".pnp.cjs").fsPath, + ); + const pkgPath = pnpApi.resolveRequest( + "@biomejs/biome/package.json", + workspaceFolder.uri.fsPath, + ); + if (!pkgPath) { + throw new Error("No @biomejs/biome dependency configured"); + } + return pnpApi.resolveRequest( + `@biomejs/cli-${process.platform}-${process.arch}/biome${ + process.platform === "win32" ? ".exe" : "" + }`, + pkgPath, + ); + } catch (err) { + outputChannel.appendLine( + `Could not resolve Biome using Yarn PnP in ${workspaceFolder.uri.fsPath}: ${err}`, + ); + } + } + // To resolve the @biomejs/cli-*, which is a transitive dependency of the // @biomejs/biome package, we need to create a custom require function that // is scoped to @biomejs/biome. This allows us to reliably resolve the