FileUtils writing to stderr

M

Mark L.

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.
 
E

Eric Hodel

Hi there,
=20
I'm having a few issues with FileUtils writing stuff to stderr when no err=
or has occurred. For example I might have the following simple
Rakefile:
=20
require 'rake'
task :clean do
rm_rf 'test'
end

Note that rake's rm_rf is not exactly FileUtils' rm_rf
When I run
=20
rake clean 2> err
=20
In the err file I see:
=20
'rm -rf test'
=20
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 w=
ithin ant. Rake doesn't seem to return the correct error code so I was testi=
ng for failure by detecting if anything was written to stderr.

You should query $?, not stderr. Rake should be behaving like a standard uni=
x command.=20
My ant task would thus look something like this:
=20
<target name=3D"rake">
<exec executable=3D"rake" failonerror=3D"true" errorproperty=3D"rake.err"=

<arg value=3D"clean">
</exec>
<fail if=3D"rake.err" message=3D"Rake failed with: ${rake.err}"/>
</target>
=20
This works fine except when I try to use the FileUtils methods. rm_rf caus= es it to fail as does mkdir_p.
=20
Any suggestions?

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

What is your rake task that exits 0 improperly?=
 
E

Eric Hodel

=20
Note that rake's rm_rf is not exactly FileUtils' rm_rf
=20
=20
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.
=20
rake email | sendmail # for example
=20
(Yes, rake currently outputs a message on stdout. I have fixed that in = trunk.)
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.
=20
You should query $?, not stderr. Rake should be behaving like a = standard unix command.=20
=20
=20
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.
 
M

Mark L.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top