Files with same file name not loaded?


#1

So I have 2 files both named a.rb.

One is located in the user folder:

C:\Users\Jim\AppData\Roaming\SketchUp\SketchUp 2015\SketchUp\Plugins\a.rb

The other located is in the ProgramData folder:

C:\ProgramData\SketchUp\SketchUp 2015\SketchUp\Plugins\a.rb

In my case, I only get the the first file in the AppData plugins folder loaded. This is incorrect behavior since each file has a unique path even though they have the same basename.

Could someone verify this?


#2

No sorry JIm, your conclusion is incorrect.
Simply read the description for Kernel.require.

(1) The first file found is loaded.

(2) The user’s %AppData% path has precedence over the %ProgramData% path.
(The user may wish to copy a script from the latter into their own “Plugins” folder, and tweak it for personal use. This is very likely to happen in an educational setting.)

So, when specifying relative script paths, it depends upon the order of the basepaths in the $LOAD_PATH array. The user’s “plugins” path should come before, the program’s “plugins” path.

If you wish a different order, you can change the $LOAD_PATH order on your machine only, but do not expect others to do this.

The usual proper way, (of ignoring the $LOAD_PATH order,) is to specify an absolute script path as the argument to Kernel.require.


If you were thinking that because the first “a.rb” has already been loaded, that Kernel.require should continue searching for another to load, that is incorrect. If it finds that the argument path has already been loaded, (by checking the $LOADED_FEATURES array,) an finds a member that has the argument as part of the pathstring, then it returns false, and does no search.

You would need to provide a more specific path argument to Kernel.require, that would not match the first “a.rb” but would match the second in %ProgramData%.


#3

This is ‘require’ behavior, but if you use ‘load’, passing the full path to the file, then it should work, loading both sets of code ?


#4

Well, it is not my code. This is SketchUp that loads all the appropriate files in these two folders. So SketchUp is not using the full path when it requires them which is why, as Dan explained, the second a.rb is not loaded. The second a.rb file resolves to the same path as the first and is not loaded because that path already exists in $LOADED_FEATURES.


#5

So that means that your ‘loader’ code file needs to explicitly load those two files from their paths, rather than let Sketchup require them as it starts…
If they are in subfolders they won’t auto-load at all, only the loader will.
I find it hard to imagine why you’d actually want to have two files with the exact same name loading from different locations ?
If their contents are different give them different names, if theit contents are the same why worry when one doesn’t load ?
Presumably the ProgramData one is put there by an Installer, whilst the Roaming one is installed from an RBZ automatically vis Preferences ?
If you actually want both loading what’s wrong with having them named differently say a.rb and a_.rb ?


#6

Ditto. This is just “kicking against the prickers”.

Too make this work, it would need to modify the Ruby environment’s behavior, which could cause strange issues with other extensions.


#7

Debug logging mostly. I wanted to symbolic link to the same file from each folder.