Skip to content

Commit 14f2fff

Browse files
authored
fix(lsp): package resolution on save (#3794)
# Description ## Problem\* ## Summary\* While saving file - procedure of compiling package should be applied to single package rather than all packages available in workspace. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
1 parent 9c9fbc1 commit 14f2fff

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

tooling/lsp/src/notifications/mod.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ops::ControlFlow;
22

33
use async_lsp::{ErrorCode, LanguageClient, ResponseError};
44
use nargo::prepare_package;
5-
use nargo_toml::{find_package_manifest, resolve_workspace_from_toml, PackageSelection};
5+
use nargo_toml::{find_file_manifest, resolve_workspace_from_toml, PackageSelection};
66
use noirc_driver::{check_crate, NOIR_ARTIFACT_VERSION_STRING};
77
use noirc_errors::{DiagnosticKind, FileDiagnostic};
88

@@ -69,29 +69,21 @@ pub(super) fn on_did_save_text_document(
6969
}
7070
};
7171

72-
let root_path = match &state.root_path {
73-
Some(root) => root,
74-
None => {
75-
return ControlFlow::Break(Err(ResponseError::new(
76-
ErrorCode::REQUEST_FAILED,
77-
"Could not find project root",
78-
)
79-
.into()));
80-
}
81-
};
72+
let package_root = find_file_manifest(file_path.as_path());
8273

83-
let toml_path = match find_package_manifest(root_path, &file_path) {
84-
Ok(toml_path) => toml_path,
85-
Err(err) => {
74+
let toml_path = match package_root {
75+
Some(toml_path) => toml_path,
76+
None => {
8677
// If we cannot find a manifest, we log a warning but return no diagnostics
8778
// We can reconsider this when we can build a file without the need for a Nargo.toml file to resolve deps
8879
let _ = state.client.log_message(LogMessageParams {
8980
typ: MessageType::WARNING,
90-
message: format!("{err}"),
81+
message: format!("Nargo.toml not found for file: {:}", file_path.display()),
9182
});
9283
return ControlFlow::Continue(());
9384
}
9485
};
86+
9587
let workspace = match resolve_workspace_from_toml(
9688
&toml_path,
9789
PackageSelection::All,

tooling/nargo_toml/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@ mod semver;
2424
pub use errors::ManifestError;
2525
use git::clone_git_repo;
2626

27+
/// Searches for a `Nargo.toml` file in the current directory and all parent directories.
28+
/// For example, if the current directory is `/workspace/package/src`, then this function
29+
/// will search for a `Nargo.toml` file in
30+
/// * `/workspace/package/src`,
31+
/// * `/workspace/package`,
32+
/// * `/workspace`.
33+
///
34+
/// Returns the [PathBuf] of the `Nargo.toml` file if found, otherwise returns None.
35+
///
36+
/// It will return innermost `Nargo.toml` file, which is the one closest to the current directory.
37+
/// For example, if the current directory is `/workspace/package/src`, then this function
38+
/// will return the `Nargo.toml` file in `/workspace/package/Nargo.toml`
39+
pub fn find_file_manifest(current_path: &Path) -> Option<PathBuf> {
40+
for path in current_path.ancestors() {
41+
if let Ok(toml_path) = get_package_manifest(path) {
42+
return Some(toml_path);
43+
}
44+
}
45+
None
46+
}
47+
2748
/// Returns the [PathBuf] of the directory containing the `Nargo.toml` by searching from `current_path` to the root of its [Path].
2849
///
2950
/// Returns a [ManifestError] if no parent directories of `current_path` contain a manifest file.

0 commit comments

Comments
 (0)