Skip to content

Commit 7cc7197

Browse files
authored
feat: don't crash LSP when there are errors resolving the workspace (#6257)
# Description ## Problem Before this PR, LSP would crash for assumed workspaces (when we can't find a Nargo.toml) or when there were errors in the Nargo.toml. ## Summary Now LSP doesn't crash in the above cases: - for assumed workspaces we don't crash (we used to not crash but I made some changes in the past that broke this) - for errors in Nargo.toml we now output to STDERR, so you can see the error in the output. This is what Rust Analyzer does, except that they also have a "rust-analyzer" thing at the bottom that becomes yellow when there's an error... I didn't implement this (it probably requires some code on the extension side) but I think if something doesn't work you'd check the logs or ask, and it's worse if it crashes. ## Additional Context ## Documentation Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** 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 e13f617 commit 7cc7197

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

tooling/lsp/src/lib.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,16 @@ fn byte_span_to_range<'a, F: files::Files<'a> + ?Sized>(
267267

268268
pub(crate) fn resolve_workspace_for_source_path(file_path: &Path) -> Result<Workspace, LspError> {
269269
if let Some(toml_path) = find_file_manifest(file_path) {
270-
return resolve_workspace_from_toml(
270+
match resolve_workspace_from_toml(
271271
&toml_path,
272272
PackageSelection::All,
273273
Some(NOIR_ARTIFACT_VERSION_STRING.to_string()),
274-
)
275-
.map_err(|err| LspError::WorkspaceResolutionError(err.to_string()));
274+
) {
275+
Ok(workspace) => return Ok(workspace),
276+
Err(error) => {
277+
eprintln!("Error while processing {:?}: {}", toml_path, error);
278+
}
279+
}
276280
}
277281

278282
let Some(parent_folder) = file_path
@@ -285,14 +289,22 @@ pub(crate) fn resolve_workspace_for_source_path(file_path: &Path) -> Result<Work
285289
file_path
286290
)));
287291
};
292+
293+
let crate_name = match CrateName::from_str(parent_folder) {
294+
Ok(name) => name,
295+
Err(error) => {
296+
eprintln!("{}", error);
297+
CrateName::from_str("root").unwrap()
298+
}
299+
};
300+
288301
let assumed_package = Package {
289302
version: None,
290303
compiler_required_version: Some(NOIR_ARTIFACT_VERSION_STRING.to_string()),
291304
root_dir: PathBuf::from(parent_folder),
292305
package_type: PackageType::Binary,
293306
entry_path: PathBuf::from(file_path),
294-
name: CrateName::from_str(parent_folder)
295-
.map_err(|err| LspError::WorkspaceResolutionError(err.to_string()))?,
307+
name: crate_name,
296308
dependencies: BTreeMap::new(),
297309
expression_width: None,
298310
};
@@ -309,7 +321,11 @@ pub(crate) fn workspace_package_for_file<'a>(
309321
workspace: &'a Workspace,
310322
file_path: &Path,
311323
) -> Option<&'a Package> {
312-
workspace.members.iter().find(|package| file_path.starts_with(&package.root_dir))
324+
if workspace.is_assumed {
325+
workspace.members.first()
326+
} else {
327+
workspace.members.iter().find(|package| file_path.starts_with(&package.root_dir))
328+
}
313329
}
314330

315331
pub(crate) fn prepare_package<'file_manager, 'parsed_files>(

0 commit comments

Comments
 (0)