If there are modules in sub-directories of a test or executable, fpm reports that it cannot find them.
[pop-os:~/tmp/test_subdirectories] tree
.
โโโ app
โย ย โโโ main.f90
โย ย โโโ sub
โย ย โโโ file.f90
โโโ fpm.toml
โโโ README.md
โโโ src
โย ย โโโ test_subdirectories.f90
โโโ test
โโโ check.f90
โโโ sub
โโโ something.f90
5 directories, 7 files
[pop-os:~/tmp/test_subdirectories] fpm run
+ mkdir -p build/dependencies
Unable to find source for module dependency: "thing_m" used by "app/main.f90"
ERROR STOP 1
Error termination. Backtrace:
#0 0x47eb9d in ???
#1 0x47ee35 in ???
#2 0x47fdde in ???
#3 0x406276 in __fpm_MOD_cmd_run
at src/fpm.f90:226
#4 0x400890 in MAIN__
at app/main.f90:28
#5 0x400890 in main
at app/main.f90:9
[pop-os:~/tmp/test_subdirectories] fpm test
Unable to find source for module dependency: "thing_m" used by "app/main.f90"
ERROR STOP 1
Error termination. Backtrace:
#0 0x47eb9d in ???
#1 0x47ee35 in ???
#2 0x47fdde in ???
#3 0x406276 in __fpm_MOD_cmd_run
at src/fpm.f90:226
#4 0x400861 in MAIN__
at app/main.f90:30
#5 0x400861 in main
at app/main.f90:9
I was under the impression that this was meant to work.
This restriction was put in with #190, that executables can only use non-library modules in the same directory as the executable source file. I need to search for the relevant discussion for the reasoning behind this.
https://github.com/fortran-lang/fpm/issues/164#issuecomment-689433768 is the relevant comment here.
I thought I recalled a discussion like that. I am on board with having multiple executables being automatically detected in the same folder (and rereading the discussion it seems I was accepting at the time). I do not recall why we wanted to disallow the programs from using modules in subdirectories, and do not see in the discussion something that would support that decision.
Based on the current implementation of the model, how sources are found and parsed, etc., I don't see a reason we can't allow it. I think it's a somewhat surprising restriction, given I was even involved in the original conversation and didn't remember it was in place. I think it's a likely (if not common) use case. I certainly have some projects that would benefit from that capability.
I'll be happy to do the fix. Are there any objections to it?
As of now, I don't see why not. It would be an implicit rule, and an fpm user may never need to learn about it if they don't want to.
Yes, please go ahead with the changes Brad. I believe the choice was made based on the original stricter rules in Haskell fpm, but I agree now that it's an unnecessary restriction.
To be clear, will you be allowing access to just modules in relative sub-directories or access to all 'non-library' modules so to speak? My preference would probably be for the latter in terms of simplicity from the user point of view.
I would say that a program should have access to all modules:
but not in other directories. I.e. a program app/prog1/main.f90 would not have access to a module app/prog2/mod2.f90.
I will note that my (naive) assumption that running add_sources_from_dir with recurse=.true. from add_executable_sources would be all that is required does not seem to do it. Would you be able to point me at what else needs to change?
Okay yeah that sounds good.
The sources should already be in the model; you just need to modify the rule for resolving module dependencies.
Edit: see resolve_module_dependencies
Thanks. Will submit a PR once I've got it all worked out.