2020from bentoml ._internal .container import split_envs_by_stage
2121from bentoml ._internal .container .frontend .dockerfile import CONTAINER_METADATA
2222from bentoml ._internal .container .frontend .dockerfile import CONTAINER_SUPPORTED_DISTROS
23+ from bentoml ._internal .utils .filesystem import chdir
24+ from bentoml ._internal .utils .filesystem import resolve_user_filepath
2325from bentoml .exceptions import BentoMLConfigException
2426from bentoml .exceptions import BentoMLException
2527
@@ -97,6 +99,7 @@ def requirements_file(self, file_path: str) -> t.Self:
9799 """
98100 if self .post_commands :
99101 raise BentoMLConfigException ("Can't separate adding python requirements" )
102+ file_path = resolve_user_filepath (file_path , None )
100103 self .python_requirements += Path (file_path ).read_text ().rstrip ("\n " ) + "\n "
101104 self ._after_pip_install = True
102105 return self
@@ -112,6 +115,7 @@ def pyproject_toml(self, file_path: str = "pyproject.toml") -> t.Self:
112115 """
113116 if self .post_commands :
114117 raise BentoMLConfigException ("Can't separate adding python requirements" )
118+ file_path = resolve_user_filepath (file_path , None )
115119 with Path (file_path ).open ("rb" ) as f :
116120 pyproject_toml = tomllib .load (f )
117121 dependencies = pyproject_toml .get ("project" , {}).get ("dependencies" , {})
@@ -164,7 +168,7 @@ def run_script(self, script: str) -> t.Self:
164168 image = Image("debian:latest").run_script("script.sh")
165169 """
166170 commands = self .post_commands if self ._after_pip_install else self .commands
167- script = Path (script ). resolve (). as_posix ( )
171+ script = resolve_user_filepath (script , None )
168172 # Files under /env/docker will be copied into the env image layer
169173 target_script = (
170174 f"./env/docker/script__{ hashlib .md5 (script .encode ()).hexdigest ()} "
@@ -327,8 +331,6 @@ def _freeze_python_requirements(
327331def populate_image_from_build_config (
328332 image : Image | None , build_config : BentoBuildConfig , build_ctx : str
329333) -> Image | None :
330- from bentoml ._internal .utils .filesystem import resolve_user_filepath
331-
332334 fallback_message = "fallback to bento v1" if image is None else "it will be ignored"
333335 if not build_config .conda .is_empty ():
334336 logger .warning (
@@ -392,12 +394,12 @@ def populate_image_from_build_config(
392394 image .python_packages (
393395 * (f"--find-links { link } " for link in python_options .find_links )
394396 )
395- if python_options . requirements_txt :
396- image . requirements_file (
397- resolve_user_filepath ( python_options .requirements_txt , build_ctx )
398- )
399- elif python_options .packages :
400- image .python_packages (* python_options .packages )
401- if docker_options .setup_script :
402- image .run_script (docker_options .setup_script )
397+
398+ with chdir ( build_ctx ):
399+ if python_options .requirements_txt :
400+ image . requirements_file ( python_options . requirements_txt )
401+ elif python_options .packages :
402+ image .python_packages (* python_options .packages )
403+ if docker_options .setup_script :
404+ image .run_script (docker_options .setup_script )
403405 return image
0 commit comments