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

It's no longer necessary to strip shebangs from modules #13129

Closed
andreubotella opened this issue Dec 17, 2021 · 1 comment · Fixed by #13220
Closed

It's no longer necessary to strip shebangs from modules #13129

andreubotella opened this issue Dec 17, 2021 · 1 comment · Fixed by #13220
Labels
needs investigation requires further investigation before determining if it is an issue or not suggestion suggestions for new features (yet to be agreed)

Comments

@andreubotella
Copy link
Contributor

andreubotella commented Dec 17, 2021

Deno's module fetcher will strip a shebang if a module file starts with one. However, there is a stage-3 TC39 proposal to support shebangs (or "hashbangs") as part of JS syntax. In fact, this was implemented in V8 in 2018 and has shipped by default since V8 7.4. So this transformation can be removed.

This affects the output of Deno.emit() though. Although emit::bundle will skip any shebangs in the source files, and so deno bundle and Deno.emit() with the bundle parameter are safe, emit::to_file_map (which powers Deno.emit() without the bundle option) will output shebangs for the files that have them. This might be bad, depending on what kinds of environments the code generated by Deno.emit() will be used in. We should consider whether to strip the shebang from the outputs.

Another potential issue is positions in sourcemaps: since currently shebangs are stripped before the module is parsed, any sourcemaps generated by tsc or swc would be off by one line for modules with shebangs. I don't know if this error is corrected at all, but if so, the correction would have to be undone. Also, if shebangs are stripped from the Deno.emit() outputs, those sourcemaps would also need to be corrected to account for it.

@kitsonk kitsonk added needs investigation requires further investigation before determining if it is an issue or not suggestion suggestions for new features (yet to be agreed) labels Dec 20, 2021
@andreubotella
Copy link
Contributor Author

Given #13021 and #13188 (comment), the sourcemap issues are probably not a blocker for this change.

Also, I noticed that, because the shebang is being stripped before the BOM, a file with both a BOM and a shebang will not have its shebang stripped, so shebangs can show up in the outputs to Deno.emit(). This is almost certainly unintentional, though, and we should consider whether that is expected.

I also realized that this shebang stripping is causing a correctness bug with JSON modules, since the shebang will be stripped when the file is fetched, regardless of the module type.

andreubotella pushed a commit to andreubotella/deno that referenced this issue Dec 29, 2021
Deno's module loader currently strips a shebang if a module file
starts with one. However, this is no longer necessary, since there is
a stage-3 TC39 that adds support for shebangs (or "hashbangs") to the
language (https://github.com/tc39/proposal-hashbang), and V8, `tsc`
and `swc` all support it.

Furthermore, stripping shebangs causes a correctness bug with JSON
modules, since a JSON file with a shebang should not parse as a JSON
module, yet it does with this stripping. This change fixes this.

Closes denoland#13129.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation requires further investigation before determining if it is an issue or not suggestion suggestions for new features (yet to be agreed)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants