From: Daan De Meyer Date: Fri, 28 Mar 2025 09:19:29 +0000 (+0100) Subject: Allow combining --force and --rerun-build-scripts X-Git-Tag: v26~287 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b29ff7709114df87da162577e87e062b08ea45db;p=thirdparty%2Fmkosi.git 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. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 49e208ac6..dc01dbd06 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -3943,7 +3943,10 @@ def build_image(context: Context) -> None: check_root_populated(context) run_build_scripts(context) - if context.config.output_format == OutputFormat.none or context.args.rerun_build_scripts: + if context.config.output_format == OutputFormat.none or ( + context.args.rerun_build_scripts + and (context.config.output_dir_or_cwd() / context.config.output).exists() + ): return if wantrepo: @@ -4668,7 +4671,7 @@ def validate_certificates_and_keys(config: Config) -> None: def needs_build(args: Args, config: Config, force: int = 1) -> bool: return ( - args.force >= force + (args.force >= force and not args.rerun_build_scripts) or not (config.output_dir_or_cwd() / config.output_with_compression).exists() # When the output is a directory, its name is the same as the symlink we create that points to the # 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: logging.info(f"Output path {output} exists already. (Use --force to rebuild.)") return - if args.rerun_build_scripts and not output.exists(): + if args.rerun_build_scripts and not args.force and not output.exists(): die( f"Image '{last.image}' must be built once before --rerun-build-scripts can be used", hint="Build the image once with 'mkosi build'", @@ -5149,7 +5152,9 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None: hint="Add the &I specifier to the cache key to avoid this issue", ) - if last.is_incremental() and (last.incremental == Incremental.strict or args.rerun_build_scripts): + if last.is_incremental() and ( + last.incremental == Incremental.strict or (args.rerun_build_scripts and not args.force) + ): if args.force > 1: die( "Cannot remove incremental caches when building with Incremental=strict", @@ -5197,6 +5202,10 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None: or last.output_format == OutputFormat.none or not (last.output_dir_or_cwd() / last.output).exists() ): + history = ( + last.output_format == OutputFormat.none or not (last.output_dir_or_cwd() / last.output).exists() + ) + for config in images: if any( source.type != KeySourceType.file @@ -5279,7 +5288,7 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None: if args.auto_bump: bump_image_version() - if last.history and not args.rerun_build_scripts: + if last.history and history: Path(".mkosi-private/history").mkdir(parents=True, exist_ok=True) Path(".mkosi-private/history/latest.json").write_text( json.dumps( diff --git a/mkosi/config.py b/mkosi/config.py index 7fce3ce5d..660ecf74b 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -4824,8 +4824,7 @@ def have_history(args: Args) -> bool: return ( args.directory is not None and args.verb.needs_build() - and (args.verb != Verb.build or args.rerun_build_scripts) - and not args.force + and ((args.verb != Verb.build and not args.force) or args.rerun_build_scripts) and Path(".mkosi-private/history/latest.json").exists() ) @@ -4860,9 +4859,6 @@ def parse_config( if args.cmdline and not args.verb.supports_cmdline(): die(f"Arguments after verb are not supported for {args.verb}.") - if args.rerun_build_scripts and args.force: - die("--force cannot be used together with --rerun-build-scripts") - # If --debug was passed, apply it as soon as possible. if ARG_DEBUG.get(): logging.getLogger().setLevel(logging.DEBUG)