Problems setting PATH in Makefile

I am new to make and Makefiles, but I am trying to create it for my next project and I am facing PATH issues. I keep getting the error: "There is no such file or directory"

I created a simple target called test that runs all my tests using mocha .

Mocha is installed as a local node module, so its executable can be found in ./node_modules/.bin/mocha . I am changing my PATH as described in this tutorial , so I can refer to it as mocha , and not to enter the full path, but something does not seem to work.

Here is what I still have:

 export PATH := node_modules/.bin:$(PATH) test: which mocha mocha .PHONY: test 

When I run make test , I get the following output:

 which mocha node_modules/.bin/mocha mocha make: mocha: No such file or directory make: *** [test] Error 1 

As you can see from the output, which mocha correctly prints the path to the mocha executable, but when I just run mocha , it cannot find it.

What am I doing wrong? Is there a bigger picture about the scope or persistence variable in Make files that I skip?

PS If this is important, I use the Mac and the version of make that comes with the Xcode developer tools. This is what I get when I run make -v

 GNU Make 3.81 Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This program built for i386-apple-darwin11.3.0 
+6
source share
4 answers

I cannot reproduce your results (using GNU make 3.81 on a GNU / Linux system). It looks like there might be some difference in how the Apple system works, or that Apple made some kind of patch for the GNU version they are sending, which causes this problem.

GNU make has two ways to run recipes: the usual way, when it calls the shell and passes the recipe to the shell that you want to run, and the β€œquick path”, where if make sees that the command is β€œsimple” enough (that is, if it does not contain special shell characters), it will break the command into words and directly execute the command without calling the shell.The latter is much faster, but since it is called directly, it inherits the PATH parameter from GNU, which is not part of the shell.

It seems that, for some reason, the GNU version provided by Apple does not work as it does not correctly set up the environment for commands that run GNU make directly through the "fast path".

I have a very vague recollection that this is being discussed at GNU, compiling mailing lists, but after spending some time I couldn't think of anything.

You can "fix" this problem by forcing your command to use the slow path by entering some special shell characters (globbing, ; , pipe, etc.). Or you can use the fully qualified path to the program.

Or you can get the source code for GNU make and create it yourself; if this difference is the result of Apple's β€œfix” that should make it work better. Or install GNU make from homebrew or ports, and I also expect you to get a newer version with more features.

+5
source

On OSX, you also need to install SHELL :

 PATH := node_modules/.bin:$(PATH) SHELL := /bin/bash 
+8
source

This should work:

 PATH := $(PATH):$(PWD)/node_modules/.bin SHELL := env PATH=$(PATH) /bin/bash 
+2
source

I'm afraid you cannot do this. Try using local variables instead.

 NODE_MODULES := node_modules/.bin test: @ $(NODE_MODULES)/mocha 
0
source

All Articles