Stripes
Stripes

Reputation: 4239

How do I debug "Cannot find 'assure' in scope” (assure in this case was defined by me)

I have a mixed ObjC/swift application.

I have an assure.swift that defines a standalone assure function:

//
//  assure.swift
//  mMail
//
//  Created by J Osborne on 11/25/24.
//

import os

fileprivate let logger = os.Logger(subsystem: Bundle.main.bundleIdentifier ?? "mMail", category: (#fileID as NSString).deletingPathExtension)

// Usage similar to assert, but generates logging and a central chokepoint to put a breakpoint, not a "crash unless in prod" deal
public func assure(
  _ condition: @autoclosure () -> Bool,
  logLevel: os.OSLogType = .fault,
  _ message: @autoclosure () -> String = String(),
  file: Swift.StaticString = #file,
  line: UInt = #line
) {
  if !_fastPath(condition()) {
    let m = message()
    logger.log(level: logLevel, "assure failure \(file):\(line): \(m, privacy: .public)")
    ErrorListManager.currentErrorListViewController.append(ErrorItem(info: .assureFailure(m, file, line), timestamp: Date.now))
  }
}

public func assure(
  _ condition: @autoclosure () -> Bool,
  _ message: @autoclosure () -> String = String(),
  file: Swift.StaticString = #file,
  line: UInt = #line
) {
  assure(condition(), logLevel: .fault, message(), file: file, line: line)
}

In other words “nothing special”. I call it like a half billion times from various other swift classes. Suddenly I started getting the

/Users/stripes/src/_mmail/SyncManager.swift:128:7 Cannot find 'assure' in scope

errors.

I’m also getting a few other “cannot find in scope” errors, mostly for classes defined as @objc public (or sometimes internal) classes inheriting from NSObject.

This is across two of the three targets in the project. The various source files are part of the same project, not a separate module or library. I’m not really sure where to start debugging. My first thought was “find the .o file for assure and make sure it has the elected symbols in it!”, but I’m not really sure where I should go poking about for that.

(assure has been part of this project for a few months, and ben part of this problem since roughly last night, it is a pretty trivial function which is why I’m focusing on it as the least complex issue of this type)

Discovery: the code calling assure, or trying to use the classes ErrorListManager, AddressUSageCache, and DraftMailDatabaseHandler and getting the scope errors makes use of

@_objcImplementation

or

@objc @implementation

If I remove those I have other issues (“extensions must not contain stored properties” and the other things you would expect), but many of the scope errors go away.

Upvotes: 1

Views: 60

Answers (1)

Rob
Rob

Reputation: 438102

If you have multiple targets, make sure that assure.swift is included in all the relevant ones. So, while having assure.swift shown in the editor, press --1 (to pull up the “File Inspector” in the panel on the right) and make sure that the desired targets are included in the “Target Membership” section (and if not, click on the “+” button to add it).

Target Membership

You may need to repeat this process for any source files for the types referenced in assure.swift, too (e.g., ErrorItem, etc.).


The other way to do this is to go to the “Build Phases” section for a target and add assure.swift to the “Compile Sources” section:

Compile Sources

The “File Inspector” might be an easier way to add a source to multiple targets, but this is the other way to do this.

Upvotes: 0

Related Questions