Skip to content
This repository has been archived by the owner on Nov 11, 2020. It is now read-only.

[Suggestion] autodetect files as bash based on the shebang #213

Closed
stof opened this issue Apr 21, 2015 · 21 comments
Closed

[Suggestion] autodetect files as bash based on the shebang #213

stof opened this issue Apr 21, 2015 · 21 comments

Comments

@stof
Copy link

stof commented Apr 21, 2015

It is very common to have shell scripts with the .sh extension. In such case, PhpStorm currently requires us to register each file individually as being treated as bash, which is painful. It would be great if the plugin could autodetect the format based on the shebang to highlight files as bash when the shebang is here.
I'm not sure PhpStorm allows to do that though.

@jansorg
Copy link
Member

jansorg commented Apr 21, 2015

Do you have .sh files which are not Bash and would like BashSupport to not parse them?
If yes, which shebang lines do you use?

BashSupport can detect files based on the shebang if there is no file extension.

If you want to treat *.sh as Bash: this should be the default.

Am 21.04.2015 um 12:29 schrieb Christophe Coevoet notifications@github.com:

It is very common to have shell scripts with the .sh extension. In such case, PhpStorm currently requires us to register each file individually as being treated as bash, which is painful. It would be great if the plugin could autodetect the format based on the shebang to highlight files as bash when the shebang is here.
I'm not sure PhpStorm allows to do that though.


Reply to this email directly or view it on GitHub.

@stof
Copy link
Author

stof commented Apr 21, 2015

no. I have files without extensions which should be treated as Bash. They have the #!/usr/bin/env bash shebang.

the plugin does not seem to detect them as bash

@stof
Copy link
Author

stof commented Apr 21, 2015

hmm, looking at the code, it seems that the detection based on the content does not support the shebangs using #!/usr/bin/env

@j-funk
Copy link

j-funk commented Nov 13, 2015

+1 to get this please, running into this in PyCharm, specifically #!/usr/bin/env bash is not recognized as Bash. See also question on StackOverflow

@jansorg
Copy link
Member

jansorg commented Nov 13, 2015

@stof Does PyCharm ask you to register a single *.sh file with BashSupport? All *.sh files should be automatically detected as Bash (see Preferences->Editor->File types -> Bourne Again Shell

@j-funk Do you use files with .sh extension or files without extension?

@j-funk
Copy link

j-funk commented Nov 13, 2015

@jansorg I'm sorry to neglect to mention. Without extension.

@jansorg
Copy link
Member

jansorg commented Nov 13, 2015

@j-funk @stof
PyCharm is a bit strange - if you add an empty file without extension then it asks to assign a file type. If you copy it, though, then the plugins like BashSupport are asked to detect the file type. BashSupport currently looks at these shebang lines to detect whether a file without extension is a Bash file:

"/sbin/bash",
"/bin/bash",
"/usr/bin/bash",
"/usr/local/bin/bash",
"/opt/local/bin/bash",
"/opt/bin/bash",
"/sbin/sh",
"/bin/sh",
"/usr/bin/sh",
"/opt/local/bin/sh",
"/opt/bin/sh",
"/usr/bin/env bash",
"/usr/bin/env sh"

The "Add new Bash file" action currently always adds a fallback extension (.sh). I could add an option to disable this fallback if you prefer to add files without extensions.

Is that a fix for your guys or do you need something else?

@j-funk
Copy link

j-funk commented Nov 13, 2015

@jansorg not sure what you mean, my usecase is the following:

  1. Clone repository
  2. Open project in PyCharm
  3. The project contains bash scripts with no .sh extension and the shebang #!/usr/bin/env bash

At the moment, there is no styling on the text so I suppose that BashSupport is not detecting. If I modify a file to add the .sh then formatting happens properly. But adding the extension is not an option in my case since it is an established, sizeable project.

@jansorg
Copy link
Member

jansorg commented Nov 13, 2015

@j-funk Which version of BashSupport do you use?

@j-funk
Copy link

j-funk commented Nov 13, 2015

@jansorg 1.5.2.141

@jansorg
Copy link
Member

jansorg commented Nov 13, 2015

@j-funk
This is weird. BashSupport 1.5.2 should detect that file.
Is the codebase public?

@j-funk
Copy link

j-funk commented Nov 16, 2015

Hey @jansorg I'm afraid it's not public. Worth noting that it does not reproduce when I try running echo '#!/usr/bin/env bash' > scriptname and open it up in PyCharm.

@wjarka
Copy link

wjarka commented Dec 1, 2015

I was facing that issue, too. Here is a workaround for files that have #!/usr/bin/env bash that don't get recognized:

  1. Delete the shebang line
  2. Save the file
  3. Add the shebang line back
  4. Save the file

It seems to work for me.

@jansorg
Copy link
Member

jansorg commented Mar 7, 2016

might be related to #262

@fabiocarneiro
Copy link

fabiocarneiro commented Aug 23, 2016

Let me resurrect this issue
#!/usr/bin/env php also doesn't work with extension-less files on phpstorm

@stof
Copy link
Author

stof commented Aug 23, 2016

@fabiocarneiro this has nothing to do with this issue. This is the issue tracker of the BashSupport plugin, so its own autodetection is about detecting Bash files. This plugin is not about detecting language for other kind of files based on the shebang (this is a nice idea for a plugin, but definitely a different one)

@fabiocarneiro
Copy link

fabiocarneiro commented Aug 24, 2016

@stof As far as I could understand the program loader(which is bash) calls PHP to execute this script.

Doesn't it make sense then, that any file without extension and/or with shebang should then be detected as a bash script by BashSupport plugin? And consequently formatted with the interpreter program language, which for me is PHP?

@jansorg
Copy link
Member

jansorg commented Aug 24, 2016

@fabiocarneiro You're right that the shell (in your case Bash) looks at the shebang line and then calls that executable with the rest of the script file (in your case php).

There are other shells which also support shebang lines (afaik).

BashSupport supports Bash/Sh syntax only. It only has handlers to detect whether a file without an extension is probably a bash file.

PHPStorm should detect your file as php. I'm not sure whether it has a feature to do this. If it doesn't I'd suggest to open a ticket in the JetBrains Youtrack issue tracker.

BashSupport can't/won't handle any other language than Bash because it would need dependencies to all kinds of other language plugins to handle all possible cases (PHP, Javascript/node, Python, Perl, ...).

@jansorg jansorg closed this as completed Aug 24, 2016
@pkit
Copy link

pkit commented Apr 3, 2017

After update to pycharm 2017.1 BashSupport no longer detects simple #!/bin/bash as bash file

@markus-hinsche
Copy link

After update to pycharm 2017.1 BashSupport sometimes fails to detects simple #!/bin/bash as bash file, for other files it works, really strange, I can't see a pattern when it works and when not

@fuzzyweapon
Copy link

This isn't working on IDEA Ultimate 2019.1.3.

It's failing to detect extension-less files with shebang: #!/usr/bin/env bash
Native IDE settings for associating extension-less files: Not set.

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

No branches or pull requests

8 participants