Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Asymmetry in Memento API's JSON encoding/decoding #209479

Closed
hyangah opened this issue Apr 3, 2024 · 2 comments
Closed

Asymmetry in Memento API's JSON encoding/decoding #209479

hyangah opened this issue Apr 3, 2024 · 2 comments
Assignees
Labels
api bug Issue identified by VS Code Team member as probable bug confirmed Issue has been confirmed by VS Code Team member debt Code quality issues insiders-released Patch has been released in VS Code Insiders workbench-state UI state across restarts
Milestone

Comments

@hyangah
Copy link

hyangah commented Apr 3, 2024

I expected Memento's get to recover the JSON-stringifyable object but it doesn't always.

Repro: hyangah/vscode-extension-samples@194f02e

In this repro, I stored a Date object using update, and later attempted to retrieve the value using get<Date>.

	const KEY = 'TEST_TIMESTAMP_KEY';
	let value = context.globalState.get<Date>(KEY);
	if (value === undefined) {
		context.globalState.update(KEY, new Date());
		value = context.globalState.get<Date>(KEY);
	}
	console.log(`TEST_TIMESTAMP_KEY = ${value} type=${typeof value}`);
	if (value === undefined || !(value instanceof Date)) {
		throw Error(`unexpected date - ${value}`);
	}

The first round (immediately storing the value) works as expected, except that the value doesn't match the usual ISO 8601 format (JSON.stringify returns)

Congratulations, your extension "helloworld-sample" is now active!
extensionHostProcess.js:144
"2024-04-03T19:24:17.476Z"
extensionHostProcess.js:144
TEST_TIMESTAMP_KEY = Wed Apr 03 2024 15:24:17 GMT-0400 (Eastern Daylight Time) type=object

Restart the extension. Now this time, the error will be thrown, because context.globalState.get<Date>(KEY) is not a Date object. It's string. I am guessing get failed to parse the persisted JSON string (ISO 8601 format), and silently returned a string object.

Congratulations, your extension "helloworld-sample" is now active!
extensionHostProcess.js:144
"2024-04-03T19:25:06.045Z"
extensionHostProcess.js:144
TEST_TIMESTAMP_KEY = 2024-04-03T19:24:17.478Z type=string
(and crash)

I think this code used to work last year, but recently, we started to notice problems caused by this asymmetry.
Did Memento API get changed recently?

@bpasero
Copy link
Member

bpasero commented Apr 10, 2024

Yeah good catch, I think we should make sure that the value we store is JSON.parse(JSON.stringify...) here:

this._value![key] = value;

So that the returned thing matches what you get when later asking.

@bpasero
Copy link
Member

bpasero commented Apr 12, 2024

Notes to self: is there more custom revive logic here:

function safeStringify(obj: any, replacer: JSONStringifyReplacer | null): string {

That could alter the result once it comes back from storage? If so, can we actually provide the right value before it comes back from the renderer?

@bpasero bpasero added the confirmed Issue has been confirmed by VS Code Team member label Aug 28, 2024
@bpasero bpasero added the debt Code quality issues label Nov 22, 2024
@bpasero bpasero modified the milestones: On Deck, January 2025 Jan 13, 2025
@bpasero bpasero added the bug Issue identified by VS Code Team member as probable bug label Jan 13, 2025
@vs-code-engineering vs-code-engineering bot added the unreleased Patch has not yet been released in VS Code Insiders label Jan 13, 2025
@vs-code-engineering vs-code-engineering bot added insiders-released Patch has been released in VS Code Insiders and removed unreleased Patch has not yet been released in VS Code Insiders labels Jan 14, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
api bug Issue identified by VS Code Team member as probable bug confirmed Issue has been confirmed by VS Code Team member debt Code quality issues insiders-released Patch has been released in VS Code Insiders workbench-state UI state across restarts
Projects
None yet
Development

No branches or pull requests

5 participants