diff --git a/backend/tests/fixtures/relative_python.sql b/backend/tests/fixtures/relative_python.sql index 7a70e06d20..40fbedfc34 100644 --- a/backend/tests/fixtures/relative_python.sql +++ b/backend/tests/fixtures/relative_python.sql @@ -38,3 +38,24 @@ def main(): '', '', 'f/system_relative/nested_script', 12348, 'python3', ''); + +-- Named after files in the job dir (loader.py) and after the running script's folder (f/system/). +INSERT INTO public.script(workspace_id, created_by, content, schema, summary, description, path, hash, language, lock) VALUES ( +'test-workspace', +'test-user', +'from .different_folder_script import main +', +'{"$schema":"https://json-schema.org/draft/2020-12/schema","properties":{},"required":[],"type":"object"}', +'', +'', +'f/system_relative/loader', 12349, 'python3', ''); + +INSERT INTO public.script(workspace_id, created_by, content, schema, summary, description, path, hash, language, lock) VALUES ( +'test-workspace', +'test-user', +'from .different_folder_script import main +', +'{"$schema":"https://json-schema.org/draft/2020-12/schema","properties":{},"required":[],"type":"object"}', +'', +'', +'f/system_relative/system', 12350, 'python3', ''); diff --git a/backend/tests/python_jobs.rs b/backend/tests/python_jobs.rs index 4236599aba..599f5c5f33 100644 --- a/backend/tests/python_jobs.rs +++ b/backend/tests/python_jobs.rs @@ -1141,9 +1141,9 @@ async fn test_relative_imports_python(db: Pool) -> anyhow::Result<()> let content = r#" from f.system.same_folder_script import main as test1 from .same_folder_script import main as test2 -from f.system_relative.different_folder_script import main as test3 -from ..system_relative.different_folder_script import main as test4 - +from f.system_relative.system import main as test3 +from ..system_relative.loader import main as test4 + def main(): return [test1(), test2(), test3(), test4()] "# diff --git a/backend/windmill-worker/loader.py b/backend/windmill-worker/loader.py index bf67266d87..fd9d2b1376 100644 --- a/backend/windmill-worker/loader.py +++ b/backend/windmill-worker/loader.py @@ -11,14 +11,14 @@ import time TEMP_SCRIPT_REFS = TEMP_SCRIPT_REFS_PLACEHOLDER class WindmillLoader(Loader): - def __init__(self, path): - self.path = path - def create_module(self, spec): return None def exec_module(self, module): - module.__path__ = self.path + # Must stay empty: PathFinder runs before WindmillFinder and would resolve + # submodules against any entry here (relative to the job dir, which holds + # loader.py, wrapper.py and the running script's f//). + module.__path__ = [] return None @@ -31,7 +31,7 @@ class WindmillFinder(MetaPathFinder): return None l = len(splitted) # noqa: E741 if l <= 2: - return ModuleSpec(name, WindmillLoader(name)) + return ModuleSpec(name, WindmillLoader()) elif l > 2: script_path = "/".join(splitted) @@ -68,7 +68,7 @@ class WindmillFinder(MetaPathFinder): os.makedirs(folder, exist_ok=True) r = response.read().decode("utf-8") if r == "WINDMILL_IS_FOLDER": - return ModuleSpec(name, WindmillLoader(name)) + return ModuleSpec(name, WindmillLoader()) # Python parses .py as UTF-8 regardless of locale, so the # file has to be written as UTF-8. Without this the ANSI # code page on a Windows worker re-encodes every @@ -80,7 +80,7 @@ class WindmillFinder(MetaPathFinder): duration = time.time() - req_start if e.code != 404: print(f"Error fetching script {script_path}: HTTP {e.code} - {e.reason} - {duration}s") - return ModuleSpec(name, WindmillLoader(name)) + return ModuleSpec(name, WindmillLoader()) except Exception as e: duration = time.time() - req_start # Check if this is errno 104 (Connection reset by peer) and we have retries left @@ -89,7 +89,7 @@ class WindmillFinder(MetaPathFinder): time.sleep(3) continue print(f"Error fetching script {script_path}: {e} - {duration}s") - return ModuleSpec(name, WindmillLoader(name)) + return ModuleSpec(name, WindmillLoader())