Files
swift-mirror/userdocs/diagnostics/canimport-missing-module.md
Allan Shortlidge 4d52c36aa4 AST: Warn when module specified by versioned canImport cannot be found.
If the module named in `#if canImport(<Module>, _version: ...)` cannot be
found, it may indicate that the developer misspelled the module name. Emit a
warning to indicate that the directive evaluates to false. If it is intentional
that the module not be found, the developer can avoid the warning by first
checking whether the module can be imported and then check its version.

Resolves rdar://128367758.
2026-06-05 06:42:18 -07:00

1.0 KiB

Missing module for canImport (CanImportMissingModule)

Warnings that indicate a versioned #if canImport(<ModuleName>, _version: <version>) or #if canImport(<ModuleName>, _underlyingVersion: <version>) directive could not find the named module.

Overview

Developers may use a versioned #if canImport directive to conditionally compile code based on the version of a dependency module:

#if canImport(Dependency, _version: 1.2)
// Use declarations introduced in version 1.2 of Dependency
#endif

A versioned canImport check evaluates to false if the named module cannot be resolved. The compiler emits a warning in case this case:

warning: cannot find module 'Dependency' for canImport check; the directive will evaluate to false [#CanImportMissingModule]

If the module is expected to be missing in some configurations, you can suppress the warning by checking whether the module can be imported at all before checking its version:

#if canImport(Dependency) && canImport(Dependency, _version: 1.2)
// ...
#endif