Repository: spack/spack

Issue:
Installation issue: libluv
### Steps to reproduce the issue

```console
$ spack install neovim
```

### Information on your system

* **Spack:** 0.16.3-4806-532ba03d60
* **Python:** 3.8.2
* **Platform:** darwin-bigsur-m1
* **Concretizer:** clingo

### Additional information

```
==> Installing libluv-1.36.0-0-d4tbvgqbrm27bvnluo4u7mbbfkmfofgi
==> No binary for libluv-1.36.0-0-d4tbvgqbrm27bvnluo4u7mbbfkmfofgi found: installing from source
==> Using cached archive: /Users/certik/repos/spack/var/spack/cache/_source-cache/archive/f2/f2e7eb372574f25c6978c1dc74280d22efdcd7df2dda4a286c7fe7dceda26445.tar.gz
==> No patches needed for libluv
==> libluv: Executing phase: 'cmake'
==> libluv: Executing phase: 'build'
==> Error: ProcessError: Command exited with status 2:
    'make' '-j8'

4 errors found in build log:
     82     /Users/certik/repos/spack/opt/spack/darwin-bigsur-m1/apple-clang-12
            .0.5/cmake-3.21.3-gkfoksq2k2e7znnilapooa5jsili7ag3/bin/cmake -E cma
            ke_link_script CMakeFiles/libluv_a.dir/link.txt --verbose=1
     83     /Users/certik/repos/spack/lib/spack/env/clang/clang -O2 -g -DNDEBUG
             -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/Mac
            OSX.sdk -dynamiclib -Wl,-headerpad_max_install_names -compatibility
            _version 1.0.0 -current_version 1.36.0 -o libluv.1.36.0.dylib -inst
            all_name /private/var/folders/43/nspmtcpj5r589x58n_spl6jm000fsc/T/c
            ertik/spack-stage/spack-stage-libluv-1.36.0-0-d4tbvgqbrm27bvnluo4u7
            mbbfkmfofgi/spack-build-d4tbvgq/libluv.1.dylib CMakeFiles/libluv.di
            r/src/luv.c.o  /Users/certik/repos/spack/opt/spack/darwin-bigsur-m1
            /apple-clang-12.0.5/libuv-1.42.0-h4obvdsf3c3s4k467tfgicahwwpdejlt/l
            ib/libuv.dylib
     84     /usr/bin/ar qc libluv_a.a CMakeFiles/libluv_a.dir/src/luv.c.o
     85     /usr/bin/ranlib libluv_a.a
     86     make[2]: Leaving directory '/private/var/folders/43/nspmtcpj5r589x5
            8n_spl6jm000fsc/T/certik/spack-stage/spack-stage-libluv-1.36.0-0-d4
            tbvgqbrm27bvnluo4u7mbbfkmfofgi/spack-build-d4tbvgq'
     87     make[2]: Leaving directory '/private/var/folders/43/nspmtcpj5r589x5
            8n_spl6jm000fsc/T/certik/spack-stage/spack-stage-libluv-1.36.0-0-d4
            tbvgqbrm27bvnluo4u7mbbfkmfofgi/spack-build-d4tbvgq'
  >> 88     Undefined symbols for architecture arm64:
     89       "_luaL_argerror", referenced from:
     90           _luv_cancel in luv.c.o
     91           _luv_timer_start in luv.c.o
     92           _luv_timer_stop in luv.c.o
     93           _luv_timer_again in luv.c.o
     94           _luv_timer_set_repeat in luv.c.o

     ...

     480          _luv_thread_arg_set in luv.c.o
     481          _luv_check_bufs in luv.c.o
     482          _luv_prep_bufs in luv.c.o
     483          _luv_check_bufs_noref in luv.c.o
     484          ...
     485    ld: symbol(s) not found for architecture arm64
  >> 486    clang: error: linker command failed with exit code 1 (use -v to see
             invocation)
  >> 487    make[2]: *** [CMakeFiles/libluv.dir/build.make:101: libluv.1.36.0.d
            ylib] Error 1
     488    make[2]: Leaving directory '/private/var/folders/43/nspmtcpj5r589x5
            8n_spl6jm000fsc/T/certik/spack-stage/spack-stage-libluv-1.36.0-0-d4
            tbvgqbrm27bvnluo4u7mbbfkmfofgi/spack-build-d4tbvgq'
  >> 489    make[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/libluv.dir/all] E
            rror 2
     490    make[1]: *** Waiting for unfinished jobs....
     491    [100%] Built target libluv_a
     492    [100%] Built target luv
     493    make[1]: Leaving directory '/private/var/folders/43/nspmtcpj5r589x5
            8n_spl6jm000fsc/T/certik/spack-stage/spack-stage-libluv-1.36.0-0-d4
            tbvgqbrm27bvnluo4u7mbbfkmfofgi/spack-build-d4tbvgq'
     494    make: *** [Makefile:139: all] Error 2

See build log for details:
  /var/folders/43/nspmtcpj5r589x58n_spl6jm000fsc/T/certik/spack-stage/spack-stage-libluv-1.36.0-0-d4tbvgqbrm27bvnluo4u7mbbfkmfofgi/spack-build-out.txt

==> Error: neovim-0.4.4-x46l5tptdaqrfscympfl7artkpfpyncf: Package was not installed
==> Error: Installation request failed.  Refer to reported errors for failing package(s).
```

### General information

- [X] I have run `spack debug report` and reported the version of Spack/Python/Platform
- [X] I have run `spack maintainers <name-of-the-package>` and **@mentioned** any maintainers
- [X] I have uploaded the build log and environment files
- [X] I have searched the issues of this repo and believe this is not a duplicate


Discussion:
I can confirm this still happens on M1 Max.
@sethrj with your installation of spack on M1, do you think you could please try this and see if it works for you? You can just do `spack install libluv`. 
I tried it both on monterey-m1 and bigsur-x86_64: no dice on either one. It looks like it's not even linking against lua, so  I think the code (unchanged since it was added in  #18100) is simply broken.

@tkameyama When was the last time you built this successfully? Can you help @certik debug this?
@sethrj thanks for trying it! I read through the documentation of Spack, but I still can't figure out how to debug a broken package. I found the directory which contains the checked out source code, e.g., in `/var/folders/8w/5mqfkhqn1nj0tw97g3hl1nyh0000gn/T/ondrej/spack-stage/spack-stage-libluv-1.36.0-0-tvx2glpetx6p24opmyhhs7t6kfigxe5z/`, but when I execute `make` in there, it says that I am not running from within Spack:
```console
$ cd /var/folders/8w/5mqfkhqn1nj0tw97g3hl1nyh0000gn/T/ondrej/spack-stage/spack-stage-libluv-1.36.0-0-tvx2glpetx6p24opmyhhs7t6kfigxe5z/
$ cd spack-build-tvx2glp
$ make
...
[spack cc] ERROR: Spack compiler must be run from Spack! Input 'SPACK_ENV_PATH' is missing.
```

How do I rerun the build after I modify the source code of the package? I want to be able to modify the package's build system and try to rebuild. I am usually pretty good at fixing these things once I can do that.
You should be able to just `source` the `spack-env.txt` in the staging directory, which will set up the PATH, CMAKE_PREFIX_PATH, secret Spack compiler wrapper variables, etc.
I think that's it! Thanks. I can now reproduce the error by hand. Let me debug it.

For others, here is how to debug it:
```console
$ cd /var/folders/8w/5mqfkhqn1nj0tw97g3hl1nyh0000gn/T/ondrej/spack-stage/spack-stage-libluv-1.36.0-0-tvx2glpetx6p24opmyhhs7t6kfigxe5z/
$ source spack-build-env.txt 
$ cd spack-build-tvx2glp
$ make
...
[ 83%] Linking C shared library libluv.dylib
/Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.22.2-pg45e44sbq4ysn7oc6ge7buvnp75iad5/bin/cmake -E cmake_link_script CMakeFiles/libluv.dir/link.txt --verbose=1
/Users/ondrej/repos/spack/lib/spack/env/clang/clang -O2 -g -DNDEBUG -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -mmacosx-version-min=12 -dynamiclib -Wl,-headerpad_max_install_names -compatibility_version 1.0.0 -current_version 1.36.0 -o libluv.1.36.0.dylib -install_name /private/var/folders/8w/5mqfkhqn1nj0tw97g3hl1nyh0000gn/T/ondrej/spack-stage/spack-stage-libluv-1.36.0-0-tvx2glpetx6p24opmyhhs7t6kfigxe5z/spack-build-tvx2glp/libluv.1.dylib CMakeFiles/libluv.dir/src/luv.c.o  /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/libuv-1.42.0-ngegcdhh5bkchrearbrm5pqbzqphsj3g/lib/libuv.dylib 
Undefined symbols for architecture arm64:
  "_luaL_addlstring", referenced from:
      _thread_dump in luv.c.o
  "_luaL_argerror", referenced from:
      _luv_cancel in luv.c.o
      _luv_timer_start in luv.c.o
      _luv_timer_stop in luv.c.o
      _luv_timer_again in luv.c.o
      _luv_timer_set_repeat in luv.c.o
      _luv_timer_get_repeat in luv.c.o
      _luv_prepare_start in luv.c.o
      ...
```
Adding `/Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/lua-5.3.5-cojs2g45u6pevsn3akvpmwxeon4ujuyv/lib/liblua.dylib` to the clang command line that fails makes it succeed. So it's just not linking against the `liblua`'s shared library, that's it. Now we need to figure out how to make cmake link against it.

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/libluv/package.py b/var/spack/repos/builtin/packages/libluv/package.py
--- a/var/spack/repos/builtin/packages/libluv/package.py
+++ b/var/spack/repos/builtin/packages/libluv/package.py
@@ -14,6 +14,7 @@ class Libluv(CMakePackage):
     homepage = "https://github.com/luvit/luv"
     url      = "https://github.com/luvit/luv/releases/download/1.36.0-0/luv-1.36.0-0.tar.gz"
 
+    version('1.43.0-0', sha256='567a6f3dcdcf8a9b54ddc57ffef89d1e950d72832b85ee81c8c83a9d4e0e9de2')
     version('1.36.0-0', sha256='f2e7eb372574f25c6978c1dc74280d22efdcd7df2dda4a286c7fe7dceda26445')
 
     depends_on('lua', type='link')


Test patch: