@@ -184,6 +184,49 @@ def test_skips_local_module_file(self, tmp_path: pytest.TempPathFactory) -> None
184184 assert "requests" in packages
185185 assert "helpers" not in packages
186186
187+ def test_skips_local_package_reachable_via_cwd (
188+ self , tmp_path : pytest .TempPathFactory , monkeypatch : pytest .MonkeyPatch ,
189+ ) -> None :
190+ """A sibling package importable from cwd is treated as local.
191+
192+ Mirrors ``pyfuse run examples/foo.py`` invoked from the project root,
193+ where ``foo.py`` imports a package living at that root.
194+ """
195+ sibling_pkg = tmp_path / "mypkg" # type: ignore[operator]
196+ sibling_pkg .mkdir ()
197+ (sibling_pkg / "__init__.py" ).write_text ("" )
198+
199+ scripts_dir = tmp_path / "scripts" # type: ignore[operator]
200+ scripts_dir .mkdir ()
201+ script = scripts_dir / "run.py"
202+ script .write_text ("from mypkg import thing\n import requests\n " )
203+
204+ monkeypatch .chdir (tmp_path )
205+ packages = _detect_script_packages (str (script ))
206+ assert "requests" in packages
207+ assert "mypkg" not in packages
208+
209+ def test_treats_unreachable_package_as_third_party (
210+ self , tmp_path : pytest .TempPathFactory , monkeypatch : pytest .MonkeyPatch ,
211+ ) -> None :
212+ """If a module isn't importable from cwd or the script dir, leave it alone:
213+ the script will fail at runtime with a clear ImportError, and the user is
214+ responsible for fixing their layout (we don't silently rewrite imports)."""
215+ # ``mypkg`` lives one level up from cwd and isn't on sys.path.
216+ sibling_pkg = tmp_path / "mypkg" # type: ignore[operator]
217+ sibling_pkg .mkdir ()
218+ (sibling_pkg / "__init__.py" ).write_text ("" )
219+
220+ scripts_dir = tmp_path / "scripts" # type: ignore[operator]
221+ scripts_dir .mkdir ()
222+ script = scripts_dir / "run.py"
223+ script .write_text ("from mypkg import thing\n " )
224+
225+ monkeypatch .chdir (scripts_dir )
226+ packages = _detect_script_packages (str (script ))
227+ # mypkg can't be imported from cwd or script_dir → treated as third-party.
228+ assert "mypkg" in packages
229+
187230 def test_install_package_as (self , tmp_path : pytest .TempPathFactory ) -> None :
188231 script = tmp_path / "test_ipa.py" # type: ignore[operator]
189232 script .write_text (
0 commit comments