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

[eas-cli] Enhance eas env:exec command by enabling shell execution for commands #2788

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tharakadesilva
Copy link

@tharakadesilva tharakadesilva commented Dec 21, 2024

Why

Initial Problem: Command Not Found in CI

The impetus for these changes originated from feedback on this pull request. The initial problem, highlighted here, was that commands passed to the env:exec functionality were failing to execute within the GitHub Actions environment. Specifically, the error message indicated that npx expo config --json --type public could not be found.

[stderr] bash: line 1: npx expo config --json --type public: command not found
❌ "npx expo config --json --type public" failed
bash -c "npx expo config --json --type public" exited with non-zero code: 127
    Error: env:exec command failed.

This error suggested that the command was not being executed within a context where npx was accessible.

Second Problem: Local Terminal Failures

After resolving the initial "command not found" error in CI, a new problem surfaced. The command, which now worked in the CI environment, began to fail when executed directly from a local terminal. This was unexpected, as local terminals typically have access to npx.

$ neas env:exec preview "npx expo config --json --type public"
No environment variables with visibility "Plain text" and "Sensitive" found for the "preview" environment on EAS servers.

Running command: npx expo config --json --type public
[stdout] Entering npm script environment at location:
[stdout] /Users/wschurman/temp/pkfsapjfasafjpfajaf
[stdout] Type 'exit' or ^D when finished

The error was due to a bash session starting without executing the code.

Third Problem: Unstripped Quotes in CI

The third issue arose when inspecting the output in the CI environment after the previous fixes. It became apparent that the command being executed was still enclosed in quotes (was trying to execute a string).

Local Output (Correct):

expo/eas-cli/packages/eas-cli/bin/run env:exec preview "npx expo config --json --type public" 
Environment variables with visibility "Plain text" and "Sensitive" loaded from the "preview" environment on EAS: BUILD_VAR, EXPO_PUBLIC_UPDATE_VAR.

Running command: npx expo config --json --type public

Remote Output (Incorrect):

eas env:exec preview "npx expo config --json --type public"
Environment variables with visibility "Plain text" and "Sensitive" loaded from the "preview" environment on EAS: BUILD_VAR, EXPO_PUBLIC_UPDATE_VAR.
Running command: "npx expo config --json --type public"

The key difference was the Running command: line. In CI, the command was being executed as a quoted string ( "npx expo config --json --type public"), rather than a direct command ( npx expo config --json --type public), indicating that the quotes were not being parsed correctly by oclif. I am still not sure why.

How

This section details the solutions implemented to address the identified problems.

Solution to "Command Not Found"

To address the initial "command not found" error in the CI environment, we implemented the suggestion found in this Stack Overflow answer. This solution involves adding shell: true to the spawn command.

While calling spawn itself, there is no npm command under spawn. Thus you got that error message. Instead of using spawn itself, while adding shell: true, spawn will use shell of your system to run that command. Since your system has npm, it works.

By setting shell: true, the command is executed via the system shell, which has the necessary context to locate and execute npx.

Solution for Local Terminal Failures

The second issue, the local terminal failures, was resolved by opting to execute the command without using bash -c. We instead adopted the approach recommended by this Stack Overflow answer, which advocates for a more direct approach. This avoids the need to shell out to bash -c and reduces the chances of issues with command parsing, especially when dealing with arguments containing spaces or quotes.

Solution for Unstripped Quotes

Finally, to address the issue of unstripped quotes in the CI environment, a basic sanitization method was added. This method is designed to remove quotes from the command string prior to execution, ensuring that the command is executed correctly as intended. It's important to note that this sanitization is intended to be a preliminary fix and may require further refinement for broader applicability.

Test Plan

This section outlines the steps taken to verify the correctness of the changes.

Verification Steps

  • GitHub CI Testing: The primary goal was to ensure the command now executes successfully within the GitHub CI environment, which was accomplished.

  • Local Command Testing: The following commands were executed locally to ensure the changes did not break existing functionality and worked as expected.

    expo/eas-cli/packages/eas-cli/bin/run env:exec preview "npx expo config --json --type public"
    Environment variables with visibility "Plain text" and "Sensitive" loaded from the "preview" environment on EAS: BUILD_VAR, EXPO_PUBLIC_UPDATE_VAR.
    
    Running command: npx expo config --json --type public
    [stdout] {"name":"Your Projects name" ... }

    This test confirms the primary command and ensures it now works locally with environment variables

    expo/eas-cli/packages/eas-cli/bin/run env:exec preview "echo 'hello
    world'"
    Environment variables with visibility "Plain text" and "Sensitive" loaded from the "preview" environment on EAS: BUILD_VAR, EXPO_PUBLIC_UPDATE_VAR.
    
    Running command: echo 'hello
    world'
    [stdout] hello
    [stdout] world

    This test confirms that commands with inner single quotes now works

These tests were conducted to ensure the fixes addressed the issues and did not introduce new problems.

Copy link

Subscribed to pull request

File Patterns Mentions
**/* @szdziedzic, @khamilowicz, @sjchmiela, @radoslawkrzemien

Generated by CodeMention

@tharakadesilva
Copy link
Author

/changelog-entry bug-fix [eas-cli] Enhance eas env:exec command by enabling shell execution for commands

…for commands

* This change allows for better command handling and execution within the environment context.
* Fix command execution in env:exec to handle various command formats properly.
@tharakadesilva tharakadesilva marked this pull request as ready for review December 22, 2024 01:52
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.

1 participant