mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-17 00:20:51 +00:00
fix(python): give folder modules an empty __path__ so relative imports resolve to workspace scripts (#12307)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
949ef234a8
commit
0323310d64
+21
@@ -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', '');
|
||||
|
||||
@@ -1141,9 +1141,9 @@ async fn test_relative_imports_python(db: Pool<Postgres>) -> 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()]
|
||||
"#
|
||||
|
||||
@@ -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/<folder>/).
|
||||
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())
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user