Skip to content

Commit

Permalink
Fix path for processing go modules
Browse files Browse the repository at this point in the history
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 <ejegrova@redhat.com>
  • Loading branch information
ejegrova authored and ejegrova committed Feb 19, 2024
1 parent b115beb commit 2c80c4d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
18 changes: 10 additions & 8 deletions cachito/workers/pkg_managers/gomod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<ver>\d\.\d+(:?.\d+)?)\s*$")
for line in f:
Expand Down
3 changes: 2 additions & 1 deletion cachito/workers/tasks/gomod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions tests/test_workers/test_tasks/test_gomod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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/")},
},
),
(
Expand All @@ -57,7 +57,7 @@
{
"present": {".": True, "foo": True},
"relpath": {".": "./go.mod", "foo": "./foo/go.mod"},
"sourcedir": {".": "./", "foo": "./foo/"},
"sourcedir": {".": Path("./"), "foo": Path("./foo/")},
},
),
(
Expand All @@ -67,7 +67,7 @@
{
"present": {".": True, "foo": True},
"relpath": {".": "./go.mod", "foo": "./foo/go.mod"},
"sourcedir": {".": "./", "foo": "./foo/"},
"sourcedir": {".": Path("./"), "foo": Path("./foo/")},
},
),
),
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2c80c4d

Please sign in to comment.