From 2c80c4d263ae3fb0d41c2804f16c938be1c8e5b4 Mon Sep 17 00:00:00 2001 From: ejegrova Date: Mon, 19 Feb 2024 17:32:25 +0100 Subject: [PATCH] Fix path for processing go modules A bug was introduced in the 1.17 release. We were relying on the git repo path instead of the actual module path, and this caused an issue when the module is not in the repo's root. JIRA: STONEBLD-2191 Signed-off-by: ejegrova --- cachito/workers/pkg_managers/gomod.py | 18 ++++++++++-------- cachito/workers/tasks/gomod.py | 3 ++- tests/test_workers/test_tasks/test_gomod.py | 14 +++++++------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/cachito/workers/pkg_managers/gomod.py b/cachito/workers/pkg_managers/gomod.py index 32d03b402..b0e871e1b 100644 --- a/cachito/workers/pkg_managers/gomod.py +++ b/cachito/workers/pkg_managers/gomod.py @@ -285,7 +285,7 @@ def resolve_gomod(app_source_path, request, dep_replacements=None, git_dir_path= """ Resolve and fetch gomod dependencies for given app source archive. - :param str app_source_path: the full path to the application source code + :param Path app_source_path: the full path to the application source code :param dict request: the Cachito request this is for :param list dep_replacements: dependency replacements with the keys "name" and "version"; this results in a series of `go mod edit -replace` commands @@ -319,7 +319,7 @@ def resolve_gomod(app_source_path, request, dep_replacements=None, git_dir_path= run_params = {"env": env, "cwd": app_source_path} - go = _select_go_toolchain(git_dir_path) + go = _select_go_toolchain(app_source_path) # Collect all the dependency names that are being replaced to later report which # dependencies were replaced @@ -383,8 +383,8 @@ def resolve_gomod(app_source_path, request, dep_replacements=None, git_dir_path= update_tags=True, subpath=( None - if app_source_path == str(git_dir_path) - else app_source_path.replace(f"{git_dir_path}/", "") + if app_source_path == git_dir_path + else str(app_source_path).replace(f"{git_dir_path}/", "") ), ) main_module = { @@ -662,7 +662,7 @@ def _vet_local_deps( dependencies: List[dict], module_name: str, allowed_patterns: List[str], - app_source_path: str, + app_source_path: Path, git_dir_path: str, ) -> None: """ @@ -693,11 +693,13 @@ def _vet_local_deps( ) -def _validate_local_dependency_path(app_source_path: str, git_dir_path: str, dep_path: str) -> None: +def _validate_local_dependency_path( + app_source_path: Path, git_dir_path: str, dep_path: str +) -> None: """ Validate that the local dependency path is not outside the repository. - :param str app_source_path: the full path to the application source code + :param Path app_source_path: the full path to the application source code :param str git_dir_path: the full path to the git repository :param str dep_path: the relative path for local replacements (the dep version) :raise ValidationError: if the local dependency path is invalid @@ -1049,7 +1051,7 @@ def _get_gomod_version(source_dir: Path) -> Optional[str]: If we cannot extract a version from the 'go' line, we return None, leaving it up to the caller to decide what to do next. """ - go_mod = source_dir / "go.mod" + go_mod = source_dir.joinpath("go.mod") with open(go_mod) as f: reg = re.compile(r"^\s*go\s+(?P\d\.\d+(:?.\d+)?)\s*$") for line in f: diff --git a/cachito/workers/tasks/gomod.py b/cachito/workers/tasks/gomod.py index 32b5f0196..9baf8ff3d 100644 --- a/cachito/workers/tasks/gomod.py +++ b/cachito/workers/tasks/gomod.py @@ -166,6 +166,7 @@ def fetch_gomod_source(request_id, dep_replacements=None, package_configs=None): "GOCACHE": {"value": "deps/gomod", "kind": "path"}, "GOPATH": {"value": "deps/gomod", "kind": "path"}, "GOMODCACHE": {"value": "deps/gomod/pkg/mod", "kind": "path"}, + "GOTOOLCHAIN": {"value": "local", "kind": "literal"}, } env_vars.update(config.cachito_default_environment_variables.get("gomod", {})) update_request_env_vars(request_id, env_vars) @@ -182,7 +183,7 @@ def fetch_gomod_source(request_id, dep_replacements=None, package_configs=None): f'Fetching the gomod dependencies at the "{subpath}" directory', ) request = get_request(request_id) - gomod_source_path = str(bundle_dir.app_subpath(subpath).source_dir) + gomod_source_path = Path(bundle_dir.app_subpath(subpath).source_dir) try: gomod = resolve_gomod( gomod_source_path, request, dep_replacements, bundle_dir.source_dir diff --git a/tests/test_workers/test_tasks/test_gomod.py b/tests/test_workers/test_tasks/test_gomod.py index 5fae60a45..9cbb9cffd 100644 --- a/tests/test_workers/test_tasks/test_gomod.py +++ b/tests/test_workers/test_tasks/test_gomod.py @@ -26,19 +26,19 @@ None, True, None, - {"present": {".": True}, "relpath": {".": "./go.mod"}, "sourcedir": {".": "./"}}, + {"present": {".": True}, "relpath": {".": "./go.mod"}, "sourcedir": {".": Path("./")}}, ), ( None, False, None, - {"present": {".": True}, "relpath": {".": "./go.mod"}, "sourcedir": {".": "./"}}, + {"present": {".": True}, "relpath": {".": "./go.mod"}, "sourcedir": {".": Path("./")}}, ), ( [{"name": "github.com/pkg/errors", "type": "gomod", "version": "v0.8.1"}], True, None, - {"present": {".": True}, "relpath": {".": "./go.mod"}, "sourcedir": {".": "./"}}, + {"present": {".": True}, "relpath": {".": "./go.mod"}, "sourcedir": {".": Path("./")}}, ), ( None, @@ -47,7 +47,7 @@ { "present": {"bar": True, "foo": True}, "relpath": {"bar": "./bar/go.mod", "foo": "./foo/go.mod"}, - "sourcedir": {"bar": "./bar/", "foo": "./foo/"}, + "sourcedir": {"bar": Path("./bar/"), "foo": Path("./foo/")}, }, ), ( @@ -57,7 +57,7 @@ { "present": {".": True, "foo": True}, "relpath": {".": "./go.mod", "foo": "./foo/go.mod"}, - "sourcedir": {".": "./", "foo": "./foo/"}, + "sourcedir": {".": Path("./"), "foo": Path("./foo/")}, }, ), ( @@ -67,7 +67,7 @@ { "present": {".": True, "foo": True}, "relpath": {".": "./go.mod", "foo": "./foo/go.mod"}, - "sourcedir": {".": "./", "foo": "./foo/"}, + "sourcedir": {".": Path("./"), "foo": Path("./foo/")}, }, ), ), @@ -191,7 +191,7 @@ def directory_present(*args, **kwargs): gomod_calls = [ mock.call( - str(mock_bundle_dir().app_subpath(path).source_dir), + mock_bundle_dir().app_subpath(path).source_dir, mock_request, dep_replacements, mock_bundle_dir().source_dir,