Skip to content

Commit

Permalink
Fixing a bug where empty variables in .env files were read as multi-l…
Browse files Browse the repository at this point in the history
…ine values (#3934) (#3951)

* Fixing a bug where empty variables in .env files were read as multi-line values

* changelog

* take 2

* formats

* adding another test case
  • Loading branch information
joehan committed Dec 15, 2021
1 parent 6fd36a8 commit f88996a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
@@ -1,6 +1,7 @@
- Fixes issue when installing a Firebase Extension where secrets would be created before validation.
- Fixes issue with filtering on a specific storage bucket using functions in the emulator. (#3893)
- Fixes check in Cloud Functions for Firebase initialization to check for API enablement before trying to enable them. (#2574)
- No longer tries to clean up function build images from Artifact Registry when Artifact Registry is not enabled (#3943)
- Show error message when running `firebase init hosting:github` with no Hosting config in `firebase.json` (#3113)
- No longer tries to clean up function build images from Artifact Registry when Artifact Registry is not enabled. (#3943)
- Show error message when running `firebase init hosting:github` with no Hosting config in `firebase.json`. (#3113)
- Fixes issue where `remoteconfig:get` was not fetching the latest version by default. (#3559)
- Fixes issue where empty variables in .env files would instead read as multi-line values. (#3934)
5 changes: 3 additions & 2 deletions src/functions/env.ts
Expand Up @@ -45,11 +45,11 @@ const LINE_RE = new RegExp(
"^" + // begin line
"\\s*" + // leading whitespaces
"(\\w+)" + // key
"\\s*=\\s*" + // separator (=)
"\\s*=[\\f\\t\\v]*" + // separator (=)
"(" + // begin optional value
"\\s*'(?:\\\\'|[^'])*'|" + // single quoted or
'\\s*"(?:\\\\"|[^"])*"|' + // double quoted or
"[^#\\r\\n]+" + // unquoted
"[^#\\r\\n]*" + // unquoted
")?" + // end optional value
"\\s*" + // trailing whitespaces
"(?:#[^\\n]*)?" + // optional comment
Expand Down Expand Up @@ -109,6 +109,7 @@ export function parse(data: string): ParseResult {
v = v.replace(/\\([\\'"])/g, "$1");
}
}

envs[k] = v;
}

Expand Down
33 changes: 24 additions & 9 deletions src/test/functions/env.spec.ts
Expand Up @@ -135,24 +135,39 @@ BAR=bar
want: { FOO: "foo" },
},
{
description: "should ignore comments",
description: "should handle empty values",
input: `
FOO=foo # comment
# line comment 1
# line comment 2
BAR=bar # another comment
FOO=
BAR= "blah"
`,
want: { FOO: "", BAR: "blah" },
},
{
description: "should handle quoted values after a newline",
input: `
FOO=
"blah"
`,
want: { FOO: "blah" },
},
{
description: "should ignore comments",
input: `
FOO=foo # comment
# line comment 1
# line comment 2
BAR=bar # another comment
`,
want: { FOO: "foo", BAR: "bar" },
},
{
description: "should ignore empty lines",
input: `
FOO=foo
FOO=foo
BAR=bar
BAR=bar
`,
`,
want: { FOO: "foo", BAR: "bar" },
},
];
Expand Down

0 comments on commit f88996a

Please sign in to comment.