Giter Club home page Giter Club logo

mod's Introduction

Mod

Command line tool to upgrade/downgrade Semantic Import Versioning in Go Modules

Motivation

There are two good use cases to do this:

  1. If you own a library and you want to introduce a breaking change, then you have to go around all your Go files and sub-packages and update the import paths to include v2, v3, etc. This tool just does it automatically with one command.

  2. If you own a library that is already tagged v2 or above but is incompatible with Semantic Import Versioning, then this tool can solve the problem for you with one command as well. Introduce a go.mod file with the correct import path, and just run mod upgrade once or mod -t=X upgrade (where x is the latest tag major) to update the import paths of your go files to match whatever tag you're at.

Install

go install github.com/marwan-at-work/mod/cmd/mod@latest

Usage

mod upgrade OR mod downgrade from any directory that has go.mod file.

The tool will update the major version in the go.mod file and it will traverse all the Go Files and upgrades/downgrades any imports as well.

For example, if you have the following package:

// go.mod

module github.com/me/example

require github.com/pkg/errors v0.8.0

that imports a sub directory in its Go files:

// example.go

package example

import (
    "github.com/me/example/subdir"
)

// rest of file...

You can run mod upgrade from the root of that directory and the above two files will be rewritten as such:

module github.com/me/example/v2

require github.com/pkg/errors
package example

import (
    "github.come/me/example/v2/subdir"
)

// rest of file...

You can of course, downgrade again or upgrade more incrementally.

You can also run this command inside the example folder and notice how the import paths and the module name alike get updated.

Upgrading a dependency

Say you are using a dependency like github.com/x/y/v2 and you want to use the new major version they just released. You don't want to change your own import paths, but you want to change the import paths of a dependency you're using from v2 to v3.

All you have to do is

mod upgrade --mod-name=github.com/x/y

Downgrading a dependency

Say you are using a dependency like github.com/x/y/v3 and you want to use the previous major version. You don't want to change your own import paths, but you want to change the import paths of a dependency you're using from v3 to v2.

All you have to do is

mod downgrade --mod-name=github.com/x/y

Status

Works as intended. Feel free to report any issues.

Troubleshooting

If you are using iTerm2 and see this error:

<html><head></head><body><p><em>Error:</em> Your request has found no candidate provider [hs="(null)", id="(null)"]</p></body</html>

Make sure you close iTerm2 and start again. See #3 for details.

mod's People

Contributors

danielfpferreira avatar dnwe avatar erjanmx avatar kk-zu avatar marwan-at-work avatar maxbrunet avatar mchill avatar nikhita avatar pascal-hofmann avatar powerman avatar sauerbraten avatar v1v avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mod's Issues

error trying to use `mod upgrade`

hi.. @marwan-at-work i'm trying to use a library that hasn't yet adopted modules (dgrijalva/jwt-go). so i forked the repo, cloned it and per the usage examples ran:

    mod upgrade

from the repo root directory (after running go mod init of course, so go.mod exists ..). on doing so, i get this error:

<html><head></head><body><p><em>Error:</em> Your request has found no candidate provider [hs="(null)", id="(null)"]</p></body></html>

needless to say, i do not know how to fix this issue. any help appreciated, thanks.

Problem updating a particular package

Hi!

I'm not sure this is the correct spot to be reporting this, but I am encountering an error trying to update github.com/google/go-github from v45.2.0 to v47.0.0:

$ go version
go version go1.18.5 linux/amd64
$ mod upgrade --mod-name=github.com/google/go-github/v45 -t=47
2022/08/29 19:51:17 Unexpected package creation during export data loading

I found golang/go#45584 which seems related.

Thank you!

'package "XXX" without types was imported from' Error message

Hello,

I'm not sure if I'm doing something wrong or if there's an incompatibility with Go 1.14 and this tool.

I installed with go get github.com/marwan-at-work/mod/cmd/mod

Steps to reproduce:

Error occurs:
2020/03/28 23:34:18 internal error: package "fmt" without types was imported from "github.com/jonbodner/taxes"

Also tried on v2 subdirectory:

  • cd v2
  • mod upgrade

Error occurs:
2020/03/28 23:37:57 internal error: package "errors" without types was imported from "github.com/jonbodner/taxes/v2"

Duplicate /v2 applied on some files

Hi!

I tried running mod on the grpc-gateway project and it produced some strange imports on some of the files:

examples/internal/server/echo.go:

package server

import (
	"context"

	"github.com/golang/glog"
	examples "github.com/grpc-ecosystem/grpc-gateway/v2/v2/examples/internal/proto/examplepb"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
)

This happened to a few of them, but we also got some even more interesting imports:
runtime/errors.go:

package runtime

import (
	"context"
	"io"
	"net/http"

	"github.com/grpc-ecosystem/grpc-gateway/v2/v2/v2/internal"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/grpclog"
	"google.golang.org/grpc/status"
)

Steps to reproduce:

  1. Check out master of grpc-gateway:
    $ git clone [email protected]:grpc-ecosystem/grpc-gateway.git
  2. Run mod upgrade
    $ mod upgrade

Provide prebuild binaries on releases

We use this tool at renovate as post upgrade step.
We like to download the tool as prebuild binary instead of compiling it on the fly to safe resources.

Currently we only need linux_amd64 but we'll need linux_arm64 soon.

//cc @rarkins

SetRequire called with conflicting versions

I am using Renovate which uses mod.
I am getting the following in both Renovate as well as locally:

mod upgrade --mod-name=gopkg.in/yaml.v2 -t=3
panic: SetRequire called with conflicting versions for path gopkg.in/yaml.v3 (latest and v3.0.1)

I am not really finding anything online indicating that others have had this issue before.
Any idea what might cause this?

Problem updating particular package

I am encountering the wrong output when updating the following packages from v1.104.0 to v2.3.0

github.com/pulumi/pulumi-azure-native-sdk/resources
github.com/pulumi/pulumi-azure-native-sdk/storage

The output is:

	github.com/pulumi/pulumi-azure-native-sdk/resources v2.3.0
	github.com/pulumi/pulumi-azure-native-sdk/storage v2.3.0

instead of

	github.com/pulumi/pulumi-azure-native-sdk/resources/v2 v2.3.0
	github.com/pulumi/pulumi-azure-native-sdk/storage/v2 v2.3.0

please update installation instructions for Go 1.17 and later

First, many thanks for your tool, as it helped me several times not only to upgrade some of my own packages from v0 to v2, but also dependencies from v1->v2, v3->v4, et cetera. Much better, more reliably, and less headaches than Ctrl-Shift-H (global replace) in VSCode.

The installation instructions in README.md are becomming outdated, as of Go 1.17 go get warns and of 1.18 blocks installation from Go modules. Please update the documentaion to use go install ...@latest.

Open source license

Would you consider adding an open source license in a top-level LICENSE file? I'm guessing your intent is for this tool to be open source, but it doesn't look like that's explicitly written anywhere.

Tool for client code?

If I upgrade a library to a new major release using "mod", what tool exist to upgrade import paths in client code?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.