Repository: systemd/mkosi

Issue:
Building with skip final phase causes exception
Hopefully the traceback explains it all. There is nothing besides a very simple build script in the working directory.

```
# mkosi --format=directory --skip-final-phase=yes --build-script build-script.sh build
....
‣  Unmounting Package Cache
‣  Copying in build script…
‣  Copying in sources…
‣  Unmounting image
‣  Running build script…
I built something
‣  Removing artifacts from development build…
‣ Skipping (second) final image build phase.
‣ Linking image file…
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.10/dist-packages/mkosi-13-py3.10.egg/mkosi/__main__.py", line 36, in <module>
  File "/usr/local/lib/python3.10/dist-packages/mkosi-13-py3.10.egg/mkosi/__main__.py", line 27, in main
  File "/usr/local/lib/python3.10/dist-packages/mkosi-13-py3.10.egg/mkosi/__init__.py", line 8337, in run_verb
  File "/usr/local/lib/python3.10/dist-packages/mkosi-13-py3.10.egg/mkosi/__init__.py", line 7725, in build_stuff
  File "/usr/local/lib/python3.10/dist-packages/mkosi-13-py3.10.egg/mkosi/__init__.py", line 4726, in link_output
FileNotFoundError: [Errno 2] No such file or directory: '/root/.mkosi-z0eod00q/root' -> '/root/image'
```

```
# cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
```


Discussion:

Resolving patch:
diff --git a/mkosi/__init__.py b/mkosi/__init__.py
--- a/mkosi/__init__.py
+++ b/mkosi/__init__.py
@@ -4517,6 +4517,9 @@ def _link_output(
 def link_output(args: MkosiArgs, root: Path, artifact: Optional[BinaryIO]) -> None:
     with complete_step("Linking image file…", f"Linked {path_relative_to_cwd(args.output)}"):
         if args.output_format in (OutputFormat.directory, OutputFormat.subvolume):
+            if not root.exists():
+                return
+
             assert artifact is None
 
             make_read_only(args, root, for_cache=False, b=False)
@@ -4528,7 +4531,9 @@ def link_output(args: MkosiArgs, root: Path, artifact: Optional[BinaryIO]) -> No
             OutputFormat.tar,
             OutputFormat.cpio,
         ):
-            assert artifact is not None
+            if artifact is None:
+                return
+
             _link_output(args, artifact.name, args.output)
 
 
@@ -4691,6 +4696,9 @@ def save_manifest(args: MkosiArgs, manifest: Manifest) -> None:
 
 
 def print_output_size(args: MkosiArgs) -> None:
+    if not args.output.exists():
+        return
+
     if args.output_format in (OutputFormat.directory, OutputFormat.subvolume):
         MkosiPrinter.print_step("Resulting image size is " + format_bytes(dir_size(args.output)) + ".")
     else:


Test patch: