-
Notifications
You must be signed in to change notification settings - Fork 3
fix: fail !env substitution when the named variable is missing (fixes #63) #67
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
Open
cpruijsen
wants to merge
1
commit into
powersync-ja:main
Choose a base branch
from
cpruijsen:fix/issue-63
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@powersync/cli-core': patch | ||
| 'powersync': patch | ||
| --- | ||
|
|
||
| fail `!env` substitution when the named environment variable is missing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { parseYamlFile } from '@powersync/cli-core'; | ||
| import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| describe('parseYamlFile !env', () => { | ||
| let tmpDir: string; | ||
| let origUri: string | undefined; | ||
|
|
||
| beforeEach(() => { | ||
| origUri = process.env.PS_DATABASE_URI; | ||
| delete process.env.PS_DATABASE_URI; | ||
| tmpDir = mkdtempSync(join(tmpdir(), 'yaml-env-')); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (origUri === undefined) { | ||
| delete process.env.PS_DATABASE_URI; | ||
| } else { | ||
| process.env.PS_DATABASE_URI = origUri; | ||
| } | ||
|
|
||
| rmSync(tmpDir, { force: true, recursive: true }); | ||
| }); | ||
|
|
||
| it('throws naming the missing variable instead of substituting the name', () => { | ||
| const filePath = join(tmpDir, 'service.yaml'); | ||
| writeFileSync(filePath, 'uri: !env PS_DATABASE_URI\n', 'utf8'); | ||
|
|
||
| expect(() => parseYamlFile(filePath)).toThrow(/PS_DATABASE_URI/); | ||
| expect(() => parseYamlFile(filePath)).toThrow(/undefined/); | ||
| }); | ||
|
|
||
| it('substitutes the environment variable when it is set', () => { | ||
| process.env.PS_DATABASE_URI = 'postgresql://repro:repro@db.example.invalid:5432/postgres'; | ||
| const filePath = join(tmpDir, 'service.yaml'); | ||
| writeFileSync(filePath, 'uri: !env PS_DATABASE_URI\n', 'utf8'); | ||
|
|
||
| expect(parseYamlFile(filePath).contents?.toJSON()).toEqual({ | ||
| uri: 'postgresql://repro:repro@db.example.invalid:5432/postgres' | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,10 +67,18 @@ const YAML_PARSE_OPTIONS = { customTags: [YamlEnvTag] }; | |
|
|
||
| /** | ||
| * Parses a YAML document, evaluating !env tags. | ||
| * Throws when substitution fails (missing or invalid env vars) so callers cannot | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This description is slightly misleading since the function now throws on any YAML parsing error, not just missing env vars. |
||
| * treat the unresolved variable name as a real value. | ||
| */ | ||
| export function parseYamlFile(filePath: string): yaml.Document { | ||
| const content = readFileSync(filePath, 'utf8'); | ||
| return yaml.parseDocument(content, YAML_PARSE_OPTIONS); | ||
| const doc = yaml.parseDocument(content, YAML_PARSE_OPTIONS); | ||
| if (doc.errors.length > 0) { | ||
| const details = doc.errors.map((error) => error.message.trim()).join('\n'); | ||
| throw new Error(`Failed to parse ${filePath}:\n${details}`); | ||
| } | ||
|
|
||
| return doc; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that when running
powersync validateandpowersync pull instancein theexamples/cloud/basic-cloud-pullexample, I got two differently formatted errors. This is preexisting and happens because only some calls toparseYamlFileare wrapped intry { ... } catch (error) { this.styledError(...); }. I'll make a followup PR to fix this; just mentioning it here.