1
0
mirror of https://github.com/golang/go synced 2024-10-01 16:28:33 -06:00
Commit Graph

13 Commits

Author SHA1 Message Date
Dmitri Shuralyov
49fb246514 refactor/rename: re-enable TestMoves on Windows
This CL is based on the work and discussion in CL 24969 and CL 32392.
It simplifies the code further and makes trybots pass on Windows.

Also update the documentation and implementation of the subpackages
function slightly to make it more clear what it's doing.

Background

CL 24943 has made progress on fixing rename.Move behavior on Windows,
but hadn't re-enabled TestMoves at the time. CL 24969 attempted to do
so, but the tests weren't passing and it didn't get merged at the time.

Since that time, CL 65672 has fixed a bug in the subpackages function.
As a result, it's possible to simplify code in Move now.

The subpackages function returns a set of packages in the given srcDir
whose import path equals to root or has "root/" as the prefix.
After CL 65672, it correctly filters out ones that are not true
sub-packages of root. That's why it's not necessary to try to append
a trailing separator when doing the string replacement in Move now.

Given subpackages reports their import paths (always '/'-separated),
not filesystem paths, it's not necessary to use filepath.ToSlash either.

Updates golang/go#16384

Change-Id: Ia808fbaaeffe44ea1e51d7cc3ed9499bc2b438cf
Reviewed-on: https://go-review.googlesource.com/c/161777
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2019-02-12 21:30:38 +00:00
Michael Fraenkel
af2bfe26b6 cmd/gomvpkg: Only process paths that are children
When determining children of a parent directory, look for the expected /
to avoid processing directories that look similar but are not direct
descendants.

Fixes golang/go#21991

Change-Id: I5c7d18076540e5588a856845f04645231856196f
Reviewed-on: https://go-review.googlesource.com/65672
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-01-17 19:58:32 +00:00
David R. Jenni
4968197297 x/tools/refactor/rename: fix nil pointer dereference.
Fix a nil pointer dereference by checking the error returned by
conf.Load.

Bug was reported on Reddit:
https://www.reddit.com/r/golang/comments/5vo0fb/

Change-Id: I2ad6f03922b772f03e282a0af6eea24f8de9cef8
Reviewed-on: https://go-review.googlesource.com/37432
Reviewed-by: Alan Donovan <adonovan@google.com>
2017-02-24 20:12:46 +00:00
Alan Donovan
9c3986db53 refactor/rename: fix two bugs related to MS Windows' path separator
In the package, the added import declarations used backslashes.
In the test, filenames in warning messages used backslashes.
Now both use forward slash.

Fixes golang/go#16384

Change-Id: I43116aab0b3209305f23ed9def7c4adf3259941e
Reviewed-on: https://go-review.googlesource.com/24943
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-07-15 14:58:04 +00:00
Brad Fitzpatrick
e047ae774b cmd/goimports, imports: make goimports great again
I felt the burn of my laptop on my legs, spinning away while processing
goimports, and felt that it was time to make goimports great again.

Over the past few years goimports fell into a slow state of disrepair
with too many feature additions and no attention to the performance
death by a thousand cuts. This was particularly terrible on OS X with
its lackluster filesystem buffering.

This CL makes goimports stronger, together with various optimizations
and more visibility into what goimports is doing.

* adds more internal documentation

* avoids scanning $GOPATH for answers when running goimports on a file
  under $GOROOT (for Go core hackers)

* don't read all $GOROOT & $GOPATH directories' Go code looking for
  their package names until much later. Require the package name of
  missing imports to be present in the last two directory path
  components.  Then only try importing them in order from best to
  worst (shortest to longest, as before), so we can stop early.

* when adding imports, add names to imports when the imported package name
  doesn't match the baes of its import path. For example:
        import foo "example.net/foo/v1"

* don't read all *.go files in a package directory once the first file
  in a directory has revealed itself to be a package we're not looking
  for. For example, if we're looking for the right "client" for "client.Foo",
  we used to consider a directory "bar/client" as a candidate and read
  all 50 of its *.go files instead of stopping after its first *.go
  file had a "package main" line.

* add some fast paths to remove allocations

* add some fast paths to remove disk I/O when looking up the base
  package name of a standard library import (of existing imports in a
  file, which are very common)

* adds a special case for import "C", to avoid some disk I/O.

* add a -verbose flag to goimports for debugging

On my Mac laptop with a huge $GOPATH, with a test file like:

	package foo
	import (
	       "fmt"
	       "net/http"
	)

	/*

	*/
	import "C"

	var _ = cloudbilling.New
	var _ = http.NewRequest
	var _ = client.New

... this took like 10 seconds before, and now 1.3 seconds. (Still
slow; disk-based caching can come later)

Updates golang/go#16367 (goimports is slow)
Updates golang/go#16384 (refactor TestRename is broken on Windows)

Change-Id: I97e85d3016afc9f2ad5501f97babad30c7989183
Reviewed-on: https://go-review.googlesource.com/24941
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-07-15 05:29:07 +00:00
Alan Donovan
b3887f7a17 refactor/rename: fix spurious conflict report when renaming x/foo to y/foo
There is no need to check for import conflicts when the package name
doesn't change.  Add test.

Also, when reporting a (non-spurious) import conflict, make clear that
the error is just a warning.  Add a test for same.

Change-Id: Idde0483b502cd041fd893230fec06b8533f54f3c
Reviewed-on: https://go-review.googlesource.com/24917
Reviewed-by: Michael Matloob <matloob@golang.org>
2016-07-14 18:43:43 +00:00
Alan Donovan
108b52ee61 cmd/gorename: -d flag prints diffs instead of rewriting files
The user may specify the diff tool using the -diffcmd flag.

+ test.

Also:
- eliminate redundant DryRun flag
- simplify Verbose messages using log.SetPrefix

Fixes issue #13355

Change-Id: I917edc73e31ddf0f5d5b9b30c43f826465529da1
Reviewed-on: https://go-review.googlesource.com/18208
Reviewed-by: Michael Matloob <matloob@golang.org>
2015-12-30 15:25:00 +00:00
David R. Jenni
13be4dfe75 refactor/mvpkg: rewrite import comments.
Fixes golang/go#10508.

Change-Id: Id9b0f12e1a81b3b16e167462fd3639a6c6c1e0bb
Reviewed-on: https://go-review.googlesource.com/15267
Reviewed-by: Alan Donovan <adonovan@google.com>
2015-10-06 18:22:23 +00:00
David R. Jenni
20d85c34f3 refactor/mvpkg: rewrite external test packages.
Rename the package name of the external test package, e.g. <from>_test to <to>_test.
Rewrite the import statements of external test packages which import the renamed package.

Fixes #10507

Change-Id: Iad702189469c54776e55ed4a821610bd9977618c
Reviewed-on: https://go-review.googlesource.com/12637
Reviewed-by: Alan Donovan <adonovan@google.com>
2015-07-30 21:17:38 +00:00
Alan Donovan
bdcea2c1b3 go/buildutil: use chan (not func) in the ForEachPackage API
The callbacks are intentionally concurrent, making this function very
easy to misuse (most clients so far have got it wrong, even my own).
Using a channel in the API makes the concurrency obvious, the
correct usage easy, and the client control flow simpler.

Change-Id: Ied38c3ed5c98b40eb1b322a984ed9dc092ac0918
Reviewed-on: https://go-review.googlesource.com/3250
Reviewed-by: Sameer Ajmani <sameer@golang.org>
2015-03-05 20:14:20 +00:00
Michael Matloob
d29758bc2e cmd/gomvpkg: fix import rewrites for top-level moved package
The top-level moved package was skipped when doing import rewrites.
Don't skip that package.

Fixes #9811

Change-Id: I1c524ed44606586b5231e5adb6168079aa0e0228
Reviewed-on: https://go-review.googlesource.com/4470
Reviewed-by: Alan Donovan <adonovan@google.com>
2015-02-11 15:58:22 +00:00
Alex Brainman
2ef5a0d23b refactor/rename: make tests pass on windows (fixes build)
- use import path not file path in go/buildutil.FakeContext OpenFile;
- use regexp to compare error messages in TestErrors, because
  they contain windows file paths;
- use OS file path (not unix path), when checking move results
  in TestMoves.

Change-Id: Ib62d344acb551fb612d8a0773ae1ab5f18341294
Reviewed-on: https://go-review.googlesource.com/3171
Reviewed-by: Alan Donovan <adonovan@google.com>
2015-01-23 05:30:55 +00:00
Michael Matloob
796e50ba32 cmd/mvpkg: a package moving tool
This change adds a command mvpkg that will move a given package and
update all its imports. It uses similar logic to gorename to update
the imports.

Change-Id: Iebbd0b4c93c2302b0a71c3b99c68f6778106012a
Reviewed-on: https://go-review.googlesource.com/1973
Reviewed-by: Alan Donovan <adonovan@google.com>
2015-01-14 17:37:29 +00:00