From 492f5ab611295de052a5c7d302dde6c84df44c18 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Fri, 22 Aug 2025 10:20:32 -0700 Subject: [PATCH] [Caching] Do not infer default on disk cas path when parsing options Currently, swift-frontend will always try to infer a default CASPath if no `-cas-path` is passed. The function `getDefaultOnDiskCASPath` called to infer the default path can fatal error in some environment where no caching directory and home directory, no matter if caching is used or not. Remove the inferred path during parsing since that is not strictly necessary. If caching is enabled and no CASPath is passed, error can be issued during CAS construction time. rdar://158899187 --- lib/Frontend/CompilerInvocation.cpp | 2 -- lib/Frontend/Frontend.cpp | 5 +++++ test/CAS/cas_output_backend.swift | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index e4d91285b4458..2308a30cf754c 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -778,8 +778,6 @@ static bool ParseCASArgs(CASOptions &Opts, ArgList &Args, Opts.CacheSkipReplay |= Args.hasArg(OPT_cache_disable_replay); if (const Arg *A = Args.getLastArg(OPT_cas_path)) Opts.CASOpts.CASPath = A->getValue(); - else if (Opts.CASOpts.CASPath.empty()) - Opts.CASOpts.CASPath = llvm::cas::getDefaultOnDiskCASPath(); if (const Arg *A = Args.getLastArg(OPT_cas_plugin_path)) Opts.CASOpts.PluginPath = A->getValue(); diff --git a/lib/Frontend/Frontend.cpp b/lib/Frontend/Frontend.cpp index a21defd4be986..b5280fb575706 100644 --- a/lib/Frontend/Frontend.cpp +++ b/lib/Frontend/Frontend.cpp @@ -466,6 +466,11 @@ bool CompilerInstance::setupCASIfNeeded(ArrayRef Args) { return false; const auto &Opts = getInvocation().getCASOptions(); + if (Opts.CASOpts.CASPath.empty() && Opts.CASOpts.PluginPath.empty()) { + Diagnostics.diagnose(SourceLoc(), diag::error_cas_initialization, + "no CAS options provided"); + return true; + } auto MaybeDB = Opts.CASOpts.getOrCreateDatabases(); if (!MaybeDB) { Diagnostics.diagnose(SourceLoc(), diag::error_cas_initialization, diff --git a/test/CAS/cas_output_backend.swift b/test/CAS/cas_output_backend.swift index 992c4b6425f7c..0120ea00174be 100644 --- a/test/CAS/cas_output_backend.swift +++ b/test/CAS/cas_output_backend.swift @@ -13,6 +13,9 @@ // RUN: echo "\"-disable-implicit-concurrency-module-import\"" >> %t/MyApp.cmd // RUN: echo "\"-parse-stdlib\"" >> %t/MyApp.cmd +// RUN: not %target-swift-frontend -c -cache-compile-job %s -o %t/test.o @%t/MyApp.cmd 2>&1 | %FileCheck %s --check-prefix=NOT-CONFIG +// NOT-CONFIG: error: CAS cannot be initialized from the specified '-cas-*' options: no CAS options provided + // RUN: %target-swift-frontend -c -cache-compile-job -cas-path %t/cas %s -o %t/test.o @%t/MyApp.cmd // RUN: %cache-tool -cas-path %t/cas -cache-tool-action print-output-keys -- \ // RUN: %target-swift-frontend -c -cache-compile-job -cas-path %t/cas %s -o %t/test.o @%t/MyApp.cmd > %t/cache_key.json