On 2023-07-07 09:52, Christian Hujer wrote: > Hello folks, > > Is this a bug or a feature? > > mkdir foo > ln -s foo bar > cd bar > echo -e 'all:\n\tpwd' >Makefile > make > > I would have expected the output to be …/bar but it actually is …/foo.
When you chdir() to a directory through a symlink, that information is lost. The process hangs on to the inode (or other low-level pointer-like thing) of the directory object. The pwd command most likely uses the getcwd() function calculate the absolute path to the current directory. The getcwd() function somehow figures out the absolute path of that directory, without caring how the process ended up in that directory. Make itself uses getcwd() in order to print the "Entering directory" and "Leaving directory" messages when you use "make -w". Now there is a related convention in this area. The environment variable PWD, which is maintained by the shell, tracks the abstract path followed by cd commands. GNU Make doesn't make use of the PWD environment variable. We can simulate that ourselves, look: $ make make: Entering directory '/home/kaz/junk/make-symlink/bar' /home/kaz/junk/make-symlink/bar make: Leaving directory '/home/kaz/junk/make-symlink/bar' The code: .PHONY: prolog all epilog epilog: all all: prolog all: @printf "%s\n" "$$PWD" prolog: @printf "make: Entering directory '%s'\n" "$$PWD" epilog: @printf "make: Leaving directory '%s'\n" "$$PWD" We implemented our own Entering/Leaving messages which look like those of GNU make, and replaced the pwd command with one that prints the contents of PWD instead. There are caveats with PWD, such as if the directory is changed by some program that doesn't update PWD, it will have incorrect contents.