firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

@firebase/firestore: Firestore (9.23.0): FIRESTORE (9.23.0) INTERNAL ASSERTION FAILED: Unexpected state

Open fkolar opened this issue 1 year ago • 10 comments

Operating System

Mac OS X 13.6

Browser Version

Headless

Firebase SDK Version

9.23

Firebase SDK Product:

Firestore

Describe your project's tooling

I am using Angular wth NX workspace that is running JEST to execute test. I am using @angular/fire 7.6.1

Describe the problem

I am unable to execute a test that is failing on

[2023-10-20T09:08:32.007Z]  @firebase/firestore: Firestore (9.23.0): FIRESTORE (9.23.0) INTERNAL ASSERTION FAILED: Unexpected state

      at Logger.defaultLogHandler [as _logHandler] (node_modules/@firebase/logger/src/logger.ts:115:57)
      at Logger.Object.<anonymous>.Logger.error (node_modules/@firebase/logger/src/logger.ts:210:5)
      at logError (node_modules/@firebase/firestore/src/util/log.ts:59:15)
      at fail (node_modules/@firebase/firestore/src/util/assert.ts:35:3)
      at hardAssert (node_modules/@firebase/firestore/src/util/assert.ts:54:5)
      at fromBytes (node_modules/@firebase/firestore/src/remote/serializer.ts:264:5)
      at fromWatchChange (node_modules/@firebase/firestore/src/remote/serializer.ts:502:25)
      at PersistentListenStream.onMessage (node_modules/@firebase/firestore/src/r

I saw many similar issues over here but unfortunatelly I have no idea how to fix it in this setup. One more thing that I need to say, when I run a test to create a document it works, but when I try to retrieve it this is where I get this error.

Steps and code to reproduce issue

I have created the repo to test. Please follow quict instruction in the REDME. Thank you

https://github.com/fkolar/firebase-test

fkolar avatar Oct 20 '23 09:10 fkolar

The same works with karma:

https://github.com/fkolar/firebase-karma

When I added a console.log to the file that does the assert

function fromBytes(serializer, value) {
    if (serializer.useProto3Json) {
        hardAssert(value === undefined || typeof value === 'string');
        return ByteString.fromBase64String(value ? value : '');
    }
    else {
          console.log('@@@@@@@@@@@@@@@@@@@@@: ', value)
          console.log('@@@@@@@@@@@@@@@@@@@@@: ', value === undefined || value instanceof Uint8Array)
        hardAssert(value === undefined || value instanceof Uint8Array);
        return ByteString.fromUint8Array(value ? value : new Uint8Array());
    }
}

The output is

 @@@@@@@@@@@@@@@@@@@@@:  <Buffer >
    @@@@@@@@@@@@@@@@@@@@@:  false

Uploading Screenshot 2023-10-20 at 19.57.03.png…

fkolar avatar Oct 20 '23 10:10 fkolar

Thanks for reporting @fkolar and for providing a reproduction repository.

ehsannas avatar Oct 20 '23 17:10 ehsannas

I wonder if you guys have any workaround in meanwhile. I dont really want to more all to Karma yet.

fkolar avatar Oct 23 '23 17:10 fkolar

I can reproduce the issue with the provided repo, but after spending time trying to resolve it, I don't have a workaround for you. There is another thread with several suggestions that may be helpful for you: https://github.com/firebase/firebase-js-sdk/issues/6931.

@hsubox76, do you have any thoughts on this one?

MarkDuckworth avatar Oct 23 '23 18:10 MarkDuckworth

@fkolar To clarify, I think what is happening is that your build is choosing the wrong entry point/bundle for the Firebase SDK when building for tests. E.g. it's using the node entry point instead of the browser entry point. I'm not familiar enough with your tool chain to advise you how to change this.

MarkDuckworth avatar Oct 23 '23 19:10 MarkDuckworth

Thanks @fkolar for the repros, by the way, they were very helpful.

The one bit of info I can offer is that the Jest test grabs the Node CJS bundle (@firebase/firestore/dist/index.node.cjs.js) while the Karma test grabs the browser ESM bundle (@firebase/firestore/dist/index.esm2017.js). I think the bundle you actually want in Jest is the browser CJS bundle (@firebase/firestore/dist/index.cjs.js).

As for how to get it, I managed to do so with a custom resolver:

Add resolver: "<rootDir>/jest.resolver.js" to jest.config.ts

Create jest.resolver.js in the root dir with the contents:

// jest.resolver.js
module.exports = (path, options) => {
  // Call the defaultResolver, so we leverage its cache, error handling, etc.
  return options.defaultResolver(path, {
    ...options,
    conditions: ['browser', 'require']
  });
};

This doesn't seem very robust, especially as it will affect all modules (including non-firebase ones) but basically you have to tinker with condition order until you make sure you're resolving to this condition for firestore: https://github.com/firebase/firebase-js-sdk/blob/f002ef36a6b427fd526696f9cd6077a217ccc6ef/packages/firestore/package.json#L66

As an aside, when I did that, I got past the error above but ran into a timeout error which may be something else, or just because I haven't got the emulator set up.

hsubox76 avatar Oct 23 '23 19:10 hsubox76

You may also be able to use the packageFilter option to make a resolution change only apply to @firebase/firestore. There's an example of how to use it here, but I wouldn't make the exact changes they make there - it won't work, it will just end up resolving to index.node.cjs.js again, as that's what "main" is. I'd probably modify exports.node.require (I think that's the field Jest will always grab) to point to index.cjs.js

hsubox76 avatar Oct 23 '23 19:10 hsubox76

Hey @fkolar. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Oct 30 '23 01:10 google-oss-bot

This is great info, in meantime I am using Karma and all works well, but I will try to this resolver thing..

fkolar avatar Nov 02 '23 10:11 fkolar

I was getting a similar error when using IndexDB persistence with Firestore. Just ended up disabling persistence and things are working for me.

Nuzzlet avatar Jan 12 '24 00:01 Nuzzlet