Skip to content

Commit

Permalink
Fixed absolute path normalisation in source code analysis (#2920)
Browse files Browse the repository at this point in the history
## Changes
Workspace API does not support relative subpaths such as "/a/b/../c".
This PR fixes the issue by resolving workspace paths before calling the
API.

### Linked issues
Resolves #2882 
Requires #databrickslabs/blueprint#156
Requires #databrickslabs/blueprint#157

### Functionality
None

### Tests
- [x] added integration tests

---------

Co-authored-by: Eric Vergnaud <eric.vergnaud@databricks.com>
Co-authored-by: Serge Smertin <259697+nfx@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 10, 2024
1 parent 792d2ad commit 9ac48a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/databricks/labs/ucx/source_code/notebooks/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def resolve(self, path_lookup: PathLookup, path: Path) -> Path | None:
return None."""
# check current working directory first
absolute_path = path_lookup.cwd / path
absolute_path = absolute_path.resolve()
if is_a_notebook(absolute_path):
return absolute_path
# When exported through Git, notebooks are saved with a .py extension. So check with and without extension
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/source_code/test_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from databricks.labs.ucx.source_code.base import CurrentSessionState
from databricks.labs.ucx.source_code.graph import Dependency, DependencyGraph
from databricks.labs.ucx.source_code.linters.files import FileLoader
from databricks.labs.ucx.source_code.notebooks.loaders import NotebookLoader
from databricks.labs.ucx.source_code.notebooks.sources import Notebook


Expand All @@ -25,3 +26,25 @@ def test_malformed_pip_cell_is_supported(simple_ctx):
)
problems = notebook.build_dependency_graph(parent)
assert not problems


def test_relative_grand_parent_path_is_supported(
simple_ctx, make_notebook, make_directory, make_random, watchdog_purge_suffix
):
grand_parent = make_notebook()
top_dir = make_directory()
child_dir = make_directory(path=f"~/{top_dir.name}/dummy-{make_random(4)}-{watchdog_purge_suffix}")
source = f"""
%run ../../{grand_parent.name}
"""
notebook_path = make_notebook(
path=f"{child_dir.as_posix()}/dummy-{make_random(4)}-{watchdog_purge_suffix}", content=source.encode("utf-8")
)
dependency = Dependency(NotebookLoader(), notebook_path)
root = DependencyGraph(
dependency, None, simple_ctx.dependency_resolver, simple_ctx.path_lookup, CurrentSessionState()
)
container = dependency.load(simple_ctx.path_lookup)
problems = container.build_dependency_graph(root)
assert not problems

0 comments on commit 9ac48a5

Please sign in to comment.