Skip to content

Commit 76a4fbe

Browse files
grievejiafacebook-github-bot
authored andcommitted
Perform build system lookup when processing types() request
Reviewed By: dkgi Differential Revision: D30146032 fbshipit-source-id: 97bf97c6034edc01a14120a7a58a18968d727aea
1 parent 4e33e83 commit 76a4fbe

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

source/new_server/lookupProcessor.ml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type lookup = {
2424
lookup: (Lookup.t, error_reason) Result.t;
2525
}
2626

27-
let get_lookups ~configuration ~build_system:_ ~environment paths =
27+
let get_lookups ~configuration ~build_system ~environment paths =
2828
let generate_lookup_for_existent_path (path, { SourcePath.qualifier; _ }) =
2929
let lookup = Lookup.create_of_module (TypeEnvironment.read_only environment) qualifier in
3030
{ path; lookup = Result.Ok lookup }
@@ -37,13 +37,19 @@ let get_lookups ~configuration ~build_system:_ ~environment paths =
3737
let { Configuration.Analysis.local_root = root; _ } = configuration in
3838
Path.create_relative ~root ~relative:path
3939
in
40-
let module_tracker = TypeEnvironment.module_tracker environment in
41-
match ModuleTracker.lookup_path ~configuration module_tracker full_path with
42-
| ModuleTracker.PathLookup.Found source_path ->
43-
generate_lookup_for_existent_path (path, source_path)
44-
| ModuleTracker.PathLookup.ShadowedBy _ ->
45-
generate_lookup_for_nonexistent_path (path, StubShadowing)
46-
| ModuleTracker.PathLookup.NotFound -> generate_lookup_for_nonexistent_path (path, FileNotFound)
40+
match BuildSystem.lookup_artifact build_system full_path with
41+
| [] -> generate_lookup_for_nonexistent_path (path, FileNotFound)
42+
| artifact_path :: _ -> (
43+
(* If a source path corresponds to multiple artifacts, randomly pick an artifact and compute
44+
results for it. *)
45+
let module_tracker = TypeEnvironment.module_tracker environment in
46+
match ModuleTracker.lookup_path ~configuration module_tracker artifact_path with
47+
| ModuleTracker.PathLookup.Found source_path ->
48+
generate_lookup_for_existent_path (path, source_path)
49+
| ModuleTracker.PathLookup.ShadowedBy _ ->
50+
generate_lookup_for_nonexistent_path (path, StubShadowing)
51+
| ModuleTracker.PathLookup.NotFound ->
52+
generate_lookup_for_nonexistent_path (path, FileNotFound))
4753
in
4854
List.map paths ~f:generate_lookup_for_path
4955

source/new_server/test/newServerTest.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ module ScratchProject = struct
7979
?(external_sources = [])
8080
?(include_typeshed_stubs = true)
8181
?(include_helper_builtins = true)
82+
?custom_source_root
8283
?watchman
8384
?build_system_initializer
8485
sources
@@ -90,7 +91,11 @@ module ScratchProject = struct
9091
in
9192
(* We assume that there's only one checked source directory that acts as the global root as
9293
well. *)
93-
let source_root = bracket_tmpdir context |> Path.create_absolute ~follow_symbolic_links:true in
94+
let source_root =
95+
match custom_source_root with
96+
| Some source_root -> source_root
97+
| None -> bracket_tmpdir context |> Path.create_absolute ~follow_symbolic_links:true
98+
in
9499
(* We assume that there's only one external source directory. *)
95100
let external_root =
96101
bracket_tmpdir context |> Path.create_absolute ~follow_symbolic_links:true

source/new_server/test/newServerTest.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module ScratchProject : sig
3737
?external_sources:(string * string) list ->
3838
?include_typeshed_stubs:bool ->
3939
?include_helper_builtins:bool ->
40+
?custom_source_root:Pyre.Path.t ->
4041
?watchman:Watchman.Raw.t ->
4142
?build_system_initializer:BuildSystem.Initializer.t ->
4243
(* A list of test sources specified in the form of (relative_path, content) *)

source/new_server/test/queryTest.ml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,42 @@ let test_handle_query_basic context =
897897
Lwt.return_unit
898898

899899

900+
let test_handle_types_query_with_build_system context =
901+
let custom_source_root =
902+
bracket_tmpdir context |> Path.create_absolute ~follow_symbolic_links:true
903+
in
904+
let build_system_initializer =
905+
let initialize () =
906+
let lookup_artifact _ =
907+
[Path.create_relative ~root:custom_source_root ~relative:"redirected.py"]
908+
in
909+
Lwt.return (BuildSystem.create_for_testing ~lookup_artifact ())
910+
in
911+
let load () = failwith "saved state loading is not supported" in
912+
BuildSystem.Initializer.create_for_testing ~initialize ~load ()
913+
in
914+
let test_types_query client =
915+
let open Lwt.Infix in
916+
Client.send_request client (Request.Query "types('original.py')")
917+
>>= fun actual_response ->
918+
let expected_response =
919+
Response.Query
920+
Query.Response.(Single (Base.TypesByPath [{ Base.path = "original.py"; types = [] }]))
921+
|> Response.to_yojson
922+
|> Yojson.Safe.to_string
923+
in
924+
assert_equal ~ctxt:context ~cmp:String.equal ~printer:Fn.id expected_response actual_response;
925+
Lwt.return_unit
926+
in
927+
ScratchProject.setup
928+
~context
929+
~include_helper_builtins:false
930+
~build_system_initializer
931+
~custom_source_root
932+
["original.py", "x: int = 42"; "redirected.py", ""]
933+
|> ScratchProject.test_server_with ~f:test_types_query
934+
935+
900936
let test_handle_query_pysa context =
901937
let queries_and_expected_responses =
902938
[
@@ -1272,6 +1308,8 @@ let () =
12721308
>::: [
12731309
"parse_query" >:: test_parse_query;
12741310
"handle_query_basic" >:: OUnitLwt.lwt_wrapper test_handle_query_basic;
1311+
"handle_types_query_with_build_system"
1312+
>:: OUnitLwt.lwt_wrapper test_handle_types_query_with_build_system;
12751313
"handle_query_pysa" >:: OUnitLwt.lwt_wrapper test_handle_query_pysa;
12761314
"inline_decorators" >:: OUnitLwt.lwt_wrapper test_inline_decorators;
12771315
]

0 commit comments

Comments
 (0)