Skip to content

Commit b29ff77

Browse files
committed
Allow combining --force and --rerun-build-scripts
Until now we didn't allow this, but it turns out there's actually a use case for this, build the image if it doesn't exist yet, reuse the existing image otherwise.
1 parent 708c2ca commit b29ff77

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

mkosi/__init__.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -3943,7 +3943,10 @@ def build_image(context: Context) -> None:
39433943
check_root_populated(context)
39443944
run_build_scripts(context)
39453945

3946-
if context.config.output_format == OutputFormat.none or context.args.rerun_build_scripts:
3946+
if context.config.output_format == OutputFormat.none or (
3947+
context.args.rerun_build_scripts
3948+
and (context.config.output_dir_or_cwd() / context.config.output).exists()
3949+
):
39473950
return
39483951

39493952
if wantrepo:
@@ -4668,7 +4671,7 @@ def validate_certificates_and_keys(config: Config) -> None:
46684671

46694672
def needs_build(args: Args, config: Config, force: int = 1) -> bool:
46704673
return (
4671-
args.force >= force
4674+
(args.force >= force and not args.rerun_build_scripts)
46724675
or not (config.output_dir_or_cwd() / config.output_with_compression).exists()
46734676
# When the output is a directory, its name is the same as the symlink we create that points to the
46744677
# actual output when not building a directory. So if the full output path exists, we have to check
@@ -5121,7 +5124,7 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None:
51215124
logging.info(f"Output path {output} exists already. (Use --force to rebuild.)")
51225125
return
51235126

5124-
if args.rerun_build_scripts and not output.exists():
5127+
if args.rerun_build_scripts and not args.force and not output.exists():
51255128
die(
51265129
f"Image '{last.image}' must be built once before --rerun-build-scripts can be used",
51275130
hint="Build the image once with 'mkosi build'",
@@ -5149,7 +5152,9 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None:
51495152
hint="Add the &I specifier to the cache key to avoid this issue",
51505153
)
51515154

5152-
if last.is_incremental() and (last.incremental == Incremental.strict or args.rerun_build_scripts):
5155+
if last.is_incremental() and (
5156+
last.incremental == Incremental.strict or (args.rerun_build_scripts and not args.force)
5157+
):
51535158
if args.force > 1:
51545159
die(
51555160
"Cannot remove incremental caches when building with Incremental=strict",
@@ -5197,6 +5202,10 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None:
51975202
or last.output_format == OutputFormat.none
51985203
or not (last.output_dir_or_cwd() / last.output).exists()
51995204
):
5205+
history = (
5206+
last.output_format == OutputFormat.none or not (last.output_dir_or_cwd() / last.output).exists()
5207+
)
5208+
52005209
for config in images:
52015210
if any(
52025211
source.type != KeySourceType.file
@@ -5279,7 +5288,7 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None:
52795288
if args.auto_bump:
52805289
bump_image_version()
52815290

5282-
if last.history and not args.rerun_build_scripts:
5291+
if last.history and history:
52835292
Path(".mkosi-private/history").mkdir(parents=True, exist_ok=True)
52845293
Path(".mkosi-private/history/latest.json").write_text(
52855294
json.dumps(

mkosi/config.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -4824,8 +4824,7 @@ def have_history(args: Args) -> bool:
48244824
return (
48254825
args.directory is not None
48264826
and args.verb.needs_build()
4827-
and (args.verb != Verb.build or args.rerun_build_scripts)
4828-
and not args.force
4827+
and ((args.verb != Verb.build and not args.force) or args.rerun_build_scripts)
48294828
and Path(".mkosi-private/history/latest.json").exists()
48304829
)
48314830

@@ -4860,9 +4859,6 @@ def parse_config(
48604859
if args.cmdline and not args.verb.supports_cmdline():
48614860
die(f"Arguments after verb are not supported for {args.verb}.")
48624861

4863-
if args.rerun_build_scripts and args.force:
4864-
die("--force cannot be used together with --rerun-build-scripts")
4865-
48664862
# If --debug was passed, apply it as soon as possible.
48674863
if ARG_DEBUG.get():
48684864
logging.getLogger().setLevel(logging.DEBUG)

0 commit comments

Comments
 (0)