Skip to content

BUG: Timestamp.toObject() returning zeroed value #222

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
kyleshepherd opened this issue Sep 13, 2024 · 9 comments
Open

BUG: Timestamp.toObject() returning zeroed value #222

kyleshepherd opened this issue Sep 13, 2024 · 9 comments
Labels
question Further information is requested

Comments

@kyleshepherd
Copy link

In the latest version, 3.21.4, the Timestamp.toObject() function isn't working correctly.

Previously it would return the timestamp as an object with keys for seconds and milliseconds. However now it is returning zero values for both.

This is a function where I noticed the issue, I've added some logging which pointed out toObject() as being the problem.

function dateToTs(date: Date): Timestamp {
  const ts = new Timestamp();
  ts.fromDate(date);
  console.log(
    "date:",
    date,
    "timestamp:",
    ts.getSeconds(),
    ts.getNanos(),
    ts.toObject(),
  );
  return ts;
}

See the screenshot below for the resulting console log, where you can see the seconds and milliseconds are set, but the toObject return is zeroed
image

@dibenede
Copy link
Contributor

I'm having trouble reproducing the issue. I get:

date: 2022-02-01T00:00:00.000Z timestamp: 1643673600 0 { seconds: 1643673600, nanos: 0 }
date: 2022-02-02T00:00:00.000Z timestamp: 1643760000 0 { seconds: 1643760000, nanos: 0 }
date: 2022-02-03T00:00:00.000Z timestamp: 1643846400 0 { seconds: 1643846400, nanos: 0 }

Are you using the google.protobuf.Timestamp proto we ship in the google-protobuf npm package (google-protobuf/google/protobuf/timestamp_pb.js)?

@dibenede dibenede added the question Further information is requested label Sep 13, 2024
@kyleshepherd
Copy link
Author

kyleshepherd commented Sep 16, 2024

odd! this is how we're importing the Timestamp

import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";

and yes we're using the google-protobuf NPM package at version 3.21.4

@dibenede
Copy link
Contributor

I don't think import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb"; works with plain google-protobuf. Our project doesn't officially support ES modules; just CommonJS.

Could there be any tooling on your side that's transforming the source to a module?

Or maybe building our package from source? I know there's a pending PR that tries to add module support.

My repro attempt was based on CommonJS require's. Trying to use import against an npm install'd google-protobuf errors out.

@kyleshepherd
Copy link
Author

I can't see anything in our project that is transforming it, we've been using it as-is for the past 12-18 months without issue up until this 3.21.4 release

@dibenede
Copy link
Contributor

Are you using plain node or ts-node? And what version?

Also, are you using any node flags related module loading? Particularly, experimental ones.

@kyleshepherd
Copy link
Author

We're using this in a React app along with Typescript, using Node v20.17.0 and Typescript v5.5.4.

I can't see any node flags anywhere, so I don't believe we are

@curtgrimes
Copy link

I am seeing this happen when both v3.21.4 and a version less than v3.21.4 are both in the dependency tree. A minimal reproduction:

import { Timestamp } from 'google-protobuf/google/protobuf/timestamp_pb';

const t = new Timestamp();
t.setSeconds(12345);

// This works:
console.log('Timestamp seconds:', t.getSeconds()); 

// If the bug is happening, this returns `{ seconds: 0, nanos: 0 }`
// instead of the expected `{ seconds: 12345, nanos: 0 }`:
console.log('Timestamp object:', t.toObject()); 

// The issue is present when we get seconds back from `t.getSeconds()` but we
// don't get the same seconds value in `t.toObject().seconds`:
console.log(
    'Issue present?',
    t.toObject().seconds === t.getSeconds()
        ? '🟣 NO'
        : '🟡 YES'
);

Possible fix or workaround

I'm seeing it fixed by upgrading dependencies to use v3.21.4 exactly, and/or forcing this resolution in package.json:

"resolutions": {
    "google-protobuf": "3.21.4"
}

@dibenede
Copy link
Contributor

dibenede commented Apr 4, 2025

@curtgrimes Thank you for the repro. This helps explain why I was having trouble repro'ing with a trivial usage.

Do you have an example of a something using an older version?

@curtgrimes
Copy link

@dibenede Unfortunately, I don’t have an example. I tried putting together a minimal reproduction, but couldn’t reproduce the issue outside of a larger codebase. That codebase has some intertwined dependencies using different versions of google-protobuf, so there ended up being multiple versions in the bundle (and looking at the lockfile or running npm why google-protobuf confirms that).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants