Skip to content

Commit 344818b

Browse files
authored
Restores the fds after builtins finish executing (#3184)
* Restores the fds after builtins finish executing * Adds an extra test
1 parent 7197a44 commit 344818b

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

.yarn/versions/8e84815d.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/shell": patch
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-essentials"
10+
- "@yarnpkg/plugin-init"
11+
- "@yarnpkg/plugin-interactive-tools"
12+
- "@yarnpkg/plugin-nm"
13+
- "@yarnpkg/plugin-npm-cli"
14+
- "@yarnpkg/plugin-pack"
15+
- "@yarnpkg/plugin-patch"
16+
- "@yarnpkg/plugin-pnp"
17+
- "@yarnpkg/plugin-stage"
18+
- "@yarnpkg/plugin-typescript"
19+
- "@yarnpkg/plugin-version"
20+
- "@yarnpkg/plugin-workspace-tools"
21+
- "@yarnpkg/builder"
22+
- "@yarnpkg/core"
23+
- "@yarnpkg/doctor"

packages/yarnpkg-shell/sources/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,11 +632,23 @@ function makeCommandAction(args: Array<string>, opts: ShellOptions, state: Shell
632632
throw new Error(`Assertion failed: A builtin should exist for "${name}"`);
633633

634634
return makeBuiltin(async ({stdin, stdout, stderr}) => {
635+
const {
636+
stdin: initialStdin,
637+
stdout: initialStdout,
638+
stderr: initialStderr,
639+
} = state;
640+
635641
state.stdin = stdin;
636642
state.stdout = stdout;
637643
state.stderr = stderr;
638644

639-
return await builtin(rest, opts, state);
645+
try {
646+
return await builtin(rest, opts, state);
647+
} finally {
648+
state.stdin = initialStdin;
649+
state.stdout = initialStdout;
650+
state.stderr = initialStderr;
651+
}
640652
});
641653
}
642654

packages/yarnpkg-shell/tests/shell.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,60 @@ describe(`Shell`, () => {
739739
});
740740
});
741741

742+
it(`shouldn't affect unrelated commands`, async () => {
743+
await xfs.mktempPromise(async tmpDir => {
744+
const file = ppath.join(tmpDir, `file` as Filename);
745+
746+
await expect(bufferResult(
747+
`echo "hello world" > "${file}"; echo foo`,
748+
)).resolves.toMatchObject({
749+
stdout: `foo\n`,
750+
});
751+
752+
await expect(xfs.readFilePromise(file, `utf8`)).resolves.toEqual(`hello world\n`);
753+
});
754+
755+
await xfs.mktempPromise(async tmpDir => {
756+
const file = ppath.join(tmpDir, `file` as Filename);
757+
758+
await expect(bufferResult(
759+
`echo "hello world" > "${file}" && echo foo`,
760+
)).resolves.toMatchObject({
761+
stdout: `foo\n`,
762+
});
763+
764+
await expect(xfs.readFilePromise(file, `utf8`)).resolves.toEqual(`hello world\n`);
765+
});
766+
});
767+
768+
it(`shouldn't do weird stuff when piping a builtin redirection`, async () => {
769+
await xfs.mktempPromise(async tmpDir => {
770+
const file1 = ppath.join(tmpDir, `file1` as Filename);
771+
const file2 = ppath.join(tmpDir, `file2` as Filename);
772+
773+
await expect(bufferResult(
774+
`echo "hello world" > "${file1}" | echo "foo bar" > "${file2}"; echo test`,
775+
)).resolves.toMatchObject({
776+
stdout: `test\n`,
777+
});
778+
779+
await expect(xfs.readFilePromise(file1, `utf8`)).resolves.toEqual(`hello world\n`);
780+
await expect(xfs.readFilePromise(file2, `utf8`)).resolves.toEqual(`foo bar\n`);
781+
});
782+
783+
await xfs.mktempPromise(async tmpDir => {
784+
const file = ppath.join(tmpDir, `file` as Filename);
785+
786+
await expect(bufferResult(
787+
`echo "hello world" > "${file}" && echo foo`,
788+
)).resolves.toMatchObject({
789+
stdout: `foo\n`,
790+
});
791+
792+
await expect(xfs.readFilePromise(file, `utf8`)).resolves.toEqual(`hello world\n`);
793+
});
794+
});
795+
742796
it(`should support output redirections from fd (stdout)`, async () => {
743797
await xfs.mktempPromise(async tmpDir => {
744798
const file = ppath.join(tmpDir, `file` as Filename);

0 commit comments

Comments
 (0)