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

Tab-indented multiline-strings are turned into single-line strings #116

Open
infinisil opened this issue Apr 5, 2023 · 3 comments
Open
Labels
bug Something isn't working
Milestone

Comments

@infinisil
Copy link
Member

Input (note that every line starts with a single tab character):

	''
	  foo
	  bar
	''

Expected output:

''
  foo
  bar
''

Actual output:

"  foo\n  bar\n"

This can be disastrous: https://gitlab.com/Fuzen-py/configsreborn/-/blob/2a6b5aa5c8bee37cc13ab35d186b8fa604f62b2c/Snow/homeManager/programs/waybar/default.nix#L93-96

@Lucus16
Copy link
Contributor

Lucus16 commented Apr 5, 2023

This does indeed generate ugly output. The thing is, your suggested expected output changes the contents of the string (it removes the leading spaces). In fact, this string is impossible to format as indented strings without those leading tabs. Those tabs are a bit of a hack and I don't want to generate code like that. The proper solution could be to use concatStringsSep or concatMap to add the spaces to a list of lines or it could be to remove the leading spaces, but that's a decision that the programmer should make. Nixfmt should never change the semantics of the code on its own. Generating function calls is also tricky because we can't be sure whether lib or builtins is bound to what we expect. In the end it's best to leave it to the user to detect these cases and manually choose a solution.

PS. If you lost access to the unformatted indented string and feel it's a lot of work to recover the indented string, you could remove the indentation of any single line in the string and reformat again and nixfmt will turn it back into an indented string since it now has a valid representation as indented string.

@Lucus16
Copy link
Contributor

Lucus16 commented Apr 5, 2023

I just checked nix repl and it seems Nix also includes the tabs as part of the string which means nixfmt is still changing semantics in this case. I'm considering making tabs in indentation a parse error.

@yorickvP yorickvP added the bug Something isn't working label Apr 6, 2023
@yorickvP yorickvP added this to the 0.6 milestone Apr 6, 2023
@infinisil infinisil moved this to Todo in Nix formatting May 28, 2024
@dasJ
Copy link
Member

dasJ commented Sep 18, 2024

This changed (probably from @infinisil's new multiline string handling). It now returns a 4-space indented multiline string:

''
    foo
    bar
''

or

00000000: 2727 0a20 2020 2066 6f6f 0a20 2020 2062  ''.    foo.    b
00000010: 6172 0a27 270a                           ar.''.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Todo
Development

No branches or pull requests

4 participants