Question about inspection 'Constant, [...] with same name is already declared in scope'

I was wondering about the semantics of the inspection 'Constant, module or class with same name is already declared in scope'. Suppose I have the following setup:

File a.rb:

require 'b'


module Demo
  CONSTANT = 42
end



File b.rb:



require 'a'


module Demo


end


CONSTANT = 42 is marked as already being declared in scope, which I didn't expect to be the case because there really is only one declaration. Is this behavior intentional?

9 comments
Comment actions Permalink

Which RubyMine version do you use? I don't see such behaviour in RubyMine 3.1 Beta. But on run-time you will get similar error.
Screen shot 2011-02-09 at 10.43.32 PM.png

0
Comment actions Permalink

P.S: The warning appeared after I'd reloaded the project. But seems it is reasonable according to run-time bahaviour.

0
Comment actions Permalink

This is the IDEA Ruby plugin 3.1.17317. But you're right: I get the same warning when running the script. I must have missed that. So I guess, the inspection result is correct after all.

What would be the best way to avoid that warning? It's not always possible to know ahead of time which files will be included from which file.

Thanks

0
Comment actions Permalink

It seems to be a little more complicated than that. I just recreated that simple test case from above and CONSTANT is not highlighted as warning (anymore???). However, I see this warning in my project. There is no other declaration of that symbol in my project (at least none that I could find with "Find in Path") and i doubt that it clashes with standard ruby libs.

Bildschirmfoto 2011-02-09 um 21.27.43.png

0
Comment actions Permalink

Got it. I'm not sure how this is triggered, but something is definitely not adding up here.
Bildschirmfoto 2011-02-09 um 21.41.00.png

The warning goes aways, if I add a new line:

Bildschirmfoto 2011-02-09 um 21.44.18.png

Removing that first line still does not show the warning. It only reappears after making more changes.

0
Comment actions Permalink

But you're right: I get the same warning when running the script. I must have missed that. So I guess, the inspection result is correct after all.
What would be the best way to avoid that warning?

In your example a and b includes each other recursively. If you launch "b" you wont see any warning on runtime.

It's not always possible to know ahead of time which files will be included from which file.

When you open file in Ruby plugin (or RubyMine) it analyzes all files which are required in current file from its point of view. So each file gives a bit different tree. This algorithm is more complicated in case of Rails projects because there are lots of implicit dynamic "require" calls.

0
Comment actions Permalink

I assume it is related to some bugs or behaviour of internal caching and optimizations. In future Ruby plugin / RubyMine version we are going to rewrite our code insight engine so I don't want to submit it as a bug but if you want you can sumbit it - http://youtrack.jetbrains.net/issues/ruby.

0
Comment actions Permalink

I agree that there should be a warning in a.rb and that there is no problem if I run b.rb.

However, I get inconsistent results for a.rb depending on the sequence of edits that I make (see my screenshots below). I believe, this shouldn't be the case. At least not for this trivial scenario.

0
Comment actions Permalink

I filed RUBY-7799 for this. I guess, I'll just have to turn that warning off for the time being.

0

Please sign in to leave a comment.