Skip to content

[CS2] Add #! support for executable scripts on Linux. #3946

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

Merged
merged 51 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
6c48af3
Add #! support for executable scripts on Linux.
Apr 16, 2015
221dfc4
refactor option parsing
Apr 20, 2017
988f2af
address comments
Apr 20, 2017
fa3fe8b
intermediate save
May 24, 2017
aee066f
add note saying where OptionParser is used in coffee command
Apr 28, 2017
3c1fb7f
add some more work
May 24, 2017
a10c653
fix flatten functions
May 24, 2017
d5e8d74
Merge branch '2' of github.com:jashkenas/coffeescript
May 24, 2017
062fe62
Merge branch 'feature/uniform-hashbang-parsing'
May 24, 2017
c929ed9
refactor tests
May 31, 2017
b1acc4b
make argument processing less confusing
May 31, 2017
f3ea781
add basic test
May 31, 2017
200126f
Merge branch '2' into test/current-cli-optparse
May 31, 2017
7970e44
remove unused file
May 31, 2017
bc92ff3
compilation now hangs
May 31, 2017
7e0d9e0
remove unnecessary changes
Jun 2, 2017
0a05e0c
add tests!!!
Jun 2, 2017
3c49c8e
Merge branch '2' into feature/uniform-hashbang-parsing
Jun 2, 2017
0e1f27e
Merge branch 'test/current-cli-optparse' into feature/uniform-hashban…
Jun 2, 2017
42434b4
add/fix some tests
Jun 2, 2017
15eb624
clarify a test
Jun 2, 2017
bb9366c
Merge branch 'test/current-cli-optparse' into feature/uniform-hashban…
Jun 2, 2017
e1bcf84
fix helpers
Jun 9, 2017
7bc6591
fix opt parsing
Jun 9, 2017
7c4723d
fix infinite loop
Jun 9, 2017
768ada6
Merge remote-tracking branch 'upstream/2' into feature/uniform-hashba…
Jun 9, 2017
79c0e56
Merge remote-tracking branch 'upstream/2' into test/current-cli-optparse
Jun 9, 2017
ca7ad49
Merge branch 'test/current-cli-optparse' into feature/uniform-hashban…
Jun 9, 2017
daa5f4d
Merge remote-tracking branch 'upstream/2'
Jun 9, 2017
c75e2b1
Merge branch 'feature/uniform-hashbang-parsing'
Jun 9, 2017
23f31c6
Merge branch '2' of github.com:jashkenas/coffeescript
Jun 19, 2017
d495841
make rule building easier to read
Jun 19, 2017
bafa82f
add tests for flag overlap
Jun 19, 2017
9247444
revamp argument parsing again and add more thorough testing
Jun 19, 2017
b509b46
add tests, comment, clean unused method
Jun 19, 2017
31bbeb2
address review comments
Jun 20, 2017
7abe19c
Merge remote-tracking branch 'upstream/2'
Jun 21, 2017
3944d94
add test for direct invocation of shebang scripts
Jun 21, 2017
f41463d
move shebang parsing test to separate file and check for browser
Jun 21, 2017
356f3ad
remove TODO
Jun 21, 2017
7c17e23
example backwards compatible warnings
Jun 21, 2017
179687d
Merge remote-tracking branch 'upstream/2'
Jun 29, 2017
5b9b786
add correct tests for warning 1
Jun 29, 2017
8448a77
add tests for warnings
Jun 29, 2017
14df734
commit output js libs and update docs
Jun 29, 2017
b9291ae
respond to review comments
Jun 30, 2017
f306859
respond to review comments
Jul 7, 2017
4093574
Merge branch '2' of github.com:jashkenas/coffeescript
Jul 7, 2017
4856fd6
fix example output
Jul 7, 2017
f7e8c2b
Rewrite argument parsing documentation to be more concise; add it to …
GeoffreyBooth Jul 9, 2017
6fb80c1
Don’t mention deprecated syntax; clean up variable names
GeoffreyBooth Jul 9, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
### Argument parsing and shebang lines

#### Trailing `--`

Previous versions of CoffeeScript required a `--` after the script to run, but this convention is now deprecated. The new standard is described in the output of `coffee -h`:

``` bash
> coffee -h

Usage: coffee [options] [--] path/to/script.coffee [args]

If called without options, `coffee` will run your script.


```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the blank lines before and after the text?

Copy link
Contributor Author

@cosmicexplorer cosmicexplorer Jun 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually screwed up the help text (see the most recent commit -- needed to change the help method to use @rules.ruleList instead of just @rules). I made that fix, changed this text, and have also added tests in test/option_parser.coffee in the most recent commit to ensure the help text will always display the banner and the flags.


If the script is run with a `--` after the script, it will show a warning, then run your script.

``` bash
> coffee path/to/script.coffee --
coffee was invoked with '--' as the second positional argument, which is
now deprecated. To pass '--' as an argument to a script to run, put an
additional '--' before the path to your script.

'--' will be removed from the argument list.
The positional arguments were: ["path/to/script.coffee","--"]
...
```

#### Shebang scripts

On non-Windows platforms, a `.coffee` file can be made executable by adding a shebang (`#!`) line at the top of the file and marking the file as executable. For example:

`executable-script.coffee`:
``` coffeescript
#!/usr/bin/env coffee

x = 2 + 2
console.log x
```

``` bash
> chmod +x ./executable-script.coffee
> ./executable-script.coffee
4
```

Due to a bug in the argument parsing of previous CoffeeScript versions, this used to fail when trying to pass arguments to the script. Some users on OSX worked around the problem by using `#!/usr/bin/env coffee --` at the top of the file instead. However, that won't work on Linux, which cannot parse shebang lines with more than a single argument. While these scripts will still run on OSX, CoffeeScript will now display a warning before compiling or evaluating files that begin with a too-long shebang line:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s “OS X”. Also the documentation uses curly quotes, e.g. “won’t”.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed both of those.


`invalid-executable-script.coffee`:
``` coffeescript
#!/usr/bin/env coffee --

x = 2 + 2
console.log x
```

``` bash
> chmod +x /path/to/invalid-executable-script.coffee
> /path/to/invalid-executable-script.coffee
The script to be run begins with a shebang line with more than one
argument. This script will fail on platforms such as Linux which only
allow a single argument.
The shebang line was: '#!/usr/bin/env coffee --' in file '/path/to/shebang-extra-args.coffee'
The arguments were: ["coffee","--"]
4
```

Note that the script *is* still run, producing the `4` at the bottom.
17 changes: 16 additions & 1 deletion lib/coffeescript/coffeescript.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions lib/coffeescript/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

187 changes: 117 additions & 70 deletions lib/coffeescript/optparse.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions src/coffeescript.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,13 @@ Error.prepareStackTrace = (err, stack) ->

checkShebangLine = (file, input) ->
firstLine = input.split(/$/m)[0]
if firstLine?.match(/^#!/)?
shebangTokens = firstLine.split /\s/
if shebangTokens.length > 2
console.error '''
The script to be run begins with a shebang line with more than one
argument. This script will fail on platforms such as Linux which only
allow a single argument.
'''
console.error "The shebang line was: '#{firstLine}' in file '#{file}'"
rest = firstLine?.match(/^#!\s*([^\s]+\s*)(.*)/)
args = rest?[2]?.split(/\s/).filter (s) -> s isnt ''
if args?.length > 1
console.error '''
The script to be run begins with a shebang line with more than one
argument. This script will fail on platforms such as Linux which only
allow a single argument.
'''
console.error "The shebang line was: '#{firstLine}' in file '#{file}'"
console.error "The arguments were: #{JSON.stringify args}"
Loading