Search code examples
gogo-modules

How do I properly use recursive replace in golang's go.mod?


I want a project structure, looking like this:

src/github.com/myname/myproject/
- tl:
    - constructor
        - someghing.go
        - go.mod:
            module github.com/myname/myproject/tl/constructor

            require github.com/myname/myproject v0.0.0
            require github.com/myname/myproject/tl/types v0.0.0
            require github.com/myname/myproject/tl/types/account v0.0.0
            require github.com/myname/myproject/tl/types/auth v0.0.0
            require github.com/myname/myproject/tl/types/bots v0.0.0

            replace github.com/myname/myproject v0.0.0 => ./../..
            replace github.com/myname/myproject/tl/types v0.0.0 => ./../types
            replace github.com/myname/myproject/tl/types/account v0.0.0 => ./../types/account
            replace github.com/myname/myproject/tl/types/auth v0.0.0 => ./../types/auth
            replace github.com/myname/myproject/tl/types/channels v0.0.0 => ./../types/channels
    - crcs
        - someghing.go
        - go.mod:
            module github.com/myname/myproject/tl/crcs

    - functions
        - account
            - someghing.go
            - go.mod:
                module github.com/myname/myproject/tl/functions/account

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./../../types
        - auth
            - someghing.go
            - go.mod:
                module github.com/myname/myproject/tl/functions/auth

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./../../types
        - bots
            - someghing.go
            - go.mod:
                module github.com/myname/myproject/tl/functions/bots

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./../../types
        - someghing.go
        - go.mod:
            module github.com/myname/myproject/tl/functions

            require github.com/myname/myproject/tl/crcs v0.0.0
            require github.com/myname/myproject v0.0.0
            require github.com/myname/myproject/tl/types v0.0.0

            replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../crcs
            replace github.com/myname/myproject v0.0.0 => ./../..
            replace github.com/myname/myproject/tl/types v0.0.0 => ./../types
    - types
        - account
            - something.go
            - go.mod:
                module github.com/myname/myproject/tl/types/account

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./..

        - auth
            - something.go
            - go.mod:
                module github.com/myname/myproject/tl/types/auth

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./..
        - channels
            - something.go
            - go.mod:
                module github.com/myname/myproject/tl/types/channels

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./..
        - someghing.go
        - go.mod:
            module github.com/myname/myproject/tl/types

            require github.com/myname/myproject/tl/crcs v0.0.0
            require github.com/myname/myproject v0.0.0

            replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../crcs
            replace github.com/myname/myproject v0.0.0 => ./../..
- something.go
- go.mod:
    module github.com/myname/myproject

    [some external dependencies]
    require github.com/myname/myproject/tl/types v0.0.0
    replace github.com/myname/myproject/l/types v0.0.0 => ./tl/types

This does not work. I use JetBrains' IDE, which executes go list on all the (sub)modules, and tells me the following error:

go: github.com/myname/myproject/tl/[email protected] requires
    github.com/myname/myproject/tl/[email protected]: reading github.com/myname/myproject/tl/crcs/go.mod at revision tl/crcs/v0.0.0: unknown revision tl/crcs/v0.0.0

I don't understand why is it happening, it looks like it tries fetching module from repo, even though everything is replaced locally. I am open to suggestions, except changing project structure. I need to be able to use something like account.SomeType in future, so placing everything in one file would be painful.

Context: I'm building MTPROTO library for telegram, which has TL schema, where each "request"/"class" is in its own namespace (e.g. "account", "channels") (or in global namespace, for me it's functions and types accordingly) (Because there is over 1900 of those functions/classes, each having its own constructor and parametrs, putting everything in one module would be hard to use)

the files in TL folder are auto-generated GO code from TL schema.


Solution

  • Thanks for Dean for pointing out the fact that you can have multiple packages in a single module (how?). It solved my problem while still maintaining same structure.