Skip to content

random-list: fix dice and the error helper on current nushell - #1301

Open
qodelicious wants to merge 1 commit into
nushell:mainfrom
qodelicious:fix-random-list-dice
Open

qodelicious wants to merge 1 commit into
nushell:mainfrom
qodelicious:fix-random-list-dice

Conversation

@qodelicious

Copy link
Copy Markdown
Contributor

random-list dice didn't parse on current nushell because random dice moved out of the builtins into std/random. The module now does use std/random at the top.

While testing that I also hit the error helper failing with "missing span column", since error make wants span in the label now instead of separate start and end. It passes the span through directly so the validation errors show up again.

Tested on 0.116.1. nu --ide-check is clean, every random-list subcommand returns what it should, and passing a negative count or a bias above 1 shows the right error pointing at the argument.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review overview

🟡 Changes recommended

The std/random import and the span change work on 0.116.2. What's left is argument checks that let 0 through.

Findings: 0 High severity · 2 Medium severity · 2 Low severity

Open (4)

Notes from my clanker, checked on 84629e8 with nu 0.116.2. I ran every random-list subcommand with valid input, 0, negative counts, an out-of-range --bias, and dice counts of 0 and 1.

@@ -7,8 +9,7 @@ def throw-error [
msg: $"(ansi red_bold)($error)(ansi reset)"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Low severity ANSI codes end up in the error's msg

ansi red_bold and ansi reset are baked into the message string, so try { random-list bool -1 } catch {|e| $e.msg } returns "\e[1;31mvalue_error\e[0m", even with NO_COLOR=1. The error renderer already colors the headline, and msg: $error keeps $e.msg easy to match. This was already there before the PR.

@@ -1,3 +1,5 @@
use std/random

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Medium severity Dice checks don't match std/random's dice

With this import, random dice is std's version, which asserts --dice > 0 and --sides > 1. The checks in random-list dice only reject negatives, so random-list dice 1 -r 0, -s 0 and -s 1 pass them and fail inside std, with the label at std/random/mod.nu:16/:17 instead of on the user's flag. Checking $roll_count < 1 and $side_count < 2, with messages that say so, keeps the error on the argument.

if $roll_count < 0 {
throw-error "value_error" "must be greater than 0" (metadata $roll_count | get span)
}
if $side_count < 0 {
throw-error "value_error" "must be greater than 0" (metadata $side_count | get span)
}

text: $msg
start: $span.start
end: $span.end
span: $span

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Low severity The same start/end label is still used in other scripts

modules/network/remoting/remoting.nu:76-77, sourced/filesystem/cdpath.nu:157-158 and sourced/fun/spark.nu:33 still pass start/end, which fails with "Value is missing required columns" on 0.116.2 when those errors fire. Fine as a follow-up if you'd rather keep this PR to random-list.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Medium severity A count of 0 returns two items

The guards only reject < 0, so 0 gets through, and 1..0 counts down to [1, 0]. random-list bool 0 | length, random-list uuid 0 | length and random-list dice 0 | length all return 2 on 0.116.2. Every subcommand uses the same 1..$list_length loop (lines 29, 46, 60, 81, 95 and 108), and 0..<$list_length gives 0 items for 0 and n items for n. The message "must be greater than 0" also says 0 is invalid while the check accepts it; "must not be negative" matches the check. This was already there before the PR, but the description says every subcommand returns what it should.

if $list_length < 0 {
throw-error "value_error" "must be greater than 0" (metadata $list_length | get span)
}
1..$list_length | each {|it|
random bool --bias $bias
}

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