Skip to content

chore: import go-vcs library with clean history - #7496

Merged
grouville merged 14 commits into
dagger:mainfrom
grouville:import-go-vcs
May 31, 2024
Merged

grouville merged 14 commits into
dagger:mainfrom
grouville:import-go-vcs

Conversation

@grouville

@grouville grouville commented May 29, 2024 •

Copy link
Copy Markdown
Member

Context

First PR of a serie, splitting the implementation of #7420, in a cleaner way.

This one imports the codebase from https://github.com/grouville/go-vcs directly into our codebase for ease/maintainability purposes. I took the liberty of making it part of the engine/ci subdir, but open to discussion

First commit

The first commit is Google's library https://pkg.go.dev/golang.org/x/tools/go/vcs#RepoRootForImportPath filtered-history squashed into one, with all the context:

This commit adds a forked / improved version of Google's RepoRootForImportPath logic. Most of this code is originally part of the go/internal section of Google's codebase: https://github.com/golang/go/blob/16d3040a84be821d801b75bd1a3d8ab4cc89ee36/src/cmd/go/internal/vcs/vcs.go#L1281.

However, Justin found a version of this library exposed by Google without all the internal dependencies. It is now deprecated as unmaintained, but the core logic: the dynamic resolving coupled with the regexes are still relevant and enough for our use-case: https://pkg.go.dev/golang.org/x/tools/go/vcs#RepoRootForImportPath.

We had to fork the library in order to upgrade the go version used, bump the dependencies and more importantly fix a few unported changes:
1. We remove unsupported VCS, as BuildKit only support Git based VCS
2. We remove the deprecated mercurial check from bitbucket vcs options (https://github.com/golang/go/commit/5b1b80b) that led to an unability to parse the root of a repo from a bitbucket private ref
3. Update the regexes and the tests for jazz, apache, git.sr.ht SCM (following current official package's logic)
4. Remove the deprecation notices, as this package does not aim to be 100% compliant with upstream but just handle the split between the root of a repo and the subpath from its URL + rely on io.ReadAll instead of ioutil

Follow-up commits

All the follow-up commits are mostly maintenance work implemented to sync the deprecated library in a simi-official state with Google's internal version: https://github.com/golang/go/blob/master/src/cmd/go/internal/vcs/vcs.go

Then, it is followed by a few Dagger specific changes:

@marcosnils

marcosnils commented May 30, 2024 •

Copy link
Copy Markdown
Contributor

engine/ci subdir, but open to discussion

how about using engine/vcs as the package name which is the same the original library is using?

@grouville

Copy link
Copy Markdown
Member Author

how about using engine/vcs as the package name which is the same the original library is using?

Given that the labels.go is refactoring all the vcs terminology to ci, I don't have a strong opinion around that: https://discord.com/channels/707636530424053791/1233176406112993350/1243237579676061767

@marcosnils

Copy link
Copy Markdown
Contributor

Given that the labels.go is refactoring all the vcs terminology to ci, I don't have a strong opinion around that: https://discord.com/channels/707636530424053791/1233176406112993350/1243237579676061767

but labels.go is for telemetry labels, right? In this case we're talking about an engine package to handle vcs information for different URL resolvers. I think having an engine/vcs package actually makes a bit more sense.

Actually, the package is already called vcs (https://github.com/dagger/dagger/pull/7496/files#diff-7e6077f012d1b44204a4d56bf23fc79ac711db5517d15e7c87fd65ebb095cd49R5) but it's in a ci directory which is not generally very go idiomatic.

@vito

vito commented May 30, 2024

Copy link
Copy Markdown
Contributor

Agreed - vcs makes sense here. I wouldn't try to line this up with the labeling terminology; this part is more low level and has separate concerns. Nothing is set in stone for the new label plans anyhow

grouville added 14 commits May 31, 2024 10:39
This commit adds a forked / improved version of Google's RepoRootForImportPath logic. Most of this code is originally part of the go/internal section of Google's codebase: https://github.com/golang/go/blob/16d3040a84be821d801b75bd1a3d8ab4cc89ee36/src/cmd/go/internal/vcs/vcs.go#L1281.

However, Justin found a version of this library exposed by Google without all the internal dependencies. It is now deprecated as unmaintained, but the core logic: the dynamic resolving coupled with the regexes are still relevant and enough for our use-case: https://pkg.go.dev/golang.org/x/tools/go/vcs#RepoRootForImportPath.

We had to fork the library in order to upgrade the go version used, bump the dependencies and more importantly fix a few unported changes:
1. We remove unsupported VCS, as BuildKit only support Git based VCS
2. We remove the deprecated mercurial check from bitbucket vcs options (golang/go@5b1b80b) leading to an unability to parse the root of a repo from a bitbucket private ref

However, in the context of Dagger, the important part is to be able to split the root of the repo from its subdir. I think it is ok as it will fail at the gitdns.Git level (to be verified)

3. Update the regexes and the tests for jazz, apache, git.sr.ht SCM (following current official package's logic)
4. Remove the deprecation notices, as this package does not aim to be 100% compliant with upstream but just handle the split between the root of a repo and the subpath from its URL + rely on io.ReadAll instead of ioutil

Signed-off-by: grouville <[email protected]>
… options

Current library https://pkg.go.dev/golang.org/x/tools/go/vcs#RepoRootForImportPath was not up-to-date on two updates:
1. cmd/go: remove mercurial from bitbucket vcs options (golang/go@5b1b80b) leading to an unability to parse the root of a repo from a bitbucket private ref

2.  allow go get on github.com/ import paths with Unicodes (golang/go@6511931). This one is a bit more subject to discussion as the current implementation (https://github.com/golang/go/blob/master/src/cmd/go/internal/vcs/vcs_test.go) still does not allow it

However, in the context of Dagger, the important part is to be able to split the root of the repo from its subdir. I think it is ok as it will fail at the gitdns.Git level (to be verified)

3. Add tests for jazz, apache, git.sr.ht

Signed-off-by: grouville <[email protected]>
License was lost with the git filter-branch

Signed-off-by: grouville <[email protected]>
BuildKit only supports Git, remove other VCS support

Signed-off-by: grouville <[email protected]>
BuildKit only supports Git, remove other VCS support

Signed-off-by: grouville <[email protected]>
- I manually updated the logic to comply with the latest changes for bitbucket and unicode.
- The deprecation is correct for Google, but in our case, regarding the split function is out of scope
- Rely on io.ReadAll instead of ioutil

Signed-off-by: grouville <[email protected]>
Bitbucket root is now resolved with regex

Signed-off-by: grouville <[email protected]>
Codeberg SCM follows GitHub structures AND also exposes the go meta head. In order to avoid unnecessary network calls, just implement the corresponding regex

Signed-off-by: grouville <[email protected]>
Weird behavior encountered on GitLab's subgroups in private mode.

After investigation, it turns out that GitLab conceils the existence of the nested project when unauthenticated: https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/lib/gitlab/middleware/go.rb#L114-126.

The current behavior of the library is to rely on the `git ls-remote -q` command when the path contains a `.git`, which relies on gitcredentials for auth. The exact behavior is detailed in this comment: golang/go#26232 (comment)

Interesting link regrouping all Go private repo issues: golang/go#29953

Signed-off-by: grouville <[email protected]>
Remove noVCSSuffix to allow the .git suffix on the SCM matched via a regex.
The underlying reason is to accept inputs with and without this VCSSuffix.

This is useful, as in order to resolve the root of a GitLab private repo, we need to specify the root of the repository with `.git` for the function to piggyback on the ping command (in our case: `git ls-remote -q`)

Signed-off-by: grouville <[email protected]>
Add a vanity URL coverage redirecting to a public repo. No auth yet, as these are specific use-cases

Signed-off-by: grouville <[email protected]>
Current state does not allow path traversal for the CI refs parsed via a regex.

Due to our internal handling of those edge cases, it is easier to extend the matching patterns to handle it

Signed-off-by: grouville <[email protected]>
Go's repoRootForImportPath is not following Dagger's golintci conventions

This commit fixes them or ignores them when it does not impact the pertinence not the security:
- exec.Command() is hidden with nolint:gosec as
- naked return are hidden with nolint:nakedret, as this is a pattern used on the official library from Google. Instead of refactoring the function, ignore the error
- use %w in fmt.Errorf to wrap errors
- use ReplaceAll instead of Replace(..., -1)

Signed-off-by: grouville <[email protected]>
Move vcs package as a subdir of engine

Signed-off-by: grouville <[email protected]>
@grouville

Copy link
Copy Markdown
Member Author

Actually, the package is already called vcs (https://github.com/dagger/dagger/pull/7496/files#diff-7e6077f012d1b44204a4d56bf23fc79ac711db5517d15e7c87fd65ebb095cd49R5) but it's in a ci directory which is not generally very go idiomatic.

You're right, I made a mistake and pushed a wrong version at first. I changed and moved to engine/vcs as per your recommendations 🙏

@grouville
grouville merged commit 370c355 into dagger:main May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants