google / zx

A tool for writing better scripts

Home Page:https://google.github.io/zx/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Runs scripts under WSL/WSL2 if installed and sets incorrect prefix

Wicked7000 opened this issue · comments

Expected Behavior

Commands should work fine on either Windows or Linux and the child process should be spawned as expected.

Actual Behavior

In windows if installed WSL will be called (due to the path to the shell defaulting to bash). Along with this due to the fact that bash can be found it will set the prefix to something that windows (cmd) can't interpret (set -euo pipefail;).

Steps to Reproduce the Problem

  1. Have WSL or WSL2 installed
  2. Run a zx script and attempt to set the shell in either CLI or using $.shell to cmd.exe
  3. Will fail with "Environment variable -euo pipefail;[INSERT COMMAND YOU TRIED TO RUN HERE] not defined"

Workaround

You can work around this bug fairly easily by setting both $.prefix and $.shell which resolves the problem. However this should likely be documented or fixed directly. (Especially as WSL has become more popular in recent years)

$.prefix = '';
$.shell = 'C:/Windows/System32/cmd.exe';

If you set the shell in CLI arguments then you can just set prefix to an empty string.

Fix

Lines 139-147 in index.mjs contain the code that causes the problem where it attempts to find bash, which if succeeds then sets the prefix. (It also sets shell to the location of bash). This code is a little strange as it sits outside the main init function in zx.mjs. I feel this code should move to the main function and the attempt to locate bash and set prefix should only be done in the case where no shell was specified.

In terms of the problem with bash being located due to WSL, it depends on what approach you'd prefer:

  • Tell people in the README that WSL/WSL2 will cause processes to spawn under it and if you don't want this behaviour make sure to change your shell before running
  • Have logic to determine if we are running on Windows/Linux and then only try to locate bash if we are on linux, although this would work it may be undesirable

Specifications

  • Version: 6.1.0
  • Platform: Windows 10 (With WSL2 installed)

@antongolub will your pr help here as well?

Probably. If we introduce smth like presets.

preset('wsl') // sets rootCtx with proper params

$`cmd`

Well, zx was never designed as platform independent tool.

But very opposite- zx is os dependent tool.

So I believe this issue not the case of support.

And it's ok. Plugins, presets, extras can be implemented on the side. But zx should definetelly provide the possibility.

I think best solution here will be to lazy init $.shell and $.prefix if not specified in user script.

Well, I think solution in to modify shell with prefix all the time