Repository: xonsh/xonsh

Issue:
redirection: unable to open file
## Current Behavior
This snippet:
```xsh
xonsh --no-rc
f = p'/tmp/file.tmp'
echo 'abc' > @(f)
```
returns error: `xonsh: ['/tmp/file.tmp']: unable to open file`

## Expected Behavior
The snippet should redirect string 'abc' to file /tmp/file.tmp

## xonfig

<details>

```xsh
+------------------+-----------------+
| xonsh            | 0.16.0          |
| Python           | 3.9.16          |
| PLY              | 3.11            |
| have readline    | True            |
| prompt toolkit   | 3.0.36          |
| shell type       | prompt_toolkit  |
| history backend  | json            |
| pygments         | 2.14.0          |
| on posix         | True            |
| on linux         | True            |
| distro           | manjaro         |
| on wsl           | False           |
| on darwin        | False           |
| on windows       | False           |
| on cygwin        | False           |
| on msys2         | False           |
| is superuser     | False           |
| default encoding | utf-8           |
| xonsh encoding   | utf-8           |
| encoding errors  | surrogateescape |
| xontrib          | []              |
| RC file          | []              |
+------------------+-----------------+
```
</details>

## For community
⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**



Discussion:

Resolving patch:
diff --git a/xonsh/procs/specs.py b/xonsh/procs/specs.py
--- a/xonsh/procs/specs.py
+++ b/xonsh/procs/specs.py
@@ -223,6 +223,9 @@ def _parse_redirects(r, loc=None):
 
 def _redirect_streams(r, loc=None):
     """Returns stdin, stdout, stderr tuple of redirections."""
+    if isinstance(loc, list):
+        raise Exception(f"Unsupported redirect: {r!r} {loc!r}")
+
     stdin = stdout = stderr = None
     no_ampersand = r.replace("&", "")
     # special case of redirecting stderr to stdout
@@ -611,7 +614,21 @@ def resolve_args_list(self):
         """Weave a list of arguments into a command."""
         resolved_cmd = []
         for c in self.cmd:
-            resolved_cmd += c if isinstance(c, list) else [c]
+            if (
+                isinstance(c, tuple)
+                and len(c) == 2
+                and isinstance(c[1], list)
+                and len(c[1]) == 1
+            ):
+                # Redirect case e.g. `> file`
+                resolved_cmd.append(
+                    (
+                        c[0],
+                        c[1][0],
+                    )
+                )
+            else:
+                resolved_cmd += c if isinstance(c, list) else [c]
         self.cmd = resolved_cmd
 
     def resolve_redirects(self):


Test patch:
diff --git a/tests/procs/test_specs.py b/tests/procs/test_specs.py
--- a/tests/procs/test_specs.py
+++ b/tests/procs/test_specs.py
@@ -300,3 +300,11 @@ def my_handler(cmd, **kwargs):
         subproc.run()
     assert "command not found: 'xonshcommandnotfound'" in str(expected.value)
     assert not fired
+
+
+def test_redirect_to_substitution(xession):
+    s = SubprocSpec.build(
+        # `echo hello > @('file')`
+        ["echo", "hello", (">", ["file"])]
+    )
+    assert s.stdout.name == "file"
diff --git a/tests/test_integrations.py b/tests/test_integrations.py
--- a/tests/test_integrations.py
+++ b/tests/test_integrations.py
@@ -1234,6 +1234,7 @@ def test_main_d():
     assert out == "dummy\n"
 
 
+@pytest.mark.flaky(reruns=3, reruns_delay=1)
 def test_catching_system_exit():
     stdin_cmd = "__import__('sys').exit(2)\n"
     out, err, ret = run_xonsh(