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

Generated code not importing from buf for a dependency #947

Closed
jlewi opened this issue Jul 29, 2024 · 2 comments
Closed

Generated code not importing from buf for a dependency #947

jlewi opened this issue Jul 29, 2024 · 2 comments

Comments

@jlewi
Copy link

jlewi commented Jul 29, 2024

I'm using buf to define some protos which depend on the module buf.build/stateful/runme.
My expectation is that the generated code would import the stateful typescript client i.e. something like

import { Cell, Notebook }  from @buf/stateful_runme.bufbuild_es@latest

But that's not what's happening. Instead its using an import

import { Cell, Notebook } from "../../runme/parser/v1/parser_pb";

That suggests its trying to generate the SDK for the dependency rather than importing the SDK for the dependency.

Here is my buf.yaml

I was originally using v1 but migrated to v2 to try to experiment with managed mode. But then I realized I didn't know what option to set to control the import used for a proto. Is there an option similar to go_package that controls what the import for the proto would be?

The stateful/runme protos I want to depend on are defined here.

I looked at the example and it seems like it correctly generates the import for the google timestamp proto here. I was confused about how thats working since I couldn't find a buf.yaml file in that repository.

@timostamm
Copy link
Member

Hey Jeremy, generated SDKs are great if you only need the generated code, and do not want to generate code yourself. In your case, the most simple solution is to include imports when generating:

  - local: protoc-gen-connect-es
+   include_imports: true
    opt:
      - target=ts

This will generate just the files you need from buf.build/stateful/runme.

Unfortunately, Protobuf does not define an equivalent to the go_package option for ECMAScript. If there was one, buf.build/stateful/runme could use it to point to the SDK.

We do have a mechanism to do something equivalent - the plugin option rewrite_imports:

  - local: protoc-gen-connect-es
    opt:
      - target=ts
+     - rewrite_imports=./runme/**/*_pb.js:@buf/stateful_runme.bufbuild_es

This option will change the generated imports as follows:

- import { Cell, Notebook } from "../../runme/parser/v1/parser_pb";
+ import { Cell, Notebook } from "@buf/stateful_runme.bufbuild_es/runme/parser/v1/parser_pb";

It's reliable and stable, but you may run into issues with bundlers because of things like import extensions. The ecosystem is in flux, and no single way of doing things exists yet.

The message google.protobuf.Timestamp is part of the "well-known types" - the standard lib of Protobuf. They are special cased. They ship with the compiler, and we ship pre-generated code with the runtime.

@jlewi
Copy link
Author

jlewi commented Jul 31, 2024

The rewriter_imports issue solved my issue. Thank you very much.

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