-
Notifications
You must be signed in to change notification settings - Fork 1k
internal/gps: Add prune functions to gps #1020
Conversation
cf31c33
to
dcd5efc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks correct. Just a few nits.
internal/gps/prune.go
Outdated
|
||
if err := pruneEmptyDirs(baseDir, logger); err != nil { | ||
return errors.Wrap(err, "failed to prune empty dirs") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this one extra, since there is one at the end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
// on the PruneOptions passed. | ||
// | ||
// A Lock must be passed if PruneUnusedPackages is toggled on. | ||
func Prune(baseDir string, options PruneOptions, l Lock, logger *log.Logger) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How fast is this? Would it be worthwhile to try to do some of the filepath.Walk
s concurrently and/or combine them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already asked about this in #952. I believe that we can merge them but I was wondering why they were separated in the first place.
Also, I still haven't done any benchmarking on this yet. I wanted to ensure that the behavior is correct first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we'll likely, though not certainly, be able to interlace the logic eventually - but i want to take it one step at a time. verification needs to be integrated first, which opens the door to partial writes of the tree. combining that with pruning will be fairly complicated, and keeping pruning standalone initially will help us walk before we run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we are talking about the same thing. I was, and I assume @jmank88, talking about the fact that we do multiple filepath.Walk
per prune option. Can we do one filepath.Walk
and calculate all the files to be pruned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ohhhhh, i see. sorry, i understand now. yeah, definitely different things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, so, yeah, we're definitely going to need to combine the filepath.Walk()
operations. maybe it's OK not to do it for this first PR, but the cost of even one walk will be order(s) of magnitude higher than any of the actual ops being performed within any of the walks.
internal/gps/prune.go
Outdated
if err := os.Remove(dir); err != nil { | ||
return err | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could call return deleteFiles(empty)
. Otherwise, can the loops be combined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should have been one loop.
internal/gps/prune.go
Outdated
|
||
if err := deleteFiles(files); err != nil { | ||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could just return deleteFiles(files)
.
internal/gps/prune.go
Outdated
|
||
if err := deleteFiles(files); err != nil { | ||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could just return deleteFiles(files)
.
f0ff2f5
to
6a9bc1e
Compare
// on the PruneOptions passed. | ||
// | ||
// A Lock must be passed if PruneUnusedPackages is toggled on. | ||
func Prune(baseDir string, options PruneOptions, l Lock, logger *log.Logger) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, so here's one big thing: this is currently focused on an entire dep/vendor tree. we need to split it up, so that it instead deals with a single project. this is arguably a better design in general, but whether you agree with that or not, our Gopkg.toml
-driven pruning implementation will need to allow different pruning options to be specified globally and per dep. that means calling this func with different PruneOptions
.
in terms of the interface, i think it may be as simple as changing from taking an l Lock
to an lp LockedProject
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think doing per-project prune options is a good thing, but I'm wondering what implications it might have.
I'll take an attempt at that tomorrow to see what I can do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the implications should be fairly contained, but i've not thought it all the way through. hopefully not too bad 😄
I'm back to working on this. Should update it today. |
9bd639a
to
ef5d7a6
Compare
@carolynvs Added some simple tests now. |
false, | ||
}, | ||
{ | ||
"nested-package", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
ef5d7a6
to
3492a04
Compare
|
||
var ( | ||
// licenseFilePrefixes is a list of name prefixes for license files. | ||
licenseFilePrefixes = []string{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest you include "contributors" and "authors" to the file list here or in legalFileSubstrings
. These files are legal in nature. For example, dep
's own AUTHORS file starts with
This source code refers to The Go Authors for copyright purposes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
internal/gps/prune.go
Outdated
// PruneUnusedPackages indicates if unused Go packages should be pruned. | ||
PruneUnusedPackages | ||
// PruneNonGoFiles indicates if non-Go files should be pruned. | ||
// LICENSE & COPYING files are kept for convience. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to update this note to include the contributors & authors files. also, not for convenience, but "to try to comply with legal requirements".
internal/gps/prune.go
Outdated
|
||
// pruneProject remove excess files according to the options passed, from | ||
// the lp directory in baseDir. | ||
func pruneProject(baseDir string, lp LockedProject, options PruneOptions, logger *log.Logger) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it's worth exporting this. even if we don't have immediate plans to use it in dep, it just feels shortsighted to only expose the top-level call.
internal/gps/prune.go
Outdated
if err != nil { | ||
return errors.Wrap(err, "unexpected error while calculating unused packages") | ||
} | ||
fmt.Println(pkg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debugging artifact?
internal/gps/prune.go
Outdated
return files, err | ||
} | ||
|
||
// isPreservedFile checks if the file name idicates that the file should be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment needs cleaning up, it seems duplicated right now. also s/idicates/indicates/
internal/gps/prune.go
Outdated
func collectUnusedPackagesFiles(projectDir string, unusedPackages map[string]struct{}) ([]string, error) { | ||
// TODO(ibrasho): is this useful? | ||
files := make([]string, 0, len(unusedPackages)) | ||
fmt.Println(unusedPackages) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debugging artifact
internal/gps/prune.go
Outdated
|
||
// collectGoTestsFile returns a slice contains all Go test files (any files | ||
// prefixed with _test.go) in baseDir. | ||
func collectGoTestsFile(baseDir string) ([]string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: s/TestsFile/TestFiles/
// on the PruneOptions passed. | ||
// | ||
// A Lock must be passed if PruneUnusedPackages is toggled on. | ||
func Prune(baseDir string, options PruneOptions, l Lock, logger *log.Logger) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, so, yeah, we're definitely going to need to combine the filepath.Walk()
operations. maybe it's OK not to do it for this first PR, but the cost of even one walk will be order(s) of magnitude higher than any of the actual ops being performed within any of the walks.
@@ -16,29 +16,27 @@ func stripVendor(path string, info os.FileInfo, err error) error { | |||
return err | |||
} | |||
|
|||
if info.Name() == "vendor" { | |||
if _, err := os.Lstat(path); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is it OK to drop this lstat?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was pretty much not doing anything. lstat
has the same error responses as stat
. If we already have a FileInfo
then stat
has succeeded.
Anyhow, I can restore it if we are unsure and don't want to break anything.
cc: @spenczar Any idea what's the original intent of this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ibrasho Just checking in to confirm that the original purpose of this got lost after a few rewrites of the surrounding code (including my own rewrites!). Your fix here looks correct to me. 👍
@sdboyer All the current rules are based on file names anyway so |
kicking CI to retest, that last one seems like an error? i'd like to merge this soon, even if it's sorta WIP, as i think we can improve as we go - in particular, i don't think we need to fix the |
bc90972
to
3690bd8
Compare
I've pushed a patch of changes. I'll keep the changes related to deduplicating |
3690bd8
to
6b8ca98
Compare
6b8ca98
to
e79c0bd
Compare
ahh, some new linting errors, as we added some linters - once we have fixes for those, i think we should be ready to go with this. |
I'm not sure these linter errors are valid...
|
56acbf0
to
45126f4
Compare
ahhhhh so the errors are at least in part because this predates #1157, which changed some of the logic. however, there is one valid complaint in there, which actually points to a real bug:
once we address that, we can merge - the other complaints will go away. (if we want to be really sure, though, we can rebase) |
Signed-off-by: Ibrahim AshShohail <[email protected]>
Signed-off-by: Ibrahim AshShohail <[email protected]>
Signed-off-by: Ibrahim AshShohail <[email protected]>
Signed-off-by: Ibrahim AshShohail <[email protected]>
45126f4
to
24bf56f
Compare
I just added that a few hours ago because I've rebased. Should be fixed and ready to merge now. |
8ad6e0e
to
2b7d61b
Compare
Signed-off-by: Ibrahim AshShohail <[email protected]>
2b7d61b
to
9b6892c
Compare
ok so it really seems like those travis linting failures must be erroneous, even though i can't immediately figure out how that's possible post-rebase. merging this now, and we'll keep an eye on subsequent PRs to see if we have a real problem or not. |
What does this do / why do we need it?
Add different functions to
gps
that implement different pruning modes.The different modes are as follows:
pruneNestedVendorDirs
: Delete any directory namedvendor
.pruneNonGoFiles
: Delete any file that doesn't end with.go
(will keep files matching common names for legal documents)pruneGoTestFiles
: Prune Go test files (any file ending with_test.go
)pruneUnusedPackages
: Delete all files in unused Go packages. Since we might import a sub package we only delete files and keep directories.What should your reviewer look out for in this PR?
Correctness.
Do you need help or clarification on anything?
Refer to https://github.com/sgotti/glide-vc/ for comparison and ideas.
Is adding an option to allow overriding deletion of specific files a good idea?
Which issue(s) does this PR fix?
Breaking down #952