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: avoid duplicate import aliases #2502

Merged
merged 11 commits into from
Aug 28, 2024
Merged

fix: avoid duplicate import aliases #2502

merged 11 commits into from
Aug 28, 2024

Conversation

matt2e
Copy link
Collaborator

@matt2e matt2e commented Aug 26, 2024

fixes #2469
When scaffolding imports for external go packages, FTL will use the shortest unique part of the path as the alias. This allows package aliases to have neat and expected aliases in simple cases, and falling back to the next best option as conflicts arise.

For example:

import (
    unique "github.com/two2/foo/unique.Type"
    one_foo_bar "github.com/one/foo/bar.Type"
    two_foo_bar "github.com/two/foo/bar.Type"
)

@matt2e matt2e added the run-all A PR with this label will run the full set of CI jobs in the PR rather than in the merge queue label Aug 26, 2024
@ftl-robot ftl-robot mentioned this pull request Aug 26, 2024
@matt2e matt2e force-pushed the matt2e/unique-import-aliases branch 3 times, most recently from 0006a28 to a0cd423 Compare August 27, 2024 06:45
@matt2e matt2e marked this pull request as ready for review August 27, 2024 06:46
@matt2e matt2e requested a review from alecthomas as a code owner August 27, 2024 06:46
@matt2e matt2e requested review from a team and gak and removed request for a team August 27, 2024 06:46
@matt2e matt2e enabled auto-merge August 27, 2024 23:48
@matt2e matt2e added this pull request to the merge queue Aug 27, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 28, 2024
// returns the import path and directory name for an external type
// package and directory names are the same (dir=bar, pkg=bar): "github.com/foo/bar.A" => "github.com/foo/bar", none
// package and directory names differ (dir=bar, pkg=baz): "github.com/foo/bar.baz.A" => "github.com/foo/bar", "baz"
func goImportFromQualifiedName(qualifiedName string) (importPath string, directoryName optional.Option[string], err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, named return signature helps heaps with readability in this case.

}

case *schema.TypeAlias:
if !aliasesMustBeExported || n.IsExported() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure about the context here, so I'm probably wrong here, but thought I'd flag it in case.

Reading this if statement makes me think this block is for exported aliases, but its checking for when aliases must not be exported? Should it be aliasesMustBeExported || n.IsExported()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think what I want is:

if aliasesMustBeExported && !n.IsExported() {
  // early exit
}

When i wrote the original line, it also struck me as odd / confusing, so I'll replace it with the above as it's a lot clearer

Copy link
Contributor

@gak gak left a comment

Choose a reason for hiding this comment

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

👍

@matt2e matt2e force-pushed the matt2e/unique-import-aliases branch from 82cbce9 to 67d63ce Compare August 28, 2024 03:20
@matt2e matt2e enabled auto-merge August 28, 2024 03:20
@matt2e matt2e added this pull request to the merge queue Aug 28, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 28, 2024
@matt2e matt2e added this pull request to the merge queue Aug 28, 2024
Merged via the queue into main with commit b2c4226 Aug 28, 2024
84 checks passed
@matt2e matt2e deleted the matt2e/unique-import-aliases branch August 28, 2024 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
run-all A PR with this label will run the full set of CI jobs in the PR rather than in the merge queue
Projects
None yet
3 participants