Skip to content

Commit d473971

Browse files
committed
fix: Allow west commands to be imported from a project subdirectory if manifest is located in subdirectory
1 parent 548b9ee commit d473971

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

src/west/manifest.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,7 +2628,7 @@ def _import_path_from_project(self, project: Project, path: str) -> None:
26282628
return
26292629

26302630
for data in imported:
2631-
self._import_data_from_project(project, data, None)
2631+
self._import_data_from_project(project, data, None, path)
26322632

26332633
_logger.debug(f'done resolving import {path} for {project}')
26342634

@@ -2642,12 +2642,13 @@ def _import_map_from_project(self, project: Project, imp: dict) -> None:
26422642
return
26432643

26442644
for data in imported:
2645-
self._import_data_from_project(project, data, imap)
2645+
self._import_data_from_project(project, data, imap, imap.file)
26462646

26472647
_logger.debug(f'done resolving import {imap} for {project}')
26482648

26492649
def _import_data_from_project(
2650-
self, project: Project, data: Any, imap: _import_map | None
2650+
self, project: Project, data: Any,
2651+
imap: _import_map | None, manifest_path: str | None = None
26512652
) -> None:
26522653
# Destructively add the imported data into our 'projects' map.
26532654

@@ -2678,8 +2679,22 @@ def _import_data_from_project(
26782679

26792680
# Patch up any extension commands in the imported data
26802681
# by allocating them to the project.
2682+
2683+
# If the manifest was imported from a project subdirectory
2684+
# (manifest_path is a relative path within the project),
2685+
# we need to adjust the west_commands paths to be relative
2686+
# to the project root, not to the manifest subdirectory.
2687+
west_commands_to_merge = submanifest._ctx.manifest_west_commands
2688+
if manifest_path and manifest_path != _WEST_YML:
2689+
manifest_dir = os.path.dirname(manifest_path)
2690+
if manifest_dir:
2691+
west_commands_to_merge = [
2692+
os.path.join(manifest_dir, cmd).replace('\\', '/')
2693+
for cmd in west_commands_to_merge
2694+
]
2695+
26812696
project.west_commands = _west_commands_merge(
2682-
project.west_commands, submanifest._ctx.manifest_west_commands
2697+
project.west_commands, west_commands_to_merge
26832698
)
26842699

26852700
def _import_content_from_project(self, project: Project, path: str) -> ImportedContentType:

tests/test_manifest.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,48 @@ def test_import_project_submanifest_commands_both(manifest_repo):
20952095
assert p1.west_commands == expected
20962096

20972097

2098+
def test_import_project_submanifest_commands_from_project_subdirectory(manifest_repo):
2099+
# When a manifest is imported from a project subdirectory (e.g., app/west.yml),
2100+
# and that manifest defines west-commands, the paths should be
2101+
# resolved relative to the manifest subdirectory.
2102+
# This tests _import_path_from_project with a string path.
2103+
2104+
with open(manifest_repo / 'west.yml', 'w') as f:
2105+
f.write('''\
2106+
manifest:
2107+
projects:
2108+
- name: p1
2109+
url: p1-url
2110+
import: app/west.yml
2111+
''')
2112+
2113+
p1 = manifest_repo / '..' / 'p1'
2114+
create_repo(p1)
2115+
create_branch(p1, 'manifest-rev', checkout=True)
2116+
add_commit(
2117+
p1,
2118+
'add app/west.yml with west-commands',
2119+
files={
2120+
'app/west.yml': '''\
2121+
manifest:
2122+
projects:
2123+
- name: p2
2124+
url: p2-url
2125+
self:
2126+
west-commands: west-commands.yml
2127+
''',
2128+
},
2129+
)
2130+
checkout_branch(p1, 'master')
2131+
2132+
p1_proj = MF().get_projects(['p1'])[0]
2133+
# The west_commands path should be 'app/west-commands.yml',
2134+
# not 'west-commands.yml', to be resolved correctly
2135+
# relative to the project root.
2136+
expected = ['app/west-commands.yml']
2137+
assert p1_proj.west_commands == expected
2138+
2139+
20982140
def test_import_map_error_handling():
20992141
# Make sure we handle expected errors when loading import:
21002142
# values that are maps.

0 commit comments

Comments
 (0)