dedupimport

command module
v0.0.0-...-d51e321 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2020 License: BSD-3-Clause Imports: 19 Imported by: 0

README

dedupimport

Build Status Godoc

Remove duplicate imports that have the same import path but different import names.

go get -u github.com/nishanths/dedupimport

See godoc for flags and usage.

Example

Given the file

package pkg

import (
	"code.org/frontend"
	fe "code.org/frontend"
)

var client frontend.Client
var server fe.Server

running dedupimport on it with default options

dedupimport file.go

will produce

package pkg

import (
	"code.org/frontend"
)

var client frontend.Client
var server frontend.Server

Documentation

Overview

Command dedupimport finds and removes duplicate imports that have the same import path but different import names. See 'dedupimport -h' for usage.

When resolving duplicate imports, by default, it keeps the unnamed import and removes the named imports. This behavior can be customized with the '-keep' flag (described below). After resolving duplicates it updates the rest of the code in the file that may be using the old, removed import identifier to use the new import identifier.

As a special case, the tool never removes side-effect imports ("_") and dot imports ("."); these imports are allowed to coexist with regular imports, even if the import paths are duplicated.

The command exits with exit code 2 if the command was invoked incorrectly; 1 if there was an error while opening, parsing, or rewriting files; and 0 otherwise.

The typical usage is:

dedupimport file1.go dir1 dir2 # prints updated versions to stdout
dedupimport -w file.go         # overwrite original source file
dedupimport -d file.go         # display diff
dedupimport -l file.go dir     # list the filenames that have duplicate imports

Example

Given the file

package pkg

import (
	"code.org/frontend"
	fe "code.org/frontend"
)

var client frontend.Client
var server fe.Server

running dedupimport with default options will produce

package pkg

import (
	"code.org/frontend"
)

var client frontend.Client
var server frontend.Server

Strategy to use when resolving duplicates

The '-keep' flag allows you to choose which import to keep and which ones to remove when resolving duplicates in a file, aka the strategy to use:

  • the "unnamed" strategy keeps the unnamed import if one exists, or the first import otherwise;
  • the "named" strategy keeps the first-occurring shortest named import if one exists, or the first import otherwise;
  • the "comment" strategy keeps the first-occurring import with either a doc or a line comment if one exists, or the first import otherwise; and
  • the "first" strategy keeps the first import.

Inability to rewrite

Sometimes rewriting a file to use the updated import declaration can be unsafe. In the following example, it is not possible to safely change "u" -> "url" inside fetch because the identifier, url, already exists in the scope and does not refer to the import.

Such contrived scenarios rarely happen in practice. But if they do, the command prints a warning and skips the file.

import u "net/url"
import "net/url"

var google = url.QueryEscape("https://google.com/?q=something")

func fetch(url string) {
   u.Parse(url)
   ...
}

Package name guessing

For unnamed imports, the command has to guess the import's package name by looking at the import path. The package name is, in most cases, the basename of the import path. The command automatically handles patterns such as these:

Import path                            Package name    Notes
-----------------                      ------------    ---------------
github.com/foo/bar                     bar             Standard naming
github.com/foo/bar/v2                  bar             Remove go module version
gopkg.in/yaml.v2                       yaml            Remove version
github.com/nishanths/go-xkcd           xkcd            Remove 'go-' prefix
github.com/nishanths/lyft-go           lyft            Remove '-go' suffix

To instruct the command on how to handle more complicated patterns, the '-m' flag can be used. The format for the flag is:

importpath=packagename

The flag can be repeated multiple times to specify multiple mappings. For example:

dedupimport -m github.com/proj/serverimpl=server \
  -m github.com/priarie/go-k8s-client=clientk8s

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL