Skip to content

Commit

Permalink
Prevent users from shadowing builtins.pyi in ptt
Browse files Browse the repository at this point in the history
Summary:
When using buck-based per-target type checking, we realised that
it is possible for users, by setting `base_module = ''` and having
an `__init__.py`, to shadow `builtins.py`.

This is pretty confusing because there's no user-visible indication
that this is happening, and we just get tons of type errors on all
the builtin types being undefined.

Reviewed By: grievejia

Differential Revision: D65076559

fbshipit-source-id: d1ea05c3d0b5c6a8b2e114ba42fb7b81e2f1f1a8
  • Loading branch information
stroxler authored and facebook-github-bot committed Oct 29, 2024
1 parent 4800c02 commit f9b8277
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions source/buck_command/buckBasedSourceCodeApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,17 @@ let create
let source_index = build_index ~configuration:dummy_configuration source_module_paths in
let dependency_index = build_index ~configuration:dummy_configuration dependency_module_paths in
let merged_index =
(* Source files always take priority over dependency files *)
(* TODO: Revisit this decision once we get more results from production *)
let combine ~key:_ source_value _dependency_value = source_value in
(* Source files always take priority over dependency files, except in the special case of the
Reference.empty module name, in which case we make sure that it is not possible for an
`__init__.py` to shadow `builtins.pyi`. *)
let combine ~key:_ source_module_path dependency_module_path =
match
( String.equal (ModulePath.relative dependency_module_path) "builtins.pyi",
String.equal (ModulePath.relative source_module_path) "builtins.pyi" )
with
| true, false -> dependency_module_path
| _, _ -> source_module_path
in
Map.merge_skewed source_index dependency_index ~combine
in
let implicit_modules =
Expand Down
2 changes: 1 addition & 1 deletion source/buck_command/test/sourceCodeApiTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ let test_source_code_api =
in
create loader listing
in
assert_lookup_relative_path ~context ~api [!"", Some "__init__.py"];
assert_lookup_relative_path ~context ~api [!"", Some "builtins.pyi"];
());
]

Expand Down

0 comments on commit f9b8277

Please sign in to comment.