danger / danger-js

⚠️ Stop saying "you forgot to …" in code review
http://danger.systems/js/
MIT License
5.29k stars 367 forks source link

Specifying an external dangerfile.js breaks project dependencies #965

Open dejay-at-work opened 4 years ago

dejay-at-work commented 4 years ago

Report

What did you do?

I invoked npx danger local within a Node.js project, specifying a dangerfile.js that lives outside of the project.

Reproduction case:

#!/bin/bash
set -eu

rm -rf ./project ./tool

mkdir tool && (
  cd tool && \
  cat >dangerfile.js <<:DANGERFILE
try{
  console.log(require('array-first'));
  console.log(require('array-last'));
} catch(e) {
  console.log('ERROR', e);
}
:DANGERFILE
)

mkdir project && (
  cd project && \
  npm init -y >/dev/null 2>&1 && \
  npm i danger array-first array-last && \
  git init . && \
  git add package.json package-lock.json && \
  git commit -m 'NPM installed' && \
  git checkout -b test && \
  cp ../tool/dangerfile.js . && \
  git add dangerfile.js && \
  git commit -m 'Initial dangerfile' )

echo 'Running Danger in project'
(
  cd project && \
  npx danger local) || true

echo 'Running Danger in project, using external Dangerfile'
(
  cd project && \
  npx danger local -d '../tool/dangerfile.js' ) || true

What did you expect to happen?

Ideally, danger should evaluate an external Dangerfile as if it lived within the project directory from which danger was invoked. Alternatively, it would be desirable to have a command-line parameter that lets me specify the directory that danger considers to be the project directory.

NOTE: danger.import_dangerfile seemed like it might be a solution for this problem, but I could not get it to work from within danger-js. The documentation is pretty sparse and unclear, so I'm not sure if I was doing something wrong.

What happened instead?

Danger was not able to see the node_modules in the directory from which I invoked danger:

Running Danger in project
[Function: arrayFirst]

[Function: last]

Danger: ✓ passed review, received no feedback.

Running Danger in project, using external Dangerfile
ERROR { Error: Cannot find module 'array-first'

Your Environment

try{
  console.log(require('array-first'));
  console.log(require('array-last'));
} catch(e) {
  console.log('ERROR', e);
}
orta commented 4 years ago

Makes sense to me, happy to take PRs on it.

danger.import_dangerfile is a feature of the ruby version of Danger for reaching across repos etc, it's not been implemented in danger-js