Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Premature close #1146

Closed
FishOrBear opened this issue Aug 15, 2024 · 23 comments
Closed

Error: Premature close #1146

FishOrBear opened this issue Aug 15, 2024 · 23 comments

Comments

@FishOrBear
Copy link

FishOrBear commented Aug 15, 2024

async function run()
{
    const { execa } = await import('execa');
    await execa({
        stdout: {
            file: "out.txt",
        }
    })`npm run tsc`;
}

run();
        "tsc": "rimraf ./dist/ && tsc --p tsconfig.json"

image

@FishOrBear
Copy link
Author

Hello, this error stack is generated by the above code

@ehmicky
Copy link
Collaborator

ehmicky commented Aug 15, 2024

Hi @FishOrBear,

Thanks for reporting this.

Do you get the same error when switching tsc --p to tsc -p? The --p CLI flag seems to be a typo.

@FishOrBear
Copy link
Author

Thanks for your reply, same error.

@FishOrBear
Copy link
Author

FishOrBear commented Aug 15, 2024

async function run()
{
    const { execa } = await import('execa');
    await execa("npm run tsc").pipeStdout("out.txt");
    console.log(123);
}

I used [email protected] and got no error.

@ehmicky
Copy link
Collaborator

ehmicky commented Aug 15, 2024

execa("npm run tsc") should not work, but execa("npm", ["run", "tsc"]) should work. (see this page for why the former does not work).

@FishOrBear
Copy link
Author

FishOrBear commented Aug 15, 2024

async function run()
{
    const { execa } = await import('execa');
    await execa("npm", ["run", "tsc"], {
        stdout: {
            file: "out.txt",
        }
    });
}

run();

I tried that and the same editor error stack appears.

@ehmicky
Copy link
Collaborator

ehmicky commented Aug 15, 2024

Thanks. Ok, what do you get with the following? Using Execa 9.3.1 (is this the version you're using?).

import { execa } from 'execa';

await execa({stdout: {file: "out.txt"}})`npm run tsc`;

I removed the dynamic import, but I am not sure whether there was a reason for it in the first place. Is your codebase in CommonJS, which would explain the dynamic import?

@FishOrBear
Copy link
Author

FishOrBear commented Aug 15, 2024

        {
            "name": "run cur ts",
            "type": "node",
            "request": "launch",
            "args": [
                "${relativeFile}"
            ],
            "runtimeArgs": [
                "--nolazy",
                "-r",
                "ts-node/register/transpile-only",
            ],
            "cwd": "${workspaceFolder}",
            "internalConsoleOptions": "openOnSessionStart"
        },

I currently use ts-node and cjs to run typescript code.
The code above is esm. I'm afraid I need to configure my project.
Wait for me to configure it.

@FishOrBear
Copy link
Author

image
image
Same error

@ehmicky
Copy link
Collaborator

ehmicky commented Aug 15, 2024

Do you only get this error in VSCode, or can you reproduce in a terminal console (that is not inside VSCode)?

There are several layers here: VSCode, ts-node, tsc, npm scripts. I cannot reproduce your bug locally, and I am suspecting this might be due to one of those "layers".

@FishOrBear
Copy link
Author

I get no errors in the console.

@FishOrBear
Copy link
Author

nodejs/undici#2540

I think this question is similar to this one

@ehmicky
Copy link
Collaborator

ehmicky commented Aug 15, 2024

From reading this thread, it appears to be a problem with Node.js streams + VSCode debugger.

Execa is just showing the problem here because the stdout.file option uses a Node.js stream under-the-hood, but it does not seem to be the actual cause of the bug. Unfortunately, this would lead me to suggest to report this to the Node.js repo, or to the VSCode repo. :-/

Just like the people on that other thread, I cannot reproduce locally in VSCode, so there might be something additional about your VSCode setup (and the other users who were having the same problem in that thread).

To unblock you though, there might some workarounds. If the problem is indeed stream-related, you might want to try whether using something different that the stdout.file option would work. For example (even though it's less efficient), would the following work?

async function run()
{
    const {execa} = await import('execa');
    const {writeFile} = await import('fs/promises');

    const {stdout} = await execa`npm run tsc`; 
    await writeFile('./out.txt', stdout);
}

run();

(or if you're using ES modules and can use top-level await):

import {execa} from 'execa';
import {writeFile} from 'fs/promises';

const {stdout} = await execa`npm run tsc`; 
await writeFile('./out.txt', stdout);

@FishOrBear
Copy link
Author

ok I create a repo
https://github.com/FishOrBear/execa_vscode_debug

11-24-00.mp4

@FishOrBear
Copy link
Author

import {execa} from 'execa';
import {writeFile} from 'fs/promises';

const {stdout} = await execa`npm run tsc`; 
await writeFile('./out.txt', stdout);

Both ways of writing give the same error.. Strange.

@FishOrBear
Copy link
Author

I will use 8.0.1 first, this version is fine

@ehmicky
Copy link
Collaborator

ehmicky commented Aug 15, 2024

ok I create a repo

Thanks. Unfortunately, I cannot reproduce the problem with that repository.

Both ways of writing give the same error.. Strange.

Do you get the error without the writeFile() line of code? I.e. only with:

import {execa} from 'execa';

await execa`npm run tsc`; 

@FishOrBear
Copy link
Author

image
image

@FishOrBear
Copy link
Author

image
Did you check this?

@FishOrBear
Copy link
Author

hashicorp/setup-terraform#125
hashicorp/setup-terraform#328

I found this, is it related?

@ehmicky
Copy link
Collaborator

ehmicky commented Aug 15, 2024

Did you check this?

Yes.

I found this, is it related?

This is not related.


Is there a way to trigger this problem outside of VSCode? If not, this is a problem with VSCode unfortunately, and there is not much we'll be able to do here. :/

@FishOrBear
Copy link
Author

After updating from nodejs20.6 to nodejs22, the problem was solved

@ehmicky
Copy link
Collaborator

ehmicky commented Aug 15, 2024

Node 22 did include a few bug fixes related to streams, so this must have been the problem. Thanks for sharing the solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants