Skip to content

dependency of husky run-node might not be suitable on Windows #228

Closed

Description

@Jack-Works

In the project run-node, I this in the readme.md

This Bash script uses some tricks to find the Node.js binary on your system and run it.

So run-node might cause an error on Windows, consider to not use it.

image

Activity

  1. typicode commented on Jan 31, 2018

    @typicode
    Owner

    Hi @Jack-Works,

    Thanks for raising this issue.

    When Git runs hooks, it should run them in a shell, so even on Windows it should work (e.g. v0.14 is creating shell scripts https://github.com/typicode/husky/blob/master/src/utils/get-hook-script.js#L52)

    I'm using husky on Windows + VSCode + PowerShell but didn't encounter this issue.
    What are the tools you're using? How did you install Git?

  2. Jack-Works commented on Jan 31, 2018

    @Jack-Works
    Author

    Oh yes, in the shell environment, husky works
    well. But in Gitkaren (a git gui tool), it doesn't work anymore. I've heard that in some system gui program will not inherit some environment they shell program will, is that the key ?

  3. shiftkey commented on Feb 1, 2018

    @shiftkey

    I'm using husky on Windows + VSCode + PowerShell but didn't encounter this issue.

    This only works if you have access to Git for Windows, which bundles a bash shell and knows how to invoke it natively.

    I work on GitHub Desktop, which ships an embedded Git that is stripped of much of the UNIX toolchain that comes with Git for Windows. We had a report about this change breaking for at least one user: desktop/desktop#3860

    Unless there are specific features of the Bash shell you plan to use, I'd recommend staying with #!/bin/sh as that will continue to be supported across more setups (I tested this on 0.14.3 and it works fine).

  4. typicode commented on Feb 2, 2018

    @typicode
    Owner

    Thank you @shiftkey for the detailed explanation. I've just changed default branch to master again (i.e. v0.14) until it's figured out.

    And v0.15 is behind a @next flag.

    I imagine that run-node really requires bash and couldn't work with sh only.

    Maybe checking if bash is available then use run-node, else just fall back to node command and if node isn't available show a message.

    What do you think?

  5. shiftkey commented on Feb 2, 2018

    @shiftkey

    I imagine that run-node really requires bash and couldn't work with sh only.

    I'm not really familiar with run-node, so here's what I did to test this theory. All commands are running in Command Prompt on Windows, to ensure I don't accidentally have a bash shell handy.

    I have NodeJS installed, but it's not in my PATH, to ensure we're using run-node as best as possible.

    > node
    'node' is not recognized as an internal or external command,
    operable program or batch file.

    Here's the contents of the package.json file I'm using to demo this:

    > type package.json
    {
      "name": "hello-run-node",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "run-node": "^0.2.0"
      }
    }
    

    Here's the contents of a simple pre-commit hook, to ensure it gets invoked by Git:

    > type .git\hooks\pre-commit
    #!/bin/sh
    
    ./node_modules/.bin/run-node -e "console.log('hello world');"

    I've tweaked the run-node.cmd file that NodeJS generated and adds to node_modules/.bin to replace all mentions of /bin/bash to /bin/sh - I'm not sure if this looks at the script header when generating it, I guess so?

    > type node_modules\.bin\run-node.cmd
    @IF EXIST "%~dp0\/bin/sh.exe" (
      "%~dp0\/bin/sh.exe"  "%~dp0\..\run-node\run-node" %*
    ) ELSE (
      @SETLOCAL
      @SET PATHEXT=%PATHEXT:;.JS;=;%
      /bin/sh  "%~dp0\..\run-node\run-node" %*
    )

    And I tweaked the run-node script itself to require /bin/sh. Everything else after the first line is unchanged.

    > type node_modules\run-node\run-node
    #!/bin/sh
    # MIT License © Sindre Sorhus
    
    ...

    And then I try to commit without any staged changes:

    > git status
    On branch master
    nothing to commit, working tree clean
    
    > git commit -m "whatever"
    Couldn't find the Node.js binary. Ensure you have Node.js installed. Open an issue on https://github.com/sindresorhus/run-node
    On branch master
    nothing to commit, working tree clean

    So this worked fine when I had node on my PATH (it printed hello world before the git output to the console), but without node on my PATH I see it spawn run-node and fail. I don't see anything Windows-specific in the run-node source about it looking at known installation locations, so you're not getting any benefits from using run-node in it's current form on the platform.

  6. typicode commented on Feb 5, 2018

    @typicode
    Owner

    Thanks for all the testing!

    v0.15.0-rc.5 has been published under @next tag, it uses sh and generates a different script on Windows that doesn't rely on run-node.

    So upgrading to this version should fix the issue on GitHub Desktop + Windows.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions