connectrpc / connect-es

The TypeScript implementation of Connect: Protobuf RPC that works.
https://connectrpc.com/
Apache License 2.0
1.39k stars 81 forks source link

Failing to generate proper types v1.4.0 #1215

Closed ZanzyTHEbar closed 2 months ago

ZanzyTHEbar commented 2 months ago

Describe the bug

  1. My connect project is generating typescript files for me, but the crearePromiseClient returns a Promise<unknown>.

Which doesn't make sense, as that should be passed down via the generic.

  1. My product_connect.ts file has multiple errors:

Module '"@bufbuild/protobuf"' has no exported member 'MethodKind'.

All I and O members have a 'X' only refers to a type, but is being used as a value here.

To Reproduce

Install the deps, spin up a base-bones project based on the example using the newest version of the protoc plugin.

Environment (please complete the following information):

If your problem is specific to bundling, please also provide the following information:

import { resolve } from 'path';
import { defineConfig } from 'vite';
import solidPlugin from 'vite-plugin-solid';
import devtools from 'solid-devtools/vite';

export default defineConfig({
  resolve: {
    alias: {
      '@interfaces': resolve(__dirname, './src/interfaces'),
      '@components': resolve(__dirname, './src/components'),
      '@redux': resolve(__dirname, './src/redux'),
      '@pages': resolve(__dirname, './src/pages'),
      '@styles': resolve(__dirname, './src/styles'),
      '@config': resolve(__dirname, './src/config'),
      '@src': resolve(__dirname, './src'),
      '@assets': resolve(__dirname, './assets'),
      '@hooks': resolve(__dirname, './src/utils/hooks'),
      '@static': resolve(__dirname, './src/static'),
      '@utils': resolve(__dirname, './src/utils'),
    },
  },
  plugins: [
    devtools(),
    solidPlugin(),
  ],
  server: {
    port: 3000,
    host: true,
    strictPort: true,
  },
  build: {
    target: 'esnext',
  },
});

Additional context Here are my relevant files:

buf.yaml


# For details on buf.yaml configuration, visit https://buf.build/docs/configuration/v2/buf-yaml
version: v2

lint: use:

package product.v1;

option go_package = "github.com/ZanzyTHEbar/goflexpro/pkgs/gen/product/v1";

message ProductDTO { int32 id = 1; string name = 2; string description = 3; double price = 4; string created_at = 5; }

message CreateProductRequest { repeated ProductDTO product = 1; }

message CreateProductResponse { repeated bool success = 1; }

message GetProductRequest { repeated int32 id = 1; }

message GetProductResponse { repeated ProductDTO product = 1; }

message UpdateProductRequest { repeated ProductDTO product = 1; }

message UpdateProductResponse { repeated bool success = 1; }

message DeleteProductRequest { repeated int32 id = 1; }

message DeleteProductResponse { repeated bool success = 1; }

service ProductService { rpc CreateProduct(CreateProductRequest) returns (CreateProductResponse); rpc GetProduct(GetProductRequest) returns (GetProductResponse); rpc UpdateProduct(UpdateProductRequest) returns (UpdateProductResponse); rpc DeleteProduct(DeleteProductRequest) returns (DeleteProductResponse); }

smaye81 commented 2 months ago

Hi @ZanzyTHEbar, it looks like you may be using Protobuf-ES v2.0 with Connect-ES v1.4.0. The 1.x versions of Connect-ES are not compatible with Protobuf-ES v2.0.

Since you are using local plugins, can you verify the version of @bufbuild/protobuf that you have in your package.json file? You should be able to fix it by pinning your version of that to ^1.10.0. Try that and let me know if it fixes your issue.

ZanzyTHEbar commented 2 months ago

I will do so. Will have to wait a few hours though, 12am for me atm ;)

Will let you know.

Thanks for the prompt response :)

Correct me if i am wrong, but isnt the newest version of connect-es at 1.4.0 - is there an intentional reason with Protobuf-es version incompatibility? Would be great if that was clearer in the documentation as well. For example, why v1.10.0? Is that the latest compatible version, or is there something special about it?

Anywho, will get back to you shortly.

ZanzyTHEbar commented 2 months ago

I have tried your suggestion

pnpm i @bufbuild/protoc-gen-es@^1.10.0 @bufbuild/buf@^1.39.0 @bufbuild/protobuf@^2.0.0 @connectrpc/protoc-gen-connect-es@^1.4.0

However, when i try to build my proto files i receive this error:

❯ make proto
Generating proto files using buf cli ...
Linting passed. Proceeding to generate code ...
protoc-gen-es: Cannot read properties of undefined (reading 'BIGINT')
Failure: plugin protoc-gen-es: exit status 1
make: *** [Makefile:63: proto] Error 1

I have changed nothing else about my proto file.

ZanzyTHEbar commented 2 months ago

If i update my protobuf-es to 2.0.0, i solve the BIGINT issue and my proto files build fine, I get my types. However, due to having a protobuf: ^1.10.0 i now get this error upon pnpm build:

error during build:
[commonjs--resolver] Missing "./codegenv1" specifier in "@bufbuild/protobuf" package
ZanzyTHEbar commented 2 months ago

For reference - here is my tsconfig.json

smaye81 commented 2 months ago

You also need @bufbuild/protobuf at 1.10.0 also:

pnpm i @bufbuild/protoc-gen-es@^1.10.0 @bufbuild/buf@^1.39.0 @bufbuild/protobuf@^1.10.0 @connectrpc/protoc-gen-connect-es@^1.4.0
ZanzyTHEbar commented 2 months ago

Okay, i uninstalled everything, pruned my cache, reinstalled everything.

✓ 143 modules transformed.
x Build failed in 326ms
error during build:
Could not resolve "./gen/status_pb.js" from "node_modules/.pnpm/@connectrpc+connect@1.4.0_@bufbuild+protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol-grpc/trailer-status.js"
file: /mnt/data/Projects/Personal/OpenSource/goflexpro/frontend/node_modules/.pnpm/@connectrpc+connect@1.4.0_@bufbuild+protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol-grpc/trailer-status.js
    at getRollupError (file:///mnt/data/Projects/Personal/OpenSource/goflexpro/frontend/node_modules/.pnpm/rollup@4.21.2/node_modules/rollup/dist/es/shared/parseAst.js:392:41)
    at error (file:///mnt/data/Projects/Personal/OpenSource/goflexpro/frontend/node_modules/.pnpm/rollup@4.21.2/node_modules/rollup/dist/es/shared/parseAst.js:388:42)
    at ModuleLoader.handleInvalidResolvedId (file:///mnt/data/Projects/Personal/OpenSource/goflexpro/frontend/node_modules/.pnpm/rollup@4.21.2/node_modules/rollup/dist/es/shared/node-entry.js:19255:24)
    at file:///mnt/data/Projects/Personal/OpenSource/goflexpro/frontend/node_modules/.pnpm/rollup@4.21.2/node_modules/rollup/dist/es/shared/node-entry.js:19215:26
 ELIFECYCLE  Command failed with exit code 1.

The correct versions are being used - but it is still not able to build. Seems its looking in the wrong directory?

ZanzyTHEbar commented 2 months ago

I apologize if this is not relevant to connect-es specifically or is a user error. I am just genuinely confused on what i could be doing wrong.

ZanzyTHEbar commented 2 months ago

I have updated my project to the syntax required, and everything in my intellisense resolves correctly. However, now i have this final error:

> vite build

vite v5.4.3 building for production...
✓ 143 modules transformed.
x Build failed in 320ms
error during build:
Could not resolve "./gen/status_pb.js" from "node_modules/.pnpm/@connectrpc+connect@1.4.0_@bufbuild+protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol-grpc/trailer-status.js"
file: /mnt/data/Projects/Personal/OpenSource/goflexpro/frontend/node_modules/.pnpm/@connectrpc+connect@1.4.0_@bufbuild+protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol-grpc/trailer-status.js
    at getRollupError (file:///mnt/data/Projects/Personal/OpenSource/goflexpro/frontend/node_modules/.pnpm/rollup@4.21.2/node_modules/rollup/dist/es/shared/parseAst.js:392:41)
    at error (file:///mnt/data/Projects/Personal/OpenSource/goflexpro/frontend/node_modules/.pnpm/rollup@4.21.2/node_modules/rollup/dist/es/shared/parseAst.js:388:42)
    at ModuleLoader.handleInvalidResolvedId (file:///mnt/data/Projects/Personal/OpenSource/goflexpro/frontend/node_modules/.pnpm/rollup@4.21.2/node_modules/rollup/dist/es/shared/node-entry.js:19255:24)
    at file:///mnt/data/Projects/Personal/OpenSource/goflexpro/frontend/node_modules/.pnpm/rollup@4.21.2/node_modules/rollup/dist/es/shared/node-entry.js:19215:26
 ELIFECYCLE  Command failed with exit code 1.
ZanzyTHEbar commented 2 months ago

I have checked the installed version of @connectrpc/connect - and there is indeed no gen folder in the dist directory. Not in esm or cjs. I am, of course, using esm for this project.

I found the file on the github: https://github.com/connectrpc/connect-es/tree/main/packages/connect/src/protocol-grpc/gen

However, i do not seem to have this directory at all

ZanzyTHEbar commented 2 months ago

I have no idea what i did - but i cleaned my cache again, removed my node_modules, etc, totally clean project and tried again ... and now everything works. I am totally confused. I would like to understand it. But .. for now .. i can say that the problem is "fixed".

smaye81 commented 2 months ago

Strange. Yeah, just to clarify things here:

Connect-ES is currently at v1.4.0, which is only compatible with v1.x versions of the following:

There is a v2 version of the above @bufbuild packages, but Connect-ES is not currently compatible with v2. So, if you are using Connect-ES with Protobuf-ES, you have to stick with v1.x versions of everything. For @bufbuild packages, the latest version is v1.10.0.

I'm not sure how your local deps got into that state, but there are corner cases where this could happen. Fwiw, when generating code, we recommend installing using the instructions here

ZanzyTHEbar commented 2 months ago

Strange. Yeah, just to clarify things here:

Connect-ES is currently at v1.4.0, which is only compatible with v1.x versions of the following:

  • @bufbuild/protobuf
  • @bufbuild/protoplugin
  • @bufbuild/protoc-gen-es

There is a v2 version of the above @bufbuild packages, but Connect-ES is not currently compatible with v2. So, if you are using Connect-ES with Protobuf-ES, you have to stick with v1.x versions of everything. For @bufbuild packages, the latest version is v1.10.0.

I'm not sure how your local deps got into that state, but there are corner cases where this could happen. Fwiw, when generating code, we recommend installing using the instructions here

This is indeed the instructions i followed. I used @latest at the end of all the install, however.

smaye81 commented 2 months ago

Gotcha. Yeah, @latest would cause this. We updated those docs to pin to the correct versions about a month ago. So, hopefully, it clears up confusion.

Going to close this PR, but if you notice anything else amiss, feel free to open another issue.