简体   繁体   中英

Git command run with child_process.spawn is unable to find git-lfs subcommand

I've got an electron app (Electron v17.4.10) running on macOS Monterey (v12.5) and on startup it attempts to perform the command git lfs install . It does so by using the Node child_process package's spawn command to invoke a direct call to a standalone git executable.

When the electron app is installed, it provides its own version of git that isn't added to the PATH to prevent interfering with any git versions that may already be installed. The standalone version of git (v2.33.0) has the folder structure shown in the image below and the git-lfs binary (v3.2.0) is contained within /PortableGit/git/libexec/git-core/

Standalone Git Directory Structure

The full command executed is:
"/Applications/MyApp/Utilities/PortableGit/bin/git" lfs install

And the output is:

git: 'lfs' is not a git command. See 'git --help'.

The most similar command is
    log

How can I get the standalone installation of git to recognize the subcommand lfs without placing it on the PATH? Is having a self-contained git package like I've described even possible? Or should I abandon the approach and ensure git is installed properly on the target machine?

The answer to this problem is that Electron's process.env is different from the target system's env. So while Git will look for submodules on the PATH, the PATH used in the Terminal will be different from the PATH used by the child process.

Appending a ':' followed by the path to where the git-lfs binary is stored to process.env.PATH will allow the standalone git installation to properly identify git-lfs subcommands.

IE

process.env.PATH = process.env.PATH + ":" + "path/to/git/lfs"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM