Issue with tasks not picking up correct ruby version - ruby

This issues comes a goes depending on the Ruby project I'm working on. VSCode Tasks often do not run from the proper rbenv shimmed Ruby version. Sometimes, if I manually run a command from the integrated terminal first, then run the Code task it picks up the correct Ruby.
Example VSCode Task,
{
"version": "2.0.0",
"tasks": [
{
"label": "Run All Tests",
"type": "shell",
"command": "bin/rails test",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
}
}
]
}
When launching Code from the project directory (or any other way for that matter) if I run this task it runs from the context of the System ruby. However, if I open the integrated terminal and type bin/rails test (which works) then run the task again it will run in the proper rbenv shimmed ruby context.
Launching a debugger session however does work in the proper Ruby context, so I really don't understand what's being missed.

This answer will get tasks working, however it doesn't fix the underlying problem with VS Code. This probably needs to be an issue posted on their GitHub repo.
If you're on a Mac, this command will put a ruby symlink in your path that links to the rbenv version. Rbenv (and most applications) only change the path inside a shell environment (the .bash_profile only changes it for bash). This command will add it to the path regardless of what shell you run.
ln -s "/Users/$(whoami)/.rbenv/shims/ruby" /usr/local/bin/ruby
My guess is that VS Code is not actually using a shell environment and is instead calling the executable directly.

Until Visual Studio Code solves what seems to be a bug or missing configuration setting or, at least, missing documentation, I have resorted to setting the PATH variable on the task's command attribute.
For example, if the ruby I'm interested in using is at /full/path/to/bin/ruby, I prepend /full/path/to/bin to the PATH environment variable.
For example:
.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "sidekiq",
"type": "shell",
"command": "export PATH=/full/path/to/bin:$PATH && bundle exec sidekiq -C config/sidekiq.yml",
"presentation": {
"reveal": "always",
"panel": "dedicated"
},
"runOptions": {
"runOn": "folderOpen"
}
}]
}

Related

Debugging (Old) Ruby Rack via VsCode

I been task to update an old ruby (2.4.10) project.
The project is a simple API server written using Cuba and Rack.
(Note that the project uses dep).
I'm trying to setup a working environment on my mac (using rbenv) and would like to debug the API server via VsCode. After hours of searching and testing all sort of launch.json configuration I came up empty.
I have created the most minimal representation of the project in at the following repo https://github.com/dannyhuly/ruby-rack.
Any help would be greatly appreciated.
FYI
I'm a ruby newbie and would like to learn as mach as I can. So any additional information and suggestions would be greatly appreciated as well.
I ran a debug session with your project using the Ruby extension.
As explained in the vscode-ruby debugger docs, first I had to install the ruby-debug-ide and the debase gems.
Then I used the following launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Rack",
"type": "Ruby",
"request": "launch",
"program": "~/.rbenv/versions/2.4.10/bin/rackup",
"args": []
}
]
}
If there's any issue starting the session, you should be able to see error logs in the debug console panel in VSCode.

Updating Ruby Version in Visual Studio Code

I am using Visual Studio Code on my macOS where I am trying to update the version of Ruby I am using within a project from ruby 2.0.0p648 to a newer version of Ruby (I have tried a number of different versions). I have tried much of the information on this site to make the updates from this site and others, but no luck.
I have tried:
curl -L https://get.rvm.io | bash -s stable –ruby
rvm install ruby-2.4.0
rvm use ruby-2.4.0 --default
But when checking that I am using / installed 2.4.0 by using ruby -v, it still comes back with 2.0.0p648.
When I run those commands via the Apple Terminal the response I receive from asking for the version comes back with 2.4.0.
How can I fix this issue?
Thanks.
If your project has a gemfile with the ruby version specified, then RVM will pick it up when you CD to the project folder from command line. VS code doesn't seem to do this till I added "cwd" to the launch config and then it works as expected. Here is my launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Rails server",
"type": "Ruby",
"request": "launch",
"program": "${workspaceRoot}/bin/rails",
"args": [
"server"
],
"useBundler": true,
"showDebuggerOutput": true,
"cwd": "${workspaceRoot}"
}
]
}
I don't have a Mac, but since RVM is available on Linux, I've tried working with it in WSL, as well as Windows with Railsinstaller, so I have some experience.
Actually, this isn't even about VS Code, but MAYBE, since Mac has an old Ruby version available not contained within the version manager, this happens.
First of all, note all the available versions (inside RVM, well as the one which is shown by the terminal), and uninstall all versions outside of RVM.
Keep this with you just for backup, if incase, anything goes wrong, you can reinstall them.
Next try setting the preferred Ruby version using RVM.
Try finding out the Ruby version
ruby -v
If that matches your description, you're good to go.
Definitely, it about your IDE's config about ruby-path, find where can set ruby-path and set the correct rvm-ruby-path.

Run `sass --watch` within SublimeText

I've been working on a way to speed up my workflow with SublimeText2 and Sass.
I was looking for a way to have ST2 compile my SASS when I save. In looking how to do this, I came across this package: https://github.com/bnlucas/SassBuilder
In execution, it is exactly what I wanted, but it does not compile SASS like I hoped (doesn't include partials support).
So after more research I found a build package that works for me:
{
"cmd": ["sass --watch 'index.scss':'index.css'", "--stop-on-error", "--no-cache"],
"line_regex": "Line ([0-9]+):",
"osx":
{
"path": "/usr/local/bin:$PATH"
},
"windows":
{
"shell": "true"
}
}
This is similar to running the following command in a cmd prompt with Ruby:
sass --watch index.scss:index.css --stop-on-error --no-cache
So I run this build system and it watches my index.scss just fine. Any changes made to any of the .scss files in that folder and it updates the .index.css file. All of the standard messages from Sass are displayed in ST's console.
My issue is I know that is not what ST's build systems are for. Ideally, they would be used to build with a set of tools multiple times in a coding session.
My question is: How can I use ST2 to launch a sass --watch and have it run in the background? Is there some sort of command/key binding I could set to run the build's cmd line ("cmd": ["sass --watch 'index.scss':'index.css'", "--stop-on-error", "--no-cache"])?
Check out the SublimeOnSaveBuild plugin, available through Package Control. Since you already have a working build system, all you need to do is configure the plugin to point to your .sublime-build file, modify the file endings if you want, and you should be all set.
Good luck!

How do I execute "bundle install" on a nodejs heroku buildpack?

package.json:
"scripts": {
"preinstall": "bundle install",
"install": "cake build",
"start": "coffee server/app.coffee"
}
My build step (cake build) invokes sass, which is a gem executable. My Gemfile has sass. When I push to heroku, I get the error sh: bundle: not found". What's the correct way of making the sass command available in time for the build step?
In order to run multiple processes, each based on different languages, you can either use heroku-buildpack-multi or even just write your own custom buildpack! The former option is much easier, but may not work in all situations.

Sublime Text Build System that just "make"

I'm trying to build my project by simple executing make in the top directory. However, when I do, I get the following error:
[Errno 2] No such file or directory
[cmd: [u'make']]
[dir: /Users/jonathanong/Workspace/template]
[path: /usr/local/bin]
[Finished]
This is after setting the build configuration to Make.
I'm on Sublime Text 2.0.1, OS X 10.8.2. My Makefile consists of executing globally installed node.js binaries. What do I have to do?
This is because make is not in your PATH. If you can build in the terminal, use which make to figure out the path to make, and then add that to the path for build. You can edit the makefile build system to add your path.
Your new makefile rule should look like:
{
"cmd": ["make"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${project_path:${folder:${file_path}}}",
"selector": "source.makefile",
"path": "/usr/local/bin:/Applications/Xcode.app/Contents/Developer/usr/bin",
"variants":
[
{
"name": "Clean",
"cmd": ["make", "clean"]
},
{
"name": "Test",
"cmd": ["make", "test"]
}
]
}
This is basically the default make target, but I added to the PATH, and (of course) a test target. You may have to extend the PATH to find gcc, ifort, or whatever you are using to compile. Use : to separate directories on Linux and Mac, and ; on Windows. You can also change other environment variables, I had to set DYLD_LIBRARY_PATH and CPATH so that my libraries and include directories would be available.
It should be saved as Make (OSX).sublime-build in the User directory of your Sublime preferences. The (OSX) will ensure that this file is only used on Mac, so when you copy your preferences to a non-mac computer, you won't gunk up the path.
A much preferable solution is IMO to adjust the PATH in a plugin, so you have to set it just once for all build systems and all plugins calling external commands.
You can just do
import os
os.environ['PATH'] += os.pathsep + '/my/extra/path'
Real world example lives at https://github.com/schlamar/ST3User/blob/master/preferences.py

Resources