apache / apache/couchdb

referenceError when using require in _view filter for _changes

Open
#2,769 1 comment 0 reactions 0 assignees View on GitHub
bug needs-triage
Dominant language
Erlang
Stars
7k
Forks
1.1k
Avg merge
1d 16h
Merged PRs (30d)
9

Description

[NOTE]: # ( ^^ Provide a general summary of the issue in the title above. ^^ )

## Description
[NOTE]: # ( Describe the problem you're encountering. )
[TIP]: # ( Do NOT give us access or passwords to your actual CouchDB! )

Given a view function using CommonJS to `require` something,
when using that view as a filter for `_changes` (see [docs](https://docs.couchdb.org/en/3.0.0/api/database/changes.html#view)),
then the response is a 500 status with a referenceError logged like below:

```erlang
{<<"ReferenceError">>,{[]}} [
{couch_os_process,prompt,2,[{file,"src/couch_os_process.erl"},{line,59}]},
{couch_query_servers,proc_prompt,2,[{file,"src/couch_query_servers.erl"},{line,520}]},
{couch_query_servers,with_ddoc_proc,2,[{file,"src/couch_query_servers.erl"},{line,514}]},
{couch_query_servers,filter_view,3,[{file,"src/couch_query_servers.erl"},{line,486}]},
{couch_changes,filter,3,[{file,"src/couch_changes.erl"},{line,236}]},
{fabric_rpc,changes_enumerator,2,[{file,"src/fabric_rpc.erl"},{line,514}]},
{couch_bt_engine,drop_reductions,4,[{file,"src/couch_bt_engine.erl"},{line,1177}]},
{couch_btree,stream_kv_node2,8,[{file,"src/couch_btree.erl"},{line,849}]}
]
```

## Steps to Reproduce
[NOTE]: # ( Include commands to reproduce, if possible. curl is preferred. )
Create a design doc like this:
```json
{
"_id": "_design/MyDesignDoc",
"views": {
"myView": {
"map": "function(doc) {\n var myLib = require('views/lib/myLib');\n log(myLib.myExport); // expecting to log 'foo'\n emit(doc._id, 1);\n}"
},
"lib": {
"myLib": "exports.myExport = \"foo\";"
}
}
}
```
Follow URL like this:
`myDb/_changes?filter=_view&view=MyDesignDoc/myView`

## Expected Behaviour

[NOTE]: # ( Tell us what you expected to happen. )
Seeing changes filtered by the map function from that view.

## Your Environment

[TIP]: # ( Include as many relevant details about your environment as possible. )
[TIP]: # ( You can paste the output of curl http://YOUR-COUCHDB:5984/ here. )

[apache/couchdb:3.0.0](https://hub.docker.com/layers/apache/couchdb/3.0.0)

## Additional Context

[TIP]: # ( Add any other context about the problem here. )
n/a

Contributor guide

Open the contributing guide

Research direction

Start with the failing _changes view-filter path in src/couch_changes.erl and the query-server handling in src/couch_query_servers.erl, using the provided design document and URL to reproduce it. Done means the CommonJS require view filters changes successfully without a 500 response or ReferenceError.

Written by the indexing model from the issue text.

Assessment

Tech stack
erlang, javascript
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.