aquaproj / aqua

Declarative CLI Version manager written in Go. Support Lazy Install, Registry, and continuous update with Renovate. CLI version is switched seamlessly
https://aquaproj.github.io
884 stars 39 forks source link

Remove the prefix module subdirectory from go_install package's version #1020

Open suzuki-shunsuke opened 2 years ago

suzuki-shunsuke commented 2 years ago

Feature Overview

Support formatting the version string. For example, support removing the prefix.

Why is the feature needed?

Please explain the problem you want to solve.

This is required to support zeromicro/go-zero, goctl.

The following configuration doesn't work.

aqua.yaml

registries:
  - name: standard
    type: local
    path: registry.yaml
packages:
  - name: zeromicro/go-zero/goctl@tools/goctl/v1.3.9

registry.yaml

packages:
  - name: zeromicro/go-zero/goctl
    type: go_install
    repo_owner: zeromicro
    repo_name: go-zero
    path: github.com/zeromicro/go-zero/tools/goctl
    version_filter: Version startsWith "tools/goctl/"
$ aqua i
INFO[0000] Installing a Go tool                          aqua_version=1.17.1 env=darwin/arm64 go_package_path=github.com/zeromicro/go-zero/tools/goctl@tools/goctl/v1.3.9 gobin=/Users/shunsukesuzuki/.local/share/aquaproj-aqua/pkgs/go_install/github.com/zeromicro/go-zero/tools/goctl/tools/goctl/v1.3.9/bin package_name=zeromicro/go-zero/goctl package_version=tools/goctl/v1.3.9 program=aqua registry=standard
go: github.com/zeromicro/go-zero/tools/goctl@tools/goctl/v1.3.9: github.com/zeromicro/go-zero/tools/goctl@tools/goctl/v1.3.9: invalid version: version "tools/goctl/v1.3.9" invalid: disallowed version string
ERRO[0000] install the package                           aqua_version=1.17.1 env=darwin/arm64 error="build Go tool: exit status 1" package_name=zeromicro/go-zero/goctl package_version=tools/goctl/v1.3.9 program=aqua registry=standard
FATA[0000] aqua failed                                   aqua_version=1.17.1 env=darwin/arm64 error="it failed to install some packages" program=aqua
go: github.com/zeromicro/go-zero/tools/goctl@tools/goctl/v1.3.9: github.com/zeromicro/go-zero/tools/goctl@tools/goctl/v1.3.9: invalid version: version "tools/goctl/v1.3.9" invalid: disallowed version string

The GitHub tag tools/goctl/v1.3.9 is invalid as Go package version. We have to format tools/goctl/v1.3.9 to v1.3.9.

https://pkg.go.dev/github.com/zeromicro/go-zero/tools/goctl?tab=versions

Does the feature include Breaking Changes?

No.

Example Code

command and configuration

$ 
suzuki-shunsuke commented 2 years ago

I didn't know this. https://go.dev/ref/mod#vcs-version

If a module is defined in a subdirectory within the repository, that is, the module subdirectory portion of the module path is not empty, then each tag name must be prefixed with the module subdirectory, followed by a slash. For example, the module golang.org/x/tools/gopls is defined in the gopls subdirectory of the repository with root path golang.org/x/tools. The version v0.4.0 of that module must have the tag named gopls/v0.4.0 in that repository.

suzuki-shunsuke commented 2 years ago

Maybe, it is difficult to get module subdirectory. I don't know the rule.

e.g.

https://gopkg.in/yaml.v3

sapphi-red commented 6 months ago

Currently, if I run aqua g -i golang.org/x/tools/gopls, - name: golang.org/x/tools/gopls@gopls/v0.15.3 gets inserted. I think there're two ways to fix this:

  1. fix the generate command to insert - name: golang.org/x/tools/gopls@v0.15.3 instead
  2. change the install command to trim the gopls/ part when running go install

It depends on which you expect the users to write:

  1. name: golang.org/x/tools/gopls@v0.15.3
  2. name: golang.org/x/tools/gopls@gopls/v0.15.3

IMHO 1 feels more natural. Or do you want to support both?

In either case, the prefix needs to be trimmed. This seems to be the way go finds the repository root path. From the definition written in this sentence, it should be able to calculate the module subdirectory from the repository root path and the path field in the registry. That said, while checking the behavior, I found that go list -m -versions -json $module_path returns a version list (example: go list -m -versions -json github.com/zeromicro/go-zero/tools/goctl). In this way, you can get the version without the module subdirectory and also support modules that are not hosted on GitHub, even those using mercurial (hg) instead of git.