Repository: packit/packit
Issue: srpm and propose_downstream fail without specfile_path

umockdev used to have `specfile_path: packaging/umockdev.spec` in its packit.yml. The [docs](https://packit.dev/docs/configuration/#specfile_path) say "Else recursively search the tree and use the first spec file found.", which sounds very sensible.
So I dropped that field (and `files_to_sync:`) in https://github.com/martinpitt/umockdev/pull/184 , and the [PR COPR build](https://dashboard.packit.dev/results/copr-builds/343764) was quite fine:

> Local spec files found: [PosixPath('packaging/umockdev.spec')]. Taking: packaging/umockdev.spec

However, now I did a [release](https://github.com/martinpitt/umockdev/releases/tag/0.17.11) and propose-downstream [failed](https://dashboard.packit.dev/results/propose-downstream/555):

> DEBUG  Local spec files found: [PosixPath('umockdev.spec')]. Taking: umockdev.spec

... this is wrong. It should be packaging/umockdev.spec, unless this is talking about the path in dist-git?

> DEBUG  No ref given or is not glob pattern

That might provide another clue. Otherwise the log does not really have an error message, it just said "failed".

@lachmanfrantisek said:

> I see `Specfile /tmp/sandcastle/umockdev.spec not found.` in the status.

So I'll try to put back `specfile_path:`, and try again. Will report here.


Changed files:
tests/unit/test_package_config.py, packit/config/package_config.py

diff --git a/packit/config/package_config.py b/packit/config/package_config.py
index 0057691e..79b992f4 100644
--- a/packit/config/package_config.py
+++ b/packit/config/package_config.py
@@ -60,25 +60,26 @@ class PackageConfig(MultiplePackages):
         # required to avoid cyclical imports
         from packit.schema import PackageConfigSchema
 
-        if config_file_path and not raw_dict.get("config_file_path", None):
-            raw_dict.update(config_file_path=config_file_path)
-
         # we need to process defaults first so they get propagated to JobConfigs
 
-        if not raw_dict.get("upstream_package_name", None) and repo_name:
-            raw_dict["upstream_package_name"] = repo_name
-
-        if not raw_dict.get("downstream_package_name", None) and repo_name:
-            raw_dict["downstream_package_name"] = repo_name
+        raw_dict.setdefault("config_file_path", config_file_path)
 
-        if not raw_dict.get("specfile_path", None):
+        if not raw_dict.get("specfile_path"):
+            default_specfile_path = None
             # we default to <downstream_package_name>.spec
             # https://packit.dev/docs/configuration/#specfile_path
-            downstream_package_name = raw_dict.get("downstream_package_name", None)
-            if downstream_package_name:
-                raw_dict["specfile_path"] = f"{downstream_package_name}.spec"
+            if downstream_package_name := raw_dict.get("downstream_package_name", None):
+                default_specfile_path = f"{downstream_package_name}.spec"
             elif search_specfile:
-                raw_dict["specfile_path"] = search_specfile(**specfile_search_args)
+                default_specfile_path = search_specfile(**specfile_search_args)
+            raw_dict["specfile_path"] = default_specfile_path
+
+        # Do this _after_ setting the default for 'specfile_path',
+        # so that 'downstream_package_name' is considered for setting
+        # 'specfile_path' _only_ when set in packit.yaml.
+        if repo_name:
+            raw_dict.setdefault("upstream_package_name", repo_name)
+            raw_dict.setdefault("downstream_package_name", repo_name)
 
         package_config = PackageConfigSchema().load(raw_dict)
 
diff --git a/tests/unit/test_package_config.py b/tests/unit/test_package_config.py
index 30845898..0feb24f3 100644
--- a/tests/unit/test_package_config.py
+++ b/tests/unit/test_package_config.py
@@ -1357,34 +1357,31 @@ def test_dist_git_package_url():
     "content,project,spec_path",
     [
         (
-            "synced_files:\\n"
+            "files_to_sync:\\n"
             "  - packit.spec\\n"
             "  - src: .packit.yaml\\n"
             "    dest: .packit2.yaml",
-            GitProject(repo="packit", service=GitService(), namespace=""),
+            flexmock(repo="packit"),
             "packit.spec",
         ),
         (
-            "synced_files:\\n"
+            "files_to_sync:\\n"
             "  - packit.spec\\n"
             "  - src: .packit.yaml\\n"
             "    dest: .packit2.yaml",
-            GitProject(repo="packit", service=GitService(), namespace=""),
+            flexmock(repo="packit"),
             "packit.spec",
         ),
     ],
 )
 def test_get_package_config_from_repo(
     content,
-    project: GitProject,
+    project,
     spec_path: Optional[str],
 ):
-    gp = flexmock(GitProject)
-    gp.should_receive("full_repo_name").and_return("a/b")
-    gp.should_receive("get_file_content").and_return(content)
-    # TODO(csomh): add this back, once we search for the specfile when
-    # downstream_package_name was not provided by the _user_.
-    # gp.should_receive("get_files").and_return(["packit.spec"]).once()
+    project.should_receive("get_file_content").and_return(content)
+    project.should_receive("full_repo_name").and_return("a/b")
+    project.should_receive("get_files").and_return([spec_path]).once()
     config = get_package_config_from_repo(project=project)
     assert isinstance(config, PackageConfig)
     assert config.specfile_path == spec_path
@@ -1563,21 +1560,16 @@ def test_get_local_package_config_path(
     )
 
 
-def test_get_local_package_config_no_spec():
-    """make sure specfile_path gets the proper default when not set"""
-    flexmock(PosixPath).should_receive("is_file").and_return(True)
-
+def test_specfile_path_from_downstream_package_name():
+    """
+    If 'downstream_package_name' is set in the package configuration,
+    'specfile_path' is set to '{downstream_package_name}.spec'
+    """
     (
         flexmock(packit.config.package_config)
         .should_receive("load_packit_yaml")
-        .and_return({})
-    )
-    (
-        flexmock(packit.config.package_config)
-        .should_receive("get_local_specfile_path")
-        .and_return(None)
+        .and_return({"downstream_package_name": "cockpit"})
     )
-
     assert (
         get_local_package_config(
             package_config_path=".packit.yaml", repo_name="cockpit"