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

declaring a typealias using a type from a package whose name is identical to the module name causes build errors #2469

Closed
mistermoe opened this issue Aug 21, 2024 · 0 comments · Fixed by #2502
Assignees

Comments

@mistermoe
Copy link
Collaborator

Issue

declaring the following type alias:

import "github.com/TBD54566975/tbdex-go/tbdex"

//ftl:typealias export
type TbdexMessage = tbdex.Message

and then using it in the tbdex module causes the module's codegen'd main.go to include two imports of the same name e.g.

package main

import (
	"context"

	"github.com/TBD54566975/tbdex-go/tbdex"

	"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
	"github.com/TBD54566975/ftl/common/plugin"
	"github.com/TBD54566975/ftl/go-runtime/ftl/reflection"
	"github.com/TBD54566975/ftl/go-runtime/server"

	"ftl/tbdex"
)

running ftl build produces the following error:

info:tbdex: # main
./main.go:30:2: tbdex redeclared in this block
	./main.go:23:2: other declaration of tbdex
./main.go:65:27: too many errors

renaming the import in the source file like so:

import tbdexsdk "github.com/TBD54566975/tbdex-go/tbdex"

//ftl:typealias export
type TbdexMessage = tbdexsdk.Message

produces a different error:

unsupported external type "github.com/TBD54566975/tbdex-go/tbdex.Message"; see FTL docs on using external types: tbd54566975.github.io/ftl/docs/reference/externaltypes/

Potential Solution

when code-gen'ing a module's main.go i think it might be helpful to generate package aliases using a pattern that has a very low likelihood of someone actually using. e.g. __tbdex "github.com/TBD54566975/tbdex-go/tbdex"

@github-actions github-actions bot added the triage Issue needs triaging label Aug 21, 2024
@ftl-robot ftl-robot mentioned this issue Aug 21, 2024
@matt2e matt2e self-assigned this Aug 23, 2024
@github-actions github-actions bot removed the triage Issue needs triaging label Aug 23, 2024
github-merge-queue bot pushed a commit that referenced this issue Aug 28, 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"
)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants