Skip to content

Cannot read properties of undefined (reading 'indexOf') #1958

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

Open
justaguyin2k23 opened this issue Jan 9, 2024 · 3 comments
Open

Cannot read properties of undefined (reading 'indexOf') #1958

justaguyin2k23 opened this issue Jan 9, 2024 · 3 comments

Comments

@justaguyin2k23
Copy link

justaguyin2k23 commented Jan 9, 2024

protobuf.js version: 7.2.5
gprc-js version 1.9.13
proto-loader version 0.7.10

line #488 of [protobuf.js/ext/descriptor/index.js]

if ((descriptor.oneofIndex = this.parent.oneofsArray.indexOf(this.partOf)) < 0)
        throw Error("missing oneof");

causes the error Cannot read properties of undefined (reading 'indexOf') when calling proto-loader's loadSync function.

const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const pathLib = require('path');
const proto_path = [
  pathLib.join(__dirname,'./protos/dummy_service.proto')
];
 const packageDefinition = protoLoader.loadSync(protopath);

This specifically happens when the latest version of buf/validate.proto is among the proto files being used.

Changing line #488 to below solves the issue,

if ((this.parent.oneofsArray && descriptor.oneofIndex = this.parent.oneofsArray.indexOf(this.partOf)) < 0)

Stacktrace

From Node.js Internals:
  TypeError: Cannot read properties of undefined (reading 'indexOf')
      at Field.toDescriptor (/Users/tiredcoder/dev/cypress-tests/node_modules/protobufjs/ext/descriptor/index.js:488:62)
      at Root_toDescriptorRecursive (/Users/tiredcoder/dev/cypress-tests/node_modules/protobufjs/ext/descriptor/index.js:142:40)
      at Root_toDescriptorRecursive (/Users/tiredcoder/dev/cypress-tests/node_modules/protobufjs/ext/descriptor/index.js:146:13)
      at Root_toDescriptorRecursive (/Users/tiredcoder/dev/cypress-tests/node_modules/protobufjs/ext/descriptor/index.js:146:13)
      at Root.toDescriptor (/Users/tiredcoder/dev/cypress-tests/node_modules/protobufjs/ext/descriptor/index.js:121:5)
      at createPackageDefinition (/Users/tiredcoder/dev/cypress-tests/node_modules/@grpc/proto-loader/src/index.ts:327:66)
      at Object.loadSync (/Users/tiredcoder/dev/cypress-tests/node_modules/@grpc/proto-loader/src/index.ts:392:10)
 
     at createService (/Users/tiredcoder/dev/cypress-tests/cypress.config.ts:38:49)

I see a somewhat similar issue, #1898 but no resolution.

@josephharrington
Copy link

We ran into this same issue. It seems to occur when an extend has an optional field, e.g,:

extend google.protobuf.FieldOptions {
  optional NfTrackerInfo nfTrackerInfo = 50001;
}

I see the original report above mentions buf/validate.proto which has similar extend patterns.

@josephharrington
Copy link

josephharrington commented Mar 26, 2025

Here is a minimal repro of the issue in [email protected]:

// extend.proto
syntax = "proto3";

message SomeMessage {
  string foo = 1;
}

extend SomeMessage {
  optional string bar = 2;
}
// index.mjs
import protobufjs from 'protobufjs';
import descriptor from 'protobufjs/ext/descriptor/index.js';
import path from 'path';

const dirname = path.dirname(new URL(import.meta.url).pathname);

const root = protobufjs.loadSync(path.join(dirname, 'extend.proto'));
root.toDescriptor('proto3').file;

And the error:

./node_modules/protobufjs/ext/descriptor/index.js:488
        if ((descriptor.oneofIndex = this.parent.oneofsArray.indexOf(this.partOf)) < 0)
                                                             ^

TypeError: Cannot read properties of undefined (reading 'indexOf')
    at Field.toDescriptor (./node_modules/protobufjs/ext/descriptor/index.js:488:62)
    at Root_toDescriptorRecursive (./node_modules/protobufjs/ext/descriptor/index.js:142:40)
    at Root.toDescriptor (./node_modules/protobufjs/ext/descriptor/index.js:121:5)
    at file:///./index.mjs:8:6

@josephharrington
Copy link

FWIW it looks like the reason the existing tests do not hit this issue is because tests/data/test.proto is proto2 instead of proto3. If I change that proto file to proto3 then the tests start failing with this exact issue.

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