SublimeJsPrettier icon indicating copy to clipboard operation
SublimeJsPrettier copied to clipboard

[Enhancement] automatic `prettier_cli_path` resolution

Open sanekb opened this issue 8 months ago • 4 comments

Is your feature request related to a problem?

It's hard to call it both a feature and a bug. It's more like something in between.

Since Deno and Bun can install dependencies in node_modules themselves, there's a small problem with automatic resolution of prettier_cli_path (when it's set to empty) on Windows.

It's all about file extensions in .bin folders, each package manager creates its own versions:

  • npm
    • Local & Global: prettier, prettier.cmd, prettier.ps1
  • deno
    • Local: prettier.cmd
    • Global: prettier, prettier.cmd
  • bun
    • Local & Global: prettier.bunx, prettier.exe
# def resolve_prettier_cli_path, sthelper.py#L164

def make_local_prettier_path(somepath):
    return os.path.join(somepath, 'node_modules', '.bin', 'prettier')

...

# 1. check for prettier installed relative to active view
active_view_parents = generate_dirs(os.path.dirname(view.file_name()), limit=500)
for parent in active_view_parents:
    closest_to_view_prettier = make_local_prettier_path(parent)
    if os.path.exists(closest_to_view_prettier):    # deno and bun will not be found
        return closest_to_view_prettier
# def resolve_prettier_cli_path, sthelper.py#L196

# 4. check globally install prettier
prettier_cmd = 'prettier'
if is_windows():
    prettier_cmd = ensure_file_has_ext(prettier_cmd, ".cmd")    # bun will not be found
return which(prettier_cmd)

Describe the solution you'd like

Perhaps this should be a subresolve function in util.py, working similarly to os.path.exists and which, but with an optional array of all extensions for Windows.

Additional context or screenshots

No response

sanekb avatar Apr 02 '25 19:04 sanekb

Hi @sanekb. Can you test out the feature on the feat/bun-pkg-mgr-support-305 branch?

If you need instructions on how to test SublimeText/JsPrettier against a Git branch, checkout this post.

jonlabelle avatar Apr 05 '25 16:04 jonlabelle

Hi! I've tested your changes, they only fix the global path search. I thought we could also use which for local cases with the following code:

#
# 1. check for prettier installed relative to active view
active_view_parents = generate_dirs(os.path.dirname(view.file_name()), limit=500)
for parent in active_view_parents:
    closest_to_view_prettier = which('prettier', make_local_prettier_path(parent))
    if closest_to_view_prettier is not None:
        return closest_to_view_prettier

#
# 2. check locally installed prettier
project_prettier_path = which('prettier', make_local_prettier_path(st_project_path))
if project_prettier_path is not None:
    return project_prettier_path
plugin_prettier_path = which('prettier', make_local_prettier_path(plugin_path))
if plugin_prettier_path is not None:
    return plugin_prettier_path

# ...

#
# 4. check globally install prettier
return which('prettier')  # your prev change here work nice for global case

Now it works well on both local and global cases for all npm, deno, bun package managers.

It turns out that in the util.py file, we don’t need to add .bunx to the pathext list since it already includes .exe, and this will work for windows, but let it remain as a precaution.

sanekb avatar Apr 07 '25 13:04 sanekb

Okay, let me give that a shot.

jonlabelle avatar Apr 15 '25 12:04 jonlabelle

@sanekb: I tried your suggestion, but it seems to break resolution of globally installed prettier.

jonlabelle avatar May 17 '25 14:05 jonlabelle

This issue has been automatically locked due to inactivity. Please open a new issue for related bugs.

github-actions[bot] avatar Sep 18 '25 02:09 github-actions[bot]