-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix #4703, 4713: Transpile fixes #4717
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
Fix #4703, 4713: Transpile fixes #4717
Conversation
|
I'm confused. I tried to trigger the "you don't have babel installed" error, but failed. Isn't the above supposed to have triggered the error? I'm on Ubuntu 16.04. |
|
@lydell I can’t explain what you’re seeing. I tried a controlled test using Docker. I created this Dockerfile: FROM node:latest
COPY ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT /entrypoint.shwith this #!/bin/bash
mkdir ~/project && cd ~/project
echo 'import path from "path"; echo path.sep' > ./test.coffee
npm install --global \
https://github.com/GeoffreyBooth/coffeescript.git#transpile-fixes
coffee --transpile test.coffee || true
npm uninstall --global coffeescript
echo '{ "private": true }' > ./package.json
echo '{}' > ./.babelrc
npm install --save-dev \
https://github.com/GeoffreyBooth/coffeescript.git#transpile-fixes
./node_modules/.bin/coffee --transpile test.coffee || trueAnd ran |
|
@lydell Wait, looking at your code again, it appears you’re running What if you try |
…e CLI and babel-core isn’t installed
…el search for its options, rather than us doing so
3528d8f to
7259265
Compare
|
Yep, when installing like you suggest I get the error message as expected. Isn’t that strange though? How can a babel-core installation in the node_modules/ of the coffeescript installation affect things? |
|
Yeah, but out of our responsibility I think. The mysteries of Node |
lib/coffeescript/command.js
Outdated
| } else { | ||
| opts.transpile.filename = base || process.cwd(); | ||
| if (opts.transpile.filename.endsWith(path.sep)) { | ||
| opts.transpile.filename += '.'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the sake of making the error messages clearer, I wonder if it's be better to have this do <stdio>.coffee <fake>.coffee or something? Not sure what cases come up where the filename isn't known.
I guess relatedly, should Babel use the .babelrc if it doesn't know a filename? Babel's CLI itself for instance does not take .babelrc files when code is passed in via stdin, though we've talked about adding an opt-in flag for that behavior. I don't have strong feelings how you do it though, since it's probably fine as long as it's documented for users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only cases I can think of where filename would be undefined are when the coffee command is called with input via stdin or via --eval, e.g.:
./bin/coffee --transpile --eval 'import path from "path"
console.log path.sep
throw new Error()'
/
Error
at Object.<anonymous> (<anonymous>:1:54)
at Module._compile (module.js:573:30)
at Object.CoffeeScript.run (~/Sites/coffeescript/lib/coffeescript/index.js:61:23)And the CoffeeScript compiler is already rewriting the stack trace in that scenario, putting <anonymous> in place of the filename.
That covers exceptions, but not the Babel compiler itself throwing an error:
./bin/coffee --transpile --eval '`import path`'
SyntaxError: ~/Sites/coffeescript/.: 'import' and 'export' may only appear at the top level (2:2)
1 | (function() {
> 2 | import path;
| ^
3 |
4 |
5 | }).call(this);
at Parser.pp$5.raise (~/Sites/coffeescript/node_modules/babel-core/node_modules/babylon/lib/index.js:4454:13)
at Parser.pp$1.parseStatement (~/Sites/coffeescript/node_modules/babel-core/node_modules/babylon/lib/index.js:1877:16)
at Parser.pp$1.parseBlockBody (~/Sites/coffeescript/node_modules/babel-core/node_modules/babylon/lib/index.js:2268:21)So the filename appears next to the SyntaxError here, but I think /. here is fine. I think it might make a bit more sense than ~/Sites/coffeescript/./<anonymous>, which would cause some head scratching since that path wouldn’t resolve. Also I’m still nervous about so much reliance on filename, which is essentially undocumented; what if a future change to filename means that it needs to be a resolvable path? Then suddenly passing it ~/Sites/coffeescript/./<anonymous> breaks --transpile.
should Babel use the
.babelrcif it doesn’t know a filename?
Yes, to handle the --eval or stdin cases. Is there a reason we shouldn’t allow --transpile to work in those scenarios? I’d rather Babel find its options the usual way based on the current folder for these cases, than have someone pass in JSON as a command-line argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the filename appears next to the SyntaxError here, but I think /. here is fine.
Sounds good, just figured I'd mention it.
Also I’m still nervous about so much reliance on filename, which is essentially undocumented
Babel's documentation just isn't great but that doesn't mean we're anxious to break our users. We do our absolute best to only introduce breaking changes on major versions. There's zero chance we'd go from treating filename as a maybe-existing file to a guaranteed-existing file without a major version bump.
I’d rather Babel find its options the usual way based on the current folder for these cases
I think it's your call. I don't feel strongly. I'll say, the reason in my mind that having the filename is important for config resolution is for cases like this:
project/
src/
index.js
.babelrc
given
babel src/index.js
// vs
babel < src/index.js
because the first will find the .babelrc and the second one won't.
For Babel, we just skip config resolution entirely unless we know the actual location to search from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@loganfsmyth Thanks for all these notes. I think the --eval and stdin use cases are edge enough that I’d rather pass the current folder as the filename so that transpilation works for these options, rather than force someone to save a test.coffee file or similar. Most likely someone compiling code inline via the CLI is just testing things, and this saves them some hassle. We should know (and pass along) the filename for all common use cases, as far as I’m aware, whether they’re compiling a single file or a folder tree.
Do you mind adding a comment in the Babel source code near where filename is parsed that upstream tools are expecting this functionality out of it? Perhaps with a link to this PR or to #4713 😄 Any little bit helps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think adding a comment isn't gonna make it any clearer. It's a core piece of the way Babel processes configs. The fact that it exists in the first place by definition means that upstream things rely on it heavily and changing it would break them.
|
Looks pretty great! Thanks, @GeoffreyBooth. My only minor quibble is that I'd probably try to trim down some of the language and redundancies around the transpilation instructions. Since (either now, or very shortly) you won't need Babel in order to use compiled CS2 for everything with the exception of JSX — it seems like we lean on folks a little too hard with instructions for installing it. |
|
@jashkenas will do, though it doesn’t look like Node is supporting @jashkenas and @lydell how do you feel about the use case of
This is irrelevant for the Node API version of |
|
I vote for using process.cwd for |
Very true, but if you're writing for Node with CS2 today, you can just use
Idle question, but does the |
No, and I think not at the moment. I don’t see any great need for REPL users to be able to use |
|
@jashkenas I’ve rewritten the transpilation section to make it slightly shorter and hopefully clearer: I think the section should be somewhat hand-holdy, as there are a significant number of people out there who use the @lydell and @jashkenas since you are good with the |
src/command.coffee
Outdated
| if filename | ||
| opts.transpile.filename = filename | ||
| else | ||
| opts.transpile.filename = base or process.cwd() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General feedback, this can all be
unless opts.transpile.filename
opts.transpile.filename = filename or path.resolve(base or process.cwd(), "(anonymous)")
if you wanted to avoid manually fiddling with the path separators and wanted to not use .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I can get behind this, though it should be <anonymous> (brackets, not parentheses) to match the stack traces for compiled strings.
./bin/coffee --transpile --eval '<div/>'
SyntaxError: ~/coffeescript/<anonymous>: Unexpected token (2:2)
1 | (function() {
> 2 | <div />;
| ^…etter resolving of paths
This PR finishes the “remove Babel as a dependency” work (#4703), and gets us out of the business of finding Babel’s options for it (#4713). The latter makes us more compatible with additional options, um, options, that Babel is introducing in Babel 7; and lessens our integration with Babel if we want to support additional transpilers in the future. The docs are also updated: http://rawgit.com/GeoffreyBooth/coffeescript/transpile-fixes/docs/v2/index.html
I would appreciate help testing this, as there aren’t tests for running this via the command line. I’m also on a Mac, so anyone on Windows or Linux, your feedback would be especially appreciated. cc @loganfsmyth
I think once this is merged in we should release a 2.0.1, that includes this and #4668 / #4712.
@jashkenas, are you okay with the docs changes?