Skip to content

Improve package manager detection for Bun 1.2 compatibility #165

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
merged 4 commits into from
Mar 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -199,6 +199,7 @@ PLATFORMS
arm64-darwin-20
arm64-darwin-21
arm64-darwin-22
arm64-darwin-24
x86_64-darwin-20
x86_64-darwin-21
x86_64-linux
4 changes: 3 additions & 1 deletion lib/install/helpers.rb
Original file line number Diff line number Diff line change
@@ -10,7 +10,9 @@ def bundler_run_cmd
end

def using_bun?
File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
tool_exists?('bun') && (File.exist?('bun.lockb') ||
File.exist?('bun.lock') ||
File.exist?('yarn.lock'))
Copy link
Contributor

@navidemad navidemad Mar 4, 2025

Choose a reason for hiding this comment

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

When a system has both bun installed alongside a project that uses Yarn (presence of a yarn.lock file)

We should remove any conditional checks for yarn.lock from bun related lines.

Even though Bun can technically manage yarn.lock file.

Keeping these existence checks may lead to use of bun when you were using yarn before this PR has been merged and want to keep using yarn for the project.

end

def tool_exists?(tool)
44 changes: 22 additions & 22 deletions lib/tasks/cssbundling/build.rake
Original file line number Diff line number Diff line change
@@ -21,42 +21,42 @@ module Cssbundling
module Tasks
extend self

LOCK_FILES = {
bun: %w[bun.lockb bun.lock yarn.lock],
Copy link
Contributor

@navidemad navidemad Mar 4, 2025

Choose a reason for hiding this comment

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

Same as : #165 (comment)

yarn: %w[yarn.lock],
pnpm: %w[pnpm-lock.yaml],
npm: %w[package-lock.json]
}

def install_command
case tool
when :bun then "bun install"
when :yarn then "yarn install"
when :pnpm then "pnpm install"
when :npm then "npm install"
case
when using_tool?(:bun) then "bun install"
when using_tool?(:yarn) then "yarn install"
when using_tool?(:pnpm) then "pnpm install"
when using_tool?(:npm) then "npm install"
else raise "cssbundling-rails: No suitable tool found for installing JavaScript dependencies"
end
end

def build_command
case tool
when :bun then "bun run build:css"
when :yarn then "yarn build:css"
when :pnpm then "pnpm build:css"
when :npm then "npm run build:css"
case
when using_tool?(:bun) then "bun run build:css"
when using_tool?(:yarn) then "yarn build:css"
when using_tool?(:pnpm) then "pnpm build:css"
when using_tool?(:npm) then "npm run build:css"
else raise "cssbundling-rails: No suitable tool found for building CSS"
end
end

def tool
case
when File.exist?('bun.lockb') then :bun
when File.exist?('yarn.lock') then :yarn
when File.exist?('pnpm-lock.yaml') then :pnpm
when File.exist?('package-lock.json') then :npm
when tool_exists?('bun') then :bun
when tool_exists?('yarn') then :yarn
when tool_exists?('pnpm') then :pnpm
when tool_exists?('npm') then :npm
end
end
private

def tool_exists?(tool)
system "command -v #{tool} > /dev/null"
end

def using_tool?(tool)
tool_exists?(tool) && LOCK_FILES[tool].any? { |file| File.exist?(file) }
end
end
end