SwiftLint working from command line but says not installed in Xcode
New Issue Checklist
- [X] Updated SwiftLint to the latest version
- [X] I searched for existing GitHub issues
Describe the bug
I'm guessing this isn't a bug at all, but I wasn't sure where else to turn. I've searched and have been unable to find anyone with this same issue. Here's hoping that someone here has a solution to my problem🤞
I installed SwiftLint using homebrew. All SwiftLint commands work as expected via command line. Our Xcode project has a script step which runs SwiftLint linting. When I build in Xcode, however, I get the warning that SwiftLint is not installed. No one else on our team is having this issue.
Complete output when running SwiftLint, including the stack trace and command used
The SwiftLint script we have set up in the build phase for our project:
if which swiftlint >/dev/null; then
# First pass just auto-corrects based on .autocorrect.yml
swiftlint autocorrect --config .autocorrect.yml
# Second pass lints based on .swiftlint.yml
swiftlint
else
echo "warning: SwiftLint not installed, fix this with 'brew install swiftlint'"
echo "Also, see: https://github.com/realm/SwiftLint"
fi
Environment
- SwiftLint version (run
swiftlint versionto be sure)? 0.38.0 - Installation method used (Homebrew, CocoaPods, building from source, etc)? Homebrew
- Paste your configuration file:
# SwiftLint configuration file
#
# For any changes to this file, tag all team members in the PR for quick approval or discussion
#
# See current configuration with:
# swiftlint rules
#
# See documentation for the latest (master branch) rules at: https://github.com/realm/SwiftLint/blob/master/Rules.md
# and https://github.com/realm/SwiftLint for an example of options in this file
#
disabled_rules: # rule identifiers to exclude from running
- class_delegate_protocol # This breaks if you have a delegate protocol that inherits from a class-bound protocol (see: SwiftyTables DataSourceDelegate)
- identifier_name
- nesting
- redundant_objc_attribute # A bit buggy when it comes to MCAppFramework
- todo
- trailing_comma
excluded: # paths to ignore during linting.
- Carthage
- Swift Style Guide.playground
- fastlane/*
- MailChimp/Classes/Analytic Events/*
opt_in_rules:
- anyobject_protocol
- closure_spacing
- collection_alignment
- conditional_returns_on_newline
- convenience_type
- empty_count
- empty_xctest_method
- explicit_init
- fatal_error_message
- first_where
- function_default_parameter_at_end
- identical_operands
- implicit_return
- joined_default_parameter
- last_where
- legacy_random
- let_var_whitespace
- modifier_order
- multiline_arguments
- multiline_parameters
- operator_usage_whitespace
- overridden_super_call
- private_outlet
- prohibited_super_call
- redundant_nil_coalescing
- static_operator
- toggle_bool
- unavailable_function
- unneeded_parentheses_in_closure_argument
- untyped_error_in_catch
- unused_control_flow_label
- vertical_parameter_alignment_on_call
- xct_specific_matcher
# Experimental
- explicit_self
- unused_import
- unused_private_declaration
# Rule options
conditional_returns_on_newline:
if_only: true
file_length:
warning: 800
error: 1000
line_length:
warning: 160
ignores_comments: true
ignores_interpolated_strings: true
multiline_arguments:
only_enforce_after_first_closure_on_first_line: true
type_body_length:
warning: 400
error: 700
vertical_whitespace:
max_empty_lines: 2
- Are you using nested configurations? No If so, paste their relative paths and respective contents.
- Which Xcode version are you using (check
xcodebuild -version)? Xcode 11.3,Build version 11C29 - Do you have a sample that shows the issue? No, since this is an issue with finding SwiftLint and not an issue with the actual linting.
Run
echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rulesto quickly test if your example is really demonstrating the issue. If your example is more complex, you can useswiftlint lint --path [file here] --no-cache --enable-all-rules.
// This triggers a violation:
let foo = try! bar()
🤦♀ I figured it out as soon as I opened the issue. My homebrew prefix isn't the default /usr/local, so the script isn't finding my swiftlint in the PATH.
Thanks for letting us know! Closing this since the problem was solved.
How did you get the script to find the swiftlint in your PATH? On M1 Macs, hombrew installs it to /opt/homebrew, but I don't know what to change to get Xcode to find it there
How did you get the script to find the swiftlint in your PATH? On M1 Macs, hombrew installs it to
/opt/homebrew, but I don't know what to change to get Xcode to find it there
Based on https://stackoverflow.com/a/58745018 I was able to get it working like this:
if test -d "/opt/homebrew/bin/"; then
PATH="/opt/homebrew/bin/:${PATH}"
fi
export PATH
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
Should we update README for this? It's useful for M1 Macs. 😃
Hi! Maybe I'm missing something but why do you rely/require Swiftlint to be installed on the machine when you can run it directly from the Pods folder (in case you use CocoaPods, sure)?
You can simply use this and you don't need to install it on the machine:
"${PODS_ROOT}/SwiftLint/swiftlint"
Hi! Maybe I'm missing something but why do you rely/require Swiftlint to be installed on the machine when you can run it directly from the Pods folder (in case you use CocoaPods, sure)?
You can simply use this and you don't need to install it on the machine:
"${PODS_ROOT}/SwiftLint/swiftlint"
A lot of projects do not or do not want to use CocoaPods, for various reasons, so it's a good idea to have a more general solution.
you can also symlink the path ln -s /opt/homebrew/bin/swiftlint /usr/local/bin/swiftlint
Same issue in Xcode despite trying these various workarounds.
When I try the symlink command I receive this error: `ln: /usr/local/bin/swiftlint: Permission denied.
Same issue in Xcode despite trying these various workarounds.
When I try the symlink command I receive this error: `ln: /usr/local/bin/swiftlint: Permission denied.
you need to run it with sudo
sudo ln -s /opt/homebrew/bin/swiftlint /usr/local/bin/swiftlint
How did you get the script to find the swiftlint in your PATH? On M1 Macs, hombrew installs it to
/opt/homebrew, but I don't know what to change to get Xcode to find it thereBased on stackoverflow.com/a/58745018 I was able to get it working like this:
if test -d "/opt/homebrew/bin/"; then PATH="/opt/homebrew/bin/:${PATH}" fi export PATH if which swiftlint >/dev/null; then swiftlint else echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint[](https://github.com/realm/SwiftLint)" fi
Basically it's quite verbose & implicit way of running swiftlint by calling its hardcoded absolute path... Why not to use explicit absolute path instead then?
if which /opt/homebrew/bin/swiftgen >/dev/null; then
/opt/homebrew/bin/swiftgen
else
echo "Error: SwiftGen not installed, download it from https://github.com/SwiftGen/SwiftGen"
exit 1
fi
How did you get the script to find the swiftlint in your PATH? On M1 Macs, hombrew installs it to
/opt/homebrew, but I don't know what to change to get Xcode to find it thereBased on stackoverflow.com/a/58745018 I was able to get it working like this:
if test -d "/opt/homebrew/bin/"; then PATH="/opt/homebrew/bin/:${PATH}" fi export PATH if which swiftlint >/dev/null; then swiftlint else echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint[](https://github.com/realm/SwiftLint)" fiBasically it's quite verbose & implicit way of running swiftlint by calling its hardcoded absolute path... Why not to use explicit absolute path instead then?
if which /opt/homebrew/bin/swiftgen >/dev/null; then /opt/homebrew/bin/swiftgen else echo "Error: SwiftGen not installed, download it from https://github.com/SwiftGen/SwiftGen" exit 1 fi
If you hardcode the absolute path like /opt/homebrew/bin/swiftlint it will break the script for non Apple Silicon users. If you just add the PATH XCode can check both folders decide where to pick Swiftlint from
@Deitsch
If you hardcode the absolute path like /opt/homebrew/bin/swiftlint it will break the script for non Apple Silicon users. If you just add the PATH XCode can check both folders decide where to pick Swiftlint from
Makes sense, thank you