pact-ruby-standalone icon indicating copy to clipboard operation
pact-ruby-standalone copied to clipboard

Issues with CDPATH and standalone pact-mock-service

Open madtrick opened this issue 5 years ago • 4 comments

Software versions

  • OS: Mac OSX 10.15.4
  • Consumer Pact library: 9.8.2
  • Provider Pact library: n/a
  • Node Version: v12.13

Expected behaviour

The pact-mock-service binary runs and my contracts can be verified.

Actual behaviour

When I try to run my contract suite locally I get the following error:

[2020-03-30T07:29:11.285Z] ERROR: [email protected]/43955 on riazor.fritz.box:
    Pact Binary Error: standalone/darwin-1.82.1/pact/bin/pact-mock-service: line 19: cd: ./standalone/darwin-1.82.1/pact/bin
    /Users/farruco/repos/work/comments-api/node_modules/@pact-foundation/pact-node/standalone/darwin-1.82.1/pact/bin: No such file or directory

Notice that the error contains two paths:

  • ./standalone/darwin-1.82.1/pact/bin
  • /Users/farruco/repos/work/comments-api/node_modules/@pact-foundation/pact-node/standalone/darwin-1.82.1/pact/bin

Steps to reproduce

After some debugging I discovered what the issue is. The error I pasted above is thrown from the script standalone/darwin-1.82.1/pact/bin/pact-mock-service when it tries to get the path to the library dir. This are the relevant lines in that script:

DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

# Figure out where this script is located.
LIBDIR="`cd \"$DIR\" && cd ../lib && pwd`"

In my current setup the variable $DIR results in a string which contains the two paths I listed above and that results in an error at the line where the script tries to generate the value for the $LIBDIR variable.

Why does $DIR contains two paths? Because I'm have the $CDPATH env variable set in my shell and that interferes with how cd behaves.

With $CDPATH set the statement cd -P "$( dirname "$SOURCE" )" will echo the path to which it changed the directory (if the path was relative). This echoed path is concatenated to the result from pwd.

With $CDPATH unset the statement cd -P "$( dirname "$SOURCE" )" won't echo anything.

Relevant log files

n/a

madtrick avatar Mar 30 '20 07:03 madtrick

As a fix you could redirect the output from cd -P "$( dirname "$SOURCE" )" like so:

DIR="$( cd -P "$( dirname "$SOURCE" )" > /dev/null && pwd )"

Also, not sure if this is the right repo for this issue since this is not particular to the JS libraries. Let me know and I'll move to a place of your preference.

madtrick avatar Mar 30 '20 07:03 madtrick

Thanks @madtrick for the detailed report. We could patch this problem in Pact JS (actually, Pact Node) by simply omitting/ignoring that environment variable when we shell out - but that won't be the root of the problem.

The issue is, as you state, in the bash scripts here.

I'm going to mark it as ready and help wanted, if you're willing to contribute that'd be great, otherwise we'll get to it when we can.

mefellows avatar Mar 30 '20 10:03 mefellows

Thanks @mefellows for moving this to the standalone repo (@madtrick, this is the one with the offending bash script)

TimothyJones avatar Mar 30 '20 23:03 TimothyJones

Btw, you can also put this in your .bashrc to avoid CDPATH affecting scripts (I would guess the Pact scripts are not the only ones affected):

if test “${PS1+set}”; then CDPATH=whatever; fi

(Source)

TimothyJones avatar Mar 30 '20 23:03 TimothyJones

I can't seem to replicate this, today on a macos m1 with the latest package, with CDPATH set to the current directory or random values.

If anyone can reproduce today and provide steps, we can apply the solution proposed to the script and see if that helps 👍🏾

Going to close due to age

Screenshot 2024-05-10 at 18 36 04

YOU54F avatar May 10 '24 17:05 YOU54F