Skip to content

Commit 9ac48a5

Browse files
ericvergnaudnfx
andauthored
Fixed absolute path normalisation in source code analysis (#2920)
## 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>
1 parent 792d2ad commit 9ac48a5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/databricks/labs/ucx/source_code/notebooks/loaders.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def resolve(self, path_lookup: PathLookup, path: Path) -> Path | None:
4141
return None."""
4242
# check current working directory first
4343
absolute_path = path_lookup.cwd / path
44+
absolute_path = absolute_path.resolve()
4445
if is_a_notebook(absolute_path):
4546
return absolute_path
4647
# When exported through Git, notebooks are saved with a .py extension. So check with and without extension

tests/integration/source_code/test_cells.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from databricks.labs.ucx.source_code.base import CurrentSessionState
66
from databricks.labs.ucx.source_code.graph import Dependency, DependencyGraph
77
from databricks.labs.ucx.source_code.linters.files import FileLoader
8+
from databricks.labs.ucx.source_code.notebooks.loaders import NotebookLoader
89
from databricks.labs.ucx.source_code.notebooks.sources import Notebook
910

1011

@@ -25,3 +26,25 @@ def test_malformed_pip_cell_is_supported(simple_ctx):
2526
)
2627
problems = notebook.build_dependency_graph(parent)
2728
assert not problems
29+
30+
31+
def test_relative_grand_parent_path_is_supported(
32+
simple_ctx, make_notebook, make_directory, make_random, watchdog_purge_suffix
33+
):
34+
grand_parent = make_notebook()
35+
top_dir = make_directory()
36+
child_dir = make_directory(path=f"~/{top_dir.name}/dummy-{make_random(4)}-{watchdog_purge_suffix}")
37+
source = f"""
38+
%run ../../{grand_parent.name}
39+
40+
"""
41+
notebook_path = make_notebook(
42+
path=f"{child_dir.as_posix()}/dummy-{make_random(4)}-{watchdog_purge_suffix}", content=source.encode("utf-8")
43+
)
44+
dependency = Dependency(NotebookLoader(), notebook_path)
45+
root = DependencyGraph(
46+
dependency, None, simple_ctx.dependency_resolver, simple_ctx.path_lookup, CurrentSessionState()
47+
)
48+
container = dependency.load(simple_ctx.path_lookup)
49+
problems = container.build_dependency_graph(root)
50+
assert not problems

0 commit comments

Comments
 (0)