krzysztofzablocki / Inject

Hot Reloading for Swift applications!
MIT License
2.1k stars 114 forks source link

SwiftUI custom modifier #81

Closed Jonathanm10 closed 7 months ago

Jonathanm10 commented 7 months ago

Hi,

Is there something special to do when working with custom modifiers? I've made one, but when using it, the hot reloading is no longer working

modifier:

extension View {
  func navigationBarColor(backgroundColor: UIColor = .clear, foregroundColor: UIColor = .white) -> some View {
    return self.modifier(NavigationBarModifier(backgroundColor: backgroundColor, foregroundColor: foregroundColor))
  }
}

usage:

  var body: some View {
    ZStack {
      // ..
    }
    .navigationBarColor(...)
    .enableInjection()
  }

error: "error: value of type 'some View' has no member 'navigationBarColor'"

Thanks for your help

johnno1962 commented 7 months ago

Is this a permission issue? Can you make your modifier public

Jonathanm10 commented 7 months ago

Unfortunately, it doesn't work.

I've managed to make it work by putting the extension + modifier class together in the view where I'm using the modifier. But that's obviously not a solution.

Jonathanm10 commented 7 months ago

So I've tested on the DemoApp and custom modifiers do work. So I came back to my project and deleted my view extension file and recreated it. It now works. I don't know why but anyway, please excuse the noise and thanks for the fantastic tool!

Jonathanm10 commented 7 months ago

I've actually been able to reproduce on the DemoApp. While my first extension was working, I've created a second one and used it. Now I have the same error.

My second extension: DemoApp/Extension/UserDefaultsExtension.swift

extension UserDefaults {
  @objc var myValue: Bool {
    get {
      bool(forKey: "myValue")
    }

    set {
      set(newValue, forKey: "myValue")
    }
  }
}

Using it in my view: DemoApp/ContentView.swift

Button("abc") {
   UserDefaults.standard.myValue = true
}

The error:

/Users/jonathan/Library/Developer/CoreSimulator/Devices/7E00BCE7-FDAB-40B9-A63B-AA32CF1E80A4/data/Containers/Data/Application/3881D0D0-2C53-44F9-89B1-7AFE8DAD0EE6/tmp/command.sh)
/Users/jonathan/Liip/InjectSwiftUIExample/DemoApp/DemoApp/ContentView.swift:11:41: error: value of type 'UserDefaults' has no member 'myValue'
                  UserDefaults.standard.myValue = true
                  ~~~~~~~~~~~~~~~~~~~~~ ^~~~~~~

Content of command.sh:

(cd "/Users/jonathan/Liip/InjectSwiftUIExample/DemoApp" && cd "/Users/jonathan/Liip/InjectSwiftUIExample/DemoApp";      /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/jonathan/Liip/InjectSwiftUIExample/DemoApp/DemoApp/ContentView.swift /Users/jonathan/Liip/InjectSwiftUIExample/DemoApp/DemoApp/ViewModel.swift /Users/jonathan/Liip/InjectSwiftUIExample/DemoApp/DemoApp/Home\ View.swift /Users/jonathan/Liip/InjectSwiftUIExample/DemoApp/DemoApp/DemoAppApp.swift /Users/jonathan/Liip/InjectSwiftUIExample/DemoApp/DemoApp/Extension/ViewExtension.swift /Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/DerivedSources/GeneratedAssetSymbols.swift -emit-localized-strings -emit-localized-strings-path /Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/Objects-normal/arm64 -target arm64-apple-ios15.4-simulator -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.2.sdk -I /Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Products/Debug-iphonesimulator -F /Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Products/Debug-iphonesimulator/PackageFrameworks -F /Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Products/Debug-iphonesimulator/PackageFrameworks -F /Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Products/Debug-iphonesimulator -no-color-diagnostics -enable-testing -g -module-cache-path /Users/jonathan/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity\=checked -Onone -D DEBUG -serialize-debugging-options -const-gather-protocols-file /Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/Objects-normal/arm64/DemoApp_const_extract_protocols.json -enable-bare-slash-regex -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file /Users/jonathan/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/jonathan/Liip/InjectSwiftUIExample/DemoApp -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -ivfsstatcache -Xcc /Users/jonathan/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/iphonesimulator17.2-21C52-4f2951bfe8f3cd53c99228b0131e163e.sdkstatcache -Xcc -I/Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/DemoApp-generated-files.hmap -Xcc -I/Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/DemoApp-own-target-headers.hmap -Xcc -I/Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/DemoApp-all-non-framework-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/DemoApp-project-headers.hmap -Xcc -I/Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Products/Debug-iphonesimulator/include -Xcc -I/Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/DerivedSources-normal/arm64 -Xcc -I/Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/DerivedSources/arm64 -Xcc -I/Users/jonathan/Library/Developer/Xcode/DerivedData/DemoApp-gkazslqtxeedylgchjqdchyhzzen/Build/Intermediates.noindex/DemoApp.build/Debug-iphonesimulator/DemoApp.build/DerivedSources -Xcc -DDEBUG\=1 -module-name DemoApp  -disable-clang-spi -target-sdk-version 17.2 -target-sdk-name iphonesimulator17.2 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.2.sdk/usr/lib/swift/host/plugins\#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.2.sdk/usr/local/lib/swift/host/plugins\#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/lib/swift/host/plugins\#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/local/lib/swift/host/plugins\#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /tmp/injection.o >"/Users/jonathan/Library/Developer/CoreSimulator/Devices/7E00BCE7-FDAB-40B9-A63B-AA32CF1E80A4/data/Containers/Data/Application/3881D0D0-2C53-44F9-89B1-7AFE8DAD0EE6/tmp/eval107.log" 2>&1)%

It seems that it's somehow not being tracked but I definitively don't understand why.

Thanks again for your help

johnno1962 commented 7 months ago

I think I can see what's happening here. You're adding and removing files and as InjectionIII has a compilation command cache it sometimes doesn't pick up your new file. The way to flush the cache is either to reboot or better rm /tmp/*_builds.plist or temporarily add a syntax error to the file you're trying to inject which will retry without using the cache and re-parse the logs. Then remove the syntax error and try again. This isn't the first issue related to adding and removing files and caching but for large projects the cache has value so I'm thinking about only enabling it if the log scan takes a certain amount of time. If rescanning the logs still doesn't fix the problem you need to make sure the old logs without the new file are cleared out, either with a "Clean Build Folder" or by closing and opening your project again and rebuilding.

johnno1962 commented 7 months ago

I published a new release candidate of the InjectionIII app https://github.com/johnno1962/InjectionIII/releases printing a hint for when recompilations are failing suggesting that you may need to relaunch your app when renaming/adding/deleting source files. It's difficult to do any more than this (I tried). I'll be releasing this in the next couple of days.

Jonathanm10 commented 7 months ago

Thanks a lot for the explanation and the solution!