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

[prettier-plugin-sh]: TypeError [Error]: Cannot set property crypto of #<Object> which has only a getter #216

Closed
jlarmstrongiv opened this issue Aug 1, 2022 · 4 comments · Fixed by un-ts/sh-syntax#52

Comments

@jlarmstrongiv
Copy link
Contributor

Setting NODE_OPTIONS="--experimental-global-webcrypto" causes a crash:

TypeError [Error]: Cannot set property crypto of #<Object> which has only a getter

The --experimental-global-webcrypto enables the Web Crypto API from the browser in NodeJS as the global crypto property.

While I’ve disabled experimental NODE_OPTIONS while running prettier, this issue will likely occur in NodeJS v18.x, which become the LTS version in October 2022.

@jlarmstrongiv
Copy link
Contributor Author

jlarmstrongiv commented Aug 1, 2022

Occurs due to node_modules/sh-syntax/lib/shim.js:

import { randomFillSync } from 'node:crypto';
import _fs from 'node:fs';
import { performance } from 'node:perf_hooks';
globalThis.fs = _fs;
globalThis.crypto = {
    // @ts-expect-error
    getRandomValues: randomFillSync,
};
// @ts-expect-error
globalThis.performance = performance;
//# sourceMappingURL=shim.js.map

Specifically:

globalThis.crypto = {
    // @ts-expect-error
    getRandomValues: randomFillSync,
};

Perhaps a workaround would be:

if (!globalThis.crypto) {
    globalThis.crypto = {
        // @ts-expect-error
        getRandomValues: randomFillSync,
    };
}

@JounQin
Copy link
Member

JounQin commented Aug 1, 2022

Would you like to raise a PR to sh-syntax instead?

fs andperformance should also be wrapped into a if block.

https://github.com/un-ts/sh-syntax/blob/5ead2a9c2304a0f5c9e1a0cfc1e4b6db69762d2b/src/shim.ts#L6

@JounQin
Copy link
Member

JounQin commented Aug 1, 2022

And also, please close issues on mvdan/sh and gopherjs, they're not related.

@jlarmstrongiv
Copy link
Contributor Author

Sure! Sorry about that. Resolved in un-ts/sh-syntax#52

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