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

Adding dependencies #1

Open
dmateusp opened this issue Apr 27, 2023 · 12 comments
Open

Adding dependencies #1

dmateusp opened this issue Apr 27, 2023 · 12 comments

Comments

@dmateusp
Copy link

hey @cloneable thanks for sharing this!

I've been using it for a while, now I wanted to use the Ent approach described here to have a Proto object store/retreived from the DB easily.

To do that, I need to import my proto generated type into my schema file, but entc doesn't seem to see my dependency at all:

entc generation failed, entc/load: parse schema dir: /redacted/schema/myschema.go:8:6: could not import github.com/my/repo/proto/myproto (invalid package name: "")

I've tried to change the rule but I'm a total beginner at writing Bazel rules so I haven't managed, do you have an idea how this could be supported?

@cloneable
Copy link
Owner

Hi @dmateusp, I'm glad you like it. I must confess I haven't looked at this in a long while and also moved mostly to rust these days, though maybe I can still help you. Looks like the go_proto_library is not properly handled. I think I can take a closer look soon.
You wrote the myproto.proto manually and even defined a go_proto_library? Or do you use entproto or sth else to generate the protobuf schema?

@dmateusp
Copy link
Author

I keep hearing good things about Rust :)

The proto is created manually, it's built like this:

proto_library(
    name = "myproto_proto",
    srcs = ["myproto.proto"],
    deps = [
    ],
)

go_proto_library(
    name = "myproto_go_proto",
    importpath = "github.com/my/repo/proto/myproto",
    proto = ":myproto_proto",
    deps = [
    ],
)

But when I import myproto_go_proto in my schema file the Ent generate breaks with the message above.

I tried to debug by listing the path, and it doesn't seem like any of the dependencies are available to entc

@cloneable
Copy link
Owner

Then what you have to do is add myproto_go_proto to deps of the go_ent_library, but I did not plan for further dependencies and go_ent_library doesn't accept deps. This needs to be changed.
Your proto lib needs to make it into this list of deps:

rules_ent/defs.bzl

Lines 220 to 223 in 7f43ad5

deps = [
":" + name + "_predicate",
":" + name + "_migrate",
] + default_deps + [":" + name + "_" + entity for entity in entities],

... via parameter of the go_ent_library macro:

go_ent_library(
  ...
  deps = [
    ":myproto_go_proto", 
  ],
)

@dmateusp
Copy link
Author

I have tried that :/, but it still isn't available to

ctx.actions.run_shell(

@cloneable
Copy link
Owner

Would you happen to have a simplified example project that I can clone and test with? I can't seem to find my old project at the moment. (Probably only on backup after I switched machines last year.)

@dmateusp
Copy link
Author

I've added a PR with an example that reproduces it

@cloneable
Copy link
Owner

Thanks for the example! I took a first look and, yes, you're right the deps are not available to the run_shell action. ctx.attr.schema[GoSource].deps needs to be added to the GOPATH of the generate tool. I'm thinking about how to do that. Maybe recursively list the source files of all deps and copy or symlink them.

@dmateusp
Copy link
Author

dmateusp commented May 1, 2023

I think that's what this rule is for: https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/rules.md#go_path but I didn't manage to get it working!

@cloneable
Copy link
Owner

Nice find! See latest commit in main: 3041436
Worked for me locally. Let me know if this works for you, too. Fyi, the previous commit also updates the dependencies.

@dmateusp
Copy link
Author

dmateusp commented May 1, 2023

I rebased and updated the PR, but I wasn't able to build locally still :/ don't you get the same error with

❯ bazel build //...
INFO: Analyzed 16 targets (182 packages loaded, 11170 targets configured).
INFO: Found 16 targets...
ERROR: /Users/dmateusp/code/rules_ent/example/schema/BUILD.bazel:18:15: Generating Ent files in bazel-out/darwin-fastbuild/bin/example/schema failed: (Exit 1): bash failed: error executing command (from target //example/schema:db_generate) /bin/bash -c ... (remaining 6 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
2023/05/01 17:14:46 entc failed: entc/load: parse schema dir: /private/var/tmp/_bazel_dmateusp/bcd25fcbe2b4fbce9df1fa3134e8a311/sandbox/darwin-sandbox/125/execroot/com_github_cloneable_rules_ent/example/schema/user.go:7:2: could not import github.com/cloneable/rules_ent/example/protos (invalid package name: "")

?

@cloneable
Copy link
Owner

Sorry, while trying out a few things yesterday I noticed that this happens with any transitive dependency and I replaced the proto lib with a regular Go package for faster testing. I did not revert that to test this, but now I know what's wrong: Because the .pb.go file is generated it's not listed like a regular source file in the tree, but under the target dir that produced it: ./bazel-out/darwin_arm64-fastbuild/bin/example/protos/user_settings_go_proto_/github.com/cloneable/rules_ent/example/protos/user_settings.pb.go Makes sense. I can tell go_path to simply zip all needed files. Unzipping that gives me a usable package layout. However, I'd like to try to do this without creating an archive. I will report back soon.

@dmateusp
Copy link
Author

@cloneable could you push the version that works with the zip step maybe? (even if it's on a branch)
I'd be interested in using that patch in the meantime

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

No branches or pull requests

2 participants