Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command not found swift-format #502

Closed
Kuro7070 opened this issue Apr 4, 2023 · 13 comments
Closed

command not found swift-format #502

Kuro7070 opened this issue Apr 4, 2023 · 13 comments

Comments

@Kuro7070
Copy link

Kuro7070 commented Apr 4, 2023

After installing swift-format through my console, it cant find the command swift-format. Same for xcode.
If I install it with brew, it works but you can't change the version and I want it to install about the official way.
What could be my problem?

That's my code in the xcode buildphase
if which swift-format >/dev/null; then swift-format lint -r -p --configuration formatConfig.json "${PROJECT_DIR}/ChatApp" else echo "warning: swift-format not installed" fi

@stackotter
Copy link
Contributor

What commands did you use to install swift-format? My guess is that the built executable didn't get added/copied to your path. I believe you can fix that by running cp .build/release/swift-format /usr/local/bin/ inside the swift format directory. Another method would be to use Mint to install the tool, however Mint is not really being maintained anymore and I don't think it's the best to recommend it.

@Kuro7070
Copy link
Author

Kuro7070 commented Apr 4, 2023

I've used these commands
VERSION=0.50700.0 # replace this with the version you need git clone https://github.com/apple/swift-format.git cd swift-format git checkout "tags/$VERSION" swift build -c release
Yes, I also thought that my created executable could not be added to the path. I already tried to added it but it didn't work. But I have to say, I'm new to the apple universe, so maybe I did something wrong.
I will try your command, thx

@Kuro7070
Copy link
Author

Kuro7070 commented Apr 4, 2023

After entering your @stackotter command and trying to enter swift-format -v
I've got the following error message.
dyld[18324]: Library not loaded: '@rpath/lib_InternalSwiftSyntaxParser.dylib' Referenced from: '/usr/local/bin/swift-format' Reason: tried: '/usr/lib/swift/lib_InternalSwiftSyntaxParser.dylib' (no such file), '/usr/local/bin/lib_InternalSwiftSyntaxParser.dylib' (no such file), '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.5/macosx/lib_InternalSwiftSyntaxParser.dylib' (no such file), '/usr/lib/swift/lib_InternalSwiftSyntaxParser.dylib' (no such file), '/usr/local/bin/lib_InternalSwiftSyntaxParser.dylib' (no such file), '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.5/macosx/lib_InternalSwiftSyntaxParser.dylib' (no such file), '/usr/local/lib/lib_InternalSwiftSyntaxParser.dylib' (no such file), '/usr/lib/lib_InternalSwiftSyntaxParser.dylib' (no such file) zsh: abort swift-format -v

@stackotter
Copy link
Contributor

Hm had a feeling that might happen with my naive copy-to-path solution. You can installing it with Mint or replacing your command with the following (temporarily):

/path/to/repo/.build/release/swift-format lint -r -p --configuration formatConfig.json "${PROJECT_DIR}/ChatApp"

I'm interested why adding it to your path didn't work though, try running the following commands. Make sure to replace /path/to/repo with the correct absolute path to the swift format repo.

rm /usr/local/bin/swift-format
echo "\nexport PATH=\"$PATH:/path/to/repo/.build/release\"" >> ~/.zshenv

The first removes the swift-format binary we moved to your path of course, and the second adds the folder containing the built binary (and accompanying libraries) to your path.

You may need to restart Xcode for that to work.

@Kuro7070
Copy link
Author

Kuro7070 commented Apr 4, 2023

Sure, thx for the help @stackotter
If I add the complete path before the command, it works in xcode
/Users/zilke/Documents/swift-format/.build/release/swift-format lint -r -p --configuration formatConfig.json "${PROJECT_DIR}/ChatApp"

But the command swift-format stillt doesn't work
That is the command I tried. Just in case I did something wrong
echo "\nexport PATH=\"$PATH:/Users/zilke/Documents/swift-format/.build/release\"" >> ~/.zshenv

By mint, did you mean this project?
https://github.com/yonaskolb/Mint
Do you have any other ideas?

Is this a general problem or just in my case?

@stackotter
Copy link
Contributor

If you run swift-format in terminal does that work? The issue could be that Xcode is ignoring the new addition to the path (not sure if it respects .zshenv, haven't used it in a while).

Mint is the only tool that I know of for installing Swift command line tools from source. There was talk about adding a swift install command to swiftpm, but that died away sadly.

@Kuro7070
Copy link
Author

Kuro7070 commented Apr 4, 2023

Unfortunatly the command swift-format doesn't work in the terminal.
Ok I'm going to speak to my teamleader about it.
But it is strange that it works after the installation via the brew command brew install swift-format

@stackotter
Copy link
Contributor

Yeah that's strange, something weird is going on with your path. My only ideas for why that might be are: you're not using zsh (common on older macos versions), your terminal isn't causing ~/.zshenv to be loaded (dunno why that'd be), or the path addition command I gave you failed (you should check the contents of ~/.zshenv).

Hopefully your teamleader can help solve the issue, they should have more luck given they have access to the device 👍

@Wendell0817
Copy link

I'm having the same issue with XC 14.2. Other devs on my team using 14.1 have no issues with swift-format. Any updates on this @stackotter ?

@allevato
Copy link
Member

allevato commented Apr 7, 2023

I'm not sure what's causing the issues in the previous versions, but you may want to try out the just-released 508.0.0 version. It no longer depends on lib_InternalSwiftSyntaxParser.dylib nor does it have the requirement that it matches the specific version of the toolchain you're using (beyond being able to compile it in the first place).

(I don't control the Brew distribution, so you'd have to install it by downloading the release sources here and building them with swift build.)

@xavierliancw
Copy link

xavierliancw commented Apr 8, 2023

@Kuro7070, you should double check your ~/.zshenv file, checking to see if your command actually made it in. You may also have to do source ~/.zshenv or restart your computer.

The crux of your problem is that your freshly-compiled executable isn't visible to your $PATH. Adding it to your .zshenv should've done the trick though. (Although, I'm not actually sure what .zshenv does...)

I would've put into my ~/.zprofile if I wanted to modify my path (but that's not what I did).
I instead moved all relevant build artifacts to a folder I made, /Applications/Utilities/CLI/swift-format/.
Then I symlinked the main executable like so: ln -s /Applications/Utilities/CLI/swift-format/swift-format /usr/local/bin/swift-format.
That way I avoided messing with my $PATH, and it's a little closer to Linux conventions.

@Wendell0817
Copy link

@allevato thanks! 508.0.0 version is working for me!

@Kuro7070
Copy link
Author

Kuro7070 commented Apr 18, 2023

Hi guys, I tried again yesterday and now it works. I don't know why the command to change the bin folder didn't work for me the first time.
Solution:
enter this in your terminal
cp .build/release/swift-format /usr/local/bin/
cp .build/release/lib_InternalSwiftSyntaxParser.dylib /usr/local/bin/

And thank you for your help guys :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants