Skip to content

Commit

Permalink
For in-memory tests, use the local root
Browse files Browse the repository at this point in the history
Summary:
Because ModulePath directly accesses the filesystem to follow symlinks
(this is tech debt which could be eliminated by a proposed ModuleTracker
refactor to use abstract filesystems, but is necessary today), we have
to bypass `ModulePath.create` when using in-memory source code for unit tests.

This is unfortunate but okay. But because we have to bypass `ModulePath.create`,
we used `ModulePath.create_for_testing` which requires hardcoding the priority
of a search root.

This is a problem if we want to change the order of the search roots, as Kyle
is experimenting with doing in order to support a source-code fallback for Buck
projects while Buck is still running.

The fix is to avoid hardcoding a priority by using `Raw.create` to determine
the priority from the configuration. Because some other unit tests (unrelated
to ScratchProject and in-memory source code) do still require directly creating
a ModulePath we make a new function `create_for_in_memory_scratch_project`
to use specifically for this case.

Reviewed By: kinto0

Differential Revision: D50584161

fbshipit-source-id: 548dd602bcf7875aeaea9b17e5eee5b6b481cd6e
  • Loading branch information
stroxler authored and facebook-github-bot committed Oct 24, 2023
1 parent d6fa969 commit ac83af9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
17 changes: 15 additions & 2 deletions source/ast/modulePath.ml
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,27 @@ let relative { raw = { Raw.relative; _ }; _ } = relative

let is_in_project { is_external; _ } = not is_external

let create_for_testing ~relative ~is_external ~priority =
let raw = Raw.{ relative; priority } in
let create_from_raw_for_testing ~is_external ({ Raw.relative; _ } as raw) =
let qualifier = qualifier_from_relative_path relative in
let is_stub = PyrePath.is_path_python_stub relative in
let is_init = PyrePath.is_path_python_init relative in
{ raw; qualifier; is_stub; is_external; is_init }


let create_for_testing ~relative ~is_external ~priority =
let raw = Raw.{ relative; priority } in
create_from_raw_for_testing ~is_external raw


let create_for_in_memory_scratch_project ~configuration ~relative ~is_external =
let raw =
let { Configuration.Analysis.local_root; _ } = configuration in
let path_in_local_root = PyrePath.create_relative ~root:local_root ~relative in
Raw.create ~configuration (ArtifactPath.create path_in_local_root) |> Option.value_exn
in
create_from_raw_for_testing ~is_external raw


let full_path ~configuration { raw; _ } = Raw.full_path ~configuration raw

(* NOTE: This comparator is expected to operate on SourceFiles that are mapped to the same module
Expand Down
6 changes: 6 additions & 0 deletions source/ast/modulePath.mli
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ val relative : t -> string

val is_in_project : t -> bool

val create_for_in_memory_scratch_project
: configuration:Configuration.Analysis.t ->
relative:string ->
is_external:bool ->
t

val create_for_testing : relative:string -> is_external:bool -> priority:int -> t

val qualifier_from_relative_path : string -> Reference.t
Expand Down
4 changes: 3 additions & 1 deletion source/test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3065,7 +3065,9 @@ module ScratchProject = struct
let in_memory_sources =
let to_in_memory_source (relative, content) ~is_external =
let code = trim_extra_indentation content in
let module_path = ModulePath.create_for_testing ~relative ~is_external ~priority:1 in
let module_path =
ModulePath.create_for_in_memory_scratch_project ~configuration ~relative ~is_external
in
module_path, code
in
List.map sources ~f:(to_in_memory_source ~is_external:false)
Expand Down

0 comments on commit ac83af9

Please sign in to comment.