You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Nu script files cannot accept the string '(', '[', or "'" as arguments. #9939
A:parserIssues related to parsingcategory:bugSomething isn't workingusage:script-fileIssues related to executing a script file (as opposed to a module or sourcing)
Nu script files cannot accept the string '(', '[', or "'" as arguments.
The commands below produce the same error:
~/dev/nushell> nu test.nu '('
Error: nu::parser::unexpected_eof
× Unexpected end of code.
╭─[<commandline>:1:1]
1 │ main (
╰────
~/dev/nushell> nu test.nu '['
Error: nu::parser::unexpected_eof
× Unexpected end of code.
╭─[<commandline>:1:1]
1 │ main [
╰────
~/dev/nushell> nu test.nu "'"
Error: nu::parser::unexpected_eof
× Unexpected end of code.
╭─[<commandline>:1:1]
1 │ main '
╰────
How to reproduce
Create a script file using the following command: echo 'def main [a] { $a }' | save --force test.nu
Execute the created script file with the string argument '(' as follows: nu test.nu '('
Expected behavior
For a script file like def main [a] { $a }, it should return the same character given, no matter what character is provided.
For example:
> echo 'def main [a] { $a }' | save --force test.nu
> nu test.nu '('
(
In the above case, when the string argument '(' is provided to the script, it should return '('.
changed the title [-]Nu script files cannot accept the string '(', '[', or "'".[/-][+]Nu script files cannot accept the string '(', '[', or "'" as arguments.[/+]on Aug 7, 2023
I've read the related issues. Here's what I think: Being able to handle all strings is a basic requirement for any script file. So, here are my suggestions:
For the main command calling code generation process:
Process quoting Optional positional parameters first.
After optional params quoting, quote all the other parameters as strings.
After quoting, generate type casting code like ('10' | into int) using Parameter types.
changed the title [-]Nu script files cannot accept the string '(', '[', or "'" as arguments.[/-][+]Nu script files cannot accept the string `'('`, `'['`, or `"'"` as arguments.[/+]on Aug 7, 2023
I've looked into escape_for_script_arg and feel there may also be other problems.
Regarding arg_value and input in the function, I believe that we should modify the code to escape all values first and then perform type casting.
As for arg_name, I'm not sure if it can be escaped as my knowledge is limited in this area. I need to investigate this. If arg_name doesn't support escaping and can't accept arbitrary strings, then I think the program should fail when there is an invalid value in arg_name.
Furthermore, I believe gather_commandline_args shouldn't escape args_to_script. Instead, escaping should happen when generating the code calling main. This would allow for type casting according to main's parameters. And also, gathering the code escaping and generation together would prevent potential issues with double escaping or forgetting to escape, thus increasing maintainability.
For example:
let args = format!("main {}", args.map(|a| escape_for_script_arg(a, type_hint_or_something)).collect().join(" "));
I've been looking into the parser code. Base on that, I am considering the following implementation:
In the gather_commandline_args function, I'll remove the call to escape_for_script_arg.
In the evaluate_file function, when generating the code for main, it will apply escape_for_script_arg for each arg.
in the escape_for_script_arg function, I'll modify it to accept a second argument named is_flag_possible. If set to true, it will try to parse arg as a long flag or a short flag if possible, and reconstruct them as nushell flags (I plan to use nu-parser's parse_long_flag and parse_short_flag in this parse process). If arg is not flag, they will undergo type-casting using the into xxx command, determined by the main block signature's SyntaxShape.
The evaluate_file function, if it encounters "--" in the args, will set is_flag_possible to false for escape_for_script_arg to ensure that any arguments after "--" are not treated as flags. I believe this is necessary to ensure the command can accept all types of strings, even those beginning with a hyphen.
We're currently investigating if we can get the raw commandline args passed to nu before they're processed. If we can, we can pass those directly to the nushell parser. If we can't, we may need to switch main to taking in strings as that's all we can ensure from outside of nushell.
@fdncred@jntrnr I apologize, I was asleep at 3pm central time. Should I wait for your investigation before starting the implementation? Regardless, I'll join the discord.
@jntrnr I'm curious about one thing, "raw commandline args ". When launched from other shells like bash, I was under the impression that command-line arguments can only be received as byte sequences (in most cases, they're utf-8 string). Is this understanding correct? Are you suggesting defining a special launch protocol when only launching a nu script file from nushell, allowing it to pass arguments while retaining their types and structures?
@amachang yes, you should wait until @jntrnr finishes the investigation. we'd hate for you to do a bunch of work and then we have to go a different direction.
So far, I haven't been able to find how to reliably get the raw commandline args, as shells are allowed to manipulate these args before they invoke the external application.
Because of this, I'm leaning to requiring main and family to take in string args as positional arguments and string arguments as as flag parameters.
I don't think we can try to detect the type, because these two would look the same:
> nu 3
> nu "3"
Since the shell will strip the quotes off the second one before it hands it to us. I think we're just stuck with strings.
@jntrnr Thank you for your prompt and detailed response. I apologize for the delay in my response due to the time difference.
We're on the same page regarding the necessity for Rust's entry point, main, to accept command line arguments as strings.
Building on that, have you considered using the declaration of parameter types in the nushell script's main command, like def main [i: int, -a: string], to convert these strings? For instance, within nu-cli's evaluate_file (eval_file.rs) there's a section like:
if engine_state.find_decl(b"main",&[]).is_some(){let args = format!("main {}", args.join(" "));
Given this, we can extract the SyntaxShape of positional args and flags from the main block's signature. This would let us know the declared parameter types for nushell's main. Using this, for a main defined as def main [i: int] { $i }, we could embed it as main ('10' | into int). What are your thoughts on this?
As mentioned earlier, it should be possible to extract flag SyntaxShape and identifiers from the main block. First, making nu-parser's parse_long_flags and parse_short_flags a bit more abstract and versatile, and then, they can be used to match only those flags that are declared in the main's signature. If we pursue this, I believe we would need a mechanism to stop flag parsing to allow positional args that start with a hyphen, possibly after a -- delimiter appears.
Please note that the above is just my opinion and you don't have to incorporate it. Once the direction of the project is decided, I'd like to follow and help out in that direction.
The example from the issue reproduction steps does not crash in 0.112.1, but nushell now adds quotations to some inputs, which breaks my specific use case, where I use a small nushell script as a shim for psql and some arguments now grow quotes around them, which breaks psql.
The arguments from this thread reach main as strings by now. #18217 fixed the delimiter and metacharacter cases, and #19486 covers = and a few more, which will be in the release after 0.117.
Arguments starting with - need a -- in front of them.
A:parserIssues related to parsingcategory:bugSomething isn't workingusage:script-fileIssues related to executing a script file (as opposed to a module or sourcing)
Activity
[-]Nu script files cannot accept the string '(', '[', or "'".[/-][+]Nu script files cannot accept the string '(', '[', or "'" as arguments.[/+]amachang commentedon Aug 7, 2023
Possibly related:
#9418
#5472
#5753
#5532
#5445
#5370
#5371
#9766
amachang commentedon Aug 7, 2023
I've read the related issues. Here's what I think: Being able to handle all strings is a basic requirement for any script file. So, here are my suggestions:
For the main command calling code generation process:
Optional positional parametersfirst.('10' | into int)usingParameter types.[-]Nu script files cannot accept the string '(', '[', or "'" as arguments.[/-][+]Nu script files cannot accept the string `'('`, `'['`, or `"'"` as arguments.[/+]amtoine commentedon Aug 7, 2023
might also be related to #9766 where i observe the quotes being removed before the arguments are passed to the script 🤔
which means
"-a"becomes-aand is not a valid option in my issue and here"("becomes(and thus gives a syntax error 😕amachang commentedon Aug 7, 2023
@amtoine Thanks for linking another issue. I added it to my comment above.
amachang commentedon Aug 7, 2023
I've looked into
escape_for_script_argand feel there may also be other problems.Regarding
arg_valueandinputin the function, I believe that we should modify the code to escape all values first and then perform type casting.As for
arg_name, I'm not sure if it can be escaped as my knowledge is limited in this area. I need to investigate this. Ifarg_namedoesn't support escaping and can't accept arbitrary strings, then I think the program should fail when there is an invalid value inarg_name.amtoine commentedon Aug 7, 2023
thanks for investigating into this @amachang 🙏
amachang commentedon Aug 7, 2023
Furthermore, I believe
gather_commandline_argsshouldn't escapeargs_to_script. Instead, escaping should happen when generating the code calling main. This would allow for type casting according to main's parameters. And also, gathering the code escaping and generation together would prevent potential issues with double escaping or forgetting to escape, thus increasing maintainability.For example:
fdncred commentedon Aug 7, 2023
Seems like something good to PR. @amachang Do you have time to try and fix this?
2 remaining items
amachang commentedon Aug 8, 2023
I've been looking into the parser code. Base on that, I am considering the following implementation:
gather_commandline_argsfunction, I'll remove the call toescape_for_script_arg.evaluate_filefunction, when generating the code formain, it will applyescape_for_script_argfor each arg.escape_for_script_argfunction, I'll modify it to accept a second argument namedis_flag_possible. If set to true, it will try to parse arg as a long flag or a short flag if possible, and reconstruct them as nushell flags (I plan to usenu-parser'sparse_long_flagandparse_short_flagin this parse process). If arg is not flag, they will undergo type-casting using theinto xxxcommand, determined by the main block signature's SyntaxShape.evaluate_filefunction, if it encounters"--"in the args, will setis_flag_possibleto false forescape_for_script_argto ensure that any arguments after"--"are not treated as flags. I believe this is necessary to ensure the command can accept all types of strings, even those beginning with a hyphen.fdncred commentedon Aug 9, 2023
We plan on talking a little about this in our meeting today at 3pm central time. Feel free to jump in on the open-mic channel in discord.
sophiajt commentedon Aug 9, 2023
We're currently investigating if we can get the raw commandline args passed to
nubefore they're processed. If we can, we can pass those directly to the nushell parser. If we can't, we may need to switchmainto taking in strings as that's all we can ensure from outside of nushell.amachang commentedon Aug 10, 2023
@fdncred @jntrnr I apologize, I was asleep at 3pm central time. Should I wait for your investigation before starting the implementation? Regardless, I'll join the discord.
amachang commentedon Aug 10, 2023
@jntrnr I'm curious about one thing, "raw commandline args ". When launched from other shells like bash, I was under the impression that command-line arguments can only be received as byte sequences (in most cases, they're utf-8 string). Is this understanding correct? Are you suggesting defining a special launch protocol when only launching a nu script file from nushell, allowing it to pass arguments while retaining their types and structures?
fdncred commentedon Aug 10, 2023
@amachang yes, you should wait until @jntrnr finishes the investigation. we'd hate for you to do a bunch of work and then we have to go a different direction.
amachang commentedon Aug 10, 2023
@fdncred Roger that 🫡
sophiajt commentedon Aug 11, 2023
So far, I haven't been able to find how to reliably get the raw commandline args, as shells are allowed to manipulate these args before they invoke the external application.
Because of this, I'm leaning to requiring
mainand family to take in string args as positional arguments and string arguments as as flag parameters.I don't think we can try to detect the type, because these two would look the same:
Since the shell will strip the quotes off the second one before it hands it to us. I think we're just stuck with strings.
amachang commentedon Aug 12, 2023
@jntrnr Thank you for your prompt and detailed response. I apologize for the delay in my response due to the time difference.
def main [i: int, -a: string], to convert these strings? For instance, within nu-cli'sevaluate_file(eval_file.rs) there's a section like:Given this, we can extract the
SyntaxShapeof positional args and flags from the main block'ssignature. This would let us know the declared parameter types for nushell's main. Using this, for a main defined asdef main [i: int] { $i }, we could embed it asmain ('10' | into int). What are your thoughts on this?SyntaxShapeand identifiers from the main block. First, making nu-parser'sparse_long_flagsandparse_short_flagsa bit more abstract and versatile, and then, they can be used to match only those flags that are declared in the main's signature. If we pursue this, I believe we would need a mechanism to stop flag parsing to allow positional args that start with a hyphen, possibly after a--delimiter appears.Please note that the above is just my opinion and you don't have to incorporate it. Once the direction of the project is decided, I'd like to follow and help out in that direction.
unnamedname commentedon Nov 18, 2023
In addition, other metacharacters are also unacceptable, like bare
=, strings containing| ; { }or beginning with$:jasha-hrp commentedon Jul 19, 2025
A workaround that I've found useful is to make the script use stdin instead of an argument:
samoylovfp commentedon Apr 29, 2026
The example from the issue reproduction steps does not crash in
0.112.1, but nushell now adds quotations to some inputs, which breaks my specific use case, where I use a small nushell script as a shim for psql and some arguments now grow quotes around them, which breaks psql.Script from the repro:
Stop script arguments from running as code (#19486)
kronberger-droid commentedon Nov 14, 2026
The arguments from this thread reach
mainas strings by now.#18217 fixed the delimiter and metacharacter cases, and #19486 covers
=and a few more, which will be in the release after 0.117.Arguments starting with
-need a--in front of them.