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

feat(lsp): use home directory as working directory #3649

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 17 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
- `--log-path`: a directory where to store the daemon logs. The commands also accepts the environment variable `BIOME_LOG_PATH`.
- `--log-prefix-name`: a prefix that's added to the file name of the logs. It defaults to `server.log`. The commands also accepts the environment variable `BIOME_LOG_PREFIX_NAME`.

@Contributed by @ematipico
Contributed by @ematipico


#### Enhancements

Expand Down Expand Up @@ -106,6 +106,21 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

- Fix [#3577](https://github.com/biomejs/biome/issues/3577), where the update of the configuration file was resulting in the creation of a new internal project. Contributed by @ematipico

#### Enhancements

- When an LSP is not able to provide a [`rootUri` or a `workspaceFolders`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialize), the Biome LSP will use the **user home directory** as working directory.
The user home directory differs based on the OS:

| Platform | Value | Example |
|----------|------------------------|------------------|
| Linux | `$HOME` | `/home/alice` |
| macOS | `$HOME` | `/home/alice` |
| Windows | `{FOLDERID_Profile}` | `C:\Users\Alice` |

If the LSP can't retrieve the user directory, it will fall back to the root directory.

Contributed by @ematipico

### Formatter

#### Enhancements
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ bpaf = { version = "0.9.12", features = ["derive"] }
countme = "3.0.1"
crossbeam = "0.8.4"
dashmap = "5.5.3"
directories = "5.0.1"
enumflags2 = "0.7.10"
getrandom = "0.2.15"
ignore = "0.4.22"
Expand Down
16 changes: 12 additions & 4 deletions crates/biome_cli/tests/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,9 @@ fn fs_error_dereferenced_symlink() {
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem::new(root_path.clone()))),
DynRef::Owned(Box::new(
OsFileSystem::new().with_working_directory(root_path.clone()),
)),
&mut console,
Args::from([("check"), root_path.display().to_string().as_str()].as_slice()),
);
Expand Down Expand Up @@ -884,7 +886,9 @@ fn fs_error_infinite_symlink_expansion_to_dirs() {
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem::new(root_path.clone()))),
DynRef::Owned(Box::new(
OsFileSystem::new().with_working_directory(root_path.clone()),
)),
&mut console,
Args::from([("check"), (root_path.display().to_string().as_str())].as_slice()),
);
Expand Down Expand Up @@ -930,7 +934,9 @@ fn fs_error_infinite_symlink_expansion_to_files() {
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem::new(root_path.clone()))),
DynRef::Owned(Box::new(
OsFileSystem::new().with_working_directory(root_path.clone()),
)),
&mut console,
Args::from([("check"), (root_path.display().to_string().as_str())].as_slice()),
);
Expand Down Expand Up @@ -1110,7 +1116,9 @@ fn fs_files_ignore_symlink() {
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem::new(root_path.clone()))),
DynRef::Owned(Box::new(
OsFileSystem::new().with_working_directory(root_path.clone()),
)),
&mut console,
Args::from(
[
Expand Down
28 changes: 21 additions & 7 deletions crates/biome_cli/tests/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,9 @@ fn fs_error_dereferenced_symlink() {
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem::new(root_path.clone()))),
DynRef::Owned(Box::new(
OsFileSystem::new().with_working_directory(root_path.clone()),
)),
&mut console,
Args::from([("lint"), root_path.display().to_string().as_str()].as_slice()),
);
Expand Down Expand Up @@ -910,7 +912,9 @@ fn fs_error_infinite_symlink_expansion_to_dirs() {
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem::new(root_path.clone()))),
DynRef::Owned(Box::new(
OsFileSystem::new().with_working_directory(root_path.clone()),
)),
&mut console,
Args::from([("lint"), (root_path.display().to_string().as_str())].as_slice()),
);
Expand Down Expand Up @@ -956,7 +960,9 @@ fn fs_error_infinite_symlink_expansion_to_files() {
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem::new(root_path.clone()))),
DynRef::Owned(Box::new(
OsFileSystem::new().with_working_directory(root_path.clone()),
)),
&mut console,
Args::from([("lint"), (root_path.display().to_string().as_str())].as_slice()),
);
Expand Down Expand Up @@ -1137,7 +1143,9 @@ fn fs_files_ignore_symlink() {
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem::new(root_path.clone()))),
DynRef::Owned(Box::new(
OsFileSystem::new().with_working_directory(root_path.clone()),
)),
&mut console,
Args::from(
[
Expand Down Expand Up @@ -1189,7 +1197,9 @@ fn include_files_in_subdir() {
.unwrap();

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem::new(root_path.clone()))),
DynRef::Owned(Box::new(
OsFileSystem::new().with_working_directory(root_path.clone()),
)),
&mut console,
Args::from([("lint"), root_path.display().to_string().as_str()].as_slice()),
);
Expand Down Expand Up @@ -1247,7 +1257,9 @@ fn include_files_in_symlinked_subdir() {
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem::new(subroot_path.clone()))),
DynRef::Owned(Box::new(
OsFileSystem::new().with_working_directory(subroot_path.clone()),
)),
&mut console,
Args::from([("lint"), subroot_path.display().to_string().as_str()].as_slice()),
);
Expand Down Expand Up @@ -1307,7 +1319,9 @@ fn ignore_file_in_subdir_in_symlinked_dir() {
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem::new(subroot_path.clone()))),
DynRef::Owned(Box::new(
OsFileSystem::new().with_working_directory(subroot_path.clone()),
)),
&mut console,
Args::from([("lint"), subroot_path.display().to_string().as_str()].as_slice()),
);
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ version = "0.5.7"
[dependencies]
biome_diagnostics = { workspace = true }
crossbeam = { workspace = true }
directories = "5.0.1"
directories = { workspace = true }
indexmap = { workspace = true }
oxc_resolver = { workspace = true }
parking_lot = { version = "0.12.3", features = ["arc_lock"] }
Expand Down
9 changes: 7 additions & 2 deletions crates/biome_fs/src/fs/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ pub struct OsFileSystem {
}

impl OsFileSystem {
pub fn new(working_directory: PathBuf) -> Self {
pub fn new() -> Self {
Self {
working_directory: Some(working_directory),
working_directory: None,
configuration_resolver: AssertUnwindSafe(Resolver::new(ResolveOptions {
condition_names: vec!["node".to_string(), "import".to_string()],
extensions: vec![".json".to_string(), ".jsonc".to_string()],
..ResolveOptions::default()
})),
}
}

pub fn with_working_directory(mut self, working_directory: impl Into<Option<PathBuf>>) -> Self {
self.working_directory = working_directory.into();
self
}
}

impl Default for OsFileSystem {
Expand Down
1 change: 1 addition & 0 deletions crates/biome_lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ biome_fs = { workspace = true }
biome_rowan = { workspace = true }
biome_service = { workspace = true }
biome_text_edit = { workspace = true }
directories = { workspace = true }
futures = "0.3.30"
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand Down
8 changes: 7 additions & 1 deletion crates/biome_lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ use biome_service::workspace::{
RageEntry, RageParams, RageResult, RegisterProjectFolderParams, UnregisterProjectFolderParams,
};
use biome_service::{workspace, DynRef, Workspace};
use directories::UserDirs;
use futures::future::ready;
use futures::FutureExt;
use rustc_hash::FxHashMap;
use serde_json::json;
use std::env;
use std::panic::RefUnwindSafe;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
Expand Down Expand Up @@ -560,7 +562,11 @@ impl ServerFactory {
}

pub fn create(&self, config_path: Option<PathBuf>) -> ServerConnection {
self.create_with_fs(config_path, DynRef::Owned(Box::<OsFileSystem>::default()))
let base_path = UserDirs::new()
.map(|dir| dir.home_dir().to_path_buf())
.or(env::current_dir().ok());
let fs = OsFileSystem::new().with_working_directory(base_path);
self.create_with_fs(config_path, DynRef::Owned(Box::new(fs)))
}

/// Create a new [ServerConnection] from this factory
Expand Down
Loading