vapor / leaf-kit

🍃 An expressive, performant, and extensible templating language built for Swift.
https://docs.vapor.codes/4.0/leaf/getting-started
MIT License
50 stars 38 forks source link

Leaf-kit cannot render the page with AppCode from JetBrains #90

Closed Divix55 closed 3 years ago

Divix55 commented 3 years ago

Describe the bug

Vapor cannot render a webpage templated in leaf, if AppCode from JetBrains is used. JetBrains by default use a .build directory in project root directory to store dependencies and build files. Because of that, LeafKit returns error:

[ ERROR ] LeafError(file: "<absolute path to my project root>/.build/checkouts/leaf-kit/Sources/LeafKit/LeafSource/NIOLeafFiles.swift", function: "file(template:escape:on:)", line: 89, column: 57, reason: LeafKit.LeafError.Reason.illegalAccess("Attempted to access .build")) [request-id: 6468838E-F42F-4252-8AC7-04BEBE2485AF]

To Reproduce

Steps to reproduce the behavior:

  1. Create Vapor project with leaf template.
  2. Open project in AppCode from JetBrains (version used by me: 2021.1)
  3. Run project

Expected behavior

Webpage render correctly.

Environment

tdotclare commented 3 years ago

This is expected behavior; NIOLeafFiles by default prohibits access to dot-leading directories/files for security purposes.

This can be configured by removing toVisibleFiles from NIOLeafFiles.limits

Divix55 commented 3 years ago

Agree, even removing toVisibleFiles are not needed. Setting working directory in AppCode to project root resolve all problems.

shimbaco commented 3 years ago

@Divix55 Could you tell me how did you set working directory? I encountered the same error. So I've set it in the Run/Debug Configurations window but the error still occurred.

Debug Configurations 2021-10-12 02-19-04

Environment:

0xTim commented 3 years ago

@shimbaco you shouldn't need to set the working directory for anything other than Xcode

shimbaco commented 3 years ago

@0xTim I got it. Thank you for your advice!

lovetodream commented 2 years ago

How did you solve it @shimbaco? I have the same issue

sp3esu commented 2 years ago

HI all.

It's all about "build path". A default one is ".build" in a working directory and it can't be changed in Appcode unfortunately (I'm using AppCode 2022.1 EAP).

Try to run your app from shell using: swift run --build-path build

For AppCode to work you need to reconfigure Leaf and inject different templates source. Try this kind of configuration (it's based on LeafTests source code):

configure.swift

import Vapor
import Leaf
import LeafKit

// configures your application
public func configure(_ app: Application) throws {
    // uncomment to serve files from /Public folder
    app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
    app.views.use(.leaf)

    if app.environment == .development {
        app.leaf.configuration.rootDirectory = projectFolder
        app.leaf.sources = LeafSources.singleSource(NIOLeafFiles(
                        fileio: app.fileio,
                        limits: [.default],
                        sandboxDirectory: projectFolder,
                        viewDirectory: templateFolder
                ))
    }

    // register routes
    try routes(app)
}

fileprivate var templateFolder: String {
    return projectFolder + "Resources/Views/"
}

fileprivate var projectFolder: String {
    let folder = #file.split(separator: "/").dropLast(3).joined(separator: "/")
    return "/" + folder + "/"
}

Regards!