mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.14k stars 338 forks source link

The npm module should work on CodeSandbox #864

Open rob-myers opened 2 years ago

rob-myers commented 2 years ago

I have a demo using mvdan-sh@0.5.0 working on CodeSandbox.

But I had to comment out two similar lines:

$r = os.$init(); /* */ $s = 5; case 5: if($c) { $c = false; $r = $r.$blk(); } if ($r && $r.$blk !== undefined) { break s; }
// ...
$r = os.$init(); /* */ $s = 7; case 7: if($c) { $c = false; $r = $r.$blk(); } if ($r && $r.$blk !== undefined) { break s; }

Otherwise, I see runtime error: makeslice: len out of range. It is reproducible by switching the imports back in CodeSandbox link:

import Sh, { syntax } from "@rob-myers/mvdan-sh";
// import Sh, { syntax } from "mvdan-sh";

The error occurs simply by importing the module, and also occurs for mvdan-sh@0.10.1.

mvdan commented 2 years ago

I wonder if this is a GopherJS bug, because we don't really control what JS code gets produced when transpiling.

Also, if WASM is an option, perhaps try https://github.com/rx-ts/sh-syntax. It does not use GopherJS, so it potentially sidesteps this bug. cc @JounQin

JounQin commented 2 years ago

It seems the reproduction is using parser.Interactive inside, which is not available in sh-syntax yet.

JounQin commented 2 years ago
image

This is because CodeSandbox is polyfillng globalThis.process unexpectedly, I think you should raise an issue for CodeSandbox instead.

JounQin commented 2 years ago

Workaround: delete window.process before import 'mvdan-sh'

See https://codesandbox.io/s/tty-demo-1-forked-d6xg0p?file=/src/sh/parse/parse.service.ts

JounQin commented 2 years ago

Also, there is also a fix in gopherjs https://github.com/gopherjs/gopherjs/pull/1117 which has not been released.

rob-myers commented 2 years ago

@JounQin thanks for looking into it. Great that there's a GopherJS fix in the works already. I will try building using go get github.com/gopherjs/gopherjs@master and see if it works.

Generally speaking, it would be nice to have a bunch of CodeSandbox links demoing mvdan-sh on the web. The example I provided is too complex, I'm thinking e.g. enter shell code and display AST etc. Do such web-based examples exist anywhere at present?

rob-myers commented 2 years ago

I want to use version 0.5.0, so I am using an old commit and go v1.2.17. Sadly, changing the build to go get github.com/gopherjs/gopherjs@master yields the following error:

package github.com/gopherjs/gopherjs@master: can only use path@version syntax with 'go get'

I guess I'll just wait for a fix.