Skip to content

Commit 64c8d4a

Browse files
committed
cmd/go/internal/modload: reject -modfile flag in workspace mode
Currently, in workspace mode, the -modfile flag affects all the modules listed in the go.work file. This is not desirable most of the time. And when it results in an error, the error message does not help. For example, when there are more than one modules listed in the go.work file, running "go list -m -modfile=path/to/go.mod" gives this error: go: module example.com/foo appears multiple times in workspace This change reject -modfile flag explicitly with this error message: go: -modfile cannot be used in workspace mode While at here, correct some typos in the modload package.
1 parent 3e35df5 commit 64c8d4a

File tree

7 files changed

+51
-14
lines changed

7 files changed

+51
-14
lines changed

src/cmd/go/internal/modload/buildlist.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type Requirements struct {
4242

4343
// rootModules is the set of root modules of the graph, sorted and capped to
4444
// length. It may contain duplicates, and may contain multiple versions for a
45-
// given module path. The root modules of the groph are the set of main
45+
// given module path. The root modules of the graph are the set of main
4646
// modules in workspace mode, and the main module's direct requirements
4747
// outside workspace mode.
4848
rootModules []module.Version
@@ -789,7 +789,7 @@ func tidyPrunedRoots(ctx context.Context, mainModule module.Version, direct map[
789789
// to the build is required by either the main module or one of the modules
790790
// it requires explicitly. This invariant is left up to the caller, who must
791791
// not load packages from outside the module graph but may add roots to the
792-
// graph, but is facilited by (3). If the caller adds roots to the graph in
792+
// graph, but is facilitated by (3). If the caller adds roots to the graph in
793793
// order to resolve missing packages, then updatePrunedRoots will retain them,
794794
// the selected versions of those roots cannot regress, and they will
795795
// eventually be written back to the main module's go.mod file.
@@ -1258,12 +1258,12 @@ func convertPruning(ctx context.Context, rs *Requirements, pruning modPruning) (
12581258
if rs.pruning == pruning {
12591259
return rs, nil
12601260
} else if rs.pruning == workspace || pruning == workspace {
1261-
panic("attempthing to convert to/from workspace pruning and another pruning type")
1261+
panic("attempting to convert to/from workspace pruning and another pruning type")
12621262
}
12631263

12641264
if pruning == unpruned {
12651265
// We are converting a pruned module to an unpruned one. The roots of a
1266-
// ppruned module graph are a superset of the roots of an unpruned one, so
1266+
// pruned module graph are a superset of the roots of an unpruned one, so
12671267
// we don't need to add any new roots — we just need to drop the ones that
12681268
// are redundant, which is exactly what updateUnprunedRoots does.
12691269
return updateUnprunedRoots(ctx, rs.direct, rs, nil)

src/cmd/go/internal/modload/import.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (e *DirectImportFromImplicitDependencyError) ImportPath() string {
164164
// We might need sums for multiple modules to verify the package is unique.
165165
//
166166
// TODO(#43653): consolidate multiple errors of this type into a single error
167-
// that suggests a 'go get' command for root packages that transtively import
167+
// that suggests a 'go get' command for root packages that transitively import
168168
// packages from modules with missing sums. load.CheckPackageErrors would be
169169
// a good place to consolidate errors, but we'll need to attach the import
170170
// stack here.
@@ -575,7 +575,7 @@ func queryImport(ctx context.Context, path string, rs *Requirements) (module.Ver
575575

576576
// Look up module containing the package, for addition to the build list.
577577
// Goal is to determine the module, download it to dir,
578-
// and return m, dir, ImpportMissingError.
578+
// and return m, dir, ImportMissingError.
579579
fmt.Fprintf(os.Stderr, "go: finding module for package %s\n", path)
580580

581581
mg, err := rs.Graph(ctx)

src/cmd/go/internal/modload/init.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var (
6262
initialized bool
6363

6464
// These are primarily used to initialize the MainModules, and should be
65-
// eventually superceded by them but are still used in cases where the module
65+
// eventually superseded by them but are still used in cases where the module
6666
// roots are required but MainModules hasn't been initialized yet. Set to
6767
// the modRoots of the main modules.
6868
// modRoots != nil implies len(modRoots) > 0
@@ -390,6 +390,9 @@ func Init() {
390390
modRoots = nil
391391
} else if workFilePath != "" {
392392
// We're in workspace mode, which implies module mode.
393+
if cfg.ModFile != "" {
394+
base.Fatalf("go: -modfile cannot be used in workspace mode")
395+
}
393396
} else {
394397
if modRoot := findModuleRoot(base.Cwd()); modRoot == "" {
395398
if cfg.ModFile != "" {

src/cmd/go/internal/modload/load.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ type PackageOpts struct {
149149
Tags map[string]bool
150150

151151
// Tidy, if true, requests that the build list and go.sum file be reduced to
152-
// the minimial dependencies needed to reproducibly reload the requested
152+
// the minimal dependencies needed to reproducibly reload the requested
153153
// packages.
154154
Tidy bool
155155

@@ -1998,7 +1998,7 @@ func (ld *loader) checkTidyCompatibility(ctx context.Context, rs *Requirements)
19981998

19991999
if pkg.isTest() {
20002000
// We already did (or will) report an error for the package itself,
2001-
// so don't report a duplicate (and more vebose) error for its test.
2001+
// so don't report a duplicate (and more verbose) error for its test.
20022002
if _, ok := mismatches[pkg.testOf]; !ok {
20032003
base.Fatalf("go: internal error: mismatch recorded for test %s, but not its non-test package", pkg.path)
20042004
}

src/cmd/go/internal/modload/query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func checkReuse(ctx context.Context, path string, old *codehost.Origin) error {
117117
// version. Any other error indicates the function was unable to determine
118118
// whether the version should be allowed, for example, the function was unable
119119
// to fetch or parse a go.mod file containing retractions. Typically, errors
120-
// other than ErrDisallowd may be ignored.
120+
// other than ErrDisallowed may be ignored.
121121
type AllowedFunc func(context.Context, module.Version) error
122122

123123
var errQueryDisabled error = queryDisabledError{}
@@ -521,7 +521,7 @@ func (qm *queryMatcher) filterVersions(ctx context.Context, versions []string) (
521521
}
522522

523523
if !needIncompatible {
524-
// We're not yet sure whether we need to include +incomptaible versions.
524+
// We're not yet sure whether we need to include +incompatible versions.
525525
// Keep track of the last compatible version we've seen, and use the
526526
// presence (or absence) of a go.mod file in that version to decide: a
527527
// go.mod file implies that the module author is supporting modules at a
@@ -1018,7 +1018,7 @@ func (e *PackageNotInModuleError) ImportPath() string {
10181018
// 1.12 at least have a go directive.
10191019
//
10201020
// This function is a heuristic, since it's possible to commit a file that would
1021-
// pass this test. However, we only need a heurstic for determining whether
1021+
// pass this test. However, we only need a heuristic for determining whether
10221022
// +incompatible versions may be "latest", which is what this function is used
10231023
// for.
10241024
//

src/cmd/go/internal/modload/vendor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ func readVendorList(mainModule module.Version) {
9898
continue
9999
}
100100

101-
if annonations, ok := strings.CutPrefix(line, "## "); ok {
101+
if annotations, ok := strings.CutPrefix(line, "## "); ok {
102102
// Metadata. Take the union of annotations across multiple lines, if present.
103103
meta := vendorMeta[mod]
104-
for _, entry := range strings.Split(annonations, ";") {
104+
for _, entry := range strings.Split(annotations, ";") {
105105
entry = strings.TrimSpace(entry)
106106
if entry == "explicit" {
107107
meta.Explicit = true
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Test that -modfile=path/to/go.mod is rejected in workspace mode.
2+
3+
! go list -m -modfile=./a/go.alt.mod
4+
stderr 'go: -modfile cannot be used in workspace mode'
5+
6+
env GOFLAGS=-modfile=./a/go.alt.mod
7+
! go list -m
8+
stderr 'go: -modfile cannot be used in workspace mode'
9+
10+
-- go.work --
11+
go 1.20
12+
13+
use (
14+
./a
15+
)
16+
17+
-- a/go.mod --
18+
module example.com/foo
19+
20+
go 1.20
21+
22+
-- a/go.alt.mod --
23+
module example.com/foo
24+
25+
go 1.20
26+
27+
-- a/main.go --
28+
package main
29+
30+
import "fmt"
31+
32+
func main() {
33+
fmt.Println("Hello world!")
34+
}

0 commit comments

Comments
 (0)