Skip to content

Commit

Permalink
Fix project files absolute paths when a work copy is used
Browse files Browse the repository at this point in the history
To avoid unintended source code modifications, Cachi2 makes a copy of
the source directory when some package managers are used (currently,
it's being done just for yarn).

This creates a problem with project files, since the absolute path of
files now is pointing to the temporary working copy. The solution
introced by this patch reverts these paths back to the original source
dir path.

This is intended to be a quick fix, and should only be kept until we
solve #712.

Signed-off-by: Bruno Pimentel <bpimente@redhat.com>
  • Loading branch information
brunoapimentel committed Jan 21, 2025
1 parent b4a413d commit 06180db
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cachi2/core/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def resolve_packages(request: Request) -> RequestOutput:
output = _resolve_packages(request)
request.source_dir = original_source_dir

# Temporary solution to project files paths that are pointing to the work copy.
# Should be replaced once we extend the work copy solution to other package managers.
# https://github.com/containerbuildsystem/cachi2/issues/712
for project_file in output.build_config.project_files:
subpath = project_file.abspath.relative_to(source_backup)
project_file.abspath = original_source_dir / subpath

return output
else:
return _resolve_packages(request)
Expand Down
38 changes: 37 additions & 1 deletion tests/unit/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_source_dir_copy(
packages=packages,
)

def _resolve_packages(request: Request) -> None:
def _resolve_packages(request: Request) -> RequestOutput:
if copy_exists:
tmp_dir_name = request.source_dir.path.name

Expand All @@ -132,6 +132,8 @@ def _resolve_packages(request: Request) -> None:
# assert the original source_dir is being used
assert request.source_dir == RootedPath(tmp_path)

return RequestOutput.empty()

mock_resolve_packages.side_effect = _resolve_packages

resolver.resolve_packages(request)
Expand All @@ -140,6 +142,40 @@ def _resolve_packages(request: Request) -> None:
assert request.source_dir == RootedPath(tmp_path)


@mock.patch("cachi2.core.resolver._resolve_packages")
def test_project_files_fix_for_work_copy(
mock_resolve_packages: mock.Mock,
tmp_path: Path,
) -> None:
request = Request(
source_dir=tmp_path,
output_dir=tmp_path,
packages=[{"type": "yarn"}],
)

def _resolve_packages(request: Request) -> RequestOutput:
# assert request is based on a copy of the source dir
assert request.source_dir.path != tmp_path
assert request.source_dir.path.name.endswith(".cachi2-source-copy")

# return a project file pointing to the source dir copy
return RequestOutput(
components=[],
build_config=BuildConfig(
environment_variables=[],
project_files=[
ProjectFile(abspath=request.source_dir.path / "package.json", template="n/a"),
],
),
)

mock_resolve_packages.side_effect = _resolve_packages
output = resolver.resolve_packages(request)

# assert the project file path was corrected to point to the original source dir
assert output.build_config.project_files[0].abspath == tmp_path / "package.json"


@pytest.mark.parametrize(
"flags",
[
Expand Down

0 comments on commit 06180db

Please sign in to comment.