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

Use array args instead of string args for system command #45

Merged
merged 3 commits into from
Jan 1, 2024
Merged

Use array args instead of string args for system command #45

merged 3 commits into from
Jan 1, 2024

Conversation

ntkme
Copy link
Contributor

@ntkme ntkme commented Sep 10, 2023

This PR has the following changes:

  1. Replace the string command with array command to avoid spawning a subshell before execute the child process. This also remove the need of escaping command and arguments, because the array form is treated literally.
  2. Add ruby interpreter to run ruby script, so that it works on windows where shebang is not supported.

Comment on lines +21 to +25
exclude:
- ruby-version: "3.0"
gemfile: gemfiles/rails_main_propshaft.gemfile
- ruby-version: "3.0"
gemfile: gemfiles/rails_main_sprockets.gemfile
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main branch no longer install on ruby 3.0, therefore exclude them from the test.

end

def dartsass_build_options
Rails.application.config.dartsass.build_options
Rails.application.config.dartsass.build_options.flat_map(&:split)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

split is used for compatibility with <=0.5.0, where build_options are simply concatenated strings:

  • --option (with a space prefix) will become ["--option"].
  • --option1 --option2 will become ["--option1", "--option2"]

end

def dartsass_compile_command
"#{EXEC_PATH} #{dartsass_build_options} #{dartsass_load_paths} #{dartsass_build_mapping}"
[ RbConfig.ruby, EXEC_PATH ].concat(dartsass_build_options).concat(dartsass_load_paths).concat(dartsass_build_mapping)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding RbConfig.ruby so that it works on windows as well.

end

namespace :dartsass do
desc "Build your Dart Sass CSS"
task build: :environment do
system dartsass_compile_command, exception: true
system(*dartsass_compile_command, exception: true)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The array form of system does not invoke a shell. It invoke the binary directly, therefore it is faster and safer.

@ntkme
Copy link
Contributor Author

ntkme commented Jan 1, 2024

@dhh I have added some comments explaining the changes here. Would you mind take a look when you get a chance? Thanks.

@dhh
Copy link
Member

dhh commented Jan 1, 2024

Looks good!

@dhh dhh merged commit 1f31ec4 into rails:main Jan 1, 2024
26 checks passed
@ntkme ntkme deleted the exec-array branch January 1, 2024 19:02
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.

assets:precompile on windows
2 participants