8000 [@types/node] SpawnSyncReturns may have null values by KSXGitHub · Pull Request #25731 · DefinitelyTyped/DefinitelyTyped · GitHub
[go: up one dir, main page]

Skip to content

[@types/node] SpawnSyncReturns may have null values #25731

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

Closed

Conversation

KSXGitHub
Copy link
Contributor

When spawn a program that doesn't exist

const res = child_process.spawnSync('UnknownProgram')
res.status // null
res.signal // null
res.output // null
res.stdout // null
res.stderr // null
res.Error // Error

When successfully spawn a program

const res = child_process.spawnSync('echo', ['hello'])
res.Error // null
res.signal // null
res.output // [null, Buffer('hello\n'), Buffer(0)]

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package- 8000 name (or tsc if no tslint.json is present).

Select one of these and delete the others:

If adding a new definition:

  • The package does not already provide its own types, or cannot have its .d.ts files generated via --declaration
  • If this is for an NPM package, match the name. If not, do not conflict with the name of an NPM package.
  • Create it with dts-gen --dt, not by basing it on an existing project.
  • tslint.json should be present, and tsconfig.json should have noImplicitAny, noImplicitThis, strictNullChecks, and strictFunctionTypes set to true.

If changing an existing definition:

  • Provide a URL to documentation or source code which provides context for the suggested changes: <>
  • Increase the version number in the header if appropriate.
  • If you are making substantial changes, consider adding a tslint.json containing { "extends": "dtslint/dt.json" }.

If removing a declaration:

  • If a package was never on DefinitelyTyped, you don't need to do anything. (If you wrote a package and provided types, you don't need to register it with us.)
  • Delete the package's directory.
  • Add it to notNeededPackages.json.

When spawn a program that doesn't exist

```javascript
const res = child_process.spawnSync('UnknownProgram')
res.status // null
res.signal // null
res.output // null
res.stdout // null
res.stderr // null
res.Error // Error
```

When successfully spawn a program

```javascript
const res = child_process.spawnSync('echo', ['hello'])
res.Error // null
res.signal // null
res.output // [null, Buffer('hello\n'), Buffer(0)]
```

��
@typescript-bot typescript-bot added Popular package This PR affects a popular package (as counted by NPM download counts). Awaiting reviewer feedback labels May 12, 2018
@typescript-bot
Copy link
Contributor
typescript-bot commented May 12, 2018

@KSXGitHub Thank you for submitting this PR!

🔔 @Alorel @parambirs @tellnes @WilcoBakker @octo-sniffle @smac89 @Flarna @mwiktorczyk @wwwy3y3 @DeividasBakanas @kjin @alvis @eps1lon @Hannes-Magnusson-CK @jkomyno @hoo29 @n-e @BrunoScheufler @islishude @ajafff @mohsen1 @a-tarasyuk - please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

@KSXGitHub
Copy link
Contributor Author
KSXGitHub commented May 12, 2018

Although CI had failed at cross-spawn, I'm sure this change is correct, someone should update cross-spawn accordingly.

@typescript-bot
Copy link
Contributor
typescript-bot commented May 12, 2018

@KSXGitHub The Travis CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

@KSXGitHub KSXGitHub changed the title SpawnSyncReturns may have null values [@types/node] SpawnSyncReturns may have null values May 13, 2018
@KSXGitHub
8000 Copy link
Contributor Author
KSXGitHub commented May 13, 2018

@Alorel please review changes I made for @types/cross-spawn

@typescript-bot
Copy link
Contributor
typescript-bot commented May 13, 2018

@KSXGitHub The Travis CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

@typescript-bot typescript-bot added Owner Approved A listed owner of this package signed off on the pull request. Merge:Express and removed Awaiting reviewer feedback labels May 15, 2018
@typescript-bot
Copy link
Contributor

A definition owner has approved this PR ⭐️. A maintainer will merge this PR shortly. If it shouldn't be merged yet, please leave a comment saying so and we'll wait. Thank you for your contribution to DefinitelyTyped!

@KSXGitHub
Copy link
Contributor Author

Breaking change is expected (I had to also edit @types/cross-spawn to let it pass). So you might want to assign an appropriate version.

@Flarna
Copy link
Contributor
Flarna commented May 15, 2018

@KSXGitHub There is no semver versioning on typings... see #25677 for details.
So your change will go into next patch version of node 8, 9 and 10. If this is not intended the PR needs to be adapted.

@KSXGitHub
Copy link
Contributor Author
KSXGitHub commented May 15, 2018

Personally, I think this PR is necessary. The very purpose purpose of TypeScript is type-safety, the old definition fails to do so by wrongly assume SpawnSyncReturns always not-null which is not the case. Therefore I think this PR should be merged. If it breaks other people builds, so be it, they just get a warning of a potential bug that might occur in the future.

@Flarna
Copy link
Contributor
Flarna commented May 15, 2018

I'm not 100% sure here.
It's similar for common callback like APIs, e.g. fs.readFile() which has currently following callback signature (err: NodeJS.ErrnoException, data: Buffer) => void).
Actually correct would be (err: NodeJS.ErrnoException | null , data: Buffer | undefined) => void as err may be null (in case of success) and data may be undefined (in case of failure).

Therefore if we would change this and you check in your code that err === null would be additionally needed to check that data is not undefined.

So it's a matter of correctness vs. usability.

It would be great if typescript would allow to express such type relationships to tell that data is a Buffer if err is null,...

@KSXGitHub
Copy link
Contributor Author

I made this pull request because I encountered a very bullshit bug in my project due to the use of spawnSync. I am sure that the old definition is nowhere near usability.

stderr: T | null;
status: number | null;
signal: string | null;
error: Error | null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be converted to a discriminated union with error as the discriminator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, I'm 100% sure how exactly spawnSync works. But I am sure though, when error is not null, the other properties are. I'm not so sure about signal.

Copy link
Contributor Author
ED4F

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also hope that no fools create an interface that extends this one. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajafff Your thought on this (diff)

@KSXGitHub
Copy link
Contributor Author
KSXGitHub commented May 15, 2018

@ajafff So, about updating cross-spawn-tests.ts, we got a problem.

type ValueCarrier<X> =
    ValueCarrier.WithoutError<X> | ValueCarrier.WithError

namespace ValueCarrier {
    export interface WithError {
        error: Error
        value: null
    }

    export interface WithoutError<Value> {
        error: null
        value: Value
    }
}

declare function getVal<X>(): ValueCarrier<X>

/* MAIN */
const carrier = getVal<string>()

if (carrier.error === null) {
    const error: null = carrier.error
    const value: string = carrier.value // Error here...
} else {
    const error: Error = carrier.error // And here
    const value: null = carrier.value
}

Perhaps I should restore the old version of test.

@KSXGitHub
Copy link
Contributor Author
KSXGitHub commented May 15, 2018

OK. After some amount of research, I finally know how to use tagged union correctly.

Nope.

@KSXGitHub
Copy link
Contributor Author

@ajafff Since Error | null is somehow equivalent to just Error, tagged union is pretty useless.

@ajafff
Copy link
Contributor
ajafff commented May 15, 2018

@KSXGitHub I agree. If TypeScript doesn't handle it correctly, using tagged unions is unnecessary and should be reverted.

@KSXGitHub
Copy link
Contributor Author
KSXGitHub commented May 15, 2018

@ajafff Since reverting it makes practically no differences, I would like to keep it in hope that TypeScript will fix it soon.

@KSXGitHub
Copy link
Contributor Author

By the way, when I read tsconfig.json I saw "strictNullChecks": false, can anyone explain why is that?

@ajafff
Copy link
Contributor
ajafff commented May 15, 2018

"strictNullChecks": false

I don't know why it's disabled. But I do know that this causes the tagged union to not work as expected.

@Flarna
Copy link
Contributor
Flarna commented May 15, 2018

Because node type definitions have been created before typescript 2.0 (and therefore strictNullChecks) was released and till now nobody did the work to fix all the issues which pop up if it is set to true. I can remember that someone started a while ago but didn't complete (#20542).
Similar the corrections of the typings for callbacks to e.g. Error | null is also to be done and has the same root cause.

@DanielRosenwasser
Copy link
Member

So just to summarize for myself, this is a breaking change and currently TypeScript doesn't use null as a discriminator, so you can't appropriately check the contents. Is that correct?

@KSXGitHub
Copy link
Contributor Author

@DanielRosenwasser Yes.

@KSXGitHub
Copy link
Contributor Author

@Flarna

So your change will go into next patch version of node 8, 9 and 10.

Node.js v10.2.0 just released.

@Flarna
Copy link
Contributor
Flarna commented May 24, 2018

@KSXGitHub The release cycle of NodeJS and of @types/node is not related. NodeJS exposes/documents javascript APIs and doesn't care about typescript.
Whenever a PR here gets merged the types published creates a new @types/XXX release using the major.minor from the index.d.ts file and increments patch. This may happen several times per Node release of there may happen several nodejs releases without any updates in type definitions.

@DanielRosenwasser
Copy link
Member

I've opened microsoft/TypeScript#24479. I feel slightly uneasy merging this in without that issue fixed.

@typescript-bot
Copy link
Contributor
typescript-bot commented Jun 1, 2018

@KSXGitHub The Travis CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

@KSXGitHub KSXGitHub closed this Jun 1, 2018
@KSXGitHub KSXGitHub reopened this Jun 1, 2018
@typescript-bot typescript-bot added the Abandoned This PR had no activity for a long time, and is considered abandoned label Jun 9, 2018
@typescript-bot
Copy link
Contributor

@KSXGitHub To keep things tidy, we have to close PRs that aren't mergeable but don't have activity from their author. No worries, though - please open a new PR if you'd like to continue with this change. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Abandoned This PR had no activity for a long time, and is considered abandoned Owner Approved A listed owner of this package signed off on the pull request. Popular package This PR affects a popular package (as counted by NPM download counts). Revision needed This PR needs code changes before it can be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0