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

[POC] go.mod replace with ModulePath #3

Open
wants to merge 4 commits into
base: hack-duplicate-go-deps-from-file
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ matrix:
- ubuntu2204
- macos
bazel: [6.x, 7.x]
bcr_variant:
- tests/bcr/go_work
- tests/bcr/go_mod
tasks:
ubuntu2204:
name: Ubuntu 22.04 with WORKSPACE
Expand Down Expand Up @@ -39,11 +42,12 @@ tasks:
name: BCR test module
platform: ${{ platform }}
bazel: ${{ bazel }}
working_directory: tests/bcr
working_directory: ${{ bcr_variant }}
shell_commands:
# Regenerate the BUILD files for the test module using Gazelle.
# Also verify -repo_config are generated correctly in gazelle.bash
- rm pkg/BUILD.bazel proto/BUILD.bazel
- touch pkg/BUILD.bazel proto/BUILD.bazel
- bazel run //:gazelle -- update pkg proto
- bazel run //:gazelle -- pkg proto
build_targets:
Expand All @@ -59,7 +63,7 @@ tasks:
name: BCR test on Windows
platform: windows
bazel: ${{ bazel }}
working_directory: tests/bcr
working_directory: ${{ bcr_variant }}
shell_commands:
# Regenerate the BUILD file for the test module using Gazelle.
# Also verify -repo_config are generated correctly in gazelle.bash
Expand Down
2 changes: 1 addition & 1 deletion .bcr/presubmit.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bcr_test_module:
module_path: tests/bcr
module_path: tests/bcr/go_mod
matrix:
platform:
- debian10
Expand Down
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
/bazel-bin
/bazel-out
/bazel-testlogs
/tests/bcr/bazel-bcr
/tests/bcr/bazel-bin
/tests/bcr/bazel-out
/tests/bcr/bazel-testlogs
/tests/bcr/MODULE.bazel.lock
/tests/bcr/*/bazel-go_*
/tests/bcr/*/bazel-bin
/tests/bcr/*/bazel-out
/tests/bcr/*/bazel-testlogs
/tests/bcr/*/MODULE.bazel.lock
MODULE.bazel.lock
.DS_STORE
.ijwb/
2 changes: 2 additions & 0 deletions cmd/fetch_repo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"fetch_repo.go",
"go_mod_download.go",
"module.go",
"path.go",
"vcs.go",
],
importpath = "github.com/bazelbuild/bazel-gazelle/cmd/fetch_repo",
Expand Down Expand Up @@ -40,6 +41,7 @@ filegroup(
"fetch_repo_test.go",
"go_mod_download.go",
"module.go",
"path.go",
"vcs.go",
],
visibility = ["//visibility:public"],
Expand Down
31 changes: 28 additions & 3 deletions cmd/fetch_repo/fetch_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
var (
// Common flags
importpath = flag.String("importpath", "", "Go importpath to the repository fetch")
path = flag.String("path", "", "absolute or relative path to a local go module")
dest = flag.String("dest", "", "destination directory")

// Repository flags
Expand All @@ -54,17 +55,41 @@ func main() {
log.SetPrefix("fetch_repo: ")

flag.Parse()
if *importpath == "" {
log.Fatal("-importpath must be set")

if *importpath == "" && *path == "" {
log.Fatal("-importpath or -path must be set")
}

if *dest == "" {
log.Fatal("-dest must be set")
}
if flag.NArg() != 0 {
log.Fatal("fetch_repo does not accept positional arguments")
}

if *version != "" {
if *path != "" {
if *importpath != "" {
log.Fatal("-importpath must not be set")
}
if *remote != "" {
log.Fatal("-remote must not be set in module path mode")
}
if *cmd != "" {
log.Fatal("-vcs must not be set in module path mode")
}
if *rev != "" {
log.Fatal("-rev must not be set in module path mode")
}
if *version != "" {
log.Fatal("-version must not be set in module path mode")
}
if *sum != "" {
log.Fatal("-sum must not be set in module path mode")
}
if err := moduleFromPath(*path, *dest); err != nil {
log.Fatal(err)
}
} else if *version != "" {
if *remote != "" {
log.Fatal("-remote must not be set in module mode")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/fetch_repo/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ package main

import (
"fmt"
"golang.org/x/mod/sumdb/dirhash"
stefanpenner marked this conversation as resolved.
Show resolved Hide resolved
"os"

"golang.org/x/mod/sumdb/dirhash"
)

func fetchModule(dest, importpath, version, sum string) error {
Expand All @@ -33,7 +34,6 @@ func fetchModule(dest, importpath, version, sum string) error {
// so we create a dummy module in the current directory (which should be
// empty).
w, err := os.OpenFile("go.mod", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o666)

if err != nil {
return fmt.Errorf("error creating temporary go.mod: %v", err)
}
Expand All @@ -46,7 +46,7 @@ func fetchModule(dest, importpath, version, sum string) error {
return fmt.Errorf("error closing temporary go.mod: %v", err)
}

var dl = GoModDownloadResult{}
dl := GoModDownloadResult{}
err = runGoModDownload(&dl, dest, importpath, version)
os.Remove("go.mod")
if err != nil {
Expand Down
37 changes: 37 additions & 0 deletions cmd/fetch_repo/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright 2019 The Bazel Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"os/exec"
)

func moduleFromPath(from string, dest string) error {
err := copyTree(dest, from)
if err != nil {
return err
}

cmd := exec.Command(findGoPath(), "mod", "download", "-json", "-modcacherw")
cmd.Dir = dest

output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error running '%v': %v in %s. Output: %s", cmd.Args, err, dest, string(output))
}
return nil
}
12 changes: 6 additions & 6 deletions def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load(
"@bazel_gazelle_is_bazel_module//:defs.bzl",
"GAZELLE_IS_BAZEL_MODULE",
)
load(
"@bazel_skylib//lib:shell.bzl",
"shell",
)
load(
"@bazel_gazelle_is_bazel_module//:defs.bzl",
"GAZELLE_IS_BAZEL_MODULE",
"//internal:gazelle_binary.bzl",
_gazelle_binary = "gazelle_binary_wrapper",
)
load(
"//internal:go_repository.bzl",
Expand All @@ -29,10 +33,6 @@ load(
_git_repository = "git_repository",
_http_archive = "http_archive",
)
load(
"//internal:gazelle_binary.bzl",
_gazelle_binary = "gazelle_binary_wrapper",
)
load(
"//internal/generationtest:generationtest.bzl",
_gazelle_generation_test = "gazelle_generation_test",
Expand Down
2 changes: 1 addition & 1 deletion docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ resulting documentation markdown files, to prevent users trying to load()
the stardoc repository, which is not a dependency users should install.
"""

load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")

# gazelle:ignore
Expand Down
3 changes: 3 additions & 0 deletions internal/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ bzl_library(
name = "go_mod",
srcs = ["go_mod.bzl"],
visibility = ["//:__subpackages__"],
deps = [
":semver",
],
)

bzl_library(
Expand Down
Loading