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

Attempt to fix osascript issue with launching Terminal #902

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

fredrikekelund
Copy link
Contributor

Related issues

Proposed Changes

There are numerous reports in Sentry about osascript failing to run the command used to launch a Terminal from Studio. It's not clear why this happens, but in this PR, I've taken a few steps to make the script safer:

  1. Escape the site folder path by wrapping it in quotes (would help if there's whitespace in the path)
  2. Use ; instead of && to separate statements in the init script (proceed even if the previous step failed)
  3. Use STDIN to pipe the script to osascript instead of passing it as an argument to osascript -e. Why? Because the manpage for osascript mentions that the -e argument technically only accepts single-line scripts:
     -e statement
           Enter one line of a script.  If -e is given, osascript will not look for a filename in the argument list.  Multiple -e options
           may be given to build up a multi-line script.  Because most scripts use characters that are special to many shell programs (for
           example, AppleScript uses single and double quote marks, “(”, “)”, and “*”), the statement will have to be correctly quoted and
           escaped to get it past the shell intact.

Testing Instructions

  1. Ensure that the Open in… Terminal button still works as expected on macOS

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

@fredrikekelund fredrikekelund requested a review from a team February 6, 2025 13:45
@fredrikekelund fredrikekelund self-assigned this Feb 6, 2025
Copy link
Contributor

@nightnei nightnei left a comment

Choose a reason for hiding this comment

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

It fails for me
Screenshot 2025-02-07 at 14 01 19

}

const escapedPath = targetPath.replace( /"/g, '\\"' );
initScriptSteps.push( `cd \\"${ escapedPath }\\"`, 'clear' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, why do we do cleaning at the end?

Copy link
Contributor

Choose a reason for hiding this comment

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

We have a separate issue to remove that https://github.com/Automattic/dotcom-forge/issues/10116

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why do we do cleaning at the end?

That's what I would expect as a user. Seeing setup commands in the terminal feels like exposing the app's inner workings.

NB that the osascript is only needed for running the experimental WP CLI code. If we only want to open the terminal in the site directory, we could follow GitHub Desktop's implementation

@fredrikekelund
Copy link
Contributor Author

It fails for me

🤦‍♂️ Looks like I failed to format the multiline string properly. Should be fixed now

Copy link
Contributor

@wojtekn wojtekn left a comment

Choose a reason for hiding this comment

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

It works well for me. However, it worked well, before, too.

@fredrikekelund
Copy link
Contributor Author

It works well for me. However, it worked well, before, too.

Same here. I haven't been able to reproduce the issue. There's no guarantee that this will fix it, but escaping the cd path is definitely an improvement, and passing the script in STDIN could maybe help.

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.

3 participants