Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple time imports #659

Closed
wants to merge 8 commits into from
Closed

Conversation

pagran
Copy link
Member

@pagran pagran commented Jan 28, 2023

Marking imports as used must consider its name and alias for correct processing of multiple time import

Fix: #658

@pagran pagran requested a review from mvdan January 28, 2023 12:43
testdata/script/literals.txtar Outdated Show resolved Hide resolved
main.go Outdated Show resolved Hide resolved
main.go Show resolved Hide resolved
Copy link
Member

@mvdan mvdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just some last nits.

@@ -357,6 +418,18 @@ package imp_type

type DotImportedType string



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid this many empty lines; one at a time should be enough.

)

func regularAndUnusedDotImport() {
const localConst = imp_mult.MultImpStr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this how you're keeping one of the imports used? it's a bit fragile, because we might end up "inlining" or simplifying all constant declarations in the future as part of obfuscation. it's probably better to have a real use via a func call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const is specially chosen because if i pass it as a parameter, then literals will just inline it and obfuscate it :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I mean is to have each of these imported packages declare a dummy func, and the importer package imports the func to have a "used" import that remains used even with -literals.

if !ok {
return true
}

if pkg := uses.Pkg(); pkg != nil {
usedImports[pkg.Path()] = true
// Handle qualified identifiers, cover renamed and simple imports
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Handle qualified identifiers, cover renamed and simple imports
// An identifier that directly references an imported package; cannot be a dot-import.
// For example, this is the identifier `pkgfoo` in the code `var _ = pkgfoo.Bar`.

Comment on lines +344 to +353
func multipleCheck(vals ...string) {
if len(vals) < 2 {
return
}
for i := 0; i < len(vals)-1; i++ {
if vals[i] != vals[i+1] {
panic("vals must be equals")
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this is necessary; if constant values were broken, our existing tests would already catch that :)

Comment on lines +1710 to +1711
// Handle dot-imported declarations
// If identifier is right part of SelectorExpr, it means it is a regular or renamed import
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Handle dot-imported declarations
// If identifier is right part of SelectorExpr, it means it is a regular or renamed import
// Only dot-imported packages remain to be tracked.
// If the identifier is the right part of a selector expression, like `bar` in `var _ = foo.bar`,
// then it cannot be an identifier from a dot-imported package.

return true
}

// If package is missing or equal to current one it is not a dot-import case
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// If package is missing or equal to current one it is not a dot-import case
// An identifier from a dot-imported package belongs to a dependency package.

return true
}

// Verify that identifier is declared in another package
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Verify that identifier is declared in another package
// Finally, verify that the identifier is defined in its package scope.

Why is this last check needed, by the way?

@mvdan
Copy link
Member

mvdan commented Jan 31, 2023

We merged the other PR, and I agree the other approach is better :) Closing.

@mvdan mvdan closed this Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

-literals can still cause unused import errors
3 participants