Repository: psf/black

Issue:
Unstable formatting on split string + % formatting
This is on master.
```
$ cat kill_senior_procs.py 
if __name__ == "__main__":
    for i in range(4, 8):
        cmd = (
            r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk '{print $2}'); do kill $pid; done"
            % (i)
        )
$ black kill_senior_procs.py 
error: cannot format kill_senior_procs.py: INTERNAL ERROR: Black produced different code on the second pass of the formatter.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: /var/folders/wx/zkst6jdx1zg_s32d23gz4_0r0000gn/T/blk_bgpw4vyu.log
Oh no! 💥 💔 💥
1 file failed to reformat.
$ cat /var/folders/wx/zkst6jdx1zg_s32d23gz4_0r0000gn/T/blk_bgpw4vyu.log
--- source
+++ first pass
@@ -1,6 +1,7 @@
 if __name__ == "__main__":
     for i in range(4, 8):
         cmd = (
-            r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk '{print $2}'); do kill $pid; done"
+            r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk"
+            r" '{print $2}'); do kill $pid; done"
             % (i)
         )
--- first pass
+++ second pass
@@ -1,7 +1,6 @@
 if __name__ == "__main__":
     for i in range(4, 8):
         cmd = (
             r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk"
-            r" '{print $2}'); do kill $pid; done"
-            % (i)
+            r" '{print $2}'); do kill $pid; done" % (i)
         )
```
Unstable formatting on split string + % formatting
This is on master.
```
$ cat kill_senior_procs.py 
if __name__ == "__main__":
    for i in range(4, 8):
        cmd = (
            r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk '{print $2}'); do kill $pid; done"
            % (i)
        )
$ black kill_senior_procs.py 
error: cannot format kill_senior_procs.py: INTERNAL ERROR: Black produced different code on the second pass of the formatter.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: /var/folders/wx/zkst6jdx1zg_s32d23gz4_0r0000gn/T/blk_bgpw4vyu.log
Oh no! 💥 💔 💥
1 file failed to reformat.
$ cat /var/folders/wx/zkst6jdx1zg_s32d23gz4_0r0000gn/T/blk_bgpw4vyu.log
--- source
+++ first pass
@@ -1,6 +1,7 @@
 if __name__ == "__main__":
     for i in range(4, 8):
         cmd = (
-            r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk '{print $2}'); do kill $pid; done"
+            r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk"
+            r" '{print $2}'); do kill $pid; done"
             % (i)
         )
--- first pass
+++ second pass
@@ -1,7 +1,6 @@
 if __name__ == "__main__":
     for i in range(4, 8):
         cmd = (
             r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk"
-            r" '{print $2}'); do kill $pid; done"
-            % (i)
+            r" '{print $2}'); do kill $pid; done" % (i)
         )
```


Discussion:
And a fourth `git bisect` pointing to 544ea9c217cad9459d7b60665db787e94b52f93d from "Improve String Handling" (#1132).
Also now good on `master`

```console
$ black --check kill_senior_procs.py
All done! ✨ 🍰 ✨
1 file would be left unchanged.
```
And a fourth `git bisect` pointing to 544ea9c217cad9459d7b60665db787e94b52f93d from "Improve String Handling" (#1132).
Also now good on `master`

```console
$ black --check kill_senior_procs.py
All done! ✨ 🍰 ✨
1 file would be left unchanged.
```

Resolving patch:
diff --git a/src/black/__init__.py b/src/black/__init__.py
--- a/src/black/__init__.py
+++ b/src/black/__init__.py
@@ -2668,9 +2668,9 @@ def rhs(line: Line, features: Collection[Feature]) -> Iterator[Line]:
                 transformers = [
                     string_merge,
                     string_paren_strip,
+                    string_split,
                     delimiter_split,
                     standalone_comment_split,
-                    string_split,
                     string_paren_wrap,
                     rhs,
                 ]
diff --git a/src/blib2to3/pgen2/pgen.py b/src/blib2to3/pgen2/pgen.py
--- a/src/blib2to3/pgen2/pgen.py
+++ b/src/blib2to3/pgen2/pgen.py
@@ -168,8 +168,7 @@ def calcfirst(self, name: Text) -> None:
                 if symbol in inverse:
                     raise ValueError(
                         "rule %s is ambiguous; %s is in the first sets of %s as well"
-                        " as %s"
-                        % (name, symbol, label, inverse[symbol])
+                        " as %s" % (name, symbol, label, inverse[symbol])
                     )
                 inverse[symbol] = label
         self.first[name] = totalset


Test patch:
diff --git a/tests/data/long_strings.py b/tests/data/long_strings.py
--- a/tests/data/long_strings.py
+++ b/tests/data/long_strings.py
@@ -380,8 +380,7 @@ def foo():
 
 old_fmt_string1 = (
     "While we are on the topic of %s, we should also note that old-style formatting"
-    " must also be preserved, since some %s still uses it."
-    % ("formatting", "code")
+    " must also be preserved, since some %s still uses it." % ("formatting", "code")
 )
 
 old_fmt_string2 = "This is a %s %s %s %s" % (
@@ -448,8 +447,7 @@ def foo():
 
 assert some_type_of_boolean_expression, (
     "Followed by a really really really long string that is used to provide context to"
-    " the AssertionError exception, which uses dynamic string %s."
-    % "formatting"
+    " the AssertionError exception, which uses dynamic string %s." % "formatting"
 )
 
 assert some_type_of_boolean_expression, (
diff --git a/tests/data/long_strings__regression.py b/tests/data/long_strings__regression.py
--- a/tests/data/long_strings__regression.py
+++ b/tests/data/long_strings__regression.py
@@ -310,6 +310,13 @@ def who(self):
             passenger_association=passenger_association,
         )
 
+if __name__ == "__main__":
+    for i in range(4, 8):
+        cmd = (
+            r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk '{print $2}'); do kill $pid; done"
+            % (i)
+        )
+
 # output
 
 
@@ -435,14 +442,12 @@ def foo():
 
 func_call_where_string_arg_has_old_fmt_and_bad_parens(
     "A long string with {}. This string is so long that it is ridiculous. It can't fit"
-    " on one line at alllll."
-    % "formatting",
+    " on one line at alllll." % "formatting",
 )
 
 func_call_where_string_arg_has_old_fmt_and_bad_parens(
     "A long string with {}. This {} is so long that it is ridiculous. It can't fit on"
-    " one line at alllll."
-    % ("formatting", "string"),
+    " one line at alllll." % ("formatting", "string"),
 )
 
 
@@ -702,3 +707,11 @@ def who(self):
                 passenger_association=passenger_association,
             )
         )
+
+
+if __name__ == "__main__":
+    for i in range(4, 8):
+        cmd = (
+            r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk"
+            r" '{print $2}'); do kill $pid; done" % (i)
+        )