Repository: pantsbuild/pants

Issue:
`terraform_deployment` cannot load vars files if the root `terraform_module` is not in the same dir
**Describe the bug**

root/BUILD:
```
terraform_deployment(root_module="//mod0:mod0", var_files=["a.tfvars"])
```
root/a.tfvars:
```
var0 = "hihello"
```
mod/BUILD:
```
terraform_module()
```
mod/main.tf:
```
resource "null_resource" "dep" {}
```

running `pants experimental-deploy //root:root` yields:
```
Engine traceback:
  in select
    ..
  in pants.core.goals.deploy.run_deploy
    `experimental-deploy` goal

Traceback (most recent call last):
  File "/home/lilatomic/vnd/pants/src/python/pants/core/goals/deploy.py", line 176, in run_deploy
    deploy_processes = await MultiGet(
  File "/home/lilatomic/vnd/pants/src/python/pants/engine/internals/selectors.py", line 374, in MultiGet
    return await _MultiGet(tuple(__arg0))
  File "/home/lilatomic/vnd/pants/src/python/pants/engine/internals/selectors.py", line 172, in __await__
    result = yield self.gets
ValueError: 'root/a.tfvars' is not in the subpath of 'mod0' OR one path is relative and the other is absolute.
```

**Pants version**
2.18+



Discussion:
problem is in `terraform_relpath`. The idea is that the dir in the root module is the value of the argument `chdir` to Terraform. This effectively changes Terraforms cwd, which means that we have to find the relative path to the var_file. In the example above, we want to synthesise the Terraform invocation: `terraform -chdir=mod0/ apply -var-file=../root/a.tfvars`.
Turns out `PurePath(target).relative_to(chdir)` doesn't do that if they're not in the subpath.

Resolving patch:
diff --git a/src/python/pants/backend/terraform/utils.py b/src/python/pants/backend/terraform/utils.py
--- a/src/python/pants/backend/terraform/utils.py
+++ b/src/python/pants/backend/terraform/utils.py
@@ -1,7 +1,7 @@
 # Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
 # Licensed under the Apache License, Version 2.0 (see LICENSE).
+import os.path
 import shlex
-from pathlib import PurePath
 
 
 def terraform_arg(name: str, value: str) -> str:
@@ -11,4 +11,4 @@ def terraform_arg(name: str, value: str) -> str:
 
 def terraform_relpath(chdir: str, target: str) -> str:
     """Compute the relative path of a target file to the Terraform deployment root."""
-    return PurePath(target).relative_to(chdir).as_posix()
+    return os.path.relpath(target, start=chdir)


Test patch:
diff --git a/src/python/pants/backend/terraform/utils_test.py b/src/python/pants/backend/terraform/utils_test.py
new file mode 100644
--- /dev/null
+++ b/src/python/pants/backend/terraform/utils_test.py
@@ -0,0 +1,39 @@
+# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
+# Licensed under the Apache License, Version 2.0 (see LICENSE).
+import pytest
+
+from pants.backend.terraform.utils import terraform_relpath
+
+
+@pytest.mark.parametrize(
+    "chdir, target, expected_result",
+    [
+        pytest.param(
+            "path/to/deployment",
+            "path/to/deployment/file.txt",
+            "file.txt",
+            id="file_in_same_directory",
+        ),
+        pytest.param(
+            "path/to/deployment",
+            "path/to/other/file.txt",
+            "../other/file.txt",
+            id="file_in_different_directory",
+        ),
+        pytest.param(
+            "path/to/deployment",
+            "path/to/deployment/subdir/file.txt",
+            "subdir/file.txt",
+            id="file_in_subdirectory",
+        ),
+        pytest.param(
+            "path/to/deployment/subdir",
+            "path/to/deployment/file.txt",
+            "../file.txt",
+            id="file_in_parent_directory",
+        ),
+    ],
+)
+def test_terraform_relpath(chdir: str, target: str, expected_result: str):
+    result = terraform_relpath(chdir, target)
+    assert result == expected_result