Skip to content

Two string literals directly added to each other should be combined #321

Closed as not planned

Description

@Pandapip1

Description

When two string literals are directly added to one another, they should be combined into a single string literal.

Small example input

  postPatch =
    ''
      # Replace hardcoded bin install path
      substituteInPlace SerialPortConnector.pro \
        --replace-fail '/opt/$$''
    + ''
      {TARGET}/bin' "$out/bin"
    '';

Expected output

  postPatch = ''
    # Replace hardcoded bin install path
    substituteInPlace SerialPortConnector.pro \
      --replace-fail '/opt/$''${TARGET}/bin' "$out/bin"
  '';

Actual output

  postPatch = ''
    # Replace hardcoded bin install path
    substituteInPlace SerialPortConnector.pro \
      --replace-fail '/opt/$$''
  + ''
    {TARGET}/bin' "$out/bin"
  '';

Activity

  1. 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
  2. piegamesde commented on Aug 1, 2025

    @piegamesde
    Member

    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).

  3. Pandapip1 commented on Aug 1, 2025

    @Pandapip1
    MemberAuthor

    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.

  4. jfly commented on Aug 1, 2025

    @jfly
    Collaborator

    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:

    1. A code correctness issue. For example: "I was supposed to interpolate something between the 2 strings but forgot to."
    2. 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).

  5. piegamesde commented on Aug 1, 2025

    @piegamesde
    Member

    to me this indicates a probable error

    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

  6. jfly commented on Aug 5, 2025

    @jfly
    Collaborator

    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.

  7. nixos-discourse commented on Aug 5, 2025

    @nixos-discourse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions