# Buggy code
def scan_fortran_module_outputs(self, target):
    compiler = None
    for lang, c in self.build.compilers.items():
        if lang == "fortran":
            compiler = c
            break
    if compiler is None:
        self.fortran_deps[target.get_basename()] = {}
        return
    modre = re.compile(r"\s*module\s+(\w+)", re.IGNORECASE)
    module_files = {}
    for s in target.get_sources():
        # FIXME, does not work for generated Fortran sources,
        # but those are really rare. I hope.
        if not compiler.can_compile(s):
            continue
        filename = os.path.join(self.environment.get_source_dir(), s.subdir, s.fname)
        with open(filename) as f:
            for line in f:
                modmatch = modre.match(line)
                if modmatch is not None:
                    modname = modmatch.group(1)
                    if modname.lower() == "procedure":
                        # MODULE PROCEDURE construct
                        continue
                    if modname in module_files:
                        raise InvalidArguments(
                            "Namespace collision: module %s defined in "
                            "two files %s and %s." % (modname, module_files[modname], s)
                        )
                    module_files[modname] = s
    self.fortran_deps[target.get_basename()] = module_files


# Traceback (FileNotFoundError)
The Meson build system
Version: 0.38.0
Source dir: /home/Adama-docs/Adam/MyDocs/praca/IMGW/dev/meson_bug
Build dir: /home/Adama-docs/Adam/MyDocs/praca/IMGW/dev/meson_bug/build
Build type: native build
Project name: simple fortran
Native fortran compiler: gfortran (gcc 5.4.1)
Build machine cpu family: x86_64
Build machine cpu: x86_64
Program pp_ser.py found: YES (/usr/bin/env python /home/adam/meson_bug/pp_ser.py)
Build targets in project: 1
Traceback (most recent call last):
File "/home/adam/.local/lib/python3.5/site-packages/mesonbuild/mesonmain.py", line 286, in run
app.generate()
File "/home/adam/.local/lib/python3.5/site-packages/mesonbuild/mesonmain.py", line 170, in generate
g.generate(intr)
File "/home/adam/.local/lib/python3.5/site-packages/mesonbuild/backend/ninjabackend.py", line 191, in generate
self.generate_target(t, outfile)
File "/home/adam/.local/lib/python3.5/site-packages/mesonbuild/backend/ninjabackend.py", line 386, in generate_target
header_deps=header_deps)
File "/home/adam/.local/lib/python3.5/site-packages/mesonbuild/backend/ninjabackend.py", line 1916, in generate_single_compile
extra_deps += self.get_fortran_deps(compiler, abs_src, target)
File "/home/adam/.local/lib/python3.5/site-packages/mesonbuild/backend/ninjabackend.py", line 1632, in get_fortran_deps
with open(src) as f:
FileNotFoundError: [Errno 2] No such file or directory: '/home/adam/meson_bug/build/dwarf@exe/src1.f90'

# Fixed code
def scan_fortran_module_outputs(self, target):
    compiler = None
    for lang, c in self.build.compilers.items():
        if lang == "fortran":
            compiler = c
            break
    if compiler is None:
        self.fortran_deps[target.get_basename()] = {}
        return
    modre = re.compile(r"\s*module\s+(\w+)", re.IGNORECASE)
    module_files = {}
    for s in target.get_sources():
        # FIXME, does not work for Fortran sources generated by
        # custom_target() and generator() as those are run after
        # the configuration (configure_file() is OK)
        if not compiler.can_compile(s):
            continue
        filename = s.absolute_path(
            self.environment.get_source_dir(), self.environment.get_build_dir()
        )
        with open(filename) as f:
            for line in f:
                modmatch = modre.match(line)
                if modmatch is not None:
                    modname = modmatch.group(1)
                    if modname.lower() == "procedure":
                        # MODULE PROCEDURE construct
                        continue
                    if modname in module_files:
                        raise InvalidArguments(
                            "Namespace collision: module %s defined in "
                            "two files %s and %s." % (modname, module_files[modname], s)
                        )
                    module_files[modname] = s
    self.fortran_deps[target.get_basename()] = module_files