Fix view engine resolution for view names ending in a dot#7392
Open
aryamirani wants to merge 1 commit into
Open
Fix view engine resolution for view names ending in a dot#7392aryamirani wants to merge 1 commit into
aryamirani wants to merge 1 commit into
Conversation
When a view name ends in a dot (e.g. 'index.'), path.extname returns '.',
causing mod to evaluate to an empty string ('' '') and throwing an opaque
TypeError [ERR_INVALID_ARG_VALUE] when require('') is called inside View constructor.
This change treats '.' as an unprovided extension when engines['.'] is not
explicitly registered, allowing view engine fallback to defaultEngine or
returning a clean view lookup error.
Fixes expressjs#7350
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #7350 where calling
res.render('index.')orapp.render('index.')throws an opaque synchronousTypeError [ERR_INVALID_ARG_VALUE]: The argument 'id' must be a non-empty string. Received ''instead of resolving the view or reporting a clean view lookup error.Root Cause
When a view name ends in a dot (such as
'index.'),path.extname('index.')returns'.'.In
lib/view.js,this.extis set to'.'. Because'.'is truthy,!this.extevaluates tofalse, skipping thedefaultEngineresolution logic.Viewthen attempts to load an engine viarequire(this.ext.slice(1))which passesrequire(), throwing a NodeTypeError.Solution
Treat
'.'as an unprovided extension whenengines['.']is not explicitly registered. This allows fallback todefaultEngine(or returning a clean view lookup error).Test Coverage
Added unit test in
test/app.render.jsverifying thatapp.render('rawr.')returns a clean lookup error message (Failed to lookup view "rawr.") when view engine is set.