Skip to content

Commit d7da4ad

Browse files
fix: run biome from the project root when determining version (#720)
Co-authored-by: Nicolas Hedger <[email protected]>
1 parent 8fa2ca1 commit d7da4ad

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

.changeset/thick-icons-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"biome": patch
3+
---
4+
5+
run biome from the project root when determining version

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dist
22
node_modules
33
out
4-
.vsix
4+
.vsix
5+
.tool-versions

src/locator.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import {
1616
platformSpecificBinaryName,
1717
platformSpecificNodePackageName,
1818
} from "./constants";
19-
import { config, fileExists, getLspBin, safeSpawnSync } from "./utils";
19+
import {
20+
config,
21+
fileExists,
22+
getLspBin,
23+
type SafeSpawnSyncOptions,
24+
safeSpawnSync,
25+
} from "./utils";
2026

2127
export default class Locator {
2228
private get globalNodeModulesPaths(): Record<string, Uri | undefined> {
@@ -63,8 +69,19 @@ export default class Locator {
6369
this.biome.logger.debug(`🔍 Unshimming Biome binary at "${biome.fsPath}"`);
6470

6571
try {
72+
const spawnSyncOptions: SafeSpawnSyncOptions = {};
73+
74+
// Set the current working directory to the project root, if it exists. This runs the `biome` binary from the
75+
// project root in case the user's local development environment depends on this, such as when using `asdf`.
76+
if (this.biome.workspaceFolder?.uri)
77+
spawnSyncOptions.cwd = this.biome.workspaceFolder.uri.fsPath;
78+
6679
// Check the version of Biome
67-
const version = safeSpawnSync(biome.fsPath, ["--version"])
80+
const version = safeSpawnSync(
81+
biome.fsPath,
82+
["--version"],
83+
spawnSyncOptions,
84+
)
6885
?.split("Version: ")[1]
6986
?.trim();
7087

@@ -83,7 +100,11 @@ export default class Locator {
83100
}
84101

85102
// If the version is 2 or higher, we can safely unshim
86-
const realPath = safeSpawnSync(biome.fsPath, ["__where_am_i"]);
103+
const realPath = safeSpawnSync(
104+
biome.fsPath,
105+
["__where_am_i"],
106+
spawnSyncOptions,
107+
);
87108

88109
if (!realPath) {
89110
this.biome.logger.warn(
@@ -153,21 +174,21 @@ export default class Locator {
153174
*
154175
* This strategy is responsible for finding the Biome binary as specified
155176
* in the user's settings in the `biome.lsp.bin` configuration option.
156-
*
177+
*
157178
* This strategy supports platform-specific settings, meaning that the user can
158179
* specify different binaries for different combos of OS, architecture and libc.
159-
*
180+
*
160181
* If the `biome.lsp.bin` setting is specified as a string, the strategy will
161182
* attempt to locate the binary at the specified path. If the binary is not found
162183
* at the specified path, the strategy will return `undefined`.
163-
*
184+
*
164185
* If the `biome.lsp.bin` setting is specified as an object, the strategy will
165186
* attempt to locate the binary at the specified path for the current platform
166187
* (OS, architecture and libc). If the binary is not found at the specified path,
167188
* the strategy will return `undefined`. The keys of the object are the OS, architecture
168189
* and libc combos, concatenated with a dash (`-`), and the values are the paths to
169190
* the binaries.
170-
*
191+
*
171192
* Example:
172193
*
173194
* ```json

src/utils.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { spawnSync } from "node:child_process";
1+
import {
2+
type SpawnSyncOptionsWithStringEncoding,
3+
spawnSync,
4+
} from "node:child_process";
25
import { extname, isAbsolute } from "node:path";
36
import {
47
type ConfigurationScope,
@@ -117,9 +120,15 @@ export const debounce = <TArgs extends unknown[]>(
117120
};
118121
};
119122

123+
export type SafeSpawnSyncOptions = Omit<
124+
SpawnSyncOptionsWithStringEncoding,
125+
"encoding"
126+
>;
127+
120128
export const safeSpawnSync = (
121129
command: string,
122130
args: readonly string[] = [],
131+
options?: SafeSpawnSyncOptions,
123132
): string | undefined => {
124133
let output: string | undefined;
125134

@@ -130,7 +139,10 @@ export const safeSpawnSync = (
130139
}
131140

132141
try {
133-
const result = spawnSync(command, args, { encoding: "utf8" });
142+
const result = spawnSync(command, args, {
143+
...(options ?? {}),
144+
encoding: "utf8",
145+
});
134146

135147
if (result.error || result.status !== 0) {
136148
output = undefined;

0 commit comments

Comments
 (0)