allenporter / flux-local

flux-local is a set of tools and libraries for managing a local flux gitops repository focused on validation steps to help improve quality of commits, PRs, and general local testing.
https://allenporter.github.io/flux-local/
Apache License 2.0
125 stars 22 forks source link

Symlinks in paths causes Path.relative_to() to fail #119

Closed kodek closed 1 year ago

kodek commented 1 year ago

It looks like the created worktree uses an absolute path (/private/var/..., [1]) whereas the process path is using the symlink path (/var/...).

This is probably hacky, but calling Path.resolve() in PathSelector.relative_to solves the issue:

        return self.process_path.resolve().relative_to(self.root.resolve())

Stack trace:

❯ fl diff hr datalore -n datalore --path k8s/
Traceback (most recent call last):
  File "/opt/homebrew/bin/flux-local", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/flux_local/tool/flux_local.py", line 86, in main
    asyncio.run(action.run(**vars(args)))
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/flux_local/tool/diff.py", line 308, in run
    await git_repo.build_manifest(selector=query)
  File "/opt/homebrew/lib/python3.11/site-packages/flux_local/git_repo.py", line 516, in build_manifest
    clusters = await get_clusters(
               ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/flux_local/git_repo.py", line 400, in get_clusters
    kustomizations = await kustomization_traversal(path_selector)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/flux_local/git_repo.py", line 289, in kustomization_traversal
    path_queue.put(path_selector.relative_path)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/flux_local/git_repo.py", line 141, in relative_path
    return self.process_path.relative_to(self.root)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 730, in relative_to
    raise ValueError("{!r} is not in the subpath of {!r}"
ValueError: '/var/folders/24/vvv5svzx1xb2q1qpsq8zc8bw0000gn/T/tmpgjrnx0em/k8s' is not in the subpath of '/private/var/folders/24/vvv5svzx1xb2q1qpsq8zc8bw0000gn/T/tmpgjrnx0em' OR one path is relative and the other is absolute.

[1] Under MacOS:

❯ ls -lsah /
total 10
 0 drwxr-xr-x   20 root  wheel   640B Jan 13 16:57 .
 0 drwxr-xr-x   20 root  wheel   640B Jan 13 16:57 ..
 <...>
 0 drwxr-xr-x    6 root  wheel   192B Feb 19 23:41 private
 0 lrwxr-xr-x@   1 root  wheel    11B Jan 13 16:57 tmp -> private/tmp
 0 lrwxr-xr-x@   1 root  wheel    11B Jan 13 16:57 var -> private/var
allenporter commented 1 year ago

Fixed in #129

allenporter commented 1 year ago

Thank you @kodek 👍🏼