Skip to content

Commit 095f66f

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modget: if building packages, only update go.mod if the build succeeds
Fixes #41315 Change-Id: I5b18a0c2d1d72ff556a882e862b95133deb3ef98 Reviewed-on: https://go-review.googlesource.com/c/go/+/255970 Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent ea42b77 commit 095f66f

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

src/cmd/go/internal/modget/get.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,20 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
588588
modload.LoadPackages(ctx, loadOpts, pkgPatterns...)
589589
}
590590

591+
// If -d was specified, we're done after the module work.
592+
// We've already downloaded modules by loading packages above.
593+
// Otherwise, we need to build and install the packages matched by
594+
// command line arguments. This may be a different set of packages,
595+
// since we only build packages for the target platform.
596+
// Note that 'go get -u' without arguments is equivalent to
597+
// 'go get -u .', so we'll typically build the package in the current
598+
// directory.
599+
if !*getD && len(pkgPatterns) > 0 {
600+
work.BuildInit()
601+
pkgs := load.PackagesForBuild(ctx, pkgPatterns)
602+
work.InstallPackages(ctx, pkgPatterns, pkgs)
603+
}
604+
591605
// Everything succeeded. Update go.mod.
592606
modload.AllowWriteGoMod()
593607
modload.WriteGoMod()
@@ -600,21 +614,6 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
600614
// contains information about direct dependencies that WriteGoMod uses.
601615
// Refactor to avoid these kinds of global side effects.
602616
reportRetractions(ctx)
603-
604-
// If -d was specified, we're done after the module work.
605-
// We've already downloaded modules by loading packages above.
606-
// Otherwise, we need to build and install the packages matched by
607-
// command line arguments. This may be a different set of packages,
608-
// since we only build packages for the target platform.
609-
// Note that 'go get -u' without arguments is equivalent to
610-
// 'go get -u .', so we'll typically build the package in the current
611-
// directory.
612-
if *getD || len(pkgPatterns) == 0 {
613-
return
614-
}
615-
work.BuildInit()
616-
pkgs := load.PackagesForBuild(ctx, pkgPatterns)
617-
work.InstallPackages(ctx, pkgPatterns, pkgs)
618617
}
619618

620619
// parseArgs parses command-line arguments and reports errors.

src/cmd/go/testdata/script/mod_get_errors.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
cp go.mod go.mod.orig
22

3+
4+
# Both 'go get' and 'go get -d' should fail, without updating go.mod,
5+
# if the transitive dependencies of the requested package (by default,
6+
# the package in the current directory) cannot be resolved.
7+
38
! go get
49
stderr '^example.com/m imports\n\texample.com/badimport imports\n\texample.net/oops: import missing$' # TODO: better error message
510
cmp go.mod.orig go.mod
@@ -8,16 +13,48 @@ cmp go.mod.orig go.mod
813
stderr '^example.com/m imports\n\texample.com/badimport imports\n\texample.net/oops: import missing$' # TODO: better error message
914
cmp go.mod.orig go.mod
1015

16+
cd importsyntax
17+
18+
19+
# If 'go get' fails due to a compile error (such as a syntax error),
20+
# it should not update the go.mod file.
21+
22+
! go get
23+
stderr '^..[/\\]badimport[/\\]syntaxerror[/\\]syntaxerror.go:1:1: expected ''package'', found pack$' # TODO: An import stack would be nice.
24+
cmp ../go.mod.orig ../go.mod
25+
26+
27+
# A syntax error in a dependency prevents the compiler from needing that
28+
# dependency's imports, so 'go get -d' should not report an error when those
29+
# imports cannot be resolved: it has all of the dependencies that the compiler
30+
# needs, and the user did not request to run the compiler.
31+
32+
go get -d
33+
cmp ../go.mod.syntax-d ../go.mod
34+
35+
1136
-- go.mod --
1237
module example.com/m
1338

1439
go 1.16
1540

1641
replace example.com/badimport v0.1.0 => ./badimport
42+
-- go.mod.syntax-d --
43+
module example.com/m
44+
45+
go 1.16
46+
47+
replace example.com/badimport v0.1.0 => ./badimport
48+
49+
require example.com/badimport v0.1.0
1750
-- m.go --
1851
package m
1952

2053
import _ "example.com/badimport"
54+
-- importsyntax/importsyntax.go --
55+
package importsyntax
56+
57+
import _ "example.com/badimport/syntaxerror"
2158
-- badimport/go.mod --
2259
module example.com/badimport
2360

@@ -26,3 +63,7 @@ go 1.16
2663
package badimport
2764

2865
import "example.net/oops"
66+
-- badimport/syntaxerror/syntaxerror.go --
67+
pack-age syntaxerror // sic
68+
69+
import "example.net/oops"

0 commit comments

Comments
 (0)