bufbuild / protobuf-es

Protocol Buffers for ECMAScript. The only JavaScript Protobuf library that is fully-compliant with Protobuf conformance tests.
Apache License 2.0
1.16k stars 69 forks source link

Incorrect dependency import path in generated code #1018

Closed VoyTechnology closed 3 days ago

VoyTechnology commented 3 days ago

Using v2.2.0, when generating the import paths are relative rather than absolute, causing errors.

(Service name redacted and replaced with service)

# buf.gen.yaml
inputs:
  - directory: proto
plugins:
  - remote: buf.build/bufbuild/es:v2.2.0
    out: proto
    opt: target=js+dts
# buf.yaml
deps:
  - buf.build/googleapis/googleapis
// service.proto
import "google/api/annotations.proto";
import "google/api/field_behavior.proto";
// @generated by protoc-gen-es v2.2.0 with parameter "target=js+dts"
// @generated from file service/v1/service.proto (package service.v1, syntax proto3)
/* eslint-disable */

import { fileDesc, messageDesc, serviceDesc } from "@bufbuild/protobuf/codegenv1";
import { file_google_api_annotations } from "../../google/api/annotations_pb";
import { file_google_api_field_behavior } from "../../google/api/field_behavior_pb";

Error Log when running npm run start in a plain React web app:

ERROR in ../proto/service/v1/service_pb.js 8:0-78
Module not found: Error: Can't resolve '../../google/api/annotations_pb' in '/Users/voy/code/<project>/proto/service/v1'
ERROR in ../proto/service/v1/service_pb.js 9:0-84
Module not found: Error: Can't resolve '../../google/api/field_behavior_pb' in '/Users/voy/code/<project>/proto/service/v1'

Briefly looking through the codebase, I don't see any tests which use dependencies. If that is missing, perhaps it should be part of the standard test suite? Happy to provide more information if needed.

timostamm commented 3 days ago

The generated imports are correct, but the files being imported are missing.

You can generate code for the (dependency) imports with this option:

# buf.gen.yaml
inputs:
  - directory: proto
plugins:
  - remote: buf.build/bufbuild/es:v2.2.0
    out: proto
    opt: target=js+dts
+   include_imports: true
kakuiho commented 3 days ago

The generated imports are correct, but the files being imported are missing.

You can generate code for the (dependency) imports with this option:

# buf.gen.yaml
inputs:
  - directory: proto
plugins:
  - remote: buf.build/bufbuild/es:v2.2.0
    out: proto
    opt: target=js+dts
+   include_imports: true

Request for add new option to support ignore import and do not generate its related code.

We don't need to generate code for import module in some cases: https://github.com/tbonesoft/protoc-gen-go-gorm2/blob/v24.12/examples/bookstore/proto/bookstore/v1/bookstore.proto#L26

timostamm commented 3 days ago

@kakuiho, can you file a separate issue with more details? Thanks!