ggrossetie / opal-node-runtime

Opal Runtime for Node.js
https://opalrb.com/
MIT License
4 stars 2 forks source link

Attempted to assign to read-only property under Bun 0.1.8 #65

Closed robogeek closed 2 years ago

robogeek commented 2 years ago

I'm trying to get my application (AkashaCMS) to run on Bun - the new Node.js-compatible platform.

At this line of code:

const asciidoctor = require('@asciidoctor/core')();

I get this error:

$ bun filez.mjs 
Config start
1861 |       return Opal.send(this, body, args, block);
1862 |     };
1863 | 
1864 |     // Try to make the browser pick the right name
1865 |     alias.displayName       = name;
1866 |     alias.length            = body.length;
        ^
TypeError: Attempted to assign to readonly property.
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:1866:4
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:2806:4
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:2311:6
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:5466:2
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:2311:6
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:20066:2
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:20075:44
      at bun:wrap:1:16354
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/index.js:23:10
      at bun:wrap:1:16354
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/@asciidoctor/core/dist/node/asciidoctor.js:2:21
      at bun:wrap:1:16354
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/akasharender/render-asciidoc.js:26:28
      at bun:wrap:1:16354
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/akasharender/index.js:177:42
      at bun:wrap:1:16354
      at /home/david/Projects/akasharender/akashacms-perftest/bench/config.mjs:6:0
robogeek commented 2 years ago

Here is a simple reproduction of the error, using your sample code:

const asciidoctor = require('@asciidoctor/core')() // <1>
const content = 'http://asciidoctor.org[*Asciidoctor*] ' +
  'running on https://opalrb.com[_Opal_] ' +
  'brings AsciiDoc to Node.js!'
const html = asciidoctor.convert(content) // <2>
console.log(html) // <3>
$ bun asciidoctor.js 
1861 |       return Opal.send(this, body, args, block);
1862 |     };
1863 | 
1864 |     // Try to make the browser pick the right name
1865 |     alias.displayName       = name;
1866 |     alias.length            = body.length;
        ^
TypeError: Attempted to assign to readonly property.
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:1866:4
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:2806:4
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:2311:6
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:5466:2
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:2311:6
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:20066:2
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/opal.js:20075:44
      at bun:wrap:1:16354
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/asciidoctor-opal-runtime/src/index.js:23:10
      at bun:wrap:1:16354
      at /home/david/Projects/akasharender/akashacms-perftest/node_modules/@asciidoctor/core/dist/node/asciidoctor.js:2:21
      at bun:wrap:1:16354
      at /home/david/Projects/akasharender/akashacms-perftest/bench/asciidoctor.js:1:28
ggrossetie commented 2 years ago

I believe this issue can also be reproduced using: var Opal = require('opal-runtime').Opal; Could you please give it a try?

For reference, the latest release of @asciidoctor/core (version 2.2.6) is using Asciidoctor Opal Runtime 0.3.3 based on Opal 0.11.x whereas Opal Runtime 2.3.0 is based on Opal 1.4.0.

This is really an issue with Opal and I want to confirm that Bun cannot run a recent version of Opal. Ultimately, if it does not work then we should fix this issue at: https://github.com/opal/opal

robogeek commented 2 years ago

I created a file containing only this:

var Opal = require('opal-runtime').Opal;

Then ran

npm install opal-runtime

And the result executed correctly with no issues. Is there a way to execute the Asciidoctor code using this Opal instance? In other words, to distinguish between opal-runtime and asciidoctor-opal-runtime

ggrossetie commented 2 years ago

Unfortunately, you cannot use a more recent of Opal runtime since the code is compiled for a specific version of the Opal runtime. In other words, if Asciidoctor (Ruby) was compiled with Opal version 0.11 it won't run with Opal runtime 1.0.

We are using a more recent version of Opal on the main branch of Asciidoctor.js. You can try to clone https://github.com/asciidoctor/asciidoctor.js and run npm i && npm run build in the packages/core directory then require the locally built version.

robogeek commented 2 years ago

I did all that - and unfortunately come up with a known issue with Bun that it gives me this kind of error on packages that are properly installed:

$ bun asciidoctor.js 

error: Could not resolve: "@asciidoctor/core". Maybe you need to "bun install"?

const asciidoctor = require('@asciidoctor/core')() // <1>
                            ^
/home/david/Projects/akasharender/akashacms-perftest/bench/asciidoctor.js:5:29 75

So... this is an inconclusive result. Bun gives this sort of error with other packages.

Thank you for your attention on this.

ggrossetie commented 2 years ago

I will try on my end to see if I get a different result.

ggrossetie commented 2 years ago

I get the following error:

 7 | import Opal from 'asciidoctor-opal-runtime'
 8 | import unxhr from 'unxhr'
 9 | 
10 | const __path__ = path
11 | const __XMLHttpRequest__ = unxhr.XMLHttpRequest
12 | const __asciidoctorDistDir__ = path.dirname(fileURLToPath(import.meta.url))
                                               ^
TypeError: Argument must be a URL
      at /home/guillaume/workspace/opensource/asciidoctor/asciidoctor.js/packages/core/dist/node/asciidoctor.js:12:44
      at /home/guillaume/workspace/opensource/asciidoctor/asciidoctor.js/packages/core/test.js:1:28
robogeek commented 2 years ago

DO you mean something like this:


import { fileURLToPath, pathToFileURL } from 'node:url';
import * as path from 'path';

console.log(import.meta.url);
console.log(fileURLToPath(import.meta.url));

const __asciidoctorDistDir__ = path.dirname(fileURLToPath(import.meta.url))
console.log(__asciidoctorDistDir__);

Which gives these results:

$ uname -a
Linux davidpc 5.15.0-41-generic #44-Ubuntu SMP Wed Jun 22 14:20:53 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

$ node -v
v18.6.0

$ node filepath.mjs 
file:///home/david/Projects/akasharender/akashacms-perftest/bench/filepath.mjs
/home/david/Projects/akasharender/akashacms-perftest/bench/filepath.mjs
/home/david/Projects/akasharender/akashacms-perftest/bench

$ bun -v
0.1.8

$ bun filepath.mjs 
file:///home/david/Projects/akasharender/akashacms-perftest/bench/filepath.mjs
1 | 
2 | import { fileURLToPath, pathToFileURL } from 'node:url';
3 | import * as path from 'path';
4 | 
5 | console.log(import.meta.url);
6 | console.log(fileURLToPath(import.meta.url));
               ^
TypeError: Argument must be a URL
      at /home/david/Projects/akasharender/akashacms-perftest/bench/filepath.mjs:6:12

I see that import.meta.url has a valid URL, but maybe Bun is not recognizing file: URL's.

ggrossetie commented 2 years ago

Yes, I opened an issue at https://github.com/oven-sh/bun because I think it should be supported.

robogeek commented 2 years ago

Okay, we both did the same thing ...

ggrossetie commented 2 years ago
$ bun --version
0.1.10
$ cat index.js
import Asciidoctor from './build/asciidoctor-node.js'

const asciidoctor = Asciidoctor()
console.log(asciidoctor.convert('Hello *world*'))
$ bun index.js
<div class="paragraph">
<p>Hello <strong>world</strong></p>
</div>

In short, Asciidoctor.js (main) is compatible with the latest version of Bun.

robogeek commented 2 years ago

Thank you, does this correspond to a package on NPM yet?

ggrossetie commented 2 years ago

Thank you, does this correspond to a package on NPM yet?

Not yet, you will still need to build Asciidoctor.js from the source.

ggrossetie commented 2 years ago

Fixed upstream. Asciidoctor.js 3.x will be compatible with Bun.