diff --git a/src/lightning_app/CHANGELOG.md b/src/lightning_app/CHANGELOG.md index b08c7edae7bf7..8a0ebf06bc97b 100644 --- a/src/lightning_app/CHANGELOG.md +++ b/src/lightning_app/CHANGELOG.md @@ -37,6 +37,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - The `params` argument in `TracerPythonScript.run` no longer prepends `--` automatically to parameters ([#15518](https://github.com/Lightning-AI/lightning/pull/15518)) +- Changed the root directory of the app (which gets uploaded) to be the folder containing the app file, rather than any parent folder containing a `.lightning` file ([#15654](https://github.com/Lightning-AI/lightning/pull/15654)) + ### Deprecated diff --git a/src/lightning_app/runners/cloud.py b/src/lightning_app/runners/cloud.py index c551c1c76ec57..074b864c367a2 100644 --- a/src/lightning_app/runners/cloud.py +++ b/src/lightning_app/runners/cloud.py @@ -63,7 +63,7 @@ from lightning_app.utilities.cloud import _get_project from lightning_app.utilities.dependency_caching import get_hash from lightning_app.utilities.load_app import _prettifiy_exception, load_app_from_file -from lightning_app.utilities.packaging.app_config import AppConfig, find_config_file +from lightning_app.utilities.packaging.app_config import _get_config_file, AppConfig from lightning_app.utilities.packaging.lightning_utils import _prepare_lightning_wheels_and_requirements from lightning_app.utilities.secrets import _names_to_ids @@ -95,9 +95,9 @@ def dispatch( # TODO: verify lightning version # _verify_lightning_version() - config_file = find_config_file(self.entrypoint_file) - app_config = AppConfig.load_from_file(config_file) if config_file else AppConfig() - root = config_file.parent if config_file else Path(self.entrypoint_file).absolute().parent + config_file = _get_config_file(self.entrypoint_file) + app_config = AppConfig.load_from_file(config_file) if config_file.exists() else AppConfig() + root = Path(self.entrypoint_file).absolute().parent cleanup_handle = _prepare_lightning_wheels_and_requirements(root) self.app._update_index_file() repo = LocalSourceCodeDir(path=root) diff --git a/src/lightning_app/utilities/packaging/app_config.py b/src/lightning_app/utilities/packaging/app_config.py index 59d05debc088c..c3e44159ffb4e 100644 --- a/src/lightning_app/utilities/packaging/app_config.py +++ b/src/lightning_app/utilities/packaging/app_config.py @@ -28,7 +28,7 @@ def save_to_file(self, path: Union[str, pathlib.Path]) -> None: def save_to_dir(self, directory: Union[str, pathlib.Path]) -> None: """Save the configuration to a file '.lightning' to the given folder in YAML format.""" - self.save_to_file(pathlib.Path(directory, _APP_CONFIG_FILENAME)) + self.save_to_file(_get_config_file(directory)) @classmethod def load_from_file(cls, path: Union[str, pathlib.Path]) -> "AppConfig": @@ -47,22 +47,14 @@ def load_from_dir(cls, directory: Union[str, pathlib.Path]) -> "AppConfig": return cls.load_from_file(pathlib.Path(directory, _APP_CONFIG_FILENAME)) -def find_config_file(source_path: pathlib.Path = pathlib.Path.cwd()) -> Optional[pathlib.Path]: - """Search for the Lightning app config file '.lightning' at the given source path. - - Relative to the given path, it will search for the '.lightning' config file by going up the directory structure - until found. Returns ``None`` if no config file is found in any of the parent directories. +def _get_config_file(source_path: Union[str, pathlib.Path]) -> pathlib.Path: + """Get the Lightning app config file '.lightning' at the given source path. Args: - source_path: A path to a folder or a file. The search for the config file will start relative to this path. + source_path: A path to a folder or a file. """ source_path = pathlib.Path(source_path).absolute() if source_path.is_file(): source_path = source_path.parent - candidate = pathlib.Path(source_path / _APP_CONFIG_FILENAME) - if candidate.is_file(): - return candidate - - if source_path.parents: - return find_config_file(source_path.parent) + return pathlib.Path(source_path / _APP_CONFIG_FILENAME) diff --git a/tests/tests_app/utilities/packaging/test_app_config.py b/tests/tests_app/utilities/packaging/test_app_config.py index 2666f0a769ace..60da494a47fb8 100644 --- a/tests/tests_app/utilities/packaging/test_app_config.py +++ b/tests/tests_app/utilities/packaging/test_app_config.py @@ -1,6 +1,6 @@ import pathlib -from lightning_app.utilities.packaging.app_config import AppConfig, find_config_file +from lightning_app.utilities.packaging.app_config import _get_config_file, AppConfig def _make_empty_config_file(folder): @@ -10,24 +10,12 @@ def _make_empty_config_file(folder): return file -def test_find_config_file(tmpdir, monkeypatch): - monkeypatch.chdir(pathlib.Path("/")) - assert find_config_file() is None - - monkeypatch.chdir(pathlib.Path.home()) - assert find_config_file() is None - +def test_get_config_file(tmpdir): _ = _make_empty_config_file(tmpdir) - config_file1 = _make_empty_config_file(tmpdir / "a" / "b") - - assert find_config_file(tmpdir) == pathlib.Path(tmpdir, ".lightning") - assert find_config_file(config_file1) == pathlib.Path(tmpdir, "a", "b", ".lightning") - assert find_config_file(pathlib.Path(tmpdir, "a")) == pathlib.Path(tmpdir, ".lightning") + config_file1 = _make_empty_config_file(tmpdir) - # the config must be a file, a folder of the same name gets ignored - fake_config_folder = pathlib.Path(tmpdir, "fake", ".lightning") - fake_config_folder.mkdir(parents=True) - assert find_config_file(tmpdir) == pathlib.Path(tmpdir, ".lightning") + assert _get_config_file(tmpdir) == pathlib.Path(tmpdir, ".lightning") + assert _get_config_file(config_file1) == pathlib.Path(tmpdir, ".lightning") def test_app_config_save_load(tmpdir):