Crash when extension changes @Published from separate file

it seems that if a function within an extension accesses an @Published property (possibly any Property Wrapper), then iff the extension is in a separate file to the main class, the compiler crashes

e.g.

//Test.swift
import Foundation
import SwiftUI

class Test:ObservableObject {
    @Published var count:Int = 0
}

and

//Test+Edit.swift
import Foundation
import SwiftUI

extension Test {
    func changeCount() {
        count = 2
    }
}

This causes a crash

whereas putting them both in one file like so:

//Test.swift
import Foundation
import SwiftUI

class Test:ObservableObject {
    @Published var count:Int = 0
}


extension Test {
    func changeCount() {
        count = 2
    }
}

this is fine

Full error message below

XCode Version 11.3.1 (11C504)
MacOS 10.15.2 (19C57)

Global is external, but doesn't have external or weak linkage!
i64* @"$s13Jump_Designer4TestC6_count33_476D991993A68C2B7C8A52F5F273EFE8LL7Combine9PublishedVySiGvpWvd"
<unknown>:0: error: fatal error encountered during compilation; please file a bug report with your project and the crash log
<unknown>:0: note: Broken module found, compilation aborted!
Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c /Users/rob/Documents/Development/Mac/CommonSwift/Categories/UIColor+Hex.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Controllers/DocStarterVC.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/String+HS.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/DispatchQueue+HS.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/JumpBottomControls.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/UIViewController+Alert.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/Published+Codable.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/JumpView.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/Comparable+clamp.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/FormationView.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Models/Season.swift /Users/rob/Documents/Development/Mac/CommonSwift/Helpers/OptionalType.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/CentreView.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/Array+HS.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Controllers/ColourPickerVC.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/UIViewController+HS.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/LoadView.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/TapDown.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/UIViewController+SwiftUI.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Models/Jumper.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/PreferencesView.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/SkydiverView.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/CGFunctions+HS.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Models/Point.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Models/JumpDoc.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/Combine+HS.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Models/Copyable.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Controllers/JumpVC.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/Optionals+HS.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/SlotView.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/UIAlertController+HS.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Controllers/AppDelegate.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/SaveView.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Models/Slot.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Controllers/ToolsController.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/ToolsView.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Models/JumpCreationOptions.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/DocStarter.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Models/Test.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/NumberConversions.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Controllers/SceneDelegate.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Models/AssignedSlot.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/CGSize+HS.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Controllers/DocBVC.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/CloseableView.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/View+HS.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Models/Jump.swift -primary-file /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Models/Test+Edit.swift /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps/MilkoJumps/Views/Tappable.swift /Users/rob/Documents/Development/Mac/CommonSwift/Categories/UIView+HS.swift -emit-module-path /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/Objects-normal/x86_64/Test+Edit~partial.swiftmodule -emit-module-doc-path /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/Objects-normal/x86_64/Test+Edit~partial.swiftdoc -serialize-diagnostics-path /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/Objects-normal/x86_64/Test+Edit.dia -emit-dependencies-path /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/Objects-normal/x86_64/Test+Edit.d -emit-reference-dependencies-path /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/Objects-normal/x86_64/Test+Edit.swiftdeps -target x86_64-apple-ios13.2-simulator -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.2.sdk -I /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Products/Debug-iphonesimulator -F /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Products/Debug-iphonesimulator -F /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Products/Debug-iphonesimulator/ALActionBlocks -F /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Products/Debug-iphonesimulator/ColourWheel -F /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Products/Debug-iphonesimulator/HSObserver -F /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Products/Debug-iphonesimulator/PureLayout -enable-testing -g -module-cache-path /Users/rob/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D DEBUG -D COCOAPODS -serialize-debugging-options -Xcc -working-directory -Xcc /Users/rob/Documents/Development/Mac/iPhoneSwift/MilkoJumps -enable-anonymous-context-mangled-names -Xcc -I/Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/Jump Designer-generated-files.hmap -Xcc -I/Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/Jump Designer-own-target-headers.hmap -Xcc -I/Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/Jump Designer-all-non-framework-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/Jump Designer-project-headers.hmap -Xcc -I/Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Products/Debug-iphonesimulator/include -Xcc -I/Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Products/Debug-iphonesimulator/ALActionBlocks/ALActionBlocks.framework/Headers -Xcc -I/Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Products/Debug-iphonesimulator/ColourWheel/ColourWheel.framework/Headers -Xcc -I/Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Products/Debug-iphonesimulator/HSObserver/HSObserver.framework/Headers -Xcc -I/Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Products/Debug-iphonesimulator/PureLayout/PureLayout.framework/Headers -Xcc -I/Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/DerivedSources-normal/x86_64 -Xcc -I/Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/DerivedSources/x86_64 -Xcc -I/Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DCOCOAPODS=1 -module-name Jump_Designer -o /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Build/Intermediates.noindex/MilkoJumps.build/Debug-iphonesimulator/MilkoJumps.build/Objects-normal/x86_64/Test+Edit.o -index-store-path /Users/rob/Library/Developer/Xcode/DerivedData/MilkoJumps-hgeofpyicwisbrgazmrmzrvopelf/Index/DataStore -index-system-modules 
0  swift                    0x0000000108f51a63 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x0000000108f51236 SignalHandler(int) + 358
2  libsystem_platform.dylib 0x00007fff63ecf42d _sigtramp + 29
3  libsystem_platform.dylib 0x00007ffeeb101d30 _sigtramp + 2267228448
4  libsystem_c.dylib        0x00007fff63da4a1c abort + 120
5  swift                    0x0000000104b92530 std::__1::unique_ptr<swift::DiagnosticConsumer, std::__1::default_delete<swift::DiagnosticConsumer> > llvm::function_ref<std::__1::unique_ptr<swift::DiagnosticConsumer, std::__1::default_delete<swift::DiagnosticConsumer> > (swift::InputFile const&)>::callback_fn<createSerializedDiagnosticConsumerIfNeeded(swift::FrontendInputsAndOutputs const&)::$_15>(long, swift::InputFile const&) + 0
6  swift                    0x0000000108eca886 llvm::report_fatal_error(llvm::Twine const&, bool) + 278
7  swift                    0x0000000108eca76b llvm::report_fatal_error(char const*, bool) + 43
8  swift                    0x0000000108e8aa4c (anonymous namespace)::VerifierLegacyPass::doFinalization(llvm::Module&) + 204
9  swift                    0x0000000108e2bfd3 llvm::FPPassManager::doFinalization(llvm::Module&) + 51
10 swift                    0x0000000108e34622 llvm::legacy::FunctionPassManagerImpl::doFinalization(llvm::Module&) + 82
11 swift                    0x0000000104dab8d5 swift::performLLVM(swift::IRGenOptions&, swift::DiagnosticEngine*, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, swift::version::Version const&, llvm::StringRef, swift::UnifiedStatsReporter*) + 4677
12 swift                    0x0000000104b9bfc2 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 38274
13 swift                    0x0000000104b8f234 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6820
14 swift                    0x0000000104b1c733 main + 1219
15 libdyld.dylib            0x00007fff63cd67fd start + 1
error: Abort trap: 6 (in target 'MilkoJumps' from project 'MilkoJumps')
2 Likes

I have the same issue, but I also use @Published in a parent class:

class A  {
    @Published var item: ItemHandler.Item
    .
    .
}

class B: A {
    // call to self.item here and build fails with the same error
}

Is this a bug or are we doing something wrong when we think we can modify the var from eg. an extension?

I did this as a workaround:

@Published var isRequestActive = false
func set(isRequestActive value: Bool) { isRequestActive = value } // Workaround

Have someone filed a swift bug regarding this?

I haven't filed a bug

Have you tried using Xcode 11.4.1 or a trunk snapshot from swift.org? The bug might have already been fixed.

Good point! I'm using 11.3.1 due to external dependencies in my project, but I have latest XCode installed but can not run it due to old macOS. I'll upgrade and report back.

Have you tried using Xcode 11.4.1 or a trunk snapshot from swift.org? The bug might have already been fixed.

nope, I don't have either of these handy. It's a trivial two-file test though if you do.

This works in Xcode Version 11.4 (11E146).

1 Like

I just found a workaround in Xcode 11.3.1.

If you wrap your code in a block and execute it the compiler does not fail.

extension Test {
    func changeCount() {
        { self.count = 2 }()
    }
}