From bbddbf3b97f374db099632c34b32e7abdee0f39a Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Wed, 31 Jan 2024 10:27:03 -0800 Subject: [PATCH] Report errors if location provider fails (#4588) Summary: Setting up the location provider in the most recent version of the VSCode extension is nearly impossible because if you don't do it perfectly (define the script using the right relative path, know which CWD the script will run in, use the right argument, parse it correctly, return the location in the correct format...) it simply fails silently with no hint as to what went worng. Adding this logging will at least enable folks to debug what's happening in the Relay LSP Output panel, if they set the log level correctly. The other option would be to simply return an error if there is a location provider provided and it errors trying to derive location info. I'm not sure how fallible we except these to be (I happen to know our Hack version has known issues where certain fields/types can't really get resolved). Pull Request resolved: https://github.com/facebook/relay/pull/4588 Test Plan: https://github.com/facebook/relay/assets/162735/c2e2c806-e6f1-4141-a450-a34774d766e6 Reviewed By: tyao1 Differential Revision: D52973219 Pulled By: captbaritone fbshipit-source-id: ddb40954b1ffefc20a058d0f2bcb96447f1f4ecd --- .../relay-lsp/src/goto_definition/mod.rs | 56 +++++++++++++------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/compiler/crates/relay-lsp/src/goto_definition/mod.rs b/compiler/crates/relay-lsp/src/goto_definition/mod.rs index 12b08395d6ff9..6e1d1610cc8c2 100644 --- a/compiler/crates/relay-lsp/src/goto_definition/mod.rs +++ b/compiler/crates/relay-lsp/src/goto_definition/mod.rs @@ -15,6 +15,8 @@ use std::sync::Arc; use graphql_ir::FragmentDefinitionName; use intern::string_key::Intern; use intern::string_key::StringKey; +use log::error; +use log::info; use lsp_types::request::GotoDefinition; use lsp_types::request::Request; use lsp_types::GotoDefinitionResponse; @@ -151,7 +153,11 @@ fn locate_type_definition( }), // If we couldn't resolve through the extra data provider, we'll fallback to // try to find a location in the server sdl. - Err(_) => { + Err(err) => { + error!( + "Failed to resolve type definition through extra data provider. Falling back to schema file. Got error: {:?}", + err + ); let type_ = schema.get_type(type_name); type_ @@ -194,25 +200,39 @@ fn locate_field_definition( is_extension: field.is_extension, }), ); - Ok(if let Ok(Some(source_info)) = provider_response { - // Step 1: does extra_data_provider know anything about this field? - if source_info.is_local { - GotoDefinitionResponse::Scalar(get_location( - &source_info.file_path, - source_info.line_number, - )?) - } else { - return Err(LSPRuntimeError::ExpectedError); + + match provider_response { + Ok(Some(source_info)) => { + // Step 1: does extra_data_provider know anything about this field? + if source_info.is_local { + return Ok(GotoDefinitionResponse::Scalar(get_location( + &source_info.file_path, + source_info.line_number, + )?)); + } else { + error!( + "Expected local source info from extra data provider, but got non-local. Falling back to schema file.", + ); + } + } + Ok(None) => { + info!( + "Extra data provider did not have any information about this field. Falling back to schema file." + ); } - } else if let Ok(location) = - transform_relay_location_to_lsp_location(root_dir, field.name.location) - { // Step 2: is field a standalone graphql file? - GotoDefinitionResponse::Scalar(location) - } else { - // Give up - return Err(LSPRuntimeError::ExpectedError); - }) + Err(err) => { + error!( + "Failed to resolve field definition through extra data provider. Falling back to schema file. Got error: {:?}", + err + ); + } + } + + transform_relay_location_to_lsp_location(root_dir, field.name.location) + .map(GotoDefinitionResponse::Scalar) + // If the field does not exist in the schema, that's fine + .map_err(|_| LSPRuntimeError::ExpectedError) } fn get_location(path: &str, line: u64) -> Result {