Skip to content

Commit 1525f6b

Browse files
committed
[SourceKit] Pass the main swift executable path when constructing a compiler invocation
#58786 (rdar://93030932) was failing because the `swift-frontend` invocations passed a `swiftExecutablePath` to `Invocation.parseArgs`. This caused the `ClangImporter` instance to point to a `clang` binary next to the `swift-frontend` executable while SourceKit used PATH to find `clang`. The clang executable next to `swift-frontend` doesn’t actually exist because `clang` lives in `llvm-linux-aarch64/bin` and `swift-frontend` lives in `swift-linux-aarch64/bin`. So some checks for a minimum clang verison failed for the normal build (because the executable doesn’t actually exists) while they pass during the SourceKit build (which used `clang` from `PATH`). This in turn caused the `outline-atomics` to be enabled to the SourceKit clang compiler arguments but not the clang compiler arguments for a normal build and thus resulted in two separate module cache directories (which includes the enabled features in the module directory hash). To fix this issue, also set the swift executable path for compiler invocations created from SourceKit. Fixes #58786 (rdar://93030932)
1 parent 438e947 commit 1525f6b

File tree

16 files changed

+77
-27
lines changed

16 files changed

+77
-27
lines changed

include/swift/IDE/Utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ bool initCompilerInvocation(
8989
FrontendOptions::ActionType Action, DiagnosticEngine &Diags,
9090
StringRef UnresolvedPrimaryFile,
9191
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
92+
const std::string &swiftExecutablePath,
9293
const std::string &runtimeResourcePath,
9394
const std::string &diagnosticDocumentationPath, time_t sessionTimestamp,
9495
std::string &Error);

lib/IDE/Utils.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ bool ide::initCompilerInvocation(
276276
FrontendOptions::ActionType Action, DiagnosticEngine &Diags,
277277
StringRef UnresolvedPrimaryFile,
278278
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
279+
const std::string &swiftExecutablePath,
279280
const std::string &runtimeResourcePath,
280281
const std::string &diagnosticDocumentationPath, time_t sessionTimestamp,
281282
std::string &Error) {
@@ -299,7 +300,9 @@ bool ide::initCompilerInvocation(
299300
driver::getSingleFrontendInvocationFromDriverArguments(
300301
Args, Diags,
301302
[&](ArrayRef<const char *> FrontendArgs) {
302-
return Invocation.parseArgs(FrontendArgs, Diags);
303+
return Invocation.parseArgs(
304+
FrontendArgs, Diags, /*ConfigurationFileBuffers=*/nullptr,
305+
/*workingDirectory=*/"", swiftExecutablePath);
303306
},
304307
/*ForceNoOutputs=*/true);
305308

test/SourceKit/Misc/match-module-cache-with-compiler.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33

44
// REQUIRES: shell
55

6-
// https://github.com/apple/swift/issues/58786
7-
// UNSUPPORTED: OS=linux-gnu
8-
96
// RUN: %empty-directory(%t)
107

118
// RUN: COMPILER_ARGS=( \

tools/SourceKit/include/SourceKit/Core/Context.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ class SlowRequestSimulator {
169169
};
170170

171171
class Context {
172+
/// The path of the swift-frontend executable.
173+
/// Used to find clang relative to it.
174+
std::string SwiftExecutablePath;
172175
std::string RuntimeLibPath;
173176
std::string DiagnosticDocumentationPath;
174177
std::unique_ptr<LangSupport> SwiftLang;
@@ -178,12 +181,15 @@ class Context {
178181
std::shared_ptr<SlowRequestSimulator> SlowRequestSim;
179182

180183
public:
181-
Context(StringRef RuntimeLibPath, StringRef DiagnosticDocumentationPath,
184+
Context(StringRef SwiftExecutablePath, StringRef RuntimeLibPath,
185+
StringRef DiagnosticDocumentationPath,
182186
llvm::function_ref<std::unique_ptr<LangSupport>(Context &)>
183187
LangSupportFactoryFn,
184188
bool shouldDispatchNotificationsOnMain = true);
185189
~Context();
186190

191+
StringRef getSwiftExecutablePath() const { return SwiftExecutablePath; }
192+
187193
StringRef getRuntimeLibPath() const { return RuntimeLibPath; }
188194
StringRef getDiagnosticDocumentationPath() const {
189195
return DiagnosticDocumentationPath;

tools/SourceKit/lib/Core/Context.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ GlobalConfig::getCompletionOpts() const {
3636
}
3737

3838
SourceKit::Context::Context(
39-
StringRef RuntimeLibPath, StringRef DiagnosticDocumentationPath,
39+
StringRef SwiftExecutablePath, StringRef RuntimeLibPath,
40+
StringRef DiagnosticDocumentationPath,
4041
llvm::function_ref<std::unique_ptr<LangSupport>(Context &)>
4142
LangSupportFactoryFn,
4243
bool shouldDispatchNotificationsOnMain)
43-
: RuntimeLibPath(RuntimeLibPath),
44+
: SwiftExecutablePath(SwiftExecutablePath), RuntimeLibPath(RuntimeLibPath),
4445
DiagnosticDocumentationPath(DiagnosticDocumentationPath),
4546
NotificationCtr(
4647
new NotificationCenter(shouldDispatchNotificationsOnMain)),

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,11 @@ struct SwiftASTManager::Implementation {
544544
std::shared_ptr<SwiftEditorDocumentFileMap> EditorDocs,
545545
std::shared_ptr<GlobalConfig> Config,
546546
std::shared_ptr<SwiftStatistics> Stats,
547-
std::shared_ptr<RequestTracker> ReqTracker, StringRef RuntimeResourcePath,
548-
StringRef DiagnosticDocumentationPath)
547+
std::shared_ptr<RequestTracker> ReqTracker, StringRef SwiftExecutablePath,
548+
StringRef RuntimeResourcePath, StringRef DiagnosticDocumentationPath)
549549
: EditorDocs(EditorDocs), Config(Config), Stats(Stats),
550-
ReqTracker(ReqTracker), RuntimeResourcePath(RuntimeResourcePath),
550+
ReqTracker(ReqTracker), SwiftExecutablePath(SwiftExecutablePath),
551+
RuntimeResourcePath(RuntimeResourcePath),
551552
DiagnosticDocumentationPath(DiagnosticDocumentationPath),
552553
SessionTimestamp(llvm::sys::toTimeT(std::chrono::system_clock::now())) {
553554
}
@@ -556,6 +557,9 @@ struct SwiftASTManager::Implementation {
556557
std::shared_ptr<GlobalConfig> Config;
557558
std::shared_ptr<SwiftStatistics> Stats;
558559
std::shared_ptr<RequestTracker> ReqTracker;
560+
/// The path of the swift-frontend executable.
561+
/// Used to find clang relative to it.
562+
std::string SwiftExecutablePath;
559563
std::string RuntimeResourcePath;
560564
std::string DiagnosticDocumentationPath;
561565
SourceManager SourceMgr;
@@ -625,10 +629,10 @@ SwiftASTManager::SwiftASTManager(
625629
std::shared_ptr<SwiftEditorDocumentFileMap> EditorDocs,
626630
std::shared_ptr<GlobalConfig> Config,
627631
std::shared_ptr<SwiftStatistics> Stats,
628-
std::shared_ptr<RequestTracker> ReqTracker, StringRef RuntimeResourcePath,
629-
StringRef DiagnosticDocumentationPath)
632+
std::shared_ptr<RequestTracker> ReqTracker, StringRef SwiftExecutablePath,
633+
StringRef RuntimeResourcePath, StringRef DiagnosticDocumentationPath)
630634
: Impl(*new Implementation(EditorDocs, Config, Stats, ReqTracker,
631-
RuntimeResourcePath,
635+
SwiftExecutablePath, RuntimeResourcePath,
632636
DiagnosticDocumentationPath)) {}
633637

634638
SwiftASTManager::~SwiftASTManager() {
@@ -665,8 +669,8 @@ bool SwiftASTManager::initCompilerInvocation(
665669
std::string &Error) {
666670
return ide::initCompilerInvocation(
667671
Invocation, OrigArgs, Action, Diags, UnresolvedPrimaryFile, FileSystem,
668-
Impl.RuntimeResourcePath, Impl.DiagnosticDocumentationPath,
669-
Impl.SessionTimestamp, Error);
672+
Impl.SwiftExecutablePath, Impl.RuntimeResourcePath,
673+
Impl.DiagnosticDocumentationPath, Impl.SessionTimestamp, Error);
670674
}
671675

672676
bool SwiftASTManager::initCompilerInvocation(

tools/SourceKit/lib/SwiftLang/SwiftASTManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ class SwiftASTManager : public std::enable_shared_from_this<SwiftASTManager> {
235235
std::shared_ptr<GlobalConfig> Config,
236236
std::shared_ptr<SwiftStatistics> Stats,
237237
std::shared_ptr<RequestTracker> ReqTracker,
238+
StringRef SwiftExecutablePath,
238239
StringRef RuntimeResourcePath,
239240
StringRef DiagnosticDocumentationPath);
240241
~SwiftASTManager();

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ configureCompletionInstance(std::shared_ptr<CompletionInstance> CompletionInst,
272272

273273
SwiftLangSupport::SwiftLangSupport(SourceKit::Context &SKCtx)
274274
: NotificationCtr(SKCtx.getNotificationCenter()),
275+
SwiftExecutablePath(SKCtx.getSwiftExecutablePath()),
275276
ReqTracker(SKCtx.getRequestTracker()), CCCache(new SwiftCompletionCache),
276277
CompileManager(RuntimeResourcePath, DiagnosticDocumentationPath) {
277278
llvm::SmallString<128> LibPath(SKCtx.getRuntimeLibPath());
@@ -283,7 +284,7 @@ SwiftLangSupport::SwiftLangSupport(SourceKit::Context &SKCtx)
283284
EditorDocuments = std::make_shared<SwiftEditorDocumentFileMap>();
284285
ASTMgr = std::make_shared<SwiftASTManager>(
285286
EditorDocuments, SKCtx.getGlobalConfiguration(), Stats, ReqTracker,
286-
RuntimeResourcePath, DiagnosticDocumentationPath);
287+
SwiftExecutablePath, RuntimeResourcePath, DiagnosticDocumentationPath);
287288

288289
CompletionInst = std::make_shared<CompletionInstance>();
289290

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ struct SwiftStatistics {
350350

351351
class SwiftLangSupport : public LangSupport {
352352
std::shared_ptr<NotificationCenter> NotificationCtr;
353+
/// The path of the swift-frontend executable.
354+
/// Used to find clang relative to it.
355+
std::string SwiftExecutablePath;
353356
std::string RuntimeResourcePath;
354357
std::string DiagnosticDocumentationPath;
355358
std::shared_ptr<SwiftASTManager> ASTMgr;

tools/SourceKit/tools/sourcekitd/bin/InProc/sourcekitdInProc-darwin.exports

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ sourcekitd_variant_uid_get_value
7070
_ZN10sourcekitd13cancelRequestEPKv
7171
_ZN10sourcekitd13enableLoggingEN4llvm9StringRefE
7272
_ZN10sourcekitd13handleRequestEPvPKvNSt3__18functionIFvS0_EEE
73-
_ZN10sourcekitd17initializeServiceEN4llvm9StringRefES1_NSt3__18functionIFvPvEEE
73+
_ZN10sourcekitd17initializeServiceEN4llvm9StringRefES1_S1_NSt3__18functionIFvPvEEE
7474
_ZN10sourcekitd24createErrorRequestFailedEN4llvm9StringRefE
7575
_ZN10sourcekitd24disposeCancellationTokenEPKv
7676
_ZN9SourceKit6Logger12LoggingLevelE

0 commit comments

Comments
 (0)