Giter Site home page Giter Site logo

Comments (5)

akyrtzi avatar akyrtzi commented on July 17, 2024

The problem is that the `SyntaxRewriter` visitation exhausts the stack space that dispatch threads get. This can be reproduced from a command-line tool by doing the formatting call on a dispatch queue other than the main one.

To workaround this run the formatting transformation under a pthread thread (which you can customize its stack allocation).

from swift-format.

akyrtzi avatar akyrtzi commented on July 17, 2024

Reproducer from a command-line tool:

import Foundation
import SwiftFormat
import SwiftFormatConfiguration
import SwiftSyntax

func doit() throws {
    let configuration = Configuration()
    let formatter = SwiftFormatter(configuration: configuration)
    let syntax = try SyntaxParser.parse(URL(fileURLWithPath: "/path/to/swift/file.swift"))
    let dummyFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString + ".swift")
    var output = String()
    try formatter.format(syntax: syntax, assumingFileURL: dummyFileURL, to: &output)
    print(output)
}
let q = DispatchQueue(label: "queue")
q.async {
    try! doit()
}
sleep(1)

from swift-format.

akyrtzi avatar akyrtzi commented on July 17, 2024

Here's how to fix this using `Thread`:

let work = DispatchWorkItem {
    doFormatting()
}
let thread = Thread {
    work.perform()
}
thread.stackSize = 8 << 20 // 8 MB.
thread.start()
work.wait()

from swift-format.

tonyarnold avatar tonyarnold commented on July 17, 2024

That did the trick! Thanks so much for the workaround, @akyrtzi - it works really nicely now.

from swift-format.

ahoppen avatar ahoppen commented on July 17, 2024

The root issue of this is being tackled in swiftlang/swift-syntax#147 Once itβ€˜s merged, the workaround using Thread should not be necessary anymore.

from swift-format.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.