Skip to content

Commit

Permalink
Report errors if location provider fails (#4588)
Browse files Browse the repository at this point in the history
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: #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
  • Loading branch information
captbaritone authored and facebook-github-bot committed Jan 31, 2024
1 parent 8fa540a commit bbddbf3
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions compiler/crates/relay-lsp/src/goto_definition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_
Expand Down Expand Up @@ -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<lsp_types::Location, LSPRuntimeError> {
Expand Down

0 comments on commit bbddbf3

Please sign in to comment.