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
Two string literals directly added to each other should be combined #321
changed the title [-]Two static strings directly added to each other should be merged[/-][+]Two string literals directly added to each other should be combined[/+]on Aug 1, 2025
This would be a syntax transformation, and a non-trivial one at that. These are out of scope for a mere formatter. The programmer may or may not have good reasons to write their code like this, writing it one way or the other carries an intent that would get lost otherwise. Nixfmt doesn't even touch redundant parentheses for that same reason. Nixfmt doesn't do any other string normalization either (it used to, but that was a mistake, and eventually got removed).
Reacted by Jeremy Fleischman, Matt Sturgeon and Peder Bergebakken Sundt
I have to disagree here: if two non-template literals are being directly concatenated to each other with +, to me this indicates a probable error. And if it isn't an error, builtins.concatStrings [ "a" "b" ... ] almost certainly makes more sense semantically. If you can show me a counterexample, that would be sufficient to change my mind on this.
I agree this would be non-trivial syntax transformation. The example I provided was one I wrote about 6 months ago when I didn't realize that you could escape ${ with ''. That being said, I don't immediately see a compelling reason why these kinds of transformations should be forbidden. If you could link to an existing discussion on why this is the case, that would be appreciated.
if two non-template literals are being directly concatenated to each other with +, to me this indicates a probable error
This could be one of 2 things:
A code correctness issue. For example: "I was supposed to interpolate something between the 2 strings but forgot to."
Just a silly mistake where transforming it into a single string isn't an issue.
We don't know which of these it is. No tool except the coder can. This is a place for a linter that calls out these red flags, not a code formatter that often runs invisibly in the background (such as when people have format on save enabled).
then the solution is to lint it, not to silently "fix" it.
If you can show me a counterexample, that would be sufficient to change my mind on this.
I don't have any specific counter example at hand rn, but some questions that would need answering in order to implement such a feature:
What if there is a comment between the two strings?
What if the strings use different literal syntax? What if there are more than two?
What if someone is splitting a string into a concatenation in order to add code in-between, but then saves the file intermittently and has autoformat-on-save enabled? (Nixfmt already has issues with diff noise that would be quite annoying with autoformat-on-save, but we are trying to reduce them and not to add more)
That being said, I don't immediately see a compelling reason why these kinds of transformations should be forbidden. If you could link to an existing discussion on why this is the case, that would be appreciated.
This is basically an empirical insight that has been gathered from the fact that it wasn't always like this. Initially it wasn't a conscious decision, just gradually rolling back string normalization features one at a time as issues arose. But by now, we effectively assert in our CI that nixfmt does not change the output of nix-instantiate --parse. The RFC166 text even states this as one of the non-goals: "Code style aspects that are not purely syntactic (e.g. optional/redundant parentheses, adding inherit statements, swapping argument order with flip, ...)"
As for discussion notes, I'd have to dig but there are a couple of old issues on this. There also were some discussions in the meetings, especially regarding the question about removing redundant parentheses, not sure when and if there are meeting notes.
"fun" fact: Changing concatenations into a string is technically a breaking change, because concatenations are not allowed in the Flake-Nix language, and thus changing this technically results in a semantic difference in observable behavior. "funner" fact: Internally, interpolations ("${...}") desugar to string concatenation, so in Flake-Nix, you are not allowed to interpolate
For the record, we just discussed this with the team, and everyone present agrees that this is a code smell, but a formatter should not silently resolve it. We encourage linters that call these sorts of things out.
Activity
[-]Two static strings directly added to each other should be merged[/-][+]Two string literals directly added to each other should be combined[/+]piegamesde commentedon Aug 1, 2025
TL;DR: No.
This would be a syntax transformation, and a non-trivial one at that. These are out of scope for a mere formatter. The programmer may or may not have good reasons to write their code like this, writing it one way or the other carries an intent that would get lost otherwise. Nixfmt doesn't even touch redundant parentheses for that same reason. Nixfmt doesn't do any other string normalization either (it used to, but that was a mistake, and eventually got removed).
Pandapip1 commentedon Aug 1, 2025
I have to disagree here: if two non-template literals are being directly concatenated to each other with
+, to me this indicates a probable error. And if it isn't an error,builtins.concatStrings [ "a" "b" ... ]almost certainly makes more sense semantically. If you can show me a counterexample, that would be sufficient to change my mind on this.I agree this would be non-trivial syntax transformation. The example I provided was one I wrote about 6 months ago when I didn't realize that you could escape
${with''. That being said, I don't immediately see a compelling reason why these kinds of transformations should be forbidden. If you could link to an existing discussion on why this is the case, that would be appreciated.jfly commentedon Aug 1, 2025
This could be one of 2 things:
We don't know which of these it is. No tool except the coder can. This is a place for a linter that calls out these red flags, not a code formatter that often runs invisibly in the background (such as when people have format on save enabled).
piegamesde commentedon Aug 1, 2025
then the solution is to lint it, not to silently "fix" it.
I don't have any specific counter example at hand rn, but some questions that would need answering in order to implement such a feature:
This is basically an empirical insight that has been gathered from the fact that it wasn't always like this. Initially it wasn't a conscious decision, just gradually rolling back string normalization features one at a time as issues arose. But by now, we effectively assert in our CI that nixfmt does not change the output of
nix-instantiate --parse. The RFC166 text even states this as one of the non-goals: "Code style aspects that are not purely syntactic (e.g. optional/redundant parentheses, addinginheritstatements, swapping argument order withflip, ...)"As for discussion notes, I'd have to dig but there are a couple of old issues on this. There also were some discussions in the meetings, especially regarding the question about removing redundant parentheses, not sure when and if there are meeting notes.
"fun" fact: Changing concatenations into a string is technically a breaking change, because concatenations are not allowed in the Flake-Nix language, and thus changing this technically results in a semantic difference in observable behavior. "funner" fact: Internally, interpolations (
"${...}") desugar to string concatenation, so in Flake-Nix, you are not allowed to interpolatejfly commentedon Aug 5, 2025
For the record, we just discussed this with the team, and everyone present agrees that this is a code smell, but a formatter should not silently resolve it. We encourage linters that call these sorts of things out.
nixos-discourse commentedon Aug 5, 2025
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/formatting-team-meeting-2025-08-05/67630/1