Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix repl renders strings improperly escaped #4909

Closed
sternenseemann opened this issue Jun 11, 2021 · 10 comments · Fixed by #8193
Closed

nix repl renders strings improperly escaped #4909

sternenseemann opened this issue Jun 11, 2021 · 10 comments · Fixed by #8193
Labels

Comments

@sternenseemann
Copy link
Member

Describe the bug

nix repl prints string values improperly sometimes where pasting the printed expression back into the repl would result in a syntax error due to missing escaping.

Steps To Reproduce

Evaluate ''''${checkFlagsArray[@]}'' or "\${checkFlagsArray[@]}" into nix repl and paste the printed result back:

nix-repl> ''''${checkFlagsArray[@]}'' 
"${checkFlagsArray[@]}"

nix-repl> "\${checkFlagsArray[@]}"    
"${checkFlagsArray[@]}"

nix-repl> "${checkFlagsArray[@]}"
error: syntax error, unexpected '@'

       at «string»:1:20:

            1| "${checkFlagsArray[@]}"
             |                    ^
            2|

Expected behavior

nix repl prints "\${checkFlagsArray[@]}"

Additional context

Affects nixUnstable and nix 2.13.11.

@roberth
Copy link
Member

roberth commented Jun 16, 2021

This isn't rendering a string, but presenting a Nix language parsing error in a confusing way.
The wrong part is «string», which should have been something like «repl». Below that is your input, not the supposed string your input intends to represent.

Does this look less confusing?

nix-repl> "${checkFlagsArray[@]}"
error: syntax error, unexpected '@'

       at «repl»:1:20:

            1| "${checkFlagsArray[@]}"
             |                    ^
            2|

An even nicer error would catch this specific mistake and show a custom error message explaining what you probably did wrong. Something like: it seems like you're using bash interpolation syntax inside a Nix interpolated string. Did you forget to escape the $ sign?

@sternenseemann
Copy link
Member Author

I don't really care about the error message, to be honest. The main issue is that the repl is actually rendering nix values in a way that they are not valid nix, i. e. a properly escaped nix string will evaluate to a nix string which is then rendered by the repl as invalid nix syntax:

nix-repl> ''''${checkFlagsArray[@]}'' 
"${checkFlagsArray[@]}"

The error message was just added to demonstrate that pasting back the printed nix expression in the repl will result in an error.

@stale
Copy link

stale bot commented Jan 3, 2022

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Jan 3, 2022
@sternenseemann
Copy link
Member Author

Hasn't been fixed.

@stale stale bot removed the stale label Jan 4, 2022
@stale
Copy link

stale bot commented Jul 10, 2022

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Jul 10, 2022
@sternenseemann
Copy link
Member Author

Still an issue in Nix 2.9.

@stale stale bot removed the stale label Jul 10, 2022
@sternenseemann
Copy link
Member Author

Basically, $ is never escaped, I guess because Nix reuses escaping rules for C++ which doesn't have string interpolation:

nix-repl> "\${foo}"
"${foo}"

@sternenseemann
Copy link
Member Author

sternenseemann commented Aug 25, 2022

Hm, this was fixed in #4012 for nix-instantiate at least:

$ nix-instantiate --eval -E '"\${foo}"'
"\${foo}"

I don't see why nix repl should behave differently here?

Edit:

// FIXME: lot of cut&paste from Nix's eval.cc.

@cyntheticfox
Copy link

Cool, I just ran into this today. I like to use REPLs though a lot, and might try to see about writing a PR to fix this. Might take a second though -- I'm a little out-of-practice with C++.

@roberth
Copy link
Member

roberth commented Apr 9, 2023

Hi @cyntheticfox, I've encountered the cause of the issue while resolving some tech debt.
I didn't read your message before, so I just wanted to let you know that a contribution would have been appreciated (but don't feel bad about it!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants