Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure relative path works within bento project #4803

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/_bentoml_impl/loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import importlib
import os
import pathlib
import sys
import typing as t
Expand Down Expand Up @@ -89,8 +90,8 @@ def normalize_identifier(
with open(yaml_path, "r") as f:
bento_yaml = yaml.safe_load(f)
assert "service" in bento_yaml, "service field is required in bento.yaml"
return normalize_package(bento_yaml["service"]), yaml_path.parent.joinpath(
"src"
return normalize_package(bento_yaml["service"]), pathlib.Path(
bento.path_of("src")
)
else:
raise ValueError(f"invalid service: {service_identifier}")
Expand Down Expand Up @@ -123,18 +124,23 @@ def import_service(
extra_python_path = None

# patch model store if needed
if (
bento_path.parent.joinpath(BENTO_YAML_FILENAME).exists()
and bento_path.parent.joinpath("models").exists()
):
if (bento_parent_dir := bento_path.parent).joinpath(
BENTO_YAML_FILENAME
).exists() and bento_parent_dir.joinpath("models").exists():
from bentoml._internal.models import ModelStore

original_path = os.getcwd()
original_model_store = BentoMLContainer.model_store.get()
# cwd into this for relative path to work
os.chdir(bento_parent_dir.absolute())
aarnphm marked this conversation as resolved.
Show resolved Hide resolved

# check in bento source

BentoMLContainer.model_store.set(
ModelStore((bento_path.parent.joinpath("models").absolute()))
ModelStore((bento_parent_dir.joinpath("models").absolute()))
)
else:
original_path = None
original_model_store = None

# load model aliases
Expand Down Expand Up @@ -179,6 +185,10 @@ def import_service(
from bentoml._internal.configuration.containers import BentoMLContainer

BentoMLContainer.model_store.set(original_model_store)

if original_path is not None:
os.chdir(original_path)

from bentoml.exceptions import ImportServiceError

raise ImportServiceError(
Expand Down
2 changes: 2 additions & 0 deletions src/bentoml/_internal/bento/bento.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ def create(
)
bento_fs.makedir(BENTO_PROJECT_DIR_NAME)
target_fs = bento_fs.opendir(BENTO_PROJECT_DIR_NAME)
with target_fs.open(DEFAULT_BENTO_BUILD_FILE, "w") as bentofile_yaml:
build_config.to_yaml(bentofile_yaml)
ignore_specs = list(specs.from_path(build_ctx))

for dir_path, _, files in ctx_fs.walk():
Expand Down
1 change: 1 addition & 0 deletions tests/unit/_internal/bento/test_bento.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ def test_bento(model_store: ModelStore):
assert set(bento_fs.listdir("src")) == {
"simplebento.py",
"subdir",
"bentofile.yaml",
".bentoignore",
}
assert set(bento_fs.listdir("src/subdir")) == {"somefile"}
Loading