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

cmd/go: go.mod replace directive with local path containing '@' #61500

Closed
inianv opened this issue Jul 21, 2023 · 6 comments
Closed

cmd/go: go.mod replace directive with local path containing '@' #61500

inianv opened this issue Jul 21, 2023 · 6 comments
Labels
GoCommand cmd/go help wanted modules NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@inianv
Copy link

inianv commented Jul 21, 2023

What version of Go are you using (go version)?

$ go version
go version go1.20.2 darwin/arm64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/ivasanth/Library/Caches/go-build"
GOENV="/Users/ivasanth/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/ivasanth/go/pkg/mod"
GONOPROXY="github.com/iceye-ltd"
GONOSUMDB="github.com/iceye-ltd"
GOOS="darwin"
GOPATH="/Users/ivasanth/go"
GOPRIVATE="github.com/iceye-ltd"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/ivasanth/GitHub/features/capture/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/pd/wwbhc60s0zlbnnzh7rj73r600000gq/T/go-build2792899238=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Using the replace directive on go.mod with a local FilePath containing an @ character is breaking the module path. Per the pkg docs, EBNF for FilePath says platform specific relative or absolute file path.

ReplaceDirective = "replace" ( ReplaceSpec | "(" newline { ReplaceSpec } ")" newline ) .
ReplaceSpec = ModulePath [ Version ] "=>" FilePath newline
            | ModulePath [ Version ] "=>" ModulePath Version newline .
FilePath = /* platform-specific relative or absolute file path */

On macOS, @ is a valid filesystem path. So expecting a replace command like below should work (for test purposes). Assuming the path ./local/@path/tools contains a valid go.mod

go mod edit -replace=golang.org/x/tools=./local/@path/tools

What did you expect to see?

The filepath should be treated as valid and be present in go.mod as

replace golang.org/x/tools => ./local/@path/tools

What did you see instead?

But the above produces a broken path in go.mod as

replace golang.org/x/tools => ./local/ path/tools
@bcmills bcmills added help wanted NeedsFix The path to resolution is known, but the work has not been done. GoCommand cmd/go modules labels Jul 21, 2023
@bcmills bcmills added this to the Backlog milestone Jul 21, 2023
@bcmills
Copy link
Contributor

bcmills commented Jul 21, 2023

Thanks for reporting. Want to send a fix?

@seankhliao seankhliao changed the title go.mod replace directive with local path containing '@' cmd/go: go.mod replace directive with local path containing '@' Jul 21, 2023
@inianv
Copy link
Author

inianv commented Jul 21, 2023

Thanks for reporting. Want to send a fix?

I can certainly give it a shot @bcmills

@gopherbot
Copy link

Change https://go.dev/cl/512975 mentions this issue: cmd/go: fix issue with replace file path containing '@' character

@inianv
Copy link
Author

inianv commented Jul 25, 2023

Along with the fix for the original problem reported, the changes also address an the error message when the @ char is present in the replace directive's new path, if specified as a import path. Though the @ character is not allowed in an import path, it throws an incorrect error, i.e.

go mod edit -replace=golang.org/x/tools=github.com/@x/tools@v2
go: -replace=golang.org/x/tools=github.com/@x/tools@v2: invalid new path: malformed import path "github.com/": trailing slash

It should have ideally warned about the @ character present in the module path. With the current CL, it rightly updates the error as

go: -replace=golang.org/x/tools=github.com/@x/tools@v2: invalid new path: malformed import path "github.com/@x/tools": invalid char '@'

@inianv
Copy link
Author

inianv commented Jul 28, 2023

Change https://go.dev/cl/512975 mentions this issue: cmd/go: fix issue with replace file path containing '@' character

@bcmills I hope I was able to solve the problem. Do take a look at it in your own time. Thanks

@gopherbot
Copy link

Change https://go.dev/cl/538916 mentions this issue: cmd/go: handle '@' in local path when running 'go mod edit -replace'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go help wanted modules NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants