Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: biome installed with pnpm fails to start #34

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
target
extension.wasm
node_modules
package-lock.json
pnpm-lock.yaml
package.json
23 changes: 22 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
# Changelog
# Changelog

## 0.1.1 (2024-07-25)

### Fixes

- biome installed with pnpm fails to start

## 0.1.0 (2024-07-24)

### Features

- use native biome binary without node (#31)
- add setting require_config_file (#29)
- add support to configure --config-path through settings (#23)

## 0.0.7 (2024-06-06)

### Features

- use lsp settings to configure custom biome binary (#19)
- add support for the CSS language (#17)

## 0.0.6 (2024-04-22)

Expand Down
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ edition = "2021"
license = "MIT"
name = "zed_biome"
publish = false
version = "0.0.6"
version = "0.1.1"

[lib]
crate-type = ["cdylib"]
Expand Down
2 changes: 1 addition & 1 deletion extension.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ id = "biome"
name = "Biome"
repository = "https://github.com/biomejs/biome-zed"
schema_version = 1
version = "0.1.0"
version = "0.1.1"

[language_servers.biome]
code_actions_kind = ["", "quickfix"]
Expand Down
81 changes: 49 additions & 32 deletions src/biome.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
use std::{env, fs, path::Path};
use std::{
env, fs,
path::{Path, PathBuf},
};
use zed::settings::LspSettings;
use zed_extension_api::{
self as zed,
serde_json::{self, Value},
LanguageServerId, Result,
};

const SERVER_PATH: &str = "node_modules/@biomejs";
const SERVER_PATH: &str = "node_modules/@biomejs/biome/bin/biome";
const PACKAGE_NAME: &str = "@biomejs/biome";

const BIOME_CONFIG_PATHS: &[&str] = &["biome.json", "biome.jsonc"];

struct BiomeExtension;

impl BiomeExtension {
fn server_exists(&self, path: &str) -> bool {
fn server_exists(&self, path: &PathBuf) -> bool {
fs::metadata(path).map_or(false, |stat| stat.is_file())
}

fn binary_specifier(&self) -> Result<String> {
let (platform, arch) = zed::current_platform();

Ok(format!(
"@biomejs/cli-{platform}-{arch}/biome",
platform = match platform {
zed::Os::Mac => "darwin",
zed::Os::Linux => "linux",
zed::Os::Windows => "win32",
},
arch = match arch {
zed::Architecture::Aarch64 => "arm64",
zed::Architecture::X8664 => "x64",
_ => return Err(format!("unsupported architecture: {arch:?}")),
},
))
}

fn resolve_binary(&self, root_path: &str) -> Result<PathBuf> {
let specifier = self.binary_specifier()?;
let local_binary_path = Path::new(root_path).join("node_modules").join(specifier);

if local_binary_path.exists() {
Ok(local_binary_path)
} else {
Err(format!("Error: biome binary not found"))
}
}

fn server_script_path(
&mut self,
language_server_id: &LanguageServerId,
Expand All @@ -31,45 +63,30 @@ impl BiomeExtension {
let package_json: Option<serde_json::Value> = serde_json::from_str(package_json.as_str()).ok();

let server_package_exists = package_json.is_some_and(|f| {
!f["dependencies"]["@biomejs/biome"].is_null()
|| !f["devDependencies"]["@biomejs/biome"].is_null()
!f["dependencies"][PACKAGE_NAME].is_null() || !f["devDependencies"][PACKAGE_NAME].is_null()
});

let (platform, arch) = zed::current_platform();
let binary_path = format!(
"{SERVER_PATH}/cli-{platform}-{arch}/biome",
platform = match platform {
zed::Os::Mac => "darwin",
zed::Os::Linux => "linux",
zed::Os::Windows => "win32",
},
arch = match arch {
zed::Architecture::Aarch64 => "arm64",
zed::Architecture::X8664 => "x64",
_ => return Err(format!("unsupported architecture: {arch:?}")),
},
);

if server_package_exists {
let worktree_root_path = worktree.root_path();
let worktree_server_path = Path::new(worktree_root_path.as_str())
.join(binary_path)
.to_string_lossy()
.to_string();

return Ok(worktree_server_path);
return Ok(
Path::new(worktree_root_path.as_str())
.join(SERVER_PATH)
.to_string_lossy()
.to_string(),
);
}

// fallback to extension owned biome
zed::set_language_server_installation_status(
language_server_id,
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
);

let fallback_server_path = binary_path.to_string();
let fallback_server_exist = self.server_exists(fallback_server_path.as_str());
let fallback_server_path = &self.resolve_binary("./")?;
let version = zed::npm_package_latest_version(PACKAGE_NAME)?;

if !fallback_server_exist
if !self.server_exists(fallback_server_path)
|| zed::npm_package_installed_version(PACKAGE_NAME)?.as_ref() != Some(&version)
{
zed::set_language_server_installation_status(
Expand All @@ -79,21 +96,21 @@ impl BiomeExtension {
let result = zed::npm_install_package(PACKAGE_NAME, &version);
match result {
Ok(()) => {
if !self.server_exists(fallback_server_path.as_str()) {
if !self.server_exists(fallback_server_path) {
Err(format!(
"installed package '{PACKAGE_NAME}' did not contain expected path '{fallback_server_path}'",
"installed package '{PACKAGE_NAME}' did not contain expected path '{fallback_server_path:?}'",
))?;
}
}
Err(error) => {
if !self.server_exists(fallback_server_path.as_str()) {
if !self.server_exists(fallback_server_path) {
Err(error)?;
}
}
}
}

Ok(fallback_server_path.to_string())
Ok(fallback_server_path.to_string_lossy().to_string())
}

// Returns the path if a config file exists
Expand Down
6 changes: 6 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function test() {
luckydye marked this conversation as resolved.
Show resolved Hide resolved
return "Hello, world!";
}

let output = test();
// This let declares a variable that is only assigned once.
Loading