From 23bab02151d43a3e9cbea3566452e15e64b9d9b7 Mon Sep 17 00:00:00 2001 From: Robert Lillack Date: Tue, 13 Feb 2024 12:38:51 +0100 Subject: [PATCH 1/5] Add support for dependency resolution using Yarn PnP --- src/main.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main.ts b/src/main.ts index ffac0d56..2636007f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -397,6 +397,36 @@ async function getWorkspaceDependency( outputChannel: OutputChannel, ): Promise { for (const workspaceFolder of workspace.workspaceFolders ?? []) { + // Check for Yarn PnP and try reolving the Biome binary without a node_modules + // folder first. + if (await fileExists(Uri.joinPath(workspaceFolder.uri, ".pnp.cjs"))) { + 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 From 8c4c00aaa38625c15ac4f696b42907edb1a069fe Mon Sep 17 00:00:00 2001 From: Robert Lillack Date: Tue, 13 Feb 2024 12:57:29 +0100 Subject: [PATCH 2/5] switch to using import() instead of require() --- src/main.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.ts b/src/main.ts index 2636007f..be1dab06 100644 --- a/src/main.ts +++ b/src/main.ts @@ -404,9 +404,9 @@ async function getWorkspaceDependency( `Looks like a Yarn PnP workspace: ${workspaceFolder.uri.fsPath}`, ); try { - const pnpApi = require( - Uri.joinPath(workspaceFolder.uri, ".pnp.cjs").fsPath, - ); + const pnpApi = ( + await import(Uri.joinPath(workspaceFolder.uri, ".pnp.cjs").fsPath) + ).default; const pkgPath = pnpApi.resolveRequest( "@biomejs/biome/package.json", workspaceFolder.uri.fsPath, From f3be9faff5c5602b8b557e2bffa88bb6dacc9ed0 Mon Sep 17 00:00:00 2001 From: Robert Lillack Date: Tue, 13 Feb 2024 13:01:40 +0100 Subject: [PATCH 3/5] Fix typo --- src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index be1dab06..c056727b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -397,7 +397,7 @@ async function getWorkspaceDependency( outputChannel: OutputChannel, ): Promise { for (const workspaceFolder of workspace.workspaceFolders ?? []) { - // Check for Yarn PnP and try reolving the Biome binary without a node_modules + // Check for Yarn PnP and try resolving the Biome binary without a node_modules // folder first. if (await fileExists(Uri.joinPath(workspaceFolder.uri, ".pnp.cjs"))) { outputChannel.appendLine( From 14374cdcd38c15d768915ad60febe473c8e29b01 Mon Sep 17 00:00:00 2001 From: Robert Lillack Date: Tue, 13 Feb 2024 13:39:06 +0100 Subject: [PATCH 4/5] Add support for Yarn v1 --- src/main.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main.ts b/src/main.ts index c056727b..333fb1e9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -399,22 +399,25 @@ async function getWorkspaceDependency( for (const workspaceFolder of workspace.workspaceFolders ?? []) { // Check for Yarn PnP and try resolving the Biome binary without a node_modules // folder first. - if (await fileExists(Uri.joinPath(workspaceFolder.uri, ".pnp.cjs"))) { + 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 = ( - await import(Uri.joinPath(workspaceFolder.uri, ".pnp.cjs").fsPath) - ).default; - const pkgPath = pnpApi.resolveRequest( + const { resolveRequest } = (await import(pnpFile.fsPath)).default; + const pkgPath = resolveRequest( "@biomejs/biome/package.json", workspaceFolder.uri.fsPath, ); if (!pkgPath) { throw new Error("No @biomejs/biome dependency configured"); } - return pnpApi.resolveRequest( + return resolveRequest( `@biomejs/cli-${process.platform}-${process.arch}/biome${ process.platform === "win32" ? ".exe" : "" }`, From 70787573786b70aab5ae1085843301b7247b134d Mon Sep 17 00:00:00 2001 From: Nicolas Hedger Date: Tue, 13 Feb 2024 19:26:28 +0100 Subject: [PATCH 5/5] revert "switch to using import() instead of require()" --- src/main.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.ts b/src/main.ts index 333fb1e9..6c696118 100644 --- a/src/main.ts +++ b/src/main.ts @@ -409,15 +409,17 @@ async function getWorkspaceDependency( `Looks like a Yarn PnP workspace: ${workspaceFolder.uri.fsPath}`, ); try { - const { resolveRequest } = (await import(pnpFile.fsPath)).default; - const pkgPath = resolveRequest( + 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 resolveRequest( + return pnpApi.resolveRequest( `@biomejs/cli-${process.platform}-${process.arch}/biome${ process.platform === "win32" ? ".exe" : "" }`,