Unverified Commit 916c855b authored by 루밀LuMir's avatar 루밀LuMir Committed by GitHub

fix: `next info` command does not output the versions of `npm` `yarn` and `pnpm` correctly (#71134)

# 🐛Fixed a bug 

Hello, I've fixed a bug where the `next info` command does not correctly
output the versions of `npm`, `yarn`, and `pnpm` especially in
**Windows**.

## Why?

Although `npm`, `yarn`, and `pnpm` are correctly installed, the `next
info` command does not accurately display their versions, as shown in
the screenshot below.


![image](https://github.com/user-attachments/assets/efcda4c6-0a54-4421-8500-86acda3155b9)

## How?

### 1. Fallback to `execSync`

If `execFileSync` throws an error (indicating that the command could not
be executed or the output was not valid), the code now attempts to run
the same command using `execSync`. This provides a second chance to
retrieve the version.

### 2. Error Handling

Both `execFileSync` and `execSync` are wrapped in their respective
try-catch blocks. If both attempts fail, the function will return
`'N/A'`, just like before.

This change enhances the robustness of the function by ensuring that it
can still retrieve the version information even if the first method
fails due to environment variable issues or other reasons related to how
the command is executed.

> `execSync` and `execFileSync` execute processes in different ways in
Node.js. The differences between these two functions can lead to
variations in the output of the Package Manager's version.
> 
> `execSync`
>
> `execSync` accepts commands as strings and executes them in a shell.
During this process, all shell environment variables are applied,
allowing the command to run with any environment variables related to
Package Managers. For example, calling `execSync('npm --version')` will
utilize all environment variables associated with the current user's
environment, ensuring that `npm` is correctly installed and located.
>
> `execFileSync`
>
> `execFileSync`, on the other hand, takes the executable file and its
arguments as an array and runs the file directly. This means that it
starts the process without going through the shell, so shell environment
variables and settings are not applied. Since Package Managers are
generally designed to work correctly when executed in a shell, running
Package Managers via `execFileSync` may lead to missing environment
variables, resulting in incorrect output.

## Result

It now works properly.


![image](https://github.com/user-attachments/assets/93ab0389-bce9-4674-b458-2df1c57a5b5d)

---

- fixes: I tried to find but no related issues.
Co-authored-by: 's avatarSam Ko <sam@vercel.com>
parent 3287d709
...@@ -82,7 +82,11 @@ function getBinaryVersion(binaryName: string) { ...@@ -82,7 +82,11 @@ function getBinaryVersion(binaryName: string) {
.toString() .toString()
.trim() .trim()
} catch { } catch {
return 'N/A' try {
return childProcess.execSync(`${binaryName} --version`).toString().trim()
} catch {
return 'N/A'
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment