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

Allow undefined env var values #615

Merged
merged 1 commit into from
Jan 27, 2020
Merged

Conversation

AArnott
Copy link
Member

@AArnott AArnott commented Jan 27, 2020

While it doesn't make any sense, the typings for process.env is:

    interface ProcessEnv {
        [key: string]: string | undefined;
    }

So assigning process.env to IExecSyncOptions.env in typescript with all typings turned on produces compiler errors (at least when strict null checking is applied).

Without this simple typings adjustment, the following workaround in user code is required:

function GetFilteredEnv() {
    const result = {};
    const block = process.env;
    for (const key in block) {
        if (block.hasOwnProperty(key)) {
            const value = block[key];
            if (value !== undefined) {
                result[key] = value;
            }
        }
    }
    return result;
}

let options: IExecSyncOptions = { env: GetFilteredEnv() };

But with this change it's as simple as:

let options: IExecSyncOptions = { env: process.env };

Which ironically is already what is done within this repo, but isn't a problem because the node typings aren't imported so process.env is typed as any.

While it doesn't make any sense, the typings for `process.env` is:

```ts
    interface ProcessEnv {
        [key: string]: string | undefined;
    }
```

So assigning `process.env` to `IExecSyncOptions.env` in typescript with all typings turned on produces compiler errors (at least when strict null checking is applied).

Without this simple typings adjustment, the following workaround in user code is required:

```ts
function GetFilteredEnv() {
    const result = {};
    const block = process.env;
    for (const key in block) {
        if (block.hasOwnProperty(key)) {
            const value = block[key];
            if (value !== undefined) {
                result[key] = value;
            }
        }
    }
    return result;
}

let options: IExecSyncOptions = { env: GetFilteredEnv() };
```

But with this change it's as simple as:

```ts
let options: IExecSyncOptions = { env: process.env };
```

Which ironically is already what is done *within* this repo, but isn't a problem because the node typings aren't imported so `process.env` is typed as `any`.
@AArnott
Copy link
Member Author

AArnott commented Jan 27, 2020

The failure on Linux appears to be an unrelated (network?) failure that I see popping up in several other builds as well including a recent CI on master.

Copy link

@damccorm damccorm left a comment

Choose a reason for hiding this comment

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

LGTM - thanks for the contribution!

@damccorm damccorm merged commit 7f62b8a into microsoft:master Jan 27, 2020
@AArnott AArnott deleted the patch-3 branch January 28, 2020 03:07
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

Successfully merging this pull request may close these issues.

None yet

2 participants