entc/load: better error reporting for import cycles (#3266)

This commit is contained in:
Ariel Mashraki
2023-01-22 18:36:22 +02:00
committed by GitHub
parent 8ca8f682a4
commit 6db3e0a59b
5 changed files with 189 additions and 3 deletions

View File

@@ -210,6 +210,23 @@ import _ "<project>/ent/runtime"
```
:::
#### Import Cycle Error
At the first attempt to set up schema hooks in your project, you may encounter an error like the following:
```text
entc/load: parse schema dir: import cycle not allowed: [ent/schema ent/hook ent/ ent/schema]
To resolve this issue, move the custom types used by the generated code to a separate package: "Type1", "Type2"
```
The error may occur because the generated code relies on custom types defined in the `ent/schema` package, but this
package also imports the `ent/hook` package. This indirect import of the `ent` package creates a loop, causing the error
to occur. To resolve this issue, follow these instructions:
- First, comment out any usage of hooks, privacy policy, or interceptors from the `ent/schema`.
- Move the custom types defined in the `ent/schema` to a new package, for example, `ent/schema/schematype`.
- Run `go generate ./...` to update the generated `ent` package to point to the new package. For example, `schema.T` becomes `schematype.T`.
- Uncomment the hooks, privacy policy, or interceptors, and run `go generate ./...` again. The code generation should now pass without error.
## Evaluation order
Hooks are called in the order they were registered to the client. Thus, `client.Use(f, g, h)`