Files
Evgeny fab6af9c2b Use builtin command sigil to avoid bugs when user aliases shadow builtin (#19224)
## Description

User aliases should have no effect on nu's STD, but if you alias `alias
metadata = /non/exist/bin` you'll get a load error `Command
`/non/exist/bin` not found

This PR fixes that smaller issue by adding `%`, but doesn't resolve the
more general issue of other commands potentially having the same
conflict

## User-facing changes (Release notes)

- Fixed an issue where aliasing `metadata` would break its use in `std`

## Additional notes


Ideally there should be some linter that forces `%` to all .nu scripts
used in nushell
2026-10-13 06:33:12 -05:00

142 lines
4.3 KiB
Nu

# Add the given paths to the PATH.
@example "adding some dummy paths to an empty PATH" {
with-env { PATH: [] } {
path add "foo"
path add "bar" "baz"
path add "fooo" --append
path add "returned" --ret
}
} --result [returned bar baz foo fooo]
@example "adding paths based on $nu.os-info.name" {
path add {linux: "foo", windows: "bar", macos: "baz"}
}
export def --env "path add" [
--ret (-r) # return $env.PATH, useful in pipelines to avoid scoping.
--append (-a) # append to $env.PATH instead of prepending to.
...paths: any # the paths to add to $env.PATH.
]: [nothing -> nothing, nothing -> list<path>] {
ignore # discard the input, otherwise the `metadata` call below would fail
let span = (%metadata $paths).span
let paths = $paths | flatten
if ($paths | is-empty) or ($paths | length) == 0 {
error make {msg: "Empty input", label: {
text: "Provide at least one string or a record",
span: $span
}}
}
for path in $paths {
if ($path | describe -d).type not-in ['string', 'record'] {
error make {msg: 'Invalid input', label: {
text: 'Path must be a string or record',
span: (%metadata $path).span
}}
}
}
let path_name = if "PATH" in $env { "PATH" } else { "Path" }
let paths = $paths | each {|p|
match ($p | describe -d).type {
'string' => { $p | path expand --no-symlink },
'record' => {
if $nu.os-info.name in ($p | columns) {
$p | get $nu.os-info.name | path expand --no-symlink
}
}
}
} | compact
load-env {$path_name: (
$env | get $path_name
| split row (char esep)
| if $append { append $paths } else { prepend $paths }
| uniq
)}
if $ret { $env | get $path_name }
}
# The cute and friendly mascot of Nushell :)
export def ellie [] {
let ellie = [
" __ ,",
" .--()°'.'",
"'|, . ,'",
" !_-(_\\",
]
$ellie | str join "\n" | $"(ansi green)($in)(ansi reset)"
}
# Repeat anything a bunch of times, yielding a list of *n* times the input
@example "repeat a string" {
"foo" | std repeat 3 | str join
} --result "foofoofoo"
export def repeat [
n: int # the number of repetitions, must be positive
]: any -> list<any> {
let item = $in
if $n < 0 {
let span = metadata $n | get span
error make {
msg: $"(ansi red_bold)invalid_argument(ansi reset)"
label: {
text: $"n should be a positive integer, found ($n)"
span: $span
}
}
}
if $n == 0 {
return []
}
1..$n | each --keep-empty { $item }
}
# null device file
export const null_device = if $nu.os-info.name == "windows" {
'\\.\NUL'
} else {
'/dev/null'
}
# Return a null device file.
@example "run a command and ignore it's stderr output" {
cat xxx.txt e> (null-device)
}
export def null-device []: nothing -> path {
$null_device
}
# Structure a command-line string into its tokens, for custom completers.
#
# A completer that declares a `buffer` parameter receives the whole command line up to the
# cursor as a string. This turns it into a clean `{text, kind, span}` table: the parser's
# duplicate spans are collapsed and the `shape_` prefix is dropped from each `kind`. It is
# steadier than a bare `ast --flatten` for this, though the parser is still lossy — reach for
# the raw `buffer` when you need the text exactly as it was typed.
@example "structure a command line" {
std structure "ls -a | where name == foo"
} --result [
[text, kind, span];
[ls, internalcall, {start: 0, end: 2}]
[-a, flag, {start: 3, end: 5}]
["|", pipe, {start: 6, end: 7}]
[where, internalcall, {start: 8, end: 13}]
[name, string, {start: 14, end: 18}]
["==", operator, {start: 19, end: 21}]
[foo, string, {start: 22, end: 25}]
]
export def structure [
buffer: string # the command line to structure, e.g. a completer's `buffer` input
]: nothing -> table<text: string, kind: string, span: record<start: int, end: int>> {
ast --flatten $buffer
| rename text kind span
| update kind { str replace "shape_" "" }
| uniq-by span
}