Skip to content

Linux: Use a DispatchSourceRead instead of a FileHandle.readabilityHandler #5

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

Merged
merged 3 commits into from
Sep 12, 2018

Conversation

spevans
Copy link
Contributor

@spevans spevans commented Aug 31, 2018

  • swift-corelibs-foundation does not currently support FileHandle handlers
    so use DispatchSource.makeReadSource instead of
    FileHandle.readabilityHandlerForReading if not on macOS.

This is only a temporary fix and there may be other Linux issues, but it was enough to get the example in the readme working (although I had to rebase this fix on top of tag swift-DEVELOPMENT-SNAPSHOT-2018-08-25-a for testing).

…ndler

- swift-corelibs-foundation does not currently support FileHandle handlers
  so use DispatchSource.makeReadSource instead of
  FileHandle.readabilityHandlerForReading if not on macOS.
@SDGGiesbrecht
Copy link

@spevans, @akyrtzi, @ahoppen, @aciidb0mb3r

This is only a temporary fix and there may be other Linux issues

I have been using a SwiftSyntax on Linux for a while now (for documentation generation and proofreading), though I have been doing it with the source from the 4.1.2 release. There were only three things I needed to adjust:

(I was lazy and hoisted SwiftSyntax up above several other modules to make it easier, so you won’t want to copy what I did, but the links will still help you find the issues.)

  1. The compiler invocation. This you already found and fixed.

  2. The search for the compiler. This is more critical on Linux because Swift tends to get installed all over the place instead of just as part of Xcode. It might be a good idea to provide an API to allow clients a means of specifying the location—and thereby the right Swift version (just like they can when linking against the Package Manager).

Looking at the current source, changing this...

private static let _swiftcURL: URL? = SwiftcRunner.locateSwiftc()

...to...

public static var swiftcURL: URL? = SwiftcRunner.locateSwiftc()

...would probably suffice. (private → public, let → var, drop underscore?)

  1. An annoying fatal error. Not really Linux specific, but SwiftSyntax was pretty much useless out here in the wild as long as it chose to terminate instead of just report an unknown node. The “get” and “set” keywords were its most notorious triggers, but maybe all that is already fixed in the 4.2 branch. My own test files are here, if someone wants to check against them with 4.2 (or add pieces of them to this package’s test suite).

@spevans
Copy link
Contributor Author

spevans commented Aug 31, 2018

For the compiler search, it looks like the full path can be passed to the SwiftcRunner(sourceFile: URL, swiftcURL: URL? = nil) constructor otherwise it will search the PATH to find it.

see https://github.com/apple/swift-syntax/blob/master/Sources/SwiftSyntax/SwiftcInvocation.swift#L175

@SDGGiesbrecht
Copy link

@spevans,

For the compiler search, it looks like the full path can be passed to the SwiftcRunner(sourceFile: URL, swiftcURL: URL? = nil) constructor otherwise it will search the PATH to find it.

Hmm... You’re right. But the structure itself is internal. Does it actually surface in the API somewhere?...

...never mind. I found it. It does. I guess that’s been dealt with since 4.1.2.

stdoutPipe.fileHandleForReading.readabilityHandler = { file in
stdoutData.append(file.availableData)
}
#else
// Temporary fix until swift-corelibs-foundation supports .readabilityHandler
let stdoutSource = DispatchSource.makeReadSource(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to also use DispatchSource on macOS? That way we wouldn't need to differentiate between macOS and Linux which would feel desirable to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to keep it like this for now just incase the solution for Linux is missing something as I don't want to regress the macOS part, but I will get the fix into corelibs-foundation asap so that this temp fix can be removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having it consistent between the two platforms would mean that the two hit the same issues. That way we would catch any bugs earlier when developing on Mac because it also fails on that platform.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahoppen I've updated it to use makeReadSource on both platforms.

@ahoppen
Copy link
Member

ahoppen commented Aug 31, 2018

Thanks for the great work here.

The ability to specify the swiftc executable to SyntaxTreeParser was added here: fbd1628. We should maybe cherry-pick it over to swift-4.2-branch so that we can get Linux support for that branch as well.

The fatalError has also been resolved on master (b3171b7). If useful for Linux development we can also cherry-pick that over to swift-4.2-branch.

@ahoppen
Copy link
Member

ahoppen commented Sep 8, 2018

Looks good! Thanks a lot for tackling this.

I would like for PR testing to be set up before merging this but that shouldn't take much longer.

Also, as I understand it, SwiftSyntax will completely work on Linux with this PR, right? If so, you can also close SR-8670 once this is merged.

@spevans
Copy link
Contributor Author

spevans commented Sep 8, 2018

@ahoppen Ive just updated the tests so the swift test will work correctly on Linux. It needs the tests to be explicitly listed on Linux and a LinuxMain.swift added as well. All of the tests pass on Linux.

@ahoppen
Copy link
Member

ahoppen commented Sep 8, 2018

That's amazing! Thanks for all your work on this.

@ahoppen
Copy link
Member

ahoppen commented Sep 12, 2018

https://ci.swift.org/view/Pull%20Request/job/swift-PR-osx/7361/
https://ci.swift.org/view/Pull%20Request/job/swift-PR-Linux/7402/

CI for linux is not activated yet, but Mac is passing so let's 🛳 it.

@ahoppen ahoppen merged commit ab1f0f5 into swiftlang:master Sep 12, 2018
Copy link
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM.

allevato pushed a commit to allevato/swift-syntax that referenced this pull request Sep 17, 2018
Linux: Use a DispatchSourceRead instead of a FileHandle.readabilityHandler
nkcsgexi added a commit that referenced this pull request Sep 17, 2018
[4.2] Merge pull request #5 from spevans/pr_linux_fix
CippoX added a commit to CippoX/swift-syntax that referenced this pull request Mar 21, 2023
# This is the 1st commit message:

fixed testAvailabilityQuery34 and testAvailabilityQueryUnavailability28

# This is the commit message swiftlang#2:

Update Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Co-authored-by: Kim de Vos <[email protected]>
# This is the commit message swiftlang#3:

added fixedSource in test case

# This is the commit message swiftlang#4:

minor changes

# This is the commit message swiftlang#5:

implemented recovery inside the parser

# This is the commit message swiftlang#6:

runned format.py

# This is the commit message swiftlang#7:

minor changes

# This is the commit message swiftlang#8:

minor changes
adevress pushed a commit to adevress/swift-syntax that referenced this pull request Jan 14, 2024
Ensure a space after `@unknown` in a default case.
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

Successfully merging this pull request may close these issues.

4 participants