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

feat: add support for absolute imports #1971

Open
david-luna opened this issue Feb 9, 2024 · 1 comment
Open

feat: add support for absolute imports #1971

david-luna opened this issue Feb 9, 2024 · 1 comment

Comments

@david-luna
Copy link

protobuf.js version: 7.2.6

The load method forces to use relative import for other `.proto` files where the spec doesn't explicitly say this.

Create a project with the following proto files

// file: org/common/common.proto
package org.common;
syntax = "proto3";

message CommonMessage {
    string common_field = 1; // becomes commonField
}
// file: org/test/test.proto
package org.test;
syntax = "proto3";

import "org/common/common.proto";

message TestMessage {
    string test_field = 1; // becomes testField
    repeated org.common.CommonMessage common_fields = 2;
}

and try to load test.proto in node

const {load} = require('protobufjs');

load(`${__dirname}/org/test/test.proto`, (err, root) => {
  if (err) {
    console.error(err);
  }

  console.log(root);
})

the load process tries to load common.proto resolving the import from the current location of test.proto file giving the following error

[Error: ENOENT: no such file or directory, open '/.../proto-load/org/test/org/common/common.proto'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/.../proto-load/org/test/org/common/common.proto'
}

not the import path being appended to the path of the current file being processed

Making the import relative import "../common/common.proto" makes the error go away but I'm not sure that this is the behavior documented in https://protobuf.dev/programming-guides/proto3/#importing. I've checked load & loadSync but I've could not find a config to set a root path for imports.

I guess what is expected is to have a way to define a root path from where to resolve imports. https://github.com/open-telemetry/opentelemetry-proto seems to assume this.

@david-luna
Copy link
Author

With v7.2.5 the error was not reported so you could load all files separately.

const {load} = require('protobufjs');

load(
  [
    `${__dirname}/org/common/common.proto`,
    `${__dirname}/org/test/test.proto`
  ],
  (err, root) => {
    if (err) {
      console.error(err);
    }
    console.log(root);
})

As a result of #1960 now we get the error

david-luna pushed a commit to elastic/elastic-otel-node that referenced this issue Feb 9, 2024
….proto` files (#66)

Contains a temporary workaround for protobufjs/protobuf.js#1971. Loading a file with non relative imports fail.
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

1 participant