Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Probably does not work with WSL2 #62

Closed
1 task done
igoradamenko opened this issue Apr 9, 2023 · 2 comments · Fixed by #128
Closed
1 task done

[BUG] Probably does not work with WSL2 #62

igoradamenko opened this issue Apr 9, 2023 · 2 comments · Fixed by #128
Labels
Bug thing that needs fixing Needs Triage needs an initial review

Comments

@igoradamenko
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

npm repo pkg fails.

Expected Behavior

npm repo pkg should open a browser page with pkg repo.

Steps To Reproduce

$ npm repo axios
npm ERR! Cannot read properties of undefined (reading 'stdin')

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ai/.npm/_logs/2023-04-09T13_02_08_591Z-debug-0.log

Environment

  • npm: 9.5.0
  • Node: 18.15.0
  • OS: Ubuntu 20.04
  • platform: WSL2
@igoradamenko igoradamenko added Bug thing that needs fixing Needs Triage needs an initial review labels Apr 9, 2023
@igoradamenko
Copy link
Author

Issue template does not have a field for a comment, so here it is.

I moved from npm v6 to npm v9.5.0. npm v6 uses opener, it works fine. npm v9.5.0 uses this package and it looks like this package runs different command, even though it detects that I'm using WSL.

I am not quite sure that it is a bug in the package. Maybe I'm using not supported version of WSL or something. But here what was going on with opener:

  1. npm run opener with a proper URL and command/browser set to null.
  2. opener wrongly detected that I was using Linux and picked xdg-open as a command to run.
  3. The final command looked like xdg-open https://github.com/axios/axios. The command worked.

WSL detection was wrong in opener because my os.release() looks like this: '4.19.128-microsoft-standard+'. It does not contain Microsoft due to case of that string. However, I have xdg-open, so everything worked.

But now:

  1. npm runs promise-spawn with a proper URL and command set to null.
  2. promise-spawn detects that I'm using WSL
  3. Hence there is no command promise-spawn sets shell as process.env.ComSpec which is undefined on my setup.
  4. And it sets the command as start "".
  5. Finally it spawns the process with this arguments:
    command: undefined 
    realArgs: [ '-c', 'start "" https://github.com/axios/axios' ] 
    options: { command: null, shell: false }
    extra: {}
    

The process fails because there is no shell and command.

I've found a way to run npm repo by setting ComSpec env to force the script to use cmd.exe, but:

image

Tbh, I do not understand why it works with way, because as debugging shows the command it generates looks like this:

cmd.exe /d /s /c start "" https://github.com/axios/axios

When I start this command manually, it works:

$ cmd.exe /d /s /c start "" https://github.com/axios/axios
'\\wsl$\Ubuntu-20.04\home\ai\funbox\frontend-build-scripts'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.

A bit noisy, but it opens the browser.

@cg2916
Copy link

cg2916 commented Jan 23, 2024

Bumping as this means that npm login fails for this reason on Node 18 on WSL. You need to specify npm login --auth-type=legacy to avoid this issue.

It looks like npm/cli#6636 is the same issue.

wraithgar added a commit that referenced this issue Oct 18, 2024
Here's a more elegant solution for opening URLs from WSL in your
favorite browser. It is based on `sensible-browser` which is included in
the default distribution for WSL. This avoids issues with the WSL
environment, `cmd.exe`. quoting parameters, etc.

In WSL, set the default browser using the `BROWSER` variable, for
example,

```sh
export BROWSER="/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"
or
export BROWSER="/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"
```

Note: To permanently set the default browser, add the appropriate entry
to your shell's RC file, e.g. .bashrc or .zshrc.

To launch a URL from the WSL command line:

```sh
sensible-browser https://google.com
```

To launch a URL using `promise-spawn`:

```js
const promiseSpawn = require('@npmcli/promise-spawn')
promiseSpawn.open('https://google.com')
```

Replaces #118
Closes #62

### Test

```
os: 5.15.153.1-microsoft-standard-WSL2
node: 20.18.0
npm: 10.8.2
```


![image](https://github.com/user-attachments/assets/899801c5-6f05-477e-92c7-f2669526fa03)

---------

Co-authored-by: Gar <wraithgar@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Needs Triage needs an initial review
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants