Skip to content

Commit 8d9c5e1

Browse files
authored
Merge pull request #75406 from z2oh/fix-sourcekitd-file-locks-windows
Fix sourcekitd persistent file-locks on Windows
2 parents 2a2dd73 + 221c703 commit 8d9c5e1

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,12 @@ namespace swift {
496496
/// Enable verification when every SubstitutionMap is constructed.
497497
bool VerifyAllSubstitutionMaps = false;
498498

499+
/// If set to true, the source manager will avoid memory mapping source files
500+
/// with the expectation they may change on disk. This is most useful when
501+
/// opening files under sourcekitd on Windows, as memory mapping on Windows
502+
/// prevents files from being written.
503+
bool OpenSourcesAsVolatile = false;
504+
499505
/// Load swiftmodule files in memory as volatile and avoid mmap.
500506
bool EnableVolatileModules = false;
501507

include/swift/Basic/SourceManager.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class SourceManager {
9898
private:
9999
llvm::SourceMgr LLVMSourceMgr;
100100
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem;
101+
bool OpenSourcesAsVolatile = false;
101102
unsigned IDEInspectionTargetBufferID = 0U;
102103
unsigned IDEInspectionTargetOffset;
103104

@@ -170,6 +171,12 @@ class SourceManager {
170171
return FileSystem;
171172
}
172173

174+
// Setting this prevents SourceManager from memory mapping sources (via opening files
175+
// with isVolatile=true);
176+
void setOpenSourcesAsVolatile() {
177+
OpenSourcesAsVolatile = true;
178+
}
179+
173180
void setIDEInspectionTarget(unsigned BufferID, unsigned Offset) {
174181
assert(BufferID != 0U && "Buffer should be valid");
175182

lib/Basic/SourceLoc.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,11 @@ unsigned SourceManager::getExternalSourceBufferID(StringRef Path) {
696696
return It->getSecond();
697697
}
698698
unsigned Id = 0u;
699-
auto InputFileOrErr = swift::vfs::getFileOrSTDIN(*getFileSystem(), Path);
699+
auto InputFileOrErr =
700+
swift::vfs::getFileOrSTDIN(*getFileSystem(), Path,
701+
/* FileSize */ -1,
702+
/* RequiresNullTerminator */ true,
703+
/* isVolatile */ this->OpenSourcesAsVolatile);
700704
if (InputFileOrErr) {
701705
// This assertion ensures we can look up from the map in the future when
702706
// using the same Path.

lib/Frontend/Frontend.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,10 @@ bool CompilerInstance::setup(const CompilerInvocation &Invoke,
574574
Invocation.getSearchPathOptions().dump(LangOpts.Target.isOSDarwin());
575575
}
576576

577+
if (LangOpts.OpenSourcesAsVolatile) {
578+
this->getSourceMgr().setOpenSourcesAsVolatile();
579+
}
580+
577581
// If we expect an implicit stdlib import, load in the standard library. If we
578582
// either fail to find it or encounter an error while loading it, bail early. Continuing will at best
579583
// trigger a bunch of other errors due to the stdlib being missing, or at

lib/IDETool/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ bool ide::initCompilerInvocation(
225225
LangOpts.AttachCommentsToDecls = true;
226226
LangOpts.DiagnosticsEditorMode = true;
227227
LangOpts.CollectParsedToken = true;
228+
#if defined(_WIN32)
229+
// Source files that might be open in an editor should not be memory mapped on Windows,
230+
// as they will become read-only.
231+
LangOpts.OpenSourcesAsVolatile = true;
232+
#endif
228233
if (LangOpts.PlaygroundTransform) {
229234
// The playground instrumenter changes the AST in ways that disrupt the
230235
// SourceKit functionality. Since we don't need the instrumenter, and all we

0 commit comments

Comments
 (0)