Repository: mesonbuild/meson

Issue:
Copying build directory makes meson stuck infinitely reconfigurating
## Steps to reproduce *(in terms of terminal commands)*

```
$ cat test.cpp
int main() {}
$ cat meson.build
project(
  'project',
  'cpp',
  version: '0.1.0',
)

executable(
  'foo',
  'test.cpp',
  install: false,
)
$ meson build
The Meson build system
Version: 0.52.0
Source dir: /tmp/foo
Build dir: /tmp/foo/build
Build type: native build
Project name: project
Project version: 0.1.0
C++ compiler for the host machine: ccache c++ (gcc 9.2.0 "c++ (GCC) 9.2.0")
C++ linker for the host machine: GNU ld.bfd 2.32
Host machine cpu family: x86_64
Host machine cpu: x86_64
Build targets in project: 1
Found ninja-1.9.0.git at /usr/bin/ninja
$ cp -r build build-release
$ meson configure build-release/ --buildtype=release
$ ninja -C build-release/
```
### Expected

Build immediately starts

### Actual

Infinite sequence of configuration prints:

```
ninja: Entering directory `build-release/'
[0/1] Regenerating build files.
The Meson build system
Version: 0.52.0
Source dir: /tmp/foo
Build dir: /tmp/foo/build
Build type: native build
Project name: project
Project version: 0.1.0
C++ compiler for the host machine: ccache c++ (gcc 9.2.0 "c++ (GCC) 9.2.0")
C++ linker for the host machine: GNU ld.bfd 2.32
Host machine cpu family: x86_64
Host machine cpu: x86_64
Build targets in project: 1
Found ninja-1.9.0.git at /usr/bin/ninja
[0/1] Regenerating build files.
The Meson build system
Version: 0.52.0
Source dir: /tmp/foo
Build dir: /tmp/foo/build
Build type: native build
Project name: project
Project version: 0.1.0
C++ compiler for the host machine: ccache c++ (gcc 9.2.0 "c++ (GCC) 9.2.0")
C++ linker for the host machine: GNU ld.bfd 2.32
Host machine cpu family: x86_64
Host machine cpu: x86_64
Build targets in project: 1
Found ninja-1.9.0.git at /usr/bin/ninja
[0/1] Regenerating build files.
The Meson build system
Version: 0.52.0
Source dir: /tmp/foo
Build dir: /tmp/foo/build
Build type: native build
Project name: project
Project version: 0.1.0
C++ compiler for the host machine: ccache c++ (gcc 9.2.0 "c++ (GCC) 9.2.0")
C++ linker for the host machine: GNU ld.bfd 2.32
Host machine cpu family: x86_64
Host machine cpu: x86_64
Build targets in project: 1
Found ninja-1.9.0.git at /usr/bin/ninja
```
## Additional information

* Meson version 0.52.0
* Platform: Archlinux


Discussion:
Apparently, the `build-release` directory has paths to the older one in various configs, which triggers this problem.
> Apparently, the build-release directory has paths to the older one in various configs, which triggers this problem.

This is expected and how any "out-of-source" build system works, not just meson. Why would you need to copy generated files as opposed to just generating new and correct ones in the new place?

This being said, no excuse is ever good enough to justify an infinite loop: this use case should fail in finite time - but still fail.
> Why would you need to copy generated files as opposed to just generating new and correct ones in the new place?

Well, the actual `meson build` command has dozens of lines, and is written inside a PKGBUILD file *(a config for assembling an Archlinux package)* with various env. variables and stuff, so I didn't want to  figure it all out and fix paths. I hoped to work around it by simply copying already configured directory.
I am hitting against  this same issue in a Gitlab CI context:
1. Build the project, save the build-folder as artifact
2. Test the built things, on a different CI stage. By downloading the built folder as artifact, then running meson test on it

PS: I do use a cross-file in one of the builds, so maybe something to do with #5505 

PPS: I could work around this issue by adding `--no-rebuild` to the `meson test` command: `meson test --verbose --no-rebuild`

Resolving patch:
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py
--- a/mesonbuild/backend/ninjabackend.py
+++ b/mesonbuild/backend/ninjabackend.py
@@ -1293,7 +1293,9 @@ def generate_rules(self):
             ['--internal',
              'regenerate',
              self.environment.get_source_dir(),
-             self.environment.get_build_dir()]
+             # Ninja always runs from the build_dir. This includes cases where the user moved the
+             # build directory and invalidated most references. Make sure it still regenerates.
+             '.']
         self.add_rule(NinjaRule('REGENERATE_BUILD',
                                 c, [],
                                 'Regenerating build files.',


Test patch: