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

RouteExists thrown when registering a directory and a missing file to the same route. #2132

Closed
na-stewart opened this issue May 10, 2021 · 2 comments · Fixed by #2244
Closed

Comments

@na-stewart
Copy link

na-stewart commented May 10, 2021

Describe the bug
When registering a route to a file and a directory, if the file doesn't exist a sanic_routing.exceptions.RouteExists error is raised. This may be misleading and unintended.

Code snippet

example.html does not exist in this example. The directory ./resources/web does.

app.static('/', './resources/web')
app.static('/', './resources/web/example.html')

Expected behavior
Currently, if a non existent file is registered to a route, no error is thrown on startup and any attempt to retrieve registered resource raises a FileNotFoundError: [Errno 2] No such file or directory and a 404 Not Found error is displayed in the browser. Instead of this happening, as mentioned before a sanic_routing.exceptions.RouteExists is raised on startup which may be misleading and unintended.

Environment (please complete the following information):

  • OS: Ubuntu 20.04.2 LTS
@ahopkins
Copy link
Member

ahopkins commented May 12, 2021

This is not really a bug, and is an intended outcome. Sanic is checking to see if a file like that exists. If not, it thinks you are trying to serve from a directory. Just by looking at the path, there is no way to determine if you intend to serve a file or a directory.

I think this is the correct behavior. If anything, perhaps what you really want is an explicit method to control this:

app.static('/', './resources/web', as="dir")
app.static('/', './resources/web/example.html', as="file")

With an explicit kwarg, we would not need to run path.isfile. This is not really new behavior. If Sanic 20.12 is not functioning this way, I think that is more a problem on the earlier version than with the current.


A bit of behind the scenes info...

Let's look at what these two are doing:

app.static("/", "./resources/web")
# Sanic checks this, sees that it is not a file and then converts to:
# "/<__file_uri__:path>"
# meaning that it will look for anything that matches using the `path` param type

app.static('/', './resources/web/example.html')
# If this is indeed a file, Sanic knows that you meant for an explicit path "/"
# There is no ambiguity, and the path does not need to be altered
# If the file does not exist, then like the first example, it tries to convert it to a `path` type
# But, since you already have a path expansion on that base path, there is now ambiguity
# and Sanic raises RouteExists

@stale
Copy link

stale bot commented Aug 21, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.

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

Successfully merging a pull request may close this issue.

3 participants