balderdashy / balderdashy/sails
Cannot re-use controller code without hacks
- Dominant language
- JavaScript
- Stars
- 22.8k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
**Node version**: 14
**Sails version** _(sails)_: 1.4.0
# Observed
If two controller files export the same object, `include-all` throws an `Error` on startup. The `Error`'s message is misleading.
## Example
### `/api/controllers/one.js`:
```
module.exports = require('../some-lib/example-controller');
```
### `/api/controllers/two.js`:
```
module.exports = require('../some-lib/example-controller');
```
# Reproduced at
https://github.com/alxndrsn/sails-repeated-controllers-issue
See the controllers which clash at:
* https://github.com/alxndrsn/sails-repeated-controllers-issue/blob/master/api/controllers/one.js
* https://github.com/alxndrsn/sails-repeated-controllers-issue/blob/master/api/controllers/two.js
Error:
```
error:
error: Error: Duplicate filename detected: `include-all` attempted to load two files named `one.js` (case-insensitive).
at /some-path/node_modules/include-all/lib/help-build-dictionary.js:119:17
at /some-path/node_modules/@sailshq/lodash/lib/index.js:3260:15
at baseForOwn (/some-path/node_modules/@sailshq/lodash/lib/index.js:2230:14)
at /some-path/node_modules/@sailshq/lodash/lib/index.js:3230:18
at Function. (/some-path/node_modules/@sailshq/lodash/lib/index.js:3533:13)
at helpBuildDictionary (/some-path/node_modules/include-all/lib/help-build-dictionary.js:54:7)
at Function.module.exports.optional (/some-path/node_modules/include-all/index.js:67:10)
at loadActionModules (/some-path/node_modules/sails/lib/app/private/controller/load-action-modules.js:27:14)
at async.auto.controller (/some-path/node_modules/sails/lib/app/load.js:102:9)
at runTask (/some-path/node_modules/sails/node_modules/async/dist/async.js:1619:13)
at /some-path/node_modules/sails/node_modules/async/dist/async.js:1559:13
at processQueue (/some-path/node_modules/sails/node_modules/async/dist/async.js:1569:13)
at taskComplete (/some-path/node_modules/sails/node_modules/async/dist/async.js:1588:9)
at /some-path/node_modules/sails/node_modules/async/dist/async.js:1612:17
at /some-path/node_modules/sails/node_modules/async/dist/async.js:906:16
at /some-path/node_modules/sails/lib/app/load.js:192:14
error: Could not load Sails app.
```
# Expected
Either:
1. the error message should be accurate - there is only one file called `one.js`, or
2. declaring two controllers which share the same code should be allowed
# Workaround
Replace one/both of the controller definitions like:
```
module.exports = require('../some-lib/example-controller');
```
with
```
module.exports = { ...require('../some-lib/example-controller') };
```
Contributor guide
Research direction
Run the linked reproduction app and inspect api/controllers/one.js and api/controllers/two.js, then read include-all/lib/help-build-dictionary.js around the reported error and sails/lib/app/private/controller/load-action-modules.js. Done means repeated controller exports either start successfully or produce an accurate error describing the conflict.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100