Repository: mesonbuild/meson

Issue:
FileNotFoundError: [Errno 2] No such file or directory: '/home/me/test01/meson-private/coredata.dat'
**Describe the bug**

Calling `meson compile` outside of a build directory (or without `-C builddir`) shows a traceback since 0.60.

**To Reproduce**

```
$ mkdir test01 && cd test01 && python3 -m venv venv && source venv/bin/activate && pip3 install meson==0.59.4 && rehash && meson --version && meson compile
Collecting meson==0.59.4
  Using cached meson-0.59.4-py3-none-any.whl (823 kB)
Installing collected packages: meson
Successfully installed meson-0.59.4
WARNING: You are using pip version 21.2.3; however, version 21.3.1 is available.
You should consider upgrading via the '/home/me/test01/venv/bin/python3 -m pip install --upgrade pip' command.
0.59.4

ERROR: Current directory is not a meson build directory: `/home/me/test01`.
Please specify a valid build dir or change the working directory to it.
It is also possible that the build directory was generated with an old
meson version. Please regenerate it in this case.
zsh: exit 1     meson compile
```

```
$ mkdir test02 && cd test02 && python3 -m venv venv && source venv/bin/activate && pip3 install meson==0.60.1 && rehash && meson --version && meson compile
Collecting meson==0.60.1
  Using cached meson-0.60.1-py3-none-any.whl (837 kB)
Installing collected packages: meson
Successfully installed meson-0.60.1
WARNING: You are using pip version 21.2.3; however, version 21.3.1 is available.
You should consider upgrading via the '/home/me/test02/venv/bin/python3 -m pip install --upgrade pip' command.
0.60.1
Traceback (most recent call last):
  File "/home/me/test02/venv/lib64/python3.10/site-packages/mesonbuild/mesonmain.py", line 138, in run
    return options.run_func(options)
  File "/home/me/test02/venv/lib64/python3.10/site-packages/mesonbuild/mcompile.py", line 324, in run
    cdata = coredata.load(options.wd)
  File "/home/me/test02/venv/lib64/python3.10/site-packages/mesonbuild/coredata.py", line 1030, in load
    with open(filename, 'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/home/me/test02/meson-private/coredata.dat'
zsh: exit 2     meson compile
```

**Expected behavior**

Same error message as in 0.59.

**system parameters**
* Is this a [cross build](https://mesonbuild.com/Cross-compilation.html) or just a plain native build (for the same computer)?
* what operating system? Fedora Linux 35
* what Python version are you using? Python 3.10.0
* what `meson --version`? 0.59.4/0.60.1
* what `ninja --version` if it's a Ninja build? 1.10.2



Discussion:
FYI: Bisected to commit: abaa980436f53100041bd5535589bb1c42019bd6
I can confirm this is still an issue on Meson 0.61.1 (running on Mac).

However, passing `-C <builddir>` is an effective workaround.
> However, passing `-C <builddir>` is an effective workaround.

It's not a workaround.

As the original report notes, the bug here is that this is *supposed* to error out, with an informative error message rather than a traceback.

That informative error message literally tells you to specify a builddir.

Resolving patch:
diff --git a/mesonbuild/mcompile.py b/mesonbuild/mcompile.py
--- a/mesonbuild/mcompile.py
+++ b/mesonbuild/mcompile.py
@@ -322,20 +322,18 @@ def add_arguments(parser: 'argparse.ArgumentParser') -> None:
     )
 
 def run(options: 'argparse.Namespace') -> int:
-    cdata = coredata.load(options.wd)
     bdir = Path(options.wd)
-    buildfile = bdir / 'meson-private' / 'build.dat'
-    if not buildfile.is_file():
-        raise MesonException(f'Directory {options.wd!r} does not seem to be a Meson build directory.')
+    validate_builddir(bdir)
+    if options.targets and options.clean:
+        raise MesonException('`TARGET` and `--clean` can\'t be used simultaneously')
+
+    cdata = coredata.load(options.wd)
     b = build.load(options.wd)
     setup_vsenv(b.need_vsenv)
 
     cmd = []    # type: T.List[str]
     env = None  # type: T.Optional[T.Dict[str, str]]
 
-    if options.targets and options.clean:
-        raise MesonException('`TARGET` and `--clean` can\'t be used simultaneously')
-
     backend = cdata.get_option(mesonlib.OptionKey('backend'))
     assert isinstance(backend, str)
     if backend == 'ninja':


Test patch: