Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Go workspaces #999

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 75 additions & 20 deletions cachito/workers/pkg_managers/gomod.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import shutil
import tempfile
from dataclasses import dataclass
from datetime import datetime
from itertools import chain
from pathlib import Path, PureWindowsPath
Expand Down Expand Up @@ -75,6 +76,7 @@ class GoModule(_GolangModel):
"""

path: str
dir: Optional[Path] = None
version: Optional[str] = None
main: bool = False
replace: Optional["GoModule"] = None
Expand All @@ -93,6 +95,43 @@ class GoPackage(_GolangModel):
deps: list[str] = []


@dataclass
class LocalModules:
"""The modules present in the current repository being processed by Cachito.

It contains a main module, which represents the current input package being processed, and all
existing worskpaces nested under the nearest directory containing a go.work file (if it exists).

Note that this class does not account for any local replacements present in the current go.mod
or go.work file.
"""

main: GoModule
workspaces: list[GoModule]

@staticmethod
def from_json_stream(modules_json_stream: str, app_source_path: Path) -> "LocalModules":
main_module = None
workspaces = []

for item in load_json_stream(modules_json_stream):
module = GoModule.parse_obj(item)

if module.dir == app_source_path:
main_module = module
else:
workspaces.append(module)

# should never happen, since the main module will always be a part of the json stream
if not main_module:
raise RuntimeError('Failed to find the main module info in the "go list -m" output.')

return LocalModules(main_module, workspaces)

def all(self) -> list[GoModule]:
return [self.main] + self.workspaces


class Go:
"""High level wrapper over the 'go' CLI command.

Expand Down Expand Up @@ -380,23 +419,12 @@ def resolve_gomod(
# Make Go ignore the vendor dir even if there is one
go_list.extend(["-mod", "readonly"])

main_module_name = go([*go_list, "-m"], run_params).rstrip()
main_module_version = get_golang_version(
main_module_name,
git_dir_path,
request["ref"],
update_tags=True,
subpath=(
None
if app_source_path == git_dir_path
else str(app_source_path).replace(f"{git_dir_path}/", "")
),
local_modules = LocalModules.from_json_stream(
go([*go_list, "-m", "-json"], run_params).rstrip(),
app_source_path,
)
main_module = {
"type": "gomod",
"name": main_module_name,
"version": main_module_version,
}

_set_local_modules_versions(local_modules, git_dir_path, request)

def go_list_deps(pattern: Literal["./...", "all"]) -> Iterator[GoPackage]:
"""Run go list -deps -json and return the parsed list of packages.
Expand All @@ -413,7 +441,7 @@ def go_list_deps(pattern: Literal["./...", "all"]) -> Iterator[GoPackage]:
mod for pkg in go_list_deps("all") if (mod := pkg.module) and not mod.main
)
main_module_deps = _deduplicate_to_gomod_dicts(
chain(package_modules, downloaded_modules), deps_to_replace
chain(package_modules, downloaded_modules, local_modules.workspaces), deps_to_replace
)

log.info("Retrieving the list of packages")
Expand Down Expand Up @@ -446,7 +474,7 @@ def go_list_deps(pattern: Literal["./...", "all"]) -> Iterator[GoPackage]:
elif not dep.module or dep.module.main:
# Standard=false, Module.Main=true
# Standard=false, Module=null <- probably a to-be-generated package
dep_version = main_module_version
dep_version = local_modules.main.version
chmeliik marked this conversation as resolved.
Show resolved Hide resolved
else:
_, dep_version = _get_name_and_version(dep.module)

Expand All @@ -455,7 +483,7 @@ def go_list_deps(pattern: Literal["./...", "all"]) -> Iterator[GoPackage]:
main_pkg = {
"type": "go-package",
"name": pkg.import_path,
"version": main_module_version,
"version": local_modules.main.version,
}
main_packages.append({"pkg": main_pkg, "pkg_deps": pkg_deps})

Expand All @@ -465,13 +493,40 @@ def go_list_deps(pattern: Literal["./...", "all"]) -> Iterator[GoPackage]:
_vet_local_file_dep_paths(package["pkg_deps"], app_source_path, git_dir_path)
_set_full_local_dep_relpaths(package["pkg_deps"], main_module_deps)

main_module_dict = {
"type": "gomod",
"name": local_modules.main.path,
"version": local_modules.main.version,
}

return {
"module": main_module,
"module": main_module_dict,
"module_deps": main_module_deps,
"packages": main_packages,
}


def _set_local_modules_versions(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the idea of updating the object attributes in place, but I also didn't want to do a deep dive into refactoring get_golang_version. This function takes a lot of attributes, and anything I wrote in the LocalModules class would bring all this crap together 😕

local_modules: LocalModules,
git_dir_path: Path,
request: dict[str, Any],
) -> None:
"""Update the local modules with their corresponding versions."""
for module in local_modules.all():
if module.dir and module.dir != git_dir_path:
subpath = str(module.dir.relative_to(git_dir_path))
else:
subpath = None

module.version = get_golang_version(
module.path,
git_dir_path,
request["ref"],
update_tags=True,
subpath=subpath,
)


def _deduplicate_to_gomod_dicts(
modules: Iterable[GoModule], user_specified_deps_to_replace: set[str]
) -> list[dict[str, Any]]:
Expand Down
24 changes: 0 additions & 24 deletions cachito/workers/tasks/gomod.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,6 @@ def _find_missing_gomod_files(bundle_dir, subpaths):
return invalid_gomod_files


def _is_workspace(repo_root: Path, subpath: str):
current_path = repo_root / subpath

while current_path != repo_root:
if (current_path / "go.work").exists():
log.warning("go.work file found at %s", current_path)
return True
current_path = current_path.parent

if (repo_root / "go.work").exists():
log.warning("go.work file found at %s", repo_root)
return True

return False


def _fail_if_bundle_dir_has_workspaces(bundle_dir: RequestBundleDir, subpaths: list[str]):
for subpath in subpaths:
if _is_workspace(bundle_dir.source_root_dir, subpath):
raise UnsupportedFeature("Go workspaces are not supported by Cachito.")


def _fail_if_parent_replacement_not_included(packages_json_data: PackagesData) -> None:
"""
Fail if any dependency replacement refers to a parent dir that isn't included in this request.
Expand Down Expand Up @@ -133,8 +111,6 @@ def fetch_gomod_source(request_id, dep_replacements=None, package_configs=None):
# Default to the root of the application source
subpaths = [os.curdir]

_fail_if_bundle_dir_has_workspaces(bundle_dir, subpaths)

invalid_gomod_files = _find_missing_gomod_files(bundle_dir, subpaths)
if invalid_gomod_files:
invalid_files_print = "; ".join(invalid_gomod_files)
Expand Down
Loading