SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

SwiftSyntax comment parsing error since 0.47.0+ on Linux builds

Open wasdkhan opened this issue 2 years ago • 12 comments

Describe the bug

Swiftlint is ignoring disable comments since 0.47.0 and flagging warning and errors when it shouldn't.

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint sample.swift

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.47.1

  • Installation method used (Homebrew, CocoaPods, building from source, etc)? building from source on linux

  • Are you using nested configurations? no If so, paste their relative paths and respective contents.

Example

// sample.swift
// swiftlint:disable colon
let noWarning :String = "" // No warning about colons immediately after variable names!
// swiftlint:enable colon
let hasWarning :String = "" // Warning generated about colons immediately after variable names

Expected: One warning

Linting Swift files at paths /home/wmkn/local/sample.swift
Linting 'sample.swift' (1/1)
/home/wmkn/local/sample.swift:5:5: warning: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 1 violation, 0 serious in 1 file.

Actual: Two warnings

Linting Swift files at paths /home/wmkn/local/sample.swift
Linting 'sample.swift' (1/1)
Could not parse the syntax tree for at least one file. Results may be invalid.
/home/wmkn/local/sample.swift:3:5: warning: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/home/wmkn/local/sample.swift:5:5: warning: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 2 violations, 0 serious in 1 file.

wasdkhan avatar Jul 20 '22 20:07 wasdkhan

Would you please provide examples?

SimplyDanny avatar Jul 20 '22 20:07 SimplyDanny

Would you please provide examples?

Updated my post of an example file where Swiftlint ignores the disable comments and flags all the lines as warnings.

wasdkhan avatar Jul 20 '22 20:07 wasdkhan

You explicitly mention Linux. Did you try the same on macOS?

SimplyDanny avatar Jul 21 '22 17:07 SimplyDanny

Could not parse the syntax tree for at least one file. Results may be invalid.

Where do you have the SwiftSyntax parser lib installed on your distribution of Linux?

If you use the precompiled binary for Linux, it has to be located at /usr/lib/lib_InternalSwiftSyntaxParser.so.

jpsim avatar Jul 21 '22 17:07 jpsim

In fact, here's all the linked libraries SwiftLint expects at runtime on Linux:

$ docker run ghcr.io/realm/swiftlint:latest ldd /usr/bin/swiftlint
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00000040051a9000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00000040052fa000)
	libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x000000400531d000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x0000004005322000)
	libxml2.so.2 => /lib/x86_64-linux-gnu/libxml2.so.2 (0x0000004005328000)
	libcurl.so.4 => /lib/x86_64-linux-gnu/libcurl.so.4 (0x00000040054e2000)
	lib_InternalSwiftSyntaxParser.so => /lib/lib_InternalSwiftSyntaxParser.so (0x0000004005574000)
	libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x000000400680d000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00000040069ef000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x0000004006a0a000)
	/lib64/ld-linux-x86-64.so.2 (0x0000004000000000)
	libicuuc.so.66 => /lib/x86_64-linux-gnu/libicuuc.so.66 (0x0000004006bfc000)
	libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x0000004006de2000)
	liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x0000004006dfe000)
	libnghttp2.so.14 => /lib/x86_64-linux-gnu/libnghttp2.so.14 (0x0000004006e29000)
	libidn2.so.0 => /lib/x86_64-linux-gnu/libidn2.so.0 (0x0000004006e52000)
	librtmp.so.1 => /lib/x86_64-linux-gnu/librtmp.so.1 (0x0000004006e73000)
	libssh.so.4 => /lib/x86_64-linux-gnu/libssh.so.4 (0x0000004006e93000)
	libpsl.so.5 => /lib/x86_64-linux-gnu/libpsl.so.5 (0x0000004006f01000)
	libssl.so.1.1 => /lib/x86_64-linux-gnu/libssl.so.1.1 (0x0000004006f14000)
	libcrypto.so.1.1 => /lib/x86_64-linux-gnu/libcrypto.so.1.1 (0x0000004006fa9000)
	libgssapi_krb5.so.2 => /lib/x86_64-linux-gnu/libgssapi_krb5.so.2 (0x000000400727f000)
	libldap_r-2.4.so.2 => /lib/x86_64-linux-gnu/libldap_r-2.4.so.2 (0x00000040072cc000)
	liblber-2.4.so.2 => /lib/x86_64-linux-gnu/liblber-2.4.so.2 (0x0000004007322000)
	libbrotlidec.so.1 => /lib/x86_64-linux-gnu/libbrotlidec.so.1 (0x0000004007333000)
	libBlocksRuntime.so => /lib/libBlocksRuntime.so (0x0000004007341000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x0000004007348000)
	libtinfo.so.6 => /lib/x86_64-linux-gnu/libtinfo.so.6 (0x0000004007352000)
	libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x0000004007382000)
	libicudata.so.66 => /lib/x86_64-linux-gnu/libicudata.so.66 (0x000000400738b000)
	libunistring.so.2 => /lib/x86_64-linux-gnu/libunistring.so.2 (0x0000004008e4c000)
	libgnutls.so.30 => /lib/x86_64-linux-gnu/libgnutls.so.30 (0x0000004008fd0000)
	libhogweed.so.5 => /lib/x86_64-linux-gnu/libhogweed.so.5 (0x00000040091a6000)
	libnettle.so.7 => /lib/x86_64-linux-gnu/libnettle.so.7 (0x00000040091dd000)
	libgmp.so.10 => /lib/x86_64-linux-gnu/libgmp.so.10 (0x0000004009217000)
	libkrb5.so.3 => /lib/x86_64-linux-gnu/libkrb5.so.3 (0x000000400929b000)
	libk5crypto.so.3 => /lib/x86_64-linux-gnu/libk5crypto.so.3 (0x000000400937a000)
	libcom_err.so.2 => /lib/x86_64-linux-gnu/libcom_err.so.2 (0x00000040093ab000)
	libkrb5support.so.0 => /lib/x86_64-linux-gnu/libkrb5support.so.0 (0x00000040093b2000)
	libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00000040093c1000)
	libsasl2.so.2 => /lib/x86_64-linux-gnu/libsasl2.so.2 (0x00000040093dd000)
	libgssapi.so.3 => /lib/x86_64-linux-gnu/libgssapi.so.3 (0x00000040093fa000)
	libbrotlicommon.so.1 => /lib/x86_64-linux-gnu/libbrotlicommon.so.1 (0x000000400943f000)
	libp11-kit.so.0 => /lib/x86_64-linux-gnu/libp11-kit.so.0 (0x0000004009464000)
	libtasn1.so.6 => /lib/x86_64-linux-gnu/libtasn1.so.6 (0x000000400959a000)
	libkeyutils.so.1 => /lib/x86_64-linux-gnu/libkeyutils.so.1 (0x00000040095b0000)
	libheimntlm.so.0 => /lib/x86_64-linux-gnu/libheimntlm.so.0 (0x00000040095b7000)
	libkrb5.so.26 => /lib/x86_64-linux-gnu/libkrb5.so.26 (0x00000040095c3000)
	libasn1.so.8 => /lib/x86_64-linux-gnu/libasn1.so.8 (0x0000004009658000)
	libhcrypto.so.4 => /lib/x86_64-linux-gnu/libhcrypto.so.4 (0x00000040096ff000)
	libroken.so.18 => /lib/x86_64-linux-gnu/libroken.so.18 (0x0000004009737000)
	libffi.so.7 => /lib/x86_64-linux-gnu/libffi.so.7 (0x0000004009750000)
	libwind.so.0 => /lib/x86_64-linux-gnu/libwind.so.0 (0x000000400975c000)
	libheimbase.so.1 => /lib/x86_64-linux-gnu/libheimbase.so.1 (0x0000004009788000)
	libhx509.so.5 => /lib/x86_64-linux-gnu/libhx509.so.5 (0x000000400979a000)
	libsqlite3.so.0 => /lib/x86_64-linux-gnu/libsqlite3.so.0 (0x00000040097e8000)
	libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x0000004009911000)

jpsim avatar Jul 21 '22 17:07 jpsim

You explicitly mention Linux. Did you try to the same on macOS?

Yes no issue on macOS using the pre-built binary in the release

Could not parse the syntax tree for at least one file. Results may be invalid.

Where do you have the SwiftSyntax parser lib installed on your distribution of Linux?

If you use the precompiled binary for Linux, it has to be located at /usr/lib/lib_InternalSwiftSyntaxParser.so.

I created a symlink of the lib_InternalSwiftSyntaxParser.so file in the same folder as the swiftlint binary so it's able to find it.

In fact, here's all the linked libraries SwiftLint expects at runtime on Linux:

Is there a way to know if Swifltint could not find a specific runtime library.

wasdkhan avatar Jul 21 '22 21:07 wasdkhan

Is there a way to know if Swifltint could not find a specific runtime library.

I think you found a way here, it sounds to me like this is why this message gets logged:

Could not parse the syntax tree for at least one file. Results may be invalid.

Is it possible for you to create a Docker container that demonstrates the problem you're facing so I can debug further?

jpsim avatar Jul 21 '22 21:07 jpsim

Hey JP, Can't make a docker container since we are using a custom compiler toolchain at my company. We use CentOS as our flavor of Linux.

Ran ldd on the swiftlint binary and it's able to find all the runtime libraries, but still getting the error that it cannot parse the syntax tree.

wasdkhan avatar Jul 25 '22 18:07 wasdkhan

What version of Swift are you getting the lib_InternalSwiftSyntaxParser.so library from? It has to match exactly for SwiftSyntax to work.

jpsim avatar Jul 25 '22 18:07 jpsim

I am using the official docker image, and am seeing this as well.

 $ docker run -it --rm ghcr.io/realm/swiftlint:5.5-latest bash

root@e4247e3b52a1:/# cat - > sample.swift
// sample.swift
// swiftlint:disable colon
let noWarning :String = "" // No warning about colons immediately after variable names!
// swiftlint:enable colon
let hasWarning :String = "" // Warning generated about colons immediately after variable names

root@e4247e3b52a1:/# swiftlint sample.swift
Linting Swift files at paths sample.swift
Linting 'sample.swift' (1/1)
Could not parse the syntax tree for at least one file. Results may be invalid.
/sample.swift:3:5: warning: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/sample.swift:5:5: warning: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 2 violations, 0 serious in 1 file.

root@e4247e3b52a1:/# swiftlint --version
0.48.0

vs. mac (brew):

$ cat - > sample.swift
// sample.swift
// swiftlint:disable colon
let noWarning :String = "" // No warning about colons immediately after variable names!
// swiftlint:enable colon
let hasWarning :String = "" // Warning generated about colons immediately after variable names

$ swiftlint sample.swift
Linting Swift files at paths sample.swift
Linting 'sample.swift' (1/1)
/Users/acobb/tmp/sample.swift:5:5: warning: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 1 violation, 0 serious in 1 file.

$ swiftlint --version
0.48.0

cobbal avatar Jul 27 '22 13:07 cobbal

Also, the problem seems to only be on the 5.5-latest docker image, it works as expected in latest

$ docker run -it --rm ghcr.io/realm/swiftlint:latest bash

root@a6deed98aecf:/# cat - > sample.swift
// sample.swift
// swiftlint:disable colon
let noWarning :String = "" // No warning about colons immediately after variable names!
// swiftlint:enable colon
let hasWarning :String = "" // Warning generated about colons immediately after variable names

root@a6deed98aecf:/# swiftlint sample.swift
Linting Swift files at paths sample.swift
Linting 'sample.swift' (1/1)
/sample.swift:5:5: warning: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 1 violation, 0 serious in 1 file.

root@a6deed98aecf:/# swiftlint --version
0.48.0

cobbal avatar Jul 27 '22 13:07 cobbal

Just wanted to update this bug, was able to update to the latest version of SwiftLint by switching to an older version of our toolchain from Swift 5.7 to Swift 5.6, my guess is there is an issue with SwiftSyntax on 5.7, relevant discussion: https://github.com/FlineDev/BartyCrouch/issues/201#issuecomment-1240827827. Any ideas? Thanks.

wasdkhan avatar Sep 16 '22 16:09 wasdkhan

We're running into the same issue with our GitHub workflow using Swift 5.7 on ubuntu-latest.

We're also getting

Could not parse the syntax tree for at least one file. Results may be invalid.

so I suspect the missing lib_InternalSwiftSyntaxParser.so could be the cause, but I don't know how to start looking for it on a GitHub runner. Any suggestions?

CraigSiemens avatar Sep 27 '22 00:09 CraigSiemens

If you're using Swift 5.7, can you please use main after #4203 was merged? I believe this is now fixed.

Also, once we land https://github.com/realm/SwiftLint/pull/4216 this will no longer be an issue.

jpsim avatar Sep 27 '22 13:09 jpsim

We just tried using main in our GitHub workflows and it's working correctly.

Though we cant adopt it until there's a new release since it's tougher for devs to install from main and there's some rule now giving new warnings.

CraigSiemens avatar Sep 27 '22 16:09 CraigSiemens

Ok this is fixed on main so I'll close this ticket then.

jpsim avatar Sep 27 '22 18:09 jpsim