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

vite-node fails to parse hashbang #1175

Closed
6 tasks done
hugo-t-b opened this issue Apr 21, 2022 · 2 comments · Fixed by #1224
Closed
6 tasks done

vite-node fails to parse hashbang #1175

hugo-t-b opened this issue Apr 21, 2022 · 2 comments · Fixed by #1224

Comments

@hugo-t-b
Copy link
Contributor

Describe the bug

When a file has a hashbang/shebang (most often #!/usr/bin/env node), vite-node fails with this error:

Error: Parse failure: Unexpected character '!' (1:1)
Contents of line 1: #!/usr/bin/env node

Reproduction

StackBlitz link: https://stackblitz.com/edit/node-fhzvli.

System Info

System:
  OS: Linux 5.10 Ubuntu 20.04.4 LTS (Focal Fossa)
  CPU: (8) x64 Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
  Memory: 1.02 GB / 3.72 GB
  Container: Yes
  Shell: 5.0.17 - /bin/bash
Binaries:
  Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
  Yarn: 1.22.15 - ~/.nvm/versions/node/v16.14.2/bin/yarn
  npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
npmPackages:
  vite: ^2.9.5 => 2.9.5 
  vitest: ^0.9.3 => 0.9.3

Used Package Manager

pnpm

Validations

@togami2864
Copy link
Contributor

This bug is troubling for cli tool development.
In vite-node, currently loading and transforming code is executed in vite.(Internally use the function doTransform, transformSSR and error occurred when the code is parsed)

So, I'm considering two solutions.

Solution 1: remove hashbang in vite-node via plugin

I added the following plugin to my local vite-node to try it out, and it works!!!

import type { Plugin } from 'vite'
// remove shebeng from code (most often #!/usr/bin/env node)
// vite recognizes the shebang as a comment and needs to be removed because it causes a parse error.
export const RemoveShebangPlugin = (): Plugin => {
  return {
    name: 'vitest:remove-shebang-plugin',
    enforce: 'pre',
    transform(code) {
      return code.replace(/^\#\!.*/, '')
    },
  }
}

(This is no longer plugin:P just remove shebang)

Bundling is not in the scope of vite-node, so I think there is no problem in deleting them when executing the script.

Solution2: Suggest that the vite side be able to handle shebang

However, I think that vite is mainly for development in the browser and is not really intended for creating cli tools...

Anyway, I'm not sure this is good solution and if I should submit a PR.

@sheremet-va
Copy link
Member

sheremet-va commented Apr 30, 2022

vite-node wraps code in a function, so hashbang appears not on the first line of the file, but inside a function.

so, we should either remove it, or move on the first line. PR welcome

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

Successfully merging a pull request may close this issue.

3 participants