From c0089454064d7104df9433f61618836fab927fdf Mon Sep 17 00:00:00 2001 From: pederbe Date: Sun, 6 Sep 2026 18:40:11 +0200 Subject: [PATCH] fix: log already-installed warning at info level --- .changeset/tidy-installed-warning.md | 5 ++ .../existing-installation.test.ts.snap | 8 +-- e2e/existing-installation.test.ts | 7 ++- e2e/repros/issue-1461.test.ts | 51 +++++++++++++++++++ src/commands/install.rs | 2 +- 5 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 .changeset/tidy-installed-warning.md create mode 100644 e2e/repros/issue-1461.test.ts diff --git a/.changeset/tidy-installed-warning.md b/.changeset/tidy-installed-warning.md new file mode 100644 index 000000000..ed74aae38 --- /dev/null +++ b/.changeset/tidy-installed-warning.md @@ -0,0 +1,5 @@ +--- +"fnm": patch +--- + +Log the already-installed warning at info level so successful repeated installations do not write to stderr or appear with `--log-level error`. Fixes #1461. diff --git a/e2e/__snapshots__/existing-installation.test.ts.snap b/e2e/__snapshots__/existing-installation.test.ts.snap index ae785ac52..ff359e1d0 100644 --- a/e2e/__snapshots__/existing-installation.test.ts.snap +++ b/e2e/__snapshots__/existing-installation.test.ts.snap @@ -4,25 +4,25 @@ exports[`Bash warns about an existing installation: Bash 1`] = ` "set -e eval "$(fnm env)" fnm install v8.11.3 -(fnm install v8.11.3 2>&1) | grep 'already installed' || (echo "Expected output to contain 'already installed'" && exit 1)" +(fnm install v8.11.3) | grep 'already installed' || (echo "Expected output to contain 'already installed'" && exit 1)" `; exports[`Fish warns about an existing installation: Fish 1`] = ` "fnm env | source fnm install v8.11.3 -begin; fnm install v8.11.3 2>&1; end | grep 'already installed'; or echo "Expected output to contain 'already installed'" && exit 1" +begin; fnm install v8.11.3; end | grep 'already installed'; or echo "Expected output to contain 'already installed'" && exit 1" `; exports[`PowerShell warns about an existing installation: PowerShell 1`] = ` "$ErrorActionPreference = "Stop" fnm env | Out-String | Invoke-Expression fnm install v8.11.3 -$($__out__ = $(fnm install v8.11.3 2>&1 | Select-String 'already installed'); if ($__out__ -eq $null) { exit 1 } else { $__out__ })" +$($__out__ = $(fnm install v8.11.3 | Select-String 'already installed'); if ($__out__ -eq $null) { exit 1 } else { $__out__ })" `; exports[`Zsh warns about an existing installation: Zsh 1`] = ` "set -e eval "$(fnm env)" fnm install v8.11.3 -(fnm install v8.11.3 2>&1) | grep 'already installed' || (echo "Expected output to contain 'already installed'" && exit 1)" +(fnm install v8.11.3) | grep 'already installed' || (echo "Expected output to contain 'already installed'" && exit 1)" `; diff --git a/e2e/existing-installation.test.ts b/e2e/existing-installation.test.ts index 2fb0f90d1..95f4e1299 100644 --- a/e2e/existing-installation.test.ts +++ b/e2e/existing-installation.test.ts @@ -1,4 +1,3 @@ -import getStderr from "./shellcode/get-stderr.js" import { script } from "./shellcode/script.js" import { Bash, Fish, PowerShell, Zsh } from "./shellcode/shells.js" import describe from "./describe.js" @@ -11,9 +10,9 @@ for (const shell of [Bash, Zsh, Fish, PowerShell]) { .then(shell.call("fnm", ["install", "v8.11.3"])) .then( shell.scriptOutputContains( - getStderr(shell.call("fnm", ["install", "v8.11.3"])), - "'already installed'" - ) + shell.call("fnm", ["install", "v8.11.3"]), + "'already installed'", + ), ) .takeSnapshot(shell) .execute(shell) diff --git a/e2e/repros/issue-1461.test.ts b/e2e/repros/issue-1461.test.ts new file mode 100644 index 000000000..fa363ea66 --- /dev/null +++ b/e2e/repros/issue-1461.test.ts @@ -0,0 +1,51 @@ +import { writeFile } from "node:fs/promises" +import { join } from "node:path" +import describe from "../describe.js" +import { script } from "../shellcode/script.js" +import { Bash, Fish, PowerShell, Zsh } from "../shellcode/shells.js" +import testCwd from "../shellcode/test-cwd.js" + +for (const shell of [Bash, Zsh, Fish, PowerShell]) { + describe(shell, () => { + test("issue #1461: repeated installation respects log levels", async () => { + await writeFile( + join(testCwd(), "check-install.cjs"), + ` + const assert = require("assert") + const { spawnSync } = require("child_process") + for (const level of ["info", "all", "error", "quiet"]) { + const installed = spawnSync("fnm", ["--log-level", level, "install", "v8.11.3"], { encoding: "utf8" }) + assert.strictEqual(installed.status, 0, installed.stderr) + assert.strictEqual(installed.stderr, "") + if (level === "info" || level === "all") { + assert.ok(/already installed/.test(installed.stdout)) + } else { + assert.strictEqual(installed.stdout, "") + } + + const failed = spawnSync("fnm", ["--log-level", level, "alias", "abcd", "efg"], { encoding: "utf8" }) + assert.strictEqual(failed.status, 1) + assert.strictEqual(failed.stdout, "") + if (level === "quiet") { + assert.strictEqual(failed.stderr, "") + } else { + assert.ok(/find requested version/.test(failed.stderr)) + } + } + console.log("logging checks passed") + `, + ) + await script(shell) + .then(shell.env({})) + .then(shell.call("fnm", ["install", "v8.11.3"])) + .then( + shell.hasCommandOutput( + shell.call("node", ["check-install.cjs"]), + "logging checks passed", + "installation logging checks", + ), + ) + .execute(shell) + }) + }) +} diff --git a/src/commands/install.rs b/src/commands/install.rs index 6db7a81cc..4ec3d0229 100644 --- a/src/commands/install.rs +++ b/src/commands/install.rs @@ -152,7 +152,7 @@ impl Command for Install { show_progress, ) { Err(err @ DownloaderError::VersionAlreadyInstalled { .. }) => { - outln!(config, Error, "{} {}", "warning:".bold().yellow(), err); + outln!(config, Info, "{} {}", "warning:".bold().yellow(), err); } Err(source) => Err(Error::DownloadError { source })?, Ok(()) => {}