-
Notifications
You must be signed in to change notification settings - Fork 224
[eas-cli] Validate local composite functions in workflow files #3931
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
Merged
sswrk
merged 1 commit into
main
from
szymonswierk/eng-22387-eas-cli-validate-custom-actions-in-workflow-files
Jul 30, 2026
+362
−0
Merged
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
262 changes: 262 additions & 0 deletions
262
packages/eas-cli/src/commandUtils/workflow/__tests__/compositeFunctions-test.ts
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,262 @@ | ||
| import { promises as fs } from 'fs'; | ||
| import os from 'os'; | ||
| import path from 'path'; | ||
|
|
||
| import { validateWorkflowLocalCompositeFunctionsAsync } from '../compositeFunctions'; | ||
|
|
||
| async function makeProjectWithCompositeFunctionAsync( | ||
| projectRoot: string, | ||
| functionName: string, | ||
| contents: string | ||
| ): Promise<void> { | ||
| const functionDir = path.join(projectRoot, '.eas', 'functions', functionName); | ||
| await fs.mkdir(functionDir, { recursive: true }); | ||
| await fs.writeFile(path.join(functionDir, 'function.yml'), contents, 'utf-8'); | ||
| } | ||
|
|
||
| describe(validateWorkflowLocalCompositeFunctionsAsync, () => { | ||
| it('validates referenced local composite functions', async () => { | ||
| const projectRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-workflow-functions-test-')); | ||
| await makeProjectWithCompositeFunctionAsync( | ||
| projectRoot, | ||
| 'setup', | ||
| ['runs:', ' steps:', ' - run: echo setup'].join('\n') | ||
| ); | ||
| const workflow = { | ||
| jobs: { | ||
| job: { | ||
| steps: [{ uses: './.eas/functions/setup' }], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await expect( | ||
| validateWorkflowLocalCompositeFunctionsAsync(workflow, projectRoot) | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('resolves local composite functions from the EAS project directory in monorepos', async () => { | ||
| const repositoryRoot = await fs.mkdtemp( | ||
| path.join(os.tmpdir(), 'eas-workflow-functions-monorepo-') | ||
| ); | ||
| const projectDir = path.join(repositoryRoot, 'apps', 'mobile'); | ||
| await fs.mkdir(projectDir, { recursive: true }); | ||
| await makeProjectWithCompositeFunctionAsync( | ||
| projectDir, | ||
| 'setup', | ||
| ['runs:', ' steps:', ' - run: echo setup'].join('\n') | ||
| ); | ||
|
|
||
| const workflow = { | ||
| jobs: { | ||
| job: { | ||
| steps: [{ uses: './.eas/functions/setup' }], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await expect( | ||
| validateWorkflowLocalCompositeFunctionsAsync(workflow, projectDir) | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('does not treat repository-root composite functions as valid when the EAS project is in a subdirectory', async () => { | ||
| const repositoryRoot = await fs.mkdtemp( | ||
| path.join(os.tmpdir(), 'eas-workflow-functions-monorepo-') | ||
| ); | ||
| const projectDir = path.join(repositoryRoot, 'apps', 'mobile'); | ||
| await fs.mkdir(projectDir, { recursive: true }); | ||
| await makeProjectWithCompositeFunctionAsync( | ||
| repositoryRoot, | ||
| 'setup', | ||
| ['runs:', ' steps:', ' - run: echo setup'].join('\n') | ||
| ); | ||
|
|
||
| const workflow = { | ||
| jobs: { | ||
| job: { | ||
| steps: [{ uses: './.eas/functions/setup' }], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await expect( | ||
| validateWorkflowLocalCompositeFunctionsAsync(workflow, projectDir) | ||
| ).rejects.toThrow( | ||
| /Local composite function "\.\/\.eas\/functions\/setup" was referenced by a step but no such composite function exists/ | ||
| ); | ||
| }); | ||
|
|
||
| it('ignores unreferenced malformed composite functions on disk', async () => { | ||
| const projectRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-workflow-functions-test-')); | ||
| await makeProjectWithCompositeFunctionAsync( | ||
| projectRoot, | ||
| 'broken', | ||
| ['name: Broken', 'runs:', ' steps: []'].join('\n') | ||
| ); | ||
| const workflow = { | ||
| jobs: { | ||
| job: { | ||
| steps: [{ run: 'echo hi' }], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await expect( | ||
| validateWorkflowLocalCompositeFunctionsAsync(workflow, projectRoot) | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('validates composite functions referenced by an arbitrary path (arbitrary path style)', async () => { | ||
| const projectRoot = await fs.mkdtemp( | ||
| path.join(os.tmpdir(), 'eas-workflow-functions-arbitrary-') | ||
| ); | ||
| const functionDir = path.join(projectRoot, 'internal-functions', 'deploy'); | ||
| await fs.mkdir(functionDir, { recursive: true }); | ||
| await fs.writeFile( | ||
| path.join(functionDir, 'function.yml'), | ||
| ['runs:', ' steps:', ' - run: echo deploy'].join('\n'), | ||
| 'utf-8' | ||
| ); | ||
| const workflow = { | ||
| jobs: { | ||
| job: { | ||
| steps: [{ uses: './internal-functions/deploy' }], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await expect( | ||
| validateWorkflowLocalCompositeFunctionsAsync(workflow, projectRoot) | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('rejects interpolated local composite function references', async () => { | ||
| const projectRoot = await fs.mkdtemp( | ||
| path.join(os.tmpdir(), 'eas-workflow-functions-interpolated-') | ||
| ); | ||
| const workflow = { | ||
| jobs: { | ||
| job: { | ||
| steps: [{ uses: './.eas/functions/${{ inputs.name }}' }], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await expect( | ||
| validateWorkflowLocalCompositeFunctionsAsync(workflow, projectRoot) | ||
| ).rejects.toThrow(/must not contain interpolation/); | ||
| }); | ||
|
|
||
| it('throws when a referenced local composite function does not exist', async () => { | ||
| const projectRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-workflow-functions-missing-')); | ||
| const workflow = { | ||
| jobs: { | ||
| job: { | ||
| steps: [{ uses: './.eas/functions/setup' }], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await expect( | ||
| validateWorkflowLocalCompositeFunctionsAsync(workflow, projectRoot) | ||
| ).rejects.toThrow( | ||
| /Local composite function "\.\/\.eas\/functions\/setup" was referenced by a step but no such composite function exists/ | ||
| ); | ||
| }); | ||
|
|
||
| it('validates cyclic local composite functions (cycles are reported at expansion time)', async () => { | ||
| const projectRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-workflow-functions-cycle-')); | ||
| await makeProjectWithCompositeFunctionAsync( | ||
| projectRoot, | ||
| 'loop', | ||
| ['runs:', ' steps:', ' - uses: ./.eas/functions/loop'].join('\n') | ||
| ); | ||
| const workflow = { | ||
| jobs: { | ||
| job: { | ||
| steps: [{ uses: './.eas/functions/loop' }], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await expect( | ||
| validateWorkflowLocalCompositeFunctionsAsync(workflow, projectRoot) | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('validates indirectly cyclic local composite functions (cycles are reported at expansion time)', async () => { | ||
| const projectRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-workflow-functions-cycle-')); | ||
| await makeProjectWithCompositeFunctionAsync( | ||
| projectRoot, | ||
| 'a', | ||
| ['runs:', ' steps:', ' - uses: ./.eas/functions/b'].join('\n') | ||
| ); | ||
| await makeProjectWithCompositeFunctionAsync( | ||
| projectRoot, | ||
| 'b', | ||
| ['runs:', ' steps:', ' - uses: ./.eas/functions/a'].join('\n') | ||
| ); | ||
| const workflow = { | ||
| jobs: { | ||
| job: { | ||
| steps: [{ uses: './.eas/functions/a' }], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await expect( | ||
| validateWorkflowLocalCompositeFunctionsAsync(workflow, projectRoot) | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('validates a shared local composite function referenced along multiple paths', async () => { | ||
| const projectRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-workflow-functions-diamond-')); | ||
| await makeProjectWithCompositeFunctionAsync( | ||
| projectRoot, | ||
| 'shared', | ||
| ['runs:', ' steps:', ' - run: echo shared'].join('\n') | ||
| ); | ||
| await makeProjectWithCompositeFunctionAsync( | ||
| projectRoot, | ||
| 'left', | ||
| ['runs:', ' steps:', ' - uses: ./.eas/functions/shared'].join('\n') | ||
| ); | ||
| await makeProjectWithCompositeFunctionAsync( | ||
| projectRoot, | ||
| 'right', | ||
| ['runs:', ' steps:', ' - uses: ./.eas/functions/shared'].join('\n') | ||
| ); | ||
| const workflow = { | ||
| jobs: { | ||
| job: { | ||
| steps: [{ uses: './.eas/functions/left' }, { uses: './.eas/functions/right' }], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await expect( | ||
| validateWorkflowLocalCompositeFunctionsAsync(workflow, projectRoot) | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('throws when a referenced local composite function is malformed', async () => { | ||
| const projectRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-workflow-functions-test-')); | ||
| await makeProjectWithCompositeFunctionAsync( | ||
| projectRoot, | ||
| 'broken', | ||
| ['name: Broken', 'runs:', ' steps: []'].join('\n') | ||
| ); | ||
| const workflow = { | ||
| jobs: { | ||
| job: { | ||
| steps: [{ uses: './.eas/functions/broken' }], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await expect( | ||
| validateWorkflowLocalCompositeFunctionsAsync(workflow, projectRoot) | ||
| ).rejects.toThrow(/must declare at least one step under "runs.steps"/); | ||
| }); | ||
| }); |
95 changes: 95 additions & 0 deletions
95
packages/eas-cli/src/commandUtils/workflow/compositeFunctions.ts
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,95 @@ | ||
| import { CompositeFunctionConfig, CompositeFunctionConfigZ } from '@expo/eas-build-job'; | ||
| import { | ||
| buildCompositeFunctionCatalogFromStepsAsync, | ||
| resolveLocalCompositeFunctionPath, | ||
| } from '@expo/steps'; | ||
| import { promises as fs } from 'fs'; | ||
| import path from 'path'; | ||
| import * as YAML from 'yaml'; | ||
| import { z } from 'zod'; | ||
|
|
||
| import Log from '../../log'; | ||
|
|
||
| const COMPOSITE_FUNCTION_FILE_EXTENSIONS = ['yml', 'yaml'] as const; | ||
|
|
||
| export async function validateWorkflowLocalCompositeFunctionsAsync( | ||
| parsedYaml: any, | ||
| projectDir: string | ||
| ): Promise<void> { | ||
| await buildCompositeFunctionCatalogFromStepsAsync({ | ||
| rootSteps: stepsFromWorkflow(parsedYaml), | ||
| loadCompositeFunction: compositeFunctionPath => | ||
| loadLocalCompositeFunctionConfigAsync(projectDir, compositeFunctionPath), | ||
| }); | ||
| } | ||
|
|
||
| async function loadLocalCompositeFunctionConfigAsync( | ||
| projectDir: string, | ||
| compositeFunctionPath: string | ||
| ): Promise<CompositeFunctionConfig> { | ||
| const resolvedPath = resolveLocalCompositeFunctionPath(projectDir, compositeFunctionPath); | ||
|
|
||
| for (const ext of COMPOSITE_FUNCTION_FILE_EXTENSIONS) { | ||
| const config = await readCompositeFunctionConfigFileAsync( | ||
| compositeFunctionPath, | ||
| path.join(resolvedPath, `function.${ext}`) | ||
| ); | ||
| if (config) { | ||
| Log.debug(`Validated local composite function "${compositeFunctionPath}"`); | ||
| return config; | ||
| } | ||
| } | ||
|
|
||
| throw new Error( | ||
| `Local composite function "${compositeFunctionPath}" was referenced by a step but no such composite function exists. A local composite function is resolved from a "function.yml" (or "function.yaml") file at the referenced path relative to the EAS project root (e.g. "uses: ${compositeFunctionPath}" resolves "${compositeFunctionPath}/function.yml"). The recommended convention is to keep composite functions under ".eas/functions/<name>".` | ||
| ); | ||
| } | ||
|
|
||
| async function readCompositeFunctionConfigFileAsync( | ||
| compositeFunctionPath: string, | ||
| absolutePath: string | ||
| ): Promise<CompositeFunctionConfig | null> { | ||
| let rawContents: string; | ||
| try { | ||
| rawContents = await fs.readFile(absolutePath, 'utf-8'); | ||
| } catch (err) { | ||
| if ((err as NodeJS.ErrnoException)?.code === 'ENOENT') { | ||
| return null; | ||
| } | ||
| throw new Error( | ||
| `Failed to read local composite function "${compositeFunctionPath}" from ${absolutePath}`, | ||
| { | ||
| cause: err as Error, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| let parsed: unknown; | ||
| try { | ||
| parsed = YAML.parse(rawContents); | ||
| } catch (err) { | ||
| throw new Error( | ||
| `Failed to parse local composite function "${compositeFunctionPath}" YAML at ${absolutePath}`, | ||
| { | ||
| cause: err as Error, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| const result = CompositeFunctionConfigZ.safeParse(parsed); | ||
| if (!result.success) { | ||
| throw new Error( | ||
| `Invalid composite function "${compositeFunctionPath}": ${z.prettifyError(result.error)}` | ||
| ); | ||
| } | ||
|
|
||
| return result.data; | ||
| } | ||
|
|
||
| function stepsFromWorkflow(parsedYaml: any): any[] { | ||
| const jobs = parsedYaml?.jobs; | ||
| if (!jobs || typeof jobs !== 'object') { | ||
| return []; | ||
| } | ||
| return Object.values(jobs).flatMap((job: any) => (Array.isArray(job?.steps) ? job.steps : [])); | ||
| } | ||
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
Oops, something went wrong.
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.
Should this be shared across worker and eas-cli somehow? I think I saw a very similar function in another PR just now.
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.
Same reason as here #3930 (comment) I'm open to moving it a layer lower, to
stepsif you think that makes senseThere 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.
Moving this discussion to #4064