Skip to content

Commit 097b716

Browse files
ConradIrwinmatloob
authored andcommitted
cmd/mod/edit: disallow relative tool paths
Allowing relative paths in `go.mod` introduced an inconsistency as we do not allow relative package paths anywhere else. For golang#48429 Change-Id: I5ef88aec4fe35f7e94a0cf6288e94099f3ca7a0e Reviewed-on: https://go-review.googlesource.com/c/go/+/614555 Reviewed-by: Sam Thanawalla <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 402dc98 commit 097b716

File tree

2 files changed

+2
-34
lines changed

2 files changed

+2
-34
lines changed

src/cmd/go/internal/modcmd/edit.go

+2-21
Original file line numberDiff line numberDiff line change
@@ -339,25 +339,6 @@ func parsePath(flag, arg string) (path string) {
339339
return path
340340
}
341341

342-
// parsePath parses -flag=arg expecting arg to be path to a tool (allows ./)
343-
func parseToolPath(flag, arg string) (path string) {
344-
if strings.Contains(arg, "@") {
345-
base.Fatalf("go: -%s=%s: need just path, not path@version", flag, arg)
346-
}
347-
if arg == "." {
348-
return arg
349-
}
350-
toCheck := arg
351-
if strings.HasPrefix(arg, "./") {
352-
toCheck = arg[2:]
353-
}
354-
if err := module.CheckImportPath(toCheck); err != nil {
355-
base.Fatalf("go: -%s=%s: invalid path: %v", flag, arg, err)
356-
}
357-
358-
return arg
359-
}
360-
361342
// parsePathVersionOptional parses path[@version], using adj to
362343
// describe any errors.
363344
func parsePathVersionOptional(adj, arg string, allowDirPath bool) (path, version string, err error) {
@@ -547,7 +528,7 @@ func flagDropRetract(arg string) {
547528

548529
// flagTool implements the -tool flag.
549530
func flagTool(arg string) {
550-
path := parseToolPath("tool", arg)
531+
path := parsePath("tool", arg)
551532
edits = append(edits, func(f *modfile.File) {
552533
if err := f.AddTool(path); err != nil {
553534
base.Fatalf("go: -tool=%s: %v", arg, err)
@@ -557,7 +538,7 @@ func flagTool(arg string) {
557538

558539
// flagDropTool implements the -droptool flag.
559540
func flagDropTool(arg string) {
560-
path := parseToolPath("droptool", arg)
541+
path := parsePath("droptool", arg)
561542
edits = append(edits, func(f *modfile.File) {
562543
if err := f.DropTool(path); err != nil {
563544
base.Fatalf("go: -droptool=%s: %v", arg, err)

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

-13
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ cd $WORK/h
102102
cp go.mod.start go.mod
103103
go mod edit -tool example.com/tool
104104
cmpenv go.mod go.mod.edit
105-
go mod edit -tool ./local
106-
cmpenv go.mod go.mod.edit2
107-
go mod edit -droptool ./local
108-
cmpenv go.mod go.mod.edit
109105
go mod edit -droptool example.com/tool2
110106
cmpenv go.mod go.mod.edit
111107
go mod edit -droptool example.com/tool
@@ -386,12 +382,3 @@ module g
386382
go 1.24
387383

388384
tool example.com/tool
389-
-- $WORK/h/go.mod.edit2 --
390-
module g
391-
392-
go 1.24
393-
394-
tool (
395-
./local
396-
example.com/tool
397-
)

0 commit comments

Comments
 (0)