Skip to content

syntax: $var#... is incorrectly parsed as a comment #1003

Closed

Description

@emilazy

Example using shfmt:

emily@yuyuko ~> cat test.bash
foo=abc
bar=def
echo foo#bar
echo foo#$bar
echo $foo#bar
echo $foo#$bar
emily@yuyuko ~> bash test.bash
foo#bar
foo#def
abc#bar
abc#def
emily@yuyuko ~> shfmt test.bash
foo=abc
bar=def
echo foo#bar
echo foo#$bar
echo $foo #bar
echo $foo #$bar
emily@yuyuko ~> shfmt test.bash | bash
foo#bar
foo#def
abc
abc

None of these contain comments, but the latter two examples get turned into comments (and this actually came up in practice; see bouk/babelfish#23). I'm not sure if it's just variable substitutions or if there are other circumstances where this incorrect parse happens.

Activity

  1. bouk commented on May 23, 2023

    @bouk
    Contributor

    Seems like adding a spaced check here is all that's needed:

    case '#':

  2. mvdan commented on May 30, 2023

    @mvdan
    Owner

    Nice find - genuinely surprised that noone spotted this bug before. @bouk has the right idea, but unfortunately the fix isn't that easy. I'm investigating.

  3. changed the title [-]`$var#...` in argument lists is parsed incorrectly[/-] [+]syntax: `$var#...` is incorrectly parsed as a comment[/+] on May 30, 2023
  4. mvdan commented on Jun 10, 2023

    @mvdan
    Owner

    I spent quite a bit of time overthinking different ways to fix this, but I landed on something relatively simple: a small variation on @bouk's idea. It's not a complete fix, as two other cases still don't parse the way they should, but the cases you showed in this issue are all fixed.

  5. emilazy commented on Jun 13, 2023

    @emilazy
    Author

    Thanks for this! Would it be good to open an issue for the remaining cases? I haven't seen nix shell $(calculate-flake-ref)#... type constructions appear in a script yet, but I think more out of luck than anything else.

  6. mvdan commented on Jun 14, 2023

    @mvdan
    Owner

    I left TODOs in the code as a personal reminder, but please feel free to open another issue if you think you'll run into it soon. Particularly if you already have any real example :)

  7. emilazy commented on Jun 14, 2023

    @emilazy
    Author

    I'll wait to run into it :)

  8. bouk commented on Jun 15, 2023

    @bouk
    Contributor

    @mvdan if you cut a release I'll update babelfish!

  9. mvdan commented on Jun 18, 2023

    @mvdan
    Owner

    done :)

  10. added a commit that references this issue on May 28, 2024
    829b814
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址