From 4800c02959abf5dc9c9115ae2a7570a88d036c89 Mon Sep 17 00:00:00 2001 From: Steven Troxler Date: Tue, 29 Oct 2024 09:13:53 -0700 Subject: [PATCH] Add a test case showing we can shadow `builtins.pyi` Summary: We discovered recently that a user's buck target can accidentally shadow builtin stubs, because user modules always take priority and users can set `base_module = ''` with an `__init__.py`. In general this behavior of allowing user files to shadow stdlib isn't so bad - as a rule they won't, and it allows users to define a custom typeshed. But for `builtins.pyi` sepecifically this is especially confusing because the errors are extremely widespread and it's pretty hard to guess what is happening since the empty qualifier is special. So, to prevent a repeat of the debugging session we had we want to special-case that `builtins.pyi` cannot be shadowed. This commit adds a new test illustrating the issue, the next commit will cut Pyre over to preferring `builtins.py` Reviewed By: rchen152 Differential Revision: D65076555 fbshipit-source-id: c9b6138a2724b5eae4eec2836bd3707d7d517519 --- source/buck_command/test/sourceCodeApiTest.ml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/source/buck_command/test/sourceCodeApiTest.ml b/source/buck_command/test/sourceCodeApiTest.ml index 6d8a1f871e8..cf166e1226f 100644 --- a/source/buck_command/test/sourceCodeApiTest.ml +++ b/source/buck_command/test/sourceCodeApiTest.ml @@ -174,6 +174,20 @@ let test_source_code_api = assert_module_tracked ~context ~api !"a.d.e" ~expected:true; assert_module_tracked ~context ~api !"a.x" ~expected:false; ()); + (Test.labeled_test_case Stdlib.__FUNCTION__ Stdlib.__LINE__ ~name:"do not shadow builtins.pyi" + @@ fun context -> + let api = + let loader = FileLoader.create_for_testing ["__init__.py", ""; "builtins.pyi", ""] in + let listing = + Sourcedb.Listing.create_for_testing + ~sources:["__init__.py"] + ~dependencies:["builtins.pyi"] + () + in + create loader listing + in + assert_lookup_relative_path ~context ~api [!"", Some "__init__.py"]; + ()); ]