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

macOS CLI script tweaks #1148

Merged
merged 8 commits into from
Apr 9, 2025
Merged

Conversation

fredrikekelund
Copy link
Contributor

Related issues

Proposed Changes

  • In the POSIX ln utility documentation, the source and target terms refer to the opposite of what you'd expect (if you expect that "target" means link destination and "source" means symlink path). To avoid additional confusion, I've changed the terms to "symlink path" and "packaged path".
  • As an additional safety measure, we now abort the installation if something other than a symlink already lives at /usr/local/bin/studio
  • Tweaked the macOS CLI bootstrap script per @mcsf's recommendations in studio cli boostrap files #1111.
    • Remove the use of command prefixes.
    • Use a static name for the Electron executable file.
    • Verify the existence of the Electron executable file.
    • Use exec to replace the shell process with the node process.

Tip

An interesting piece of trivia is that the GNU ln utility apparently uses the opposite meaning for "target".

Testing Instructions

Same as #1132

Pre-merge Checklist

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

@fredrikekelund fredrikekelund requested review from mcsf, youknowriad, nightnei, bcotrim and a team March 28, 2025 09:39
@fredrikekelund fredrikekelund self-assigned this Mar 28, 2025
Copy link
Member

@mcsf mcsf left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my feedback! One of my comments here is opinion, but the other one should be addressed, I think.

Comment on lines 62 to 66
const fileType = await getFileType( cliSymlinkPath );

// Avoid overwriting an existing file if it's not a symlink.
if ( fileType === FileType.NonSymlink ) {
throw new Error( InstallError.FileAlreadyExists );
Copy link
Member

Choose a reason for hiding this comment

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

Studio isn't my project, so please take this as opinion beyond the scope of reviewing the shell script. :)

IMO these changes go beyond YAGNI and add unnecessary indirection. getFileType isn't used anywhere else, nor is FileType. There will be less code to maintain and fewer jumps required of the reader if we inline a throw/catch construct with the system call in plain sight.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair. I've inlined this logic

Comment on lines 35 to 45
let message = __( 'Please ensure you grant Studio admin permissions when prompted.' );
if ( error instanceof Error && error.message === InstallError.FileAlreadyExists ) {
message = sprintf(
/* translators: 1: Installation path */
__(
'The installation path %1$s is already occupied by a file or directory. Please remove it and try again.'
),
cliSymlinkPath
);
}

Copy link
Member

Choose a reason for hiding this comment

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

This fails to propagate legitimate errors occurred during execution of the sudo'd command. Try it yourself by exiting early from install-studio-cli.sh with exit 1, or even echoing an error and exiting with 1. The code assumes a failure will be due to permissions not being granted, but that can be verified by looking at error.message:

Problem error.message
Permission not granted User did not grant permission.
Non-zero exit with no echoed output Command failed: /bin/sh "/Applications/Studio.app/Contents/Resources/bin/install-studio-cli.sh"
Non-zero exit with echoed output Command failed: /bin/sh "/Applications/Studio.app/Contents/Resources/bin/install-studio-cli.sh"\nError: Foo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is true, but I think it's fair to assume that most errors generated here will be due to missing permissions. Moreover, the logic in install-studio-cli.sh is unlikely to generate errors (especially considering that we verify that cliSymlinkPath is not occupied before running the script).

Still, I added some additional error message parsing here for completeness' sake, so we now also distinguish between known permission errors and unknown errors.

@fredrikekelund fredrikekelund requested a review from mcsf April 8, 2025 12:23
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.

LGTM and works still well 👍
Left just two comments about enum and translations.

Screenshot 2025-04-08 at 14 39 39

Comment on lines +3 to +4
# This script is used to install the Studio CLI on macOS. It creates a symlink at CLI_SYMLINK_PATH
# (e.g. /usr/local/bin/studio) pointing to the packaged Studio CLI JS file at CLI_PACKAGED_PATH.
Copy link
Contributor

Choose a reason for hiding this comment

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

CLI_SYMLINK_PATH and CLI_PACKAGED_PATH are great names 👍
Similar to what I proposed ( CLI_SYMLINK_PATH / CLI_TARGET_PATH ), but CLI_PACKAGED_PATH is much clear 👍

const installScriptPath = path.join( binPath, 'install-studio-cli.sh' );

enum InstallError {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, it seems, I remember that either Tumblr or Dotcom or both didn't recommend using enums, due to tree shaking issues or something like that. Do you remember something about it? Or maybe I remember something incorrectly 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's wrap strings with translation function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These error messages aren't displayed to users, so we shouldn't need to translate them. You're right that we don't need an enum, though. I've changed it to regular constants.

Comment on lines 69 to 77
try {
const stats = await lstat( cliSymlinkPath );

if ( ! stats.isSymbolicLink() ) {
throw new Error( InstallError.FileAlreadyExists );
}
} catch ( error ) {
// File does not exist, do nothing
}
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for addressing my feedback in general.

I don't want to be the one delaying this PR any longer, so please use your best judgment here. I just wanted to point out, though, that — although they are unlikely — lstat can potentially throw 13 different errors. So this empty catch block seems a bit bold to me. This is what I meant earlier with propagating the error.

(Currently traveling back from a meetup, so I won't be very responsive.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Checking for ENOENT errors specifically is fair 👍 I've made that change

@fredrikekelund fredrikekelund merged commit 142fa09 into trunk Apr 9, 2025
8 checks passed
@fredrikekelund fredrikekelund deleted the f26d/cli-install-script-tweaks branch April 9, 2025 12:18
Copy link

sentry-io bot commented Apr 9, 2025

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ TypeError: Object has been destroyed <anonymous>(main/studio/./src/screenshot-window) View Issue

Did you find this useful? React with a 👍 or 👎

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