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

fix: error invalid arg type on WSL systems on url open #118

Closed

Conversation

kchindam-infy
Copy link

@kchindam-infy kchindam-infy commented Jul 26, 2024

Fix the error invalid arg type on open url in browser on WSL envoirnment.
The npm error on WSL
npm notice Log in on https://registry.npmjs.org/
Login at:
https://www.npmjs.com/login?next=/login/cli/CENSORED
Press ENTER to open in the browser...
npm ERR! code ERR_INVALID_ARG_TYPE
npm ERR! The "file" argument must be of type string. Received undefined
npm ERR! Cannot read properties of undefined (reading 'stdin')

The process.env.ComSpec is undefined in WSL, leading to an incorrect shell configuration. The original command 'start ""' isn't recognized as a valid command in WSL. The fixed implementation correctly constructs the full Windows command, ensuring it works in both regular windows and WSL environments.

References

lib/index.js Fixed Show fixed Hide fixed
lib/index.js Fixed Show fixed Hide fixed
lib/index.js Fixed Show fixed Hide fixed
lib/index.js Fixed Show fixed Hide fixed
lib/index.js Fixed Show fixed Hide fixed
lib/index.js Fixed Show fixed Hide fixed
lib/index.js Fixed Show fixed Hide fixed
lib/index.js Fixed Show fixed Hide fixed
lib/index.js Fixed Show fixed Hide fixed
@kchindam-infy kchindam-infy marked this pull request as ready for review August 16, 2024 19:52
@kchindam-infy kchindam-infy requested a review from a team as a code owner August 16, 2024 19:52
@@ -141,11 +141,11 @@ const open = (_args, opts = {}, extra = {}) => {
if (platform === 'win32') {
// spawnWithShell does not do the additional os.release() check, so we
// have to force the shell here to make sure we treat WSL as windows.
options.shell = process.env.ComSpec
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting options.shell was load bearing here, spawnWithShell needs that behavior.

lib/index.js Outdated
// also, the start command accepts a title so to make sure that we don't
// accidentally interpret the first arg as the title, we stick an empty
// string immediately after the start command
command = 'start ""'
command = `${shellCommand} /c start ""`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing options.shell is likely why this had to change. it's not the right solution, you need to keep options.shell set and let the rest of the code build the /c part.

@@ -36,7 +36,7 @@
reject(Object.assign(er, resultError))
}

const proc = spawn(cmd, args, opts)
const proc = spawn(cmd, args, { ...opts, shell: true })

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This shell argument which depends on
library input
is later used in a
shell command
.
This shell argument which depends on
library input
is later used in a
shell command
.
This shell argument which depends on
library input
is later used in a
shell command
.
This shell argument which depends on
library input
is later used in a
shell command
.
This shell argument which depends on
library input
is later used in a
shell command
.
This shell argument which depends on
library input
is later used in a
shell command
.
wraithgar added a commit that referenced this pull request 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>
@wraithgar
Copy link
Member

#128

@wraithgar wraithgar closed this Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants