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

running bundle created with esbuild results in Invalid asm.js: Type mismatch in assignment #856

Closed
vladmandic opened this issue Feb 20, 2021 · 3 comments

Comments

@vladmandic
Copy link

i have a simple project that used to work, but now fails with Invalid asm.js: Type mismatch in assignment after bundling it with esbuild.

Note that running non-bundled code works fine, it's only running bundled code that fails.

running code directly:

vlado@wyse:~/dev/piacme $ node src/piacme.js
2021-02-20 09:41:36 INFO:  ACME certificate check: ./cert/fullchain.pem
2021-02-20 09:41:37 STATE:  SSL certificate expires in 89.1 days: skipping renewal
2021-02-20 09:41:37 INFO:  SSL account: mailto:mandic00@live.com Created: 2020-04-23 21:55:15
2021-02-20 09:41:37 INFO:  SSL keys server: RSA Account: EC
2021-02-20 09:41:37 INFO:  SSL certificate subject: pidash.ddns.net Issuer: R3
2021-02-20 09:41:37 DATA:  Parsed details: {
  serverKey: { type: 'RSA', use: 'sig' },
  accountKey: { type: 'EC', crv: 'P-256' },
  fullChain: { subject: 'pidash.ddns.net', issuer: 'R3', notBefore: 2021-02-19T18:12:37.000Z, notAfter: 2021-05-20T18:12:37.000Z },
  account: { contact: 'mailto:mandic00@live.com', initialIP: '138.207.150.136', createdAt: 2020-04-24T01:55:15.058Z, status: 'valid' }
}

esbuild --outdir=dist --target=es2018 --platform=node --format=cjs --color=true --log-level=error --bundle src/piacme.js

running bundled code:

vlado@wyse:~/dev/piacme $ node dist/piacme.js
2021-02-20 09:41:44 INFO:  ACME certificate check: ./cert/fullchain.pem
(node:81303) V8: /home/vlado/dev/piacme/dist/piacme.js:40132 Invalid asm.js: Type mismatch in assignment
}

however, if i enable --minify esbuild flag, it works!
this must be first time where non-minified code fails and minified works...

project code (it's a single source file) is at https://github.com/vladmandic/piacme

i don't know which upgrade broke it as it's been a while since i've last checked, it could be esbuild itself, included module x509.js or newer nodejs.

environment: nodejs 15.7.0 on ubuntu 20.10 with esbuild 0.8.49

@evanw
Copy link
Owner

evanw commented Feb 20, 2021

I didn't design esbuild to be able to handle asm.js. The reason why it isn't valid asm.js without the minify flag is that 0.0 is printed as 0 which is a different data type according to asm.js. The reason why it appears to be valid with the minify flag is that the "use asm"; directive is removed, which causes node to not try to validate it as asm.js.

but now fails with Invalid asm.js: Type mismatch in assignment

This is only a warning, not a failure. The bundled code should still work. The whole design of asm.js is based around it just being JavaScript so if it doesn't validate or if the VM doesn't know anything about asm.js it should still work fine. One option could be for you to just ignore V8's warning.

I don't think encoding asm.js-specific logic into esbuild is a good idea. There are many complicated validation rules for asm.js (it has its own type system) and it's basically just a legacy format that was mainly useful for convincing standards committees that WebAssembly was a good idea. Now everyone is using WebAssembly instead, which is superior in pretty much every way. The asm.js format is quickly getting less and less relevant.

The most straightforward "fix" for this I can think of is for esbuild to just always remove the "use asm"; pragma when bundling, even when not minifying. Then the VM will never try to validate it as asm.js so you shouldn't get the warning. It will probably run more slowly, however.

If this being asm.js is important to you and you still want to use esbuild, you could use a plugin to ensure that the content is passed through as a string. I hacked this plugin together and it seems to work:

require('esbuild').build({
  entryPoints: ['x509.js'],
  bundle: true,
  platform: 'node',
  outfile: './out.js',
  plugins: [{
    name: 'asm.js-fix',
    setup(build) {
      build.onResolve({ filter: /em-x509/ }, args => {
        return {
          path: require.resolve(args.path, { paths: [args.resolveDir] }),
          namespace: 'wrap-in-string',
        }
      })
      build.onLoad({ filter: /.*/, namespace: 'wrap-in-string' }, async (args) => {
        return {
          contents: `
            eval(${JSON.stringify(await require('fs').promises.readFile(args.path, 'utf8'))})
            return module.exports
          `,
        }
      })
    },
  }],
}).then(() => {
  console.log('result:', require('./out.js'))
})

@Jarred-Sumner
Copy link
Contributor

Jarred-Sumner commented Feb 20, 2021

Oh I wonder if this was the actual cause of vadimdemedes/ink#415.

I worked around this issue like this:

// TODO: remove this when https://github.com/vadimdemedes/ink/issues/415 is resolved.
const _disableWarning = process.emitWarning;
process.emitWarning = () => {};
const { renderConfirm } = require(CONFIRM_PROMPT_PATH);
process.emitWarning = _disableWarning;

@vladmandic
Copy link
Author

vladmandic commented Feb 20, 2021

@evanw thanks for the quick & detailed feedback.

honestly, i don't care about asm.js (e.g., i don't use it myself - especially since this is in nodejs, not in browser), it's internally used by x509.js which i need to parse the ssl certs.
and performance is irrelevant since it happens on startup and on renewal every few months.

i am surprised that this popped up when it used to work without any warnings for a while now (i only noticed when my personal web server did not renew it's certificate automatically).

This is only a warning, not a failure

maybe it would be if i didn't run my apps in strict mode :)

@Jarred-Sumner that seems like nasty hack, but i might keep it at hand for future usage :)

in any case, this feedback is sufficient to close the issue.

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

Successfully merging a pull request may close this issue.

3 participants