Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Uncaught TypeError: Cannot read property 'emptyArray' of undefined #1503

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
ZiyunHe opened this issue Nov 1, 2020 · 18 comments
Open

Uncaught TypeError: Cannot read property 'emptyArray' of undefined #1503

ZiyunHe opened this issue Nov 1, 2020 · 18 comments

Comments

@ZiyunHe
Copy link

ZiyunHe commented Nov 1, 2020

protobuf.js version: 6.10.1

Im using typescript and trying with awesomemessage example. It reports error of emptyArray.
import protobuf from 'protobufjs';
protobuf.load("t.proto", function(err, root) {
...
})
Anyone has idea? Thanks

types.defaults = bake([
    /* double   */ 0,
    /* float    */ 0,
    /* int32    */ 0,
    /* uint32   */ 0,
    /* sint32   */ 0,
    /* fixed32  */ 0,
    /* sfixed32 */ 0,
    /* int64    */ 0,
    /* uint64   */ 0,
    /* sint64   */ 0,
    /* fixed64  */ 0,
    /* sfixed64 */ 0,
    /* bool     */ false,
    /* string   */ "",
    /* bytes    */ util_1.emptyArray, <------Uncaught TypeError: Cannot read property 'emptyArray' of undefined
    /* message  */ null
]);
@vicb
Copy link
Contributor

vicb commented Nov 11, 2020

I do have the same error on my project.

It's quite a big project so I don't think it would be super helpful to use it as a repro - it's open source code but too big to help.

@vicb
Copy link
Contributor

vicb commented Nov 11, 2020

@ZiyunHe could you share your repro if it's smaller or give instructions to reproduce your config ?

Thanks

On my side the issue is new ~1 week so I have not yet a clear issue of where it comes from (I have no direct dep on protobufjs).

@ZiyunHe
Copy link
Author

ZiyunHe commented Nov 11, 2020

@vicb I actually figured out because I was using snowpack.

rollup/rollup#2747 check out this issue for more information.

Are you using snowpack as well? I had to switch to webpack because of the need of protobufjs library

@vicb
Copy link
Contributor

vicb commented Nov 11, 2020

I just figure out the circular dependency problem by checking the generated code:

var types_1 = createCommonjsModule(function (module, exports) {
    console.log('types_1');

 //...

types.defaults = bake([
    /* double   */ 0,
    /* float    */ 0,
    /* int32    */ 0,
    /* uint32   */ 0,
    /* sint32   */ 0,
    /* fixed32  */ 0,
    /* sfixed32 */ 0,
    /* int64    */ 0,
    /* uint64   */ 0,
    /* sint64   */ 0,
    /* fixed64  */ 0,
    /* sfixed64 */ 0,
    /* bool     */ false,
    /* string   */ "",
    /* bytes    */ util_1$1.emptyArray,
    /* message  */ null
]);
});


var util_1$1 = createCommonjsModule(function (module) {
    console.log('util_1$1');
    //...
});

You would expect to see the dependency logged before types_1 but it is not the case...

I do use rollup, your link will help, thanks !

@vicb
Copy link
Contributor

vicb commented Nov 11, 2020

@ZiyunHe I am trying to create a simple repro to submit a bug to submit a bug to the repo of @rollup/plugin-commonjs.

Any chance you have a repro available somewhere that I could use as a starting point for a REPL ?

Thanks

@ZiyunHe
Copy link
Author

ZiyunHe commented Nov 11, 2020

@vicb
sorry i dont have one now. i actually didn't use rollup. i used snowpack. i tried with snowpack create app and install protobufjs right away. it won't work. I'm planning to create a repo and submit an issue to snowpack coummity.

@vicb
Copy link
Contributor

vicb commented Nov 11, 2020

Thanks @ZiyunHe.

Please you please cc when you create that issue. It will probably help me reproduce with rollup too.

@Janaka-Steph
Copy link

Same here. Any update @ZiyunHe ?

@choyongjoon
Copy link

I commented on the issue of @ZiyunHe in the snowpack. It might be a module resolution problem due to the same name between a file and a directory.

FredKSchott/snowpack#1499

@logvik
Copy link

logvik commented Feb 19, 2021

Temporary bypass for rollup users:

./node_modules/.bin/pbjs -t static-module -o ./src/utils/protocol.js ./src/assets/protocol.proto

then import { protocol } from '/src/utils/protocol.js' // if in .proto you have package protocol; - replace on own package name

the reason in circular dependencies, but even https://github.com/rollup/plugins/tree/feat/commonjs-circular-dependencies/packages/commonjs won't fix this issue (I have compiled code on local env and replace in node_modules folder - no luck). The main solution for now, just to create static module.

@vicb
Copy link
Contributor

vicb commented Feb 19, 2021

@logvik maybe you should comment on rollup/plugins#658 if you have an easy repro you can share with @lukastaegert

@choyongjoon the problem with rollup is not cause by the file src/util.js and the folder src/util having the same name. I have tried to rename the file but the result is still the same (that is circular deps).

@logvik
Copy link

logvik commented Feb 19, 2021

@vicb rollup is not the reason of circular dependencies. That's why I have publish my bypass way here. Many people will read and use. Also, I think that need to change this package, because it is not ok to have circular dependencies at any case.
Circular dependencies:

node_modules\protobufjs\src\util\minimal.js -> node_modules\protobufjs\src\util\longbits.js -> node_modules\protobufjs\src\util\minimal.js
node_modules\protobufjs\src\util\minimal.js -> node_modules\protobufjs\src\util\longbits.js -> D:\serverdoc\vegcook\node_modules\protobufjs\src\util\minimal.js?commonjs-proxy -> node_modules\protobufjs\src\util\minimal.js

@vicb
Copy link
Contributor

vicb commented Feb 19, 2021

rollup is not the reason of circular dependencies

Agreed.

it is not ok to have circular dependencies at any case

Here I don't agree.
Some circular dependencies are benign and can be used.
For an example, see the first example in the rollup PR:

// main.js
const dep = require('./dep.js');
exports.foo = 'foo';
console.log(dep.getMain().foo);

// dep.js
const main = require('./main.js');
exports.getMain = () => main;

That's why I have publish my bypass way here

Your workaround works because your app (or lib) is the direct consumer of the proto.
My use case is that my app transitively includes protobufjs which is used in runtime mode. There is nothing I can do about that in my app.

@yongjun21
Copy link

yongjun21 commented May 17, 2021

Anyone still looking at this? I have a project I trying to migrate to Vite. managed to get everything working except production build. It seems rollup/plugins#658 is not the only fix required. According to the disclaimer in rollup/plugins#658 (comment)

While this PR adds support for circular references, it does not add support for exact execution order for inline require statements. As some packages with circular references also rely on that, they may still not work. But often, they can now be made to work by manually tweaking the execution order via strategic imports

@kskalski
Copy link

Is there any workaround for rollup to handle protobufjs in production build? Maybe a way to skip optimization of specific modules/files?

@kskalski
Copy link

A note from my case - when using

import { Reader } from 'protobufjs/minimal';

instead of

import { Reader } from 'protobufjs';

I managed to get it working with vite production build. Previously it was failing on circular dependency related to ReflectionObject

@assafmo
Copy link

assafmo commented Nov 26, 2021

I can confirm that @kskalski's solution fixed it for me too! Using vite (probably an esbuild issue)

@lukastaegert
Copy link

We are finalizing a new version of the commonjs plugin in rollup/plugins#1038. It is pre-published as @rollup/plugin-commonjs@beta. Please give it a spin to see if it resolves the issue, ideally without additional config needed.

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

9 participants