Skip to content

proposal: cmd/go: make it easier to fork modules #74884

@josharian

Description

@josharian

Proposal Details

Forking a module is painful, because of intra-module import paths.

If I fork github.com/upstream/foo as github.com/josharian/foo, and I still want things to just work, I must not only change the go.mod module name, but I must also change every single import of github.com/upstream/foo/PACKAGE/PATH to be github.com/josharian/foo/PACKAGE/PATH.

This is a drag. Today, I need a two or three line change in a module. It should be trivial, a thing one just does, but instead will generate over 10x that in unrelated changes.

I have a simple proposal to ease this situation: in go.mod, as a special case, allow for replace directives to do a version-unqualified replacement to the current module.

An example will make this clearer.

-- go.mod --
module github.com/josharian/foo
-- p/p.go
package p
-- x.go --
package x

import _ "github.com/upstream/foo/p"

This does not build today, and illustrates the problem: I must change the import to github.com/josharian/foo/p for it to compile.

I propose that adding this line to go.mod should work:

replace github.com/upstream/foo => github.com/josharian/foo

This says: While compiling this module, when you see github.com/upstream/foo, get it from github.com/josharian/foo. And github.com/josharian/foo is right at hand.

If this worked, then forking a module would be exactly two changes, a module change and a replace directive.

Right now doing this yields this error message:

go.mod:5: replacement module without version must be directory path (rooted or starting with . or ..)

So I believe this should be fully backwards compatible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ProposalToolProposalIssues describing a requested change to a Go tool or command-line program.modules

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions