8000 Allow multiple lines of hashbangs · Issue #715 · google/zx · GitHub
[go: up one dir, main page]

Skip to content

Allow multiple lines of hashbangs #715

Open

Description

@fmnxl

Expected Behavior

Nix allows the use of shebang to define a custom interpreter, for example:

#! /usr/bin/env nix-shell
#! nix-shell -i zx -p zx

await $`echo 1`

zx should ignore multiple shebang lines instead only the first one.

Actual Behavior

SyntaxError: Invalid or unexpected token

Steps to Reproduce the Problem

  1. (setup) Install nix
  2. Add the above shebang lines to the start of the script (script.mjs)
  3. Try to run the script with zx script.mjs

Specifications

  • Version: 7.1.1
  • Platform: macOS

Activity

  1. jlbribeiro commented on Aug 17, 2024

    @jlbribeiro

    @fmnxl (also cc @rockofox), and anyone interested: this issue keeps bugging me, probably for the same reason as you, nix-shell (so we can use zx in a "fully contained" way).
    Given both #715 and #716 got closed without this being addressed, using "two shebangs" still results in a SyntaxError.

    As I managed to find a different not-as-portable approach to overcome this limitation (which makes it a single shebang line), I'm sharing it here.
    Afaict, it requires two things:

    • nix shell (and not nix-shell), i.e. the nix-command experimental feature turned on;
    • env with the -S flag:
        -S, --split-string=S  process and split S into separate arguments;
                              used to pass multiple arguments on shebang lines
      

    If you can live with those two requirements, this is the shebang line:

    #!/usr/bin/env -S nix shell nixpkgs#zx --command zx
    

    Please let me know if this works for you, and if you manage to find a way to simplify this.

    (given the current files-without-extension temporary files behavior, I advise you to still use the .mjs extension for now, unfortunately.)

  2. antonmedv commented on Aug 17, 2024

    @antonmedv
    Collaborator

    Actually shbang supported via node. Zx not handles shbang.

  3. jlbribeiro commented on Sep 13, 2024

    @jlbribeiro

    @antonmedv I guess the reasoning behind this issue being a feature request was because it was possible for zx to be hackishly modified to support them (as implemented in #716).

    @fmnxl @rockofox I just accidentally discovered that nix-shell parses nix-shell option hashbangs anywhere in the file.
    This was discussed in NixOS/nix#2570 and finally documented in NixOS/nix#11202 (so the first reference to this in documentation appears in the 2.24 docs (last paragraph)).

    As such, @antonmedv, I believe this issue can be closed because, as in the unofficial NixOS wiki Lua example, we can resort to JavaScript's block comments ("multi-line") to make this work with nix-shell:

    #! /usr/bin/env nix-shell
    /*
    #! nix-shell -i zx -p zx
    */
    
    $.verbose = true;
    
    await Promise.all([
      $`sleep 1; echo 1`,
      $`sleep 2; echo 2`,
      $`sleep 3; echo 3`,
    ]);
  4. antonmedv commented on Sep 14, 2024

    @antonmedv
    Collaborator

    Should we add some notes about this behavior to documentation? A nix-guide of docs website?

    @jlbribeiro maybe you can write a short .md file?

  5. added a commit that references this issue on Oct 1, 2026
    572dc93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0