Skip to content
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

ts-proto never retrieves globalThis #732

Closed
andykais opened this issue Dec 14, 2022 · 5 comments · Fixed by #735
Closed

ts-proto never retrieves globalThis #732

andykais opened this issue Dec 14, 2022 · 5 comments · Fixed by #735
Labels

Comments

@andykais
Copy link

andykais commented Dec 14, 2022

Hi, I noticed that your library has this code in it:

var globalThis = (() => {
    if (typeof globalThis !== 'undefined') {
        return globalThis;
    }
    if (typeof self !== 'undefined') {
        return self;
    }
    if (typeof window !== 'undefined') {
        return window;
    }
    if (typeof global !== 'undefined') {
        return global;
    }
    throw 'Unable to locate global object';
})();

I was seeing failures when only globalThis was available in the js program. When I reduce that code down to this:

var globalThis = (() => {
    if (typeof globalThis !== 'undefined') {
        return globalThis;
    }
    throw 'Unable to locate global object';
})();

it always fails. Tested on node v12.19.0, v19.2.0 and deno 1.28.3.

This looks to be a problem with var declared variable hoisting. var globalThis will declare globalThis as undefined before the code block executes, so it is always undefined. If you renamed the outer variable to something other than globalThis, the code will function.

var tsProtoGlobalThis = (() => {
    if (typeof globalThis !== 'undefined') {
        return globalThis;
    }
    throw 'Unable to locate global object';
})();
console.log(tsProtoGlobalThis)
@stephenh
Copy link
Owner

Huh! Good catch @andykais !

Are you interested in submitting a PR to fix this? That'd be great if so!

Otherwise yeah I'll get around to fixing this at some point; kinda surprised no one had noticed this was broken, but I guess maybe it was just always hitting one of the non-globalThis fallbacks.

@andykais
Copy link
Author

so unfortunately getting approval to contribute to existing open source projects is something fairly difficult to do through my job 😞. It can take months. The best I can do is give some advice on what a good change would be

@stephenh
Copy link
Owner

Ah sure @andykais , that makes sense wrt getting approval. Given your great diagnosis/articulation of the problem, this was a pretty simple quick fix, so I went ahead and pushed it out. Thank you!

@andykais
Copy link
Author

nice! Thanks @stephenh

stephenh pushed a commit that referenced this issue Dec 16, 2022
## [1.136.1](v1.136.0...v1.136.1) (2022-12-16)

### Bug Fixes

* Avoid namespace conflict on globalThis. ([#735](#735)) ([71e919e](71e919e)), closes [#732](#732)
@stephenh
Copy link
Owner

🎉 This issue has been resolved in version 1.136.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

2 participants