FileUtils writing to stderr

Hi there,

I'm having a few issues with FileUtils writing stuff to stderr when no
error has occurred. For example I might have the following simple
Rakefile:

require 'rake'
task :clean do
  rm_rf 'test'
end

When I run

rake clean 2> err

In the err file I see:

'rm -rf test'

I would expect this to be sent to stdout rather than err.

The problem that I have is that I am trying to detect a rake failure
from within ant. Rake doesn't seem to return the correct error code so I
was testing for failure by detecting if anything was written to stderr.
My ant task would thus look something like this:

<target name="rake">
  <exec executable="rake" failonerror="true" errorproperty="rake.err">
    <arg value="clean">
  </exec>
  <fail if="rake.err" message="Rake failed with: ${rake.err}"/>
</target>

This works fine except when I try to use the FileUtils methods. rm_rf
causes it to fail as does mkdir_p.

Any suggestions?
Cheers,
Mark.

···

--
Posted via http://www.ruby-forum.com/.

Hi there,

I'm having a few issues with FileUtils writing stuff to stderr when no error has occurred. For example I might have the following simple
Rakefile:

require 'rake'
task :clean do
rm_rf 'test'
end

Note that rake's rm_rf is not exactly FileUtils' rm_rf

When I run

rake clean 2> err

In the err file I see:

'rm -rf test'

I would expect this to be sent to stdout rather than err.

Why? I expect informational messages on stderr. This way rake's messages don't interfere with a rake user's output. By having rake's messages on stderr a rake task author can write tasks that can be piped to other commands.

rake email | sendmail # for example

(Yes, rake currently outputs a message on stdout. I have fixed that in trunk.)

The problem that I have is that I am trying to detect a rake failure from within ant. Rake doesn't seem to return the correct error code so I was testing for failure by detecting if anything was written to stderr.

You should query $?, not stderr. Rake should be behaving like a standard unix command.

My ant task would thus look something like this:

<target name="rake">
<exec executable="rake" failonerror="true" errorproperty="rake.err">
   <arg value="clean">
</exec>
<fail if="rake.err" message="Rake failed with: ${rake.err}"/>
</target>

This works fine except when I try to use the FileUtils methods. rm_rf causes it to fail as does mkdir_p.

Any suggestions?

AFAIK rm -rf and mkdir -p always exit 0.

What is your rake task that exits 0 improperly?

···

On Jan 24, 2011, at 5:25, "Mark L." <marcuslamb@yahoo.com> wrote:

Hi Eric,

I see your point. I should be able to check the return code for errors.
This would make the ant task even simpler as I would just have to set
the 'failonerror' flag.

I guess my question above was trying to find another way of doing this
because initially the return code was returning 0 when I was not
expecting it.

After reading your comments I've gone back to look at the code to see if
I could pinpoint where it was going wrong (you'll have to forgive me -
I'm new to ruby and am modifying a script written by someone else)

I traced the problem to a 'system' command. We are using a javascript
closure compiler to minify some javascript. It would fail if there was a
syntax error in the javascript - but the rake script would exit
normally. The original piece of code looked like:

system `#{JAVA_CMD} -jar #{JS_ROOT}/lib/compiler.jar --js=#{src}
--js_output_file=#{dst}`

and then the rake script carried on as normal. I then tried to capture
the $? of the command and raise an exception if it was non-zero.
Unfortunately, as it stands the above command always returns a non-zero
response. I eventually figured out that it was the back-ticks that was
causing the issue and so my final solution was to do this:

system("#{JAVA_CMD} -jar #{JS_ROOT}/lib/compiler.jar --js=#{src}
--js_output_file=#{dst}")
   # Raise an exception if the compiler fails
   if( !$?.success? )
     raise "Closure Compiler failed to compile javascript"
   end

and this finally seemed to resolve my problem.

Thanks for the response,
Mark.

···

--
Posted via http://www.ruby-forum.com/.

Hi there,

I'm having a few issues with FileUtils writing stuff to stderr when no error has occurred. For example I might have the following simple
Rakefile:

require 'rake'
task :clean do
rm_rf 'test'
end

Note that rake's rm_rf is not exactly FileUtils' rm_rf

When I run

rake clean 2> err

In the err file I see:

'rm -rf test'

I would expect this to be sent to stdout rather than err.

Why? I expect informational messages on stderr. This way rake's messages don't interfere with a rake user's output. By having rake's messages on stderr a rake task author can write tasks that can be piped to other commands.

rake email | sendmail # for example

(Yes, rake currently outputs a message on stdout. I have fixed that in trunk.)

The problem that I have is that I am trying to detect a rake failure from within ant. Rake doesn't seem to return the correct error code so I was testing for failure by detecting if anything was written to stderr.

You should query $?, not stderr. Rake should be behaving like a standard unix command.

My ant task would thus look something like this:

<target name="rake">
<exec executable="rake" failonerror="true" errorproperty="rake.err">
  <arg value="clean">
</exec>
<fail if="rake.err" message="Rake failed with: ${rake.err}"/>
</target>

This works fine except when I try to use the FileUtils methods. rm_rf causes it to fail as does mkdir_p.

Any suggestions?

AFAIK rm -rf and mkdir -p always exit 0.

My mistake, these can exit 1.

I suppose the question then is "what does rm -f mean for rake's exit status?"

If I'm writing a clean task that uses rm -f I suppose I'm saying it's OK for clean to not delete some things.

···

On Jan 24, 2011, at 09:32, Eric Hodel wrote:

On Jan 24, 2011, at 5:25, "Mark L." <marcuslamb@yahoo.com> wrote:

What is your rake task that exits 0 improperly?