Skip to content

feat: add support for absolute imports #1971

@david-luna

Description

@david-luna

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions