From 34708ecde1cbf91897325b0a518f46767f357cc1 Mon Sep 17 00:00:00 2001 From: Kaushik Date: Thu, 29 Jan 2026 15:17:01 +0000 Subject: [PATCH 1/2] Fix strip_root not removing root directory from Resource paths Signed-off-by: Kaushik --- src/scancode/cli.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/scancode/cli.py b/src/scancode/cli.py index 1376c6cfee9..474a9289ebd 100644 --- a/src/scancode/cli.py +++ b/src/scancode/cli.py @@ -1070,6 +1070,16 @@ def echo_func(*_args, **_kwargs): results = get_results(codebase, as_list=True, **requested_options) elif return_codebase: results = codebase + # Strip root from paths when strip_root is enabled. + # See https://github.com/aboutcode-org/scancode-toolkit/issues/2985 + if strip_root and not codebase.has_single_resource: + from commoncode.resource import strip_first_path_segment + new_resources_by_path = {} + for old_path, resource in list(codebase.resources_by_path.items()): + stripped_path = strip_first_path_segment(old_path) + resource.path = stripped_path + new_resources_by_path[stripped_path] = resource + codebase.resources_by_path = new_resources_by_path finally: # remove temporary files From 956183ea6ec3ad239674e473d5b51562dd40bae8 Mon Sep 17 00:00:00 2001 From: Kaushik Date: Fri, 30 Jan 2026 09:21:51 +0000 Subject: [PATCH 2/2] Fix parent() method for strip_root to handle empty parent paths Signed-off-by: Kaushik --- src/scancode/cli.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/scancode/cli.py b/src/scancode/cli.py index 474a9289ebd..faa3375c5c5 100644 --- a/src/scancode/cli.py +++ b/src/scancode/cli.py @@ -1080,6 +1080,17 @@ def echo_func(*_args, **_kwargs): resource.path = stripped_path new_resources_by_path[stripped_path] = resource codebase.resources_by_path = new_resources_by_path + + # Fix parent() to handle empty parent_path for direct children of root. + # commoncode's parent() uses `return path and get_resource(path)` which + # returns '' (empty string) when path is '', breaking tree traversal. + original_parent = codebase.resource_class.parent + def patched_parent(self, codebase_arg): + parent_path = self.parent_path() + if parent_path == '': + return codebase_arg.root + return original_parent(self, codebase_arg) + codebase.resource_class.parent = patched_parent finally: # remove temporary files