2

Recently at work, we encountered a bug in Puppet. I decided to try to fix the bug in my free time. The first step in this process is getting it working using a git clone. I went through the instructions, then I tried to use rake and I started getting this error:

PS C:\Puppet-Code\Puppet> rake
rake/gempackagetask is deprecated.  Use rubygems/package_task instead
The system cannot find the path specified.
rake aborted!
No such file or directory - pwd

(See full trace by running task with --trace)

I ran it with --trace and got this:

PS C:\Puppet-Code\Puppet> rake --trace
rake/gempackagetask is deprecated.  Use rubygems/package_task instead
The system cannot find the path specified.
rake aborted!
No such file or directory - pwd
ext/packaging/tasks/00_utils.rake:198:in ``'
ext/packaging/tasks/00_utils.rake:198:in `get_pwd_version'
ext/packaging/tasks/00_utils.rake:171:in `get_dash_version'
ext/packaging/tasks/10_setupvars.rake:65:in `<top (required)>'
C:/Puppet-Code/Puppet/Rakefile:25:in `load'
C:/Puppet-Code/Puppet/Rakefile:25:in `block in <top (required)>'
C:/Puppet-Code/Puppet/Rakefile:25:in `each'
C:/Puppet-Code/Puppet/Rakefile:25:in `<top (required)>'
C:/Ruby193/lib/ruby/1.9.1/rake/rake_module.rb:25:in `load'
C:/Ruby193/lib/ruby/1.9.1/rake/rake_module.rb:25:in `load_rakefile'
C:/Ruby193/lib/ruby/1.9.1/rake/application.rb:501:in `raw_load_rakefile'
C:/Ruby193/lib/ruby/1.9.1/rake/application.rb:82:in `block in load_rakefile'
C:/Ruby193/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
C:/Ruby193/lib/ruby/1.9.1/rake/application.rb:81:in `load_rakefile'
C:/Ruby193/lib/ruby/1.9.1/rake/application.rb:65:in `block in run'
C:/Ruby193/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
C:/Ruby193/lib/ruby/1.9.1/rake/application.rb:63:in `run'
C:/Ruby193/bin/rake:32:in `<main>'

After looking at the file I found that this was the line in question:

%x{pwd}.strip.split('.')[-1]

I am using Powershell to run the rake file and when I run pwd directly in Powershell it works. However, if I run it from within the irb using %x{pwd} I just get an error message that there is no such file or directory 'pwd'. My understanding is that %x just passes that command to the shell (which I think is Powershell because that's where I'm running it from).

Can anyone explain why %x{pwd} is not working and what, if anything, I can do to fix it?

EDIT: I am using Powershell on Windows.

Thanks.

mnuzzo
  • 3,529
  • 4
  • 26
  • 29
  • Where did the rake task come from? – lurker Jun 29 '13 at 19:55
  • 1
    `%x{pwd}` works fine using irb on osx. are you using windows? if so, perhaps report the bug to the gem's maintainer? – Denis de Bernardy Jun 29 '13 at 19:55
  • @mbratch: rather than a bug, I was actually wondering if windows had a `pwd` shell command: "No such file or directory - pwd" would indicate that it's not recognizing it. – Denis de Bernardy Jun 29 '13 at 20:06
  • 1
    @Denis oh right. Indeed, it does not. Duh. The equivalent in Windows is just `cd` (unfortunately). – lurker Jun 29 '13 at 20:07
  • @mbratch Microsoft's Powershell does have pwd aliased to a command. – mnuzzo Jun 29 '13 at 20:20
  • Ok thanks! Interesting. I just ran `irb` in a power shell and it just doesn't want to recognize `pwd` no matter what syntax I use for doing a system command (I tried several). It will do `cd` fine, however. There's something "special" about the way they implemented `pwd` in PowerShell. – lurker Jun 29 '13 at 20:23

3 Answers3

3

In Ruby by default backticks, %x, system, or exec will not run under PowerShell.

On Windows all system commands will be running under cmd.exe, even if you start the process, subshell, irb etc. in another shell. It doesn't matter - Ruby will always spawn cmd.exe to run any system command.

From the documentation of Kernel.exec:

The standard shell always means "/bin/sh" on Unix-like systems, same as ENV["RUBYSHELL"] (or ENV["COMSPEC"] on Windows NT series), and similar.

You can try and change RUBYSHELL or COMSPEC to reference PowerShell, however you might have a hard time getting it to work.

Instead I would try other solutions, like finding an equivalent pwd command that runs under cmd.exe and use that.

Or why not just use Dir.pwd.

Casper
  • 33,403
  • 4
  • 84
  • 79
2

Use OS-Agnostic Methods

Instead of relying on the shell to provide you with the current working directory, use Dir#pwd instead. For example:

directory = Dir.pwd

Alternatively, if you really want to use a Windows shell for this, you might look at this older question. However, an OS-agnostic approach is usually sounder.

Example

Given a current working directory of C:\tmp\foo.bar the Dir module should result in the following:

Dir.pwd.strip.split('.')[-1]
# => "bar"

Assuming that "bar" is indeed the string you're looking for, all should be well. If you're doing something more complicated, you may need to update your question.

Community
  • 1
  • 1
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • 1
    +1. There is also the [Pathname](http://rdoc.info/stdlib/pathname/frames) class included with Ruby. It's like a blend of Dir and FileUtils. – the Tin Man Jun 30 '13 at 00:39
0

Reposting and expanding on the comments as the likely answer. If you're on windows, pwd is not available as a comment. You'd need to use cd or echo %cd%:

Windows equivalent to UNIX pwd

Don't miss this answer in the above-mentioned thread:

https://stackoverflow.com/a/4634089/417194

Open notepad as administrator and write:

@echo %cd%

Save it in c:\windows\system32\ with the name "pwd.cmd" (be careful not to save pwd.cmd.txt)

Then you have the pwd command.

There also is the built-in ruby function:

http://ruby-doc.org/core-2.0/Dir.html#method-c-pwd

Community
  • 1
  • 1
Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154