[go1.19] Limit augmentation to native files which need it #1267
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The part of augmentation that prunes imports based on usage was designed to handle removing imports specific to a type parameter. For example, the import
cmp
might only exist in a file because of the type parameter constraintcmp.Ordered
. Go doesn't like it if there are unused imports.However, the code written for pruning imports uses the given name or guesses at the package name via the given path. This does not work for any relative paths (e.g.
./..
), versioned packages (e.g.github.com/foo/bar/v2
where the package isbar
), or using thego.mod
to define a path for one package to an arbitrary path using areplace
. These problematic paths do not exist in the natives which are being altered by augmentation, but problematic paths may exist in customer code.Since we only really want to prune imports for files which are augmented, I'm adding some checks so that the prune imports code is only run when overlays exist and those overlays have changed the original file.
I also fixed an issue where a left over
break
was making only the first found directive based import be checked for and none of the following. Added a test that demonstrates the issue and checks the fix.