This repository was archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
[WIP] Report Ineffectual Rules #1494
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a25a9ba
Add DirectDependencies method on project
JackyChiu d855d65
Refactor to use new DirectDependencies func
JackyChiu 0a3251f
Add package rule validation
JackyChiu 1149dea
Don't validate package rules on dep status
JackyChiu baf1731
Add pr improvements to the changelog
JackyChiu 29857b2
Address PR comments:
JackyChiu 6651d40
- make errInefficientRules it's own err type
JackyChiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,6 +324,90 @@ func ValidateProjectRoots(c *Ctx, m *Manifest, sm gps.SourceManager) error { | |
return valErr | ||
} | ||
|
||
type errInefficientRules struct { | ||
constraints []gps.ProjectRoot | ||
ignores []gps.ProjectRoot | ||
} | ||
|
||
func (e errInefficientRules) notEmpty() bool { | ||
return len(e.constraints) > 0 || len(e.ignores) > 0 | ||
} | ||
|
||
func (e errInefficientRules) Error() string { | ||
var err bytes.Buffer | ||
if len(e.constraints) > 0 { | ||
err.WriteString("The following constraints aren't imported:\n\n") | ||
for _, pr := range e.constraints { | ||
str := fmt.Sprintf(" ✗ %s\n", pr) | ||
err.WriteString(str) | ||
} | ||
} | ||
if len(e.ignores) > 0 { | ||
err.WriteString("The following ignored packages aren't imported:\n\n") | ||
for _, pr := range e.ignores { | ||
str := fmt.Sprintf(" ✗ %s\n", pr) | ||
err.WriteString(str) | ||
} | ||
} | ||
return err.String() | ||
} | ||
|
||
// ValidatePackageRules ensure that there are no ineffectual package declarations | ||
// in the constraints, overrides, and ignored rules in the manifest. | ||
func ValidatePackageRules(proj *Project, sm gps.SourceManager) error { | ||
directDeps, err := proj.DirectDependencies(sm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var errRules errInefficientRules | ||
ic := findIneffectualConstraints(proj.Manifest, directDeps) | ||
errRules.constraints = append(errRules.constraints, ic...) | ||
|
||
ig := findIneffectualIgnores(proj.Manifest, directDeps) | ||
errRules.ignores = append(errRules.ignores, ig...) | ||
|
||
if errRules.notEmpty() { | ||
return errRules | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// findIneffectualConstraints looks for constraints decleared in the project | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. decleared |
||
// manifest that aren't directly used in the project. | ||
func findIneffectualConstraints(m *Manifest, directDeps map[string]bool) []gps.ProjectRoot { | ||
ineffectuals := make([]gps.ProjectRoot, 0) | ||
// Check constraints in the manifest. | ||
for pr := range m.DependencyConstraints() { | ||
if !directDeps[string(pr)] { | ||
ineffectuals = append(ineffectuals, pr) | ||
} | ||
} | ||
|
||
// Check overrides in the manifest. | ||
for pr := range m.Overrides() { | ||
if !directDeps[string(pr)] { | ||
ineffectuals = append(ineffectuals, pr) | ||
} | ||
} | ||
|
||
return ineffectuals | ||
} | ||
|
||
// findIneffectualIgnores looks for ignored packages decleared in the project | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. declared |
||
// manifest that aren't directly used in the project. | ||
func findIneffectualIgnores(m *Manifest, directDeps map[string]bool) []gps.ProjectRoot { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not possible to determine if ignores are ineffectual at this point |
||
ineffectuals := make([]gps.ProjectRoot, 0) | ||
ignoredPkgs := m.IgnoredPackages() | ||
for _, pr := range ignoredPkgs.ToSlice() { | ||
if !directDeps[pr] { | ||
ineffectuals = append(ineffectuals, gps.ProjectRoot(pr)) | ||
} | ||
} | ||
return ineffectuals | ||
} | ||
|
||
// readManifest returns a Manifest read from r and a slice of validation warnings. | ||
func readManifest(r io.Reader) (*Manifest, []error, error) { | ||
buf := &bytes.Buffer{} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
ineffectual, not inefficient